Add full reference to column identifier in const columns

main
Jonas Letzbor 2024-04-11 08:01:01 +02:00
parent 07465cfafe
commit 49a2362280
Signed by: RPJosh
GPG Key ID: 43ACB900522EA740
3 changed files with 30 additions and 12 deletions

View File

@ -245,11 +245,20 @@ func (c *constructor) getGoFile(existingContent string, tbl *ddl.Table, tblConfi
fieldName := GetFieldName(col.Name) fieldName := GetFieldName(col.Name)
jsonName := GetJsonName(col.Name) jsonName := GetJsonName(col.Name)
rtc += fmt.Sprintf("\t%s %s `json:\"%s\" %s:\"%s\"`\n", fieldName, dataType, jsonName, ColumnTagId, tags.ToTag()) rtc += fmt.Sprintf("\t%s %s `json:\"%s\" %s:\"%s\"`\n", fieldName, dataType, jsonName, ColumnTagId, tags.ToTag())
columns += fmt.Sprintf("\t %s_%s string = \"%s\"\n", tableName, fieldName, fieldName)
// We also add the full reference to the column inside the string value.
// It's needed to reference it without information of the table (which we can't get
// with constants and no support for package reflection)
identifier := tbl.Name + "." + col.Name
if tbl.Schema != "" {
identifier = tbl.Schema + "." + identifier
}
columns += fmt.Sprintf("\t %s_%s string = \"%s|%s\"\n", tableName, fieldName, fieldName, identifier)
} }
// Add foreign key columns // Add foreign key columns
rtcAdd, importsAdd := c.getOneToMany(tblConfig, tbl) rtcAdd, columnsAdd, importsAdd := c.getOneToMany(tblConfig, tbl)
if rtcAdd != "" { if rtcAdd != "" {
rtc += rtcAdd rtc += rtcAdd
for _, imp := range importsAdd { for _, imp := range importsAdd {
@ -258,6 +267,7 @@ func (c *constructor) getGoFile(existingContent string, tbl *ddl.Table, tblConfi
} }
} }
} }
columns += columnsAdd
// Add metadata tag // Add metadata tag
metaData := &MetadataTag{ metaData := &MetadataTag{
@ -293,7 +303,7 @@ func (c *constructor) getGoFile(existingContent string, tbl *ddl.Table, tblConfi
// getDataType returns the data type to use for the column as a string expression // getDataType returns the data type to use for the column as a string expression
// and the extra imports required for this data type. // and the extra imports required for this data type.
// The tags my be updated within this function // The tags my be updated within this function
func (c *constructor) getDataType(column *ddl.Column, tblConfig *TableConfig, tags *ColumnTag) (name string, imp string) { func (c *constructor) getDataType(column *ddl.Column, tblConfig *TableConfig, _ *ColumnTag) (name string, imp string) {
// Find 1:1 relationship // Find 1:1 relationship
if oneToOne := c.findOneToOne(column, tblConfig); oneToOne != "" { if oneToOne := c.findOneToOne(column, tblConfig); oneToOne != "" {
@ -374,13 +384,12 @@ func (c *constructor) findOneToOne(column *ddl.Column, tblConfig *TableConfig) s
// other tables to this table. // other tables to this table.
// It returns an empty string if no relationship was found or it's disable in the config. // It returns an empty string if no relationship was found or it's disable in the config.
// Otherwise this function returns any additional fields to add to the struct with it's required imports // Otherwise this function returns any additional fields to add to the struct with it's required imports
func (c *constructor) getOneToMany(tblConfig *TableConfig, tbl *ddl.Table) (fields string, imp []string) { func (c *constructor) getOneToMany(tblConfig *TableConfig, tbl *ddl.Table) (rtc string, constValues string, imp []string) {
rtc := ""
imports := []string{} imports := []string{}
// The user explicity has to enable this feature // The user explicity has to enable this feature
if !tblConfig.IncludePointedStructs { if !tblConfig.IncludePointedStructs {
return rtc, imports return rtc, constValues, imports
} }
// Loop through every table and column and find any foreign key to this table // Loop through every table and column and find any foreign key to this table
@ -397,12 +406,21 @@ func (c *constructor) getOneToMany(tblConfig *TableConfig, tbl *ddl.Table) (fiel
PointedKeyReference: t.Schema + "." + t.Name + "." + c.Name, 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)
identifier := tbl.Name + "." + fieldNameRoot
if tbl.Schema != "" {
identifier = tbl.Schema + "." + identifier
}
constValues += fmt.Sprintf("\t %s_%s string = \"%s|#%s\"\n", GetFieldName(tbl.Name)+tblConfig.Suffix, fieldNameRoot, fieldNameRoot, identifier)
} }
} }
} }
// No relation found // No relation found
return rtc, imports return rtc, constValues, imports
} }
// patchFile patches the content of an existing file with the new struct. // patchFile patches the content of an existing file with the new struct.
@ -499,7 +517,7 @@ func (c *constructor) patchImports(existingContent string, imports map[string]bo
// Build a new import string. We wan't to sort it // Build a new import string. We wan't to sort it
keys := make([]string, 0) keys := make([]string, 0)
for k, _ := range imports { for k := range imports {
keys = append(keys, k) keys = append(keys, k)
} }
sort.Strings(keys) sort.Strings(keys)

View File

@ -106,8 +106,8 @@ type MyTableNameTab struct {
} }
// MyTableNameTab // MyTableNameTab
const ( const (
MyTableNameTab_Id string = "Id" MyTableNameTab_Id string = "Id|here_is_me.my_table_name.id"
MyTableNameTab_WithUnder string = "WithUnder" MyTableNameTab_WithUnder string = "WithUnder|here_is_me.my_table_name.with_under"
) )
` `
goFile := c.getGoFile("", table, tableConfig) goFile := c.getGoFile("", table, tableConfig)
@ -258,7 +258,7 @@ func TestRelationshipOneToMany(t *testing.T) {
} }
// Expecting 1:1 reference to struct // Expecting 1:1 reference to struct
dt, _ := c.getOneToMany(tableConfig2, tables[1]) dt, _, _ := c.getOneToMany(tableConfig2, tables[1])
expectedTag := &ColumnTag{ expectedTag := &ColumnTag{
PointedKeyReference: "here_is_me.workout_details.workout_id", PointedKeyReference: "here_is_me.workout_details.workout_id",
} }