Improve generated structs

main
Jonas Letzbor 2024-04-12 11:55:36 +02:00
parent 49a2362280
commit b31c3d327b
Signed by: RPJosh
GPG Key ID: 43ACB900522EA740
4 changed files with 14 additions and 10 deletions

View File

@ -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

View File

@ -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)

View File

@ -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(

View File

@ -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:]