From b31c3d327b70c8bb998eddc26c2a06bac9e7cac5 Mon Sep 17 00:00:00 2001 From: RPJosh Date: Fri, 12 Apr 2024 11:55:36 +0200 Subject: [PATCH] Improve generated structs --- mariadb.go | 2 +- structt/struct.go | 2 +- structt/struct_test.go | 2 +- structt/tags.go | 18 +++++++++++------- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/mariadb.go b/mariadb.go index 99258b7..3d29b24 100644 --- a/mariadb.go +++ b/mariadb.go @@ -129,7 +129,7 @@ func (s *Mariadb) GetTable(schema, name string) (*Table, error) { // We got no data if count == 0 { - return nil, fmt.Errorf("schema.table was not found") + return nil, fmt.Errorf("%s.%s was not found", schema, name) } return table, nil diff --git a/structt/struct.go b/structt/struct.go index 923a977..a541168 100644 --- a/structt/struct.go +++ b/structt/struct.go @@ -405,7 +405,7 @@ func (c *constructor) getOneToMany(tblConfig *TableConfig, tbl *ddl.Table) (rtc tag := &ColumnTag{ PointedKeyReference: t.Schema + "." + t.Name + "." + c.Name, } - rtc += fmt.Sprintf("\t%s []*%s `%s:\"%s\"`\n", GetFieldName(t.Name), tblName, ColumnTagId, tag.ToTag()) + rtc += fmt.Sprintf("\t%s []%s `%s:\"%s\"`\n", GetFieldName(t.Name), tblName, ColumnTagId, tag.ToTag()) // We also add the full reference to the column inside the string value. fieldNameRoot := GetFieldName(t.Name) diff --git a/structt/struct_test.go b/structt/struct_test.go index 3bf33a0..90fd45b 100644 --- a/structt/struct_test.go +++ b/structt/struct_test.go @@ -262,7 +262,7 @@ func TestRelationshipOneToMany(t *testing.T) { expectedTag := &ColumnTag{ PointedKeyReference: "here_is_me.workout_details.workout_id", } - expected := fmt.Sprintf("\tWorkoutDetails []*WorkoutDetailsTab `%s:\"%s\"`\n", ColumnTagId, expectedTag.ToTag()) + expected := fmt.Sprintf("\tWorkoutDetails []WorkoutDetailsTab `%s:\"%s\"`\n", ColumnTagId, expectedTag.ToTag()) // Compare structs if diff := cmp.Diff( diff --git a/structt/tags.go b/structt/tags.go index f422afc..f7994bd 100644 --- a/structt/tags.go +++ b/structt/tags.go @@ -86,8 +86,14 @@ func GetColumnTag(col *ddl.Column) *ColumnTag { // ToTag transforms this columnTag to a string that can be applied as // struct tag -func (c *ColumnTag) ToTag() string { - rtc := "Column:" + c.Name +func (c *ColumnTag) ToTag() (rtc string) { + + // PointedKeyReference doesn't contain column name + if c.PointedKeyReference == "" { + rtc = "Column:" + c.Name + } else { + rtc = "PointedForeignKey:" + c.PointedKeyReference + } if c.AutoIncrement { rtc += ",AutoIncrement" @@ -98,9 +104,6 @@ func (c *ColumnTag) ToTag() string { if c.ForeignKeyReference != "" { rtc += ",ForeignKey:" + c.ForeignKeyReference } - if c.PointedKeyReference != "" { - rtc += ",PointedForeignKey:" + c.PointedKeyReference - } return rtc } @@ -171,9 +174,10 @@ func FromMetadataTag(tag string) *MetadataTag { point := strings.Index(val, ":") key := val[0:point] - // No value specified - if point+1 == len(val) { + // No value specified (schema is optional) + if point+1 == len(val) && key != "Schema" { logger.Warning("No value specified for metadata tag %q", val) + logger.Debug("Tag value for mising metadata: %q", val) continue } value := val[point+1:]