Change text helpers to use refactored func naming
- This cleans up the function naming so it's easier to follow in all cases. Provides better tests for the same.
This commit is contained in:
parent
14c5e5f149
commit
093db148e3
3 changed files with 193 additions and 19 deletions
|
@ -107,7 +107,7 @@ func (o *{{$rel.LocalTable.NameGo}}) Set{{$rel.Function.Name}}(exec boil.Executo
|
||||||
}
|
}
|
||||||
|
|
||||||
{{if .ToJoinTable -}}
|
{{if .ToJoinTable -}}
|
||||||
remove{{$rel.LocalTable.NameGo}}From{{$rel.ForeignTable.NameGo}}Slice(o, related)
|
remove{{$rel.Function.Name}}From{{$rel.Function.ForeignName}}Slice(o, related)
|
||||||
o.R.{{$rel.Function.Name}} = nil
|
o.R.{{$rel.Function.Name}} = nil
|
||||||
{{else -}}
|
{{else -}}
|
||||||
if o.R != nil {
|
if o.R != nil {
|
||||||
|
@ -163,7 +163,7 @@ func (o *{{$rel.LocalTable.NameGo}}) Remove{{$rel.Function.Name}}(exec boil.Exec
|
||||||
{{end -}}
|
{{end -}}
|
||||||
|
|
||||||
{{if .ToJoinTable -}}
|
{{if .ToJoinTable -}}
|
||||||
remove{{$rel.LocalTable.NameGo}}From{{$rel.ForeignTable.NameGo}}Slice(o, related)
|
remove{{$rel.Function.Name}}From{{$rel.Function.ForeignName}}Slice(o, related)
|
||||||
{{end -}}
|
{{end -}}
|
||||||
if o.R == nil {
|
if o.R == nil {
|
||||||
return nil
|
return nil
|
||||||
|
@ -188,7 +188,7 @@ func (o *{{$rel.LocalTable.NameGo}}) Remove{{$rel.Function.Name}}(exec boil.Exec
|
||||||
}
|
}
|
||||||
|
|
||||||
{{if .ToJoinTable -}}
|
{{if .ToJoinTable -}}
|
||||||
func remove{{$rel.LocalTable.NameGo}}From{{$rel.ForeignTable.NameGo}}Slice(o *{{$rel.LocalTable.NameGo}}, related []*{{$rel.ForeignTable.NameGo}}) {
|
func remove{{$rel.Function.Name}}From{{$rel.Function.ForeignName}}Slice(o *{{$rel.LocalTable.NameGo}}, related []*{{$rel.ForeignTable.NameGo}}) {
|
||||||
for _, rel := range related {
|
for _, rel := range related {
|
||||||
if rel.R == nil {
|
if rel.R == nil {
|
||||||
continue
|
continue
|
||||||
|
|
112
text_helpers.go
112
text_helpers.go
|
@ -50,12 +50,7 @@ func txtsFromFKey(tables []bdb.Table, table bdb.Table, fkey bdb.ForeignKey) TxtT
|
||||||
r.ForeignTable.ColumnName = fkey.ForeignColumn
|
r.ForeignTable.ColumnName = fkey.ForeignColumn
|
||||||
r.ForeignTable.ColumnNameGo = strmangle.TitleCase(strmangle.Singular(fkey.ForeignColumn))
|
r.ForeignTable.ColumnNameGo = strmangle.TitleCase(strmangle.Singular(fkey.ForeignColumn))
|
||||||
|
|
||||||
r.Function.Name = strmangle.TitleCase(strmangle.Singular(trimSuffixes(fkey.Column)))
|
r.Function.Name, r.Function.ForeignName = txtNameToOne(fkey)
|
||||||
plurality := strmangle.Plural
|
|
||||||
if fkey.Unique {
|
|
||||||
plurality = strmangle.Singular
|
|
||||||
}
|
|
||||||
r.Function.ForeignName = mkFunctionName(strmangle.Singular(fkey.ForeignTable), strmangle.TitleCase(plurality(fkey.Table)), fkey.Column, false)
|
|
||||||
r.Function.Varname = strmangle.CamelCase(strmangle.Singular(fkey.ForeignTable))
|
r.Function.Varname = strmangle.CamelCase(strmangle.Singular(fkey.ForeignTable))
|
||||||
|
|
||||||
if fkey.Nullable {
|
if fkey.Nullable {
|
||||||
|
@ -102,8 +97,7 @@ func txtsFromOneToOne(tables []bdb.Table, table bdb.Table, oneToOne bdb.ToOneRel
|
||||||
rel.ForeignKey.Nullable, rel.ForeignKey.ForeignColumnNullable = rel.ForeignKey.ForeignColumnNullable, rel.ForeignKey.Nullable
|
rel.ForeignKey.Nullable, rel.ForeignKey.ForeignColumnNullable = rel.ForeignKey.ForeignColumnNullable, rel.ForeignKey.Nullable
|
||||||
rel.ForeignKey.Unique, rel.ForeignKey.ForeignColumnUnique = rel.ForeignKey.ForeignColumnUnique, rel.ForeignKey.Unique
|
rel.ForeignKey.Unique, rel.ForeignKey.ForeignColumnUnique = rel.ForeignKey.ForeignColumnUnique, rel.ForeignKey.Unique
|
||||||
|
|
||||||
rel.Function.Name = strmangle.TitleCase(strmangle.Singular(oneToOne.ForeignTable))
|
rel.Function.Name, rel.Function.ForeignName = rel.Function.ForeignName, rel.Function.Name
|
||||||
rel.Function.ForeignName = mkFunctionName(strmangle.Singular(oneToOne.Table), strmangle.TitleCase(strmangle.Singular(oneToOne.Table)), oneToOne.ForeignColumn, false)
|
|
||||||
rel.Function.UsesBytes = col.Type == "[]byte"
|
rel.Function.UsesBytes = col.Type == "[]byte"
|
||||||
return rel
|
return rel
|
||||||
}
|
}
|
||||||
|
@ -151,14 +145,7 @@ func txtsFromToMany(tables []bdb.Table, table bdb.Table, rel bdb.ToManyRelations
|
||||||
r.ForeignTable.Slice = fmt.Sprintf("%sSlice", strmangle.TitleCase(r.ForeignTable.NameSingular))
|
r.ForeignTable.Slice = fmt.Sprintf("%sSlice", strmangle.TitleCase(r.ForeignTable.NameSingular))
|
||||||
r.ForeignTable.NameHumanReadable = strings.Replace(rel.ForeignTable, "_", " ", -1)
|
r.ForeignTable.NameHumanReadable = strings.Replace(rel.ForeignTable, "_", " ", -1)
|
||||||
|
|
||||||
r.Function.Name = mkFunctionName(r.LocalTable.NameSingular, r.ForeignTable.NamePluralGo, rel.ForeignColumn, rel.ToJoinTable)
|
r.Function.Name, r.Function.ForeignName = txtNameToMany(rel)
|
||||||
plurality := strmangle.Singular
|
|
||||||
foreignNamingColumn := rel.ForeignColumn
|
|
||||||
if rel.ToJoinTable {
|
|
||||||
plurality = strmangle.Plural
|
|
||||||
foreignNamingColumn = rel.JoinLocalColumn
|
|
||||||
}
|
|
||||||
r.Function.ForeignName = strmangle.TitleCase(plurality(trimSuffixes(foreignNamingColumn)))
|
|
||||||
|
|
||||||
col := table.GetColumn(rel.Column)
|
col := table.GetColumn(rel.Column)
|
||||||
if rel.Nullable {
|
if rel.Nullable {
|
||||||
|
@ -180,6 +167,99 @@ func txtsFromToMany(tables []bdb.Table, table bdb.Table, rel bdb.ToManyRelations
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// txtNameToOne creates the local and foreign function names for
|
||||||
|
// one-to-many and one-to-one relationships, where local == lhs (one).
|
||||||
|
//
|
||||||
|
// = many-to-one
|
||||||
|
// users - videos : user_id
|
||||||
|
// users - videos : producer_id
|
||||||
|
//
|
||||||
|
// fk == table = user.Videos | video.User
|
||||||
|
// fk != table = user.ProducerVideos | video.Producer
|
||||||
|
//
|
||||||
|
// = many-to-one
|
||||||
|
// industries - industries : parent_id
|
||||||
|
//
|
||||||
|
// fk == table = industry.Industries | industry.Industry
|
||||||
|
// fk != table = industry.ParentIndustries | industry.Parent
|
||||||
|
//
|
||||||
|
// = one-to-one
|
||||||
|
// users - videos : user_id
|
||||||
|
// users - videos : producer_id
|
||||||
|
//
|
||||||
|
// fk == table = user.Video | video.User
|
||||||
|
// fk != table = user.ProducerVideo | video.Producer
|
||||||
|
//
|
||||||
|
// = one-to-one
|
||||||
|
// industries - industries : parent_id
|
||||||
|
//
|
||||||
|
// fk == table = industry.Industry | industry.Industry
|
||||||
|
// fk != table = industry.ParentIndustry | industry.Industry
|
||||||
|
func txtNameToOne(fk bdb.ForeignKey) (localFn, foreignFn string) {
|
||||||
|
localFn = trimSuffixes(fk.Column)
|
||||||
|
fkeyIsTableName := localFn != strmangle.Singular(fk.ForeignTable)
|
||||||
|
localFn = strmangle.TitleCase(localFn)
|
||||||
|
|
||||||
|
if fkeyIsTableName {
|
||||||
|
foreignFn = localFn
|
||||||
|
}
|
||||||
|
|
||||||
|
plurality := strmangle.Plural
|
||||||
|
if fk.Unique {
|
||||||
|
plurality = strmangle.Singular
|
||||||
|
}
|
||||||
|
foreignFn += strmangle.TitleCase(plurality(fk.Table))
|
||||||
|
|
||||||
|
return localFn, foreignFn
|
||||||
|
}
|
||||||
|
|
||||||
|
// txtNameToMany creates the local and foreign function names for
|
||||||
|
// many-to-one and many-to-many relationship, where local == lhs (many)
|
||||||
|
//
|
||||||
|
// cases:
|
||||||
|
// = many-to-many
|
||||||
|
// sponsors - constests
|
||||||
|
// sponsor_id contest_id
|
||||||
|
// fk == table = sponsor.Contests | contest.Sponsors
|
||||||
|
//
|
||||||
|
// = many-to-many
|
||||||
|
// sponsors - constests
|
||||||
|
// wiggle_id jiggle_id
|
||||||
|
// fk != table = sponsor.JiggleSponsors | contest.WiggleContests
|
||||||
|
//
|
||||||
|
// = many-to-many
|
||||||
|
// industries - industries
|
||||||
|
// industry_id mapped_industry_id
|
||||||
|
//
|
||||||
|
// fk == table = industry.Industries
|
||||||
|
// fk != table = industry.MappedIndustryIndustry
|
||||||
|
func txtNameToMany(toMany bdb.ToManyRelationship) (localFn, foreignFn string) {
|
||||||
|
if toMany.ToJoinTable {
|
||||||
|
localFkey := trimSuffixes(toMany.JoinLocalColumn)
|
||||||
|
foreignFkey := trimSuffixes(toMany.JoinForeignColumn)
|
||||||
|
|
||||||
|
if localFkey != strmangle.Singular(toMany.Table) {
|
||||||
|
foreignFn = strmangle.TitleCase(localFkey)
|
||||||
|
}
|
||||||
|
foreignFn += strmangle.TitleCase(strmangle.Plural(toMany.Table))
|
||||||
|
|
||||||
|
if foreignFkey != strmangle.Singular(toMany.ForeignTable) {
|
||||||
|
localFn = strmangle.TitleCase(foreignFkey)
|
||||||
|
}
|
||||||
|
localFn += strmangle.TitleCase(strmangle.Plural(toMany.ForeignTable))
|
||||||
|
|
||||||
|
return localFn, foreignFn
|
||||||
|
}
|
||||||
|
|
||||||
|
fkeyName := trimSuffixes(toMany.ForeignColumn)
|
||||||
|
if fkeyName != strmangle.Singular(toMany.Table) {
|
||||||
|
localFn = strmangle.TitleCase(fkeyName)
|
||||||
|
}
|
||||||
|
localFn += strmangle.TitleCase(strmangle.Plural(toMany.ForeignTable))
|
||||||
|
foreignFn = strmangle.TitleCase(strmangle.Singular(fkeyName))
|
||||||
|
return localFn, foreignFn
|
||||||
|
}
|
||||||
|
|
||||||
// mkFunctionName checks to see if the foreign key name is the same as the local table name (minus _id suffix)
|
// 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
|
// 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
|
// Not simple case: We have to name the function based off the foreign key and the foreign table name
|
||||||
|
|
|
@ -170,6 +170,100 @@ func TestTxtsFromMany(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTxtNameToOne(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
Table string
|
||||||
|
Column string
|
||||||
|
Unique bool
|
||||||
|
ForeignTable string
|
||||||
|
ForeignColumn string
|
||||||
|
ForeignColumnUnique bool
|
||||||
|
|
||||||
|
LocalFn string
|
||||||
|
ForeignFn string
|
||||||
|
}{
|
||||||
|
{"jets", "airport_id", false, "airports", "id", true, "Airport", "Jets"},
|
||||||
|
{"jets", "airport_id", true, "airports", "id", true, "Airport", "Jet"},
|
||||||
|
|
||||||
|
{"jets", "holiday_id", false, "airports", "id", true, "Holiday", "HolidayJets"},
|
||||||
|
{"jets", "holiday_id", true, "airports", "id", true, "Holiday", "HolidayJet"},
|
||||||
|
|
||||||
|
{"jets", "holiday_airport_id", false, "airports", "id", true, "HolidayAirport", "HolidayAirportJets"},
|
||||||
|
{"jets", "holiday_airport_id", true, "airports", "id", true, "HolidayAirport", "HolidayAirportJet"},
|
||||||
|
|
||||||
|
{"jets", "jet_id", false, "jets", "id", true, "Jet", "Jets"},
|
||||||
|
{"jets", "jet_id", true, "jets", "id", true, "Jet", "Jet"},
|
||||||
|
{"jets", "plane_id", false, "jets", "id", true, "Plane", "PlaneJets"},
|
||||||
|
{"jets", "plane_id", true, "jets", "id", true, "Plane", "PlaneJet"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, test := range tests {
|
||||||
|
fk := bdb.ForeignKey{
|
||||||
|
Table: test.Table, Column: test.Column, Unique: test.Unique,
|
||||||
|
ForeignTable: test.ForeignTable, ForeignColumn: test.ForeignColumn, ForeignColumnUnique: test.ForeignColumnUnique,
|
||||||
|
}
|
||||||
|
|
||||||
|
local, foreign := txtNameToOne(fk)
|
||||||
|
if local != test.LocalFn {
|
||||||
|
t.Error(i, "local wrong:", local, "want:", test.LocalFn)
|
||||||
|
}
|
||||||
|
if foreign != test.ForeignFn {
|
||||||
|
t.Error(i, "foreign wrong:", foreign, "want:", test.ForeignFn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTxtNameToMany(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
Table string
|
||||||
|
Column string
|
||||||
|
|
||||||
|
ForeignTable string
|
||||||
|
ForeignColumn string
|
||||||
|
|
||||||
|
ToJoinTable bool
|
||||||
|
JoinLocalColumn string
|
||||||
|
JoinForeignColumn string
|
||||||
|
|
||||||
|
LocalFn string
|
||||||
|
ForeignFn string
|
||||||
|
}{
|
||||||
|
{"airports", "id", "jets", "airport_id", false, "", "", "Jets", "Airport"},
|
||||||
|
{"airports", "id", "jets", "holiday_airport_id", false, "", "", "HolidayAirportJets", "HolidayAirport"},
|
||||||
|
|
||||||
|
{"jets", "id", "jets", "jet_id", false, "", "", "Jets", "Jet"},
|
||||||
|
{"jets", "id", "jets", "plane_id", false, "", "", "PlaneJets", "Plane"},
|
||||||
|
|
||||||
|
{"pilots", "id", "languages", "id", true, "pilot_id", "language_id", "Languages", "Pilots"},
|
||||||
|
{"pilots", "id", "languages", "id", true, "captain_id", "lingo_id", "LingoLanguages", "CaptainPilots"},
|
||||||
|
|
||||||
|
{"pilots", "id", "pilots", "id", true, "pilot_id", "mentor_id", "MentorPilots", "Pilots"},
|
||||||
|
{"pilots", "id", "pilots", "id", true, "mentor_id", "pilot_id", "Pilots", "MentorPilots"},
|
||||||
|
{"pilots", "id", "pilots", "id", true, "captain_id", "mentor_id", "MentorPilots", "CaptainPilots"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, test := range tests {
|
||||||
|
fk := bdb.ToManyRelationship{
|
||||||
|
Table: test.Table, Column: test.Column,
|
||||||
|
ForeignTable: test.ForeignTable, ForeignColumn: test.ForeignColumn,
|
||||||
|
ToJoinTable: test.ToJoinTable,
|
||||||
|
JoinLocalColumn: test.JoinLocalColumn, JoinForeignColumn: test.JoinForeignColumn,
|
||||||
|
}
|
||||||
|
|
||||||
|
local, foreign := txtNameToMany(fk)
|
||||||
|
if local != test.LocalFn {
|
||||||
|
t.Error(i, "local wrong:", local, "want:", test.LocalFn)
|
||||||
|
}
|
||||||
|
if foreign != test.ForeignFn {
|
||||||
|
t.Error(i, "foreign wrong:", foreign, "want:", test.ForeignFn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestTrimSuffixes(t *testing.T) {
|
func TestTrimSuffixes(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue