Refactor function name calculations
This commit is contained in:
parent
f6206ea6b7
commit
84e13cf0d4
1 changed files with 13 additions and 9 deletions
|
@ -137,15 +137,7 @@ func textsFromRelationship(tables []bdb.Table, table bdb.Table, rel bdb.ToManyRe
|
||||||
r.ForeignTable.NameHumanReadable = strings.Replace(rel.ForeignTable, "_", " ", -1)
|
r.ForeignTable.NameHumanReadable = strings.Replace(rel.ForeignTable, "_", " ", -1)
|
||||||
|
|
||||||
r.Function.Receiver = strings.ToLower(table.Name[:1])
|
r.Function.Receiver = strings.ToLower(table.Name[:1])
|
||||||
|
r.Function.Name = mkFunctionName(r.LocalTable.NameSingular, r.ForeignTable.NamePluralGo, rel.ForeignColumn, rel.ToJoinTable)
|
||||||
// Check to see if the foreign key name is the same as the local table name.
|
|
||||||
// Simple case: yes - we can name the function the same as the plural table name
|
|
||||||
// Not simple case: We have to name the function based off the foreign key and
|
|
||||||
if colName := strings.TrimSuffix(rel.ForeignColumn, "_id"); rel.ToJoinTable || r.LocalTable.NameSingular == colName {
|
|
||||||
r.Function.Name = r.ForeignTable.NamePluralGo
|
|
||||||
} else {
|
|
||||||
r.Function.Name = strmangle.TitleCase(colName) + r.ForeignTable.NamePluralGo
|
|
||||||
}
|
|
||||||
|
|
||||||
if rel.Nullable {
|
if rel.Nullable {
|
||||||
col := table.GetColumn(rel.Column)
|
col := table.GetColumn(rel.Column)
|
||||||
|
@ -164,3 +156,15 @@ func textsFromRelationship(tables []bdb.Table, table bdb.Table, rel bdb.ToManyRe
|
||||||
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// mkFunctionName checks to see if the foreign key name is the same as the local table name (minus _id suffix)
|
||||||
|
// Simple case: yes - we can name the function the same as the plural table name
|
||||||
|
// Not simple case: We have to name the function based off the foreign key and the foreign table name
|
||||||
|
func mkFunctionName(localTableSingular, foreignTablePluralGo, foreignColumn string, toJoinTable bool) string {
|
||||||
|
colName := strings.TrimSuffix(foreignColumn, "_id")
|
||||||
|
if toJoinTable || localTableSingular == colName {
|
||||||
|
return foreignTablePluralGo
|
||||||
|
}
|
||||||
|
|
||||||
|
return strmangle.TitleCase(colName) + foreignTablePluralGo
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue