diff --git a/example/yoyo.yml b/example/yoyo.yml index 87ac50f..2c291ea 100644 --- a/example/yoyo.yml +++ b/example/yoyo.yml @@ -33,5 +33,6 @@ schema: - favorite_color references: city: + go_name: Hometown has_one: true required: false diff --git a/example/yoyo/repositories/query/person/query.go b/example/yoyo/repositories/query/person/query.go index af4ac92..bb0a996 100644 --- a/example/yoyo/repositories/query/person/query.go +++ b/example/yoyo/repositories/query/person/query.go @@ -62,100 +62,100 @@ func (q Query) AgeNot(in float64) Query { }} } -func (q Query) CityId(in int32) Query { +func (q Query) FavoriteColor(in string) Query { return Query{query.Node{ - Children: &[2]query.Node{q.n, CityId(in).n}, + Children: &[2]query.Node{q.n, FavoriteColor(in).n}, Operator: query.And, }} } -func (q Query) CityIdGreaterOrEqual(in int32) Query { +func (q Query) FavoriteColorContains(in string) Query { return Query{query.Node{ - Children: &[2]query.Node{q.n, CityIdGreaterOrEqual(in).n}, + Children: &[2]query.Node{q.n, FavoriteColorContains(in).n}, Operator: query.And, }} } -func (q Query) CityIdGreaterThan(in int32) Query { +func (q Query) FavoriteColorContainsNot(in string) Query { return Query{query.Node{ - Children: &[2]query.Node{q.n, CityIdGreaterThan(in).n}, + Children: &[2]query.Node{q.n, FavoriteColorContainsNot(in).n}, Operator: query.And, }} } -func (q Query) CityIdLessOrEqual(in int32) Query { +func (q Query) FavoriteColorEndsWith(in string) Query { return Query{query.Node{ - Children: &[2]query.Node{q.n, CityIdLessOrEqual(in).n}, + Children: &[2]query.Node{q.n, FavoriteColorEndsWith(in).n}, Operator: query.And, }} } -func (q Query) CityIdLessThan(in int32) Query { +func (q Query) FavoriteColorEndsWithNot(in string) Query { return Query{query.Node{ - Children: &[2]query.Node{q.n, CityIdLessThan(in).n}, + Children: &[2]query.Node{q.n, FavoriteColorEndsWithNot(in).n}, Operator: query.And, }} } -func (q Query) CityIdNot(in int32) Query { +func (q Query) FavoriteColorNot(in string) Query { return Query{query.Node{ - Children: &[2]query.Node{q.n, CityIdNot(in).n}, + Children: &[2]query.Node{q.n, FavoriteColorNot(in).n}, Operator: query.And, }} } -func (q Query) FavoriteColor(in string) Query { +func (q Query) FavoriteColorStartsWith(in string) Query { return Query{query.Node{ - Children: &[2]query.Node{q.n, FavoriteColor(in).n}, + Children: &[2]query.Node{q.n, FavoriteColorStartsWith(in).n}, Operator: query.And, }} } -func (q Query) FavoriteColorContains(in string) Query { +func (q Query) FavoriteColorStartsWithNot(in string) Query { return Query{query.Node{ - Children: &[2]query.Node{q.n, FavoriteColorContains(in).n}, + Children: &[2]query.Node{q.n, FavoriteColorStartsWithNot(in).n}, Operator: query.And, }} } -func (q Query) FavoriteColorContainsNot(in string) Query { +func (q Query) HometownId(in int32) Query { return Query{query.Node{ - Children: &[2]query.Node{q.n, FavoriteColorContainsNot(in).n}, + Children: &[2]query.Node{q.n, HometownId(in).n}, Operator: query.And, }} } -func (q Query) FavoriteColorEndsWith(in string) Query { +func (q Query) HometownIdGreaterOrEqual(in int32) Query { return Query{query.Node{ - Children: &[2]query.Node{q.n, FavoriteColorEndsWith(in).n}, + Children: &[2]query.Node{q.n, HometownIdGreaterOrEqual(in).n}, Operator: query.And, }} } -func (q Query) FavoriteColorEndsWithNot(in string) Query { +func (q Query) HometownIdGreaterThan(in int32) Query { return Query{query.Node{ - Children: &[2]query.Node{q.n, FavoriteColorEndsWithNot(in).n}, + Children: &[2]query.Node{q.n, HometownIdGreaterThan(in).n}, Operator: query.And, }} } -func (q Query) FavoriteColorNot(in string) Query { +func (q Query) HometownIdLessOrEqual(in int32) Query { return Query{query.Node{ - Children: &[2]query.Node{q.n, FavoriteColorNot(in).n}, + Children: &[2]query.Node{q.n, HometownIdLessOrEqual(in).n}, Operator: query.And, }} } -func (q Query) FavoriteColorStartsWith(in string) Query { +func (q Query) HometownIdLessThan(in int32) Query { return Query{query.Node{ - Children: &[2]query.Node{q.n, FavoriteColorStartsWith(in).n}, + Children: &[2]query.Node{q.n, HometownIdLessThan(in).n}, Operator: query.And, }} } -func (q Query) FavoriteColorStartsWithNot(in string) Query { +func (q Query) HometownIdNot(in int32) Query { return Query{query.Node{ - Children: &[2]query.Node{q.n, FavoriteColorStartsWithNot(in).n}, + Children: &[2]query.Node{q.n, HometownIdNot(in).n}, Operator: query.And, }} } @@ -317,142 +317,142 @@ func AgeNot(in float64) Query { }} } -func CityId(in int32) Query { +func FavoriteColor(in string) Query { return Query{query.Node{ Condition: query.Condition{ - Column: "fk_city_id", + Column: "favorite_color", Operator: query.Equals, Value: in, }, }} } -func CityIdGreaterOrEqual(in int32) Query { +func FavoriteColorContains(in string) Query { return Query{query.Node{ Condition: query.Condition{ - Column: "fk_city_id", - Operator: query.GreaterOrEqual, - Value: in, + Column: "favorite_color", + Operator: query.Like, + Value: fmt.Sprintf("'%%%s%%'", in), }, }} } -func CityIdGreaterThan(in int32) Query { +func FavoriteColorContainsNot(in string) Query { return Query{query.Node{ Condition: query.Condition{ - Column: "fk_city_id", - Operator: query.GreaterThan, - Value: in, + Column: "favorite_color", + Operator: query.NotLike, + Value: fmt.Sprintf("'%%%s%%'", in), }, }} } -func CityIdLessOrEqual(in int32) Query { +func FavoriteColorEndsWith(in string) Query { return Query{query.Node{ Condition: query.Condition{ - Column: "fk_city_id", - Operator: query.LessOrEqual, - Value: in, + Column: "favorite_color", + Operator: query.Like, + Value: fmt.Sprintf("'%%%s'", in), }, }} } -func CityIdLessThan(in int32) Query { +func FavoriteColorEndsWithNot(in string) Query { return Query{query.Node{ Condition: query.Condition{ - Column: "fk_city_id", - Operator: query.LessThan, - Value: in, + Column: "favorite_color", + Operator: query.NotLike, + Value: fmt.Sprintf("'%%%s'", in), }, }} } -func CityIdNot(in int32) Query { +func FavoriteColorNot(in string) Query { return Query{query.Node{ Condition: query.Condition{ - Column: "fk_city_id", + Column: "favorite_color", Operator: query.NotEquals, Value: in, }, }} } -func FavoriteColor(in string) Query { +func FavoriteColorStartsWith(in string) Query { return Query{query.Node{ Condition: query.Condition{ Column: "favorite_color", - Operator: query.Equals, - Value: in, + Operator: query.Like, + Value: fmt.Sprintf("'%s%%'", in), }, }} } -func FavoriteColorContains(in string) Query { +func FavoriteColorStartsWithNot(in string) Query { return Query{query.Node{ Condition: query.Condition{ Column: "favorite_color", - Operator: query.Like, - Value: fmt.Sprintf("'%%%s%%'", in), + Operator: query.NotLike, + Value: fmt.Sprintf("'%s%%'", in), }, }} } -func FavoriteColorContainsNot(in string) Query { +func HometownId(in int32) Query { return Query{query.Node{ Condition: query.Condition{ - Column: "favorite_color", - Operator: query.NotLike, - Value: fmt.Sprintf("'%%%s%%'", in), + Column: "fk_city_id", + Operator: query.Equals, + Value: in, }, }} } -func FavoriteColorEndsWith(in string) Query { +func HometownIdGreaterOrEqual(in int32) Query { return Query{query.Node{ Condition: query.Condition{ - Column: "favorite_color", - Operator: query.Like, - Value: fmt.Sprintf("'%%%s'", in), + Column: "fk_city_id", + Operator: query.GreaterOrEqual, + Value: in, }, }} } -func FavoriteColorEndsWithNot(in string) Query { +func HometownIdGreaterThan(in int32) Query { return Query{query.Node{ Condition: query.Condition{ - Column: "favorite_color", - Operator: query.NotLike, - Value: fmt.Sprintf("'%%%s'", in), + Column: "fk_city_id", + Operator: query.GreaterThan, + Value: in, }, }} } -func FavoriteColorNot(in string) Query { +func HometownIdLessOrEqual(in int32) Query { return Query{query.Node{ Condition: query.Condition{ - Column: "favorite_color", - Operator: query.NotEquals, + Column: "fk_city_id", + Operator: query.LessOrEqual, Value: in, }, }} } -func FavoriteColorStartsWith(in string) Query { +func HometownIdLessThan(in int32) Query { return Query{query.Node{ Condition: query.Condition{ - Column: "favorite_color", - Operator: query.Like, - Value: fmt.Sprintf("'%s%%'", in), + Column: "fk_city_id", + Operator: query.LessThan, + Value: in, }, }} } -func FavoriteColorStartsWithNot(in string) Query { +func HometownIdNot(in int32) Query { return Query{query.Node{ Condition: query.Condition{ - Column: "favorite_color", - Operator: query.NotLike, - Value: fmt.Sprintf("'%s%%'", in), + Column: "fk_city_id", + Operator: query.NotEquals, + Value: in, }, }} } diff --git a/internal/repository/generate_query.go b/internal/repository/generate_query.go index af2a748..d9c2f24 100644 --- a/internal/repository/generate_query.go +++ b/internal/repository/generate_query.go @@ -35,7 +35,7 @@ func NewQueryFileGenerator(reposPath string, findPackagePath Finder, db schema.D for i, n := range r.ColNames(ft) { c := ft.PKColumns()[i] // Override the GoName in order to generate correct method/function names - c.GoName = ft.ExportedGoName() + c.ExportedGoName() + c.GoName = r.ExportedGoName() + c.ExportedGoName() ms, fs, is = template.GenerateQueryLogic(n, c) } diff --git a/internal/schema/schema.go b/internal/schema/schema.go index 5579104..fd1bd09 100644 --- a/internal/schema/schema.go +++ b/internal/schema/schema.go @@ -37,6 +37,7 @@ type Column struct { // Reference represents a relationship boetween tables. // Not a SQL-native concept, more of an ORM-style design. Translates to foreign keys and constraints in SQL type Reference struct { + GoName string TableName string HasOne bool HasMany bool diff --git a/internal/schema/schema_column.go b/internal/schema/schema_column.go index 185c720..c4da6ae 100644 --- a/internal/schema/schema_column.go +++ b/internal/schema/schema_column.go @@ -2,6 +2,7 @@ package schema import "fmt" +// ExportedGoName returns the string that will be used for naming Exported types, functions, etc in generated Go code func (c *Column) ExportedGoName() string { if c.GoName != "" { return pascal(c.GoName) @@ -10,6 +11,7 @@ func (c *Column) ExportedGoName() string { return pascal(c.Name) } +// GoTypeString returns the string keyword of the column type's corresponding Go type func (c *Column) GoTypeString() string { s := c.Datatype.GoTypeString() @@ -20,6 +22,7 @@ func (c *Column) GoTypeString() string { return s } +// RequiredImport returns any packages that need to be imported to support the Go type of a column in generated Go code func (c *Column) RequiredImport() string { if c.Datatype.IsTime() { return `"time"` diff --git a/internal/schema/schema_reference.go b/internal/schema/schema_reference.go index 0af063a..dbcdba4 100644 --- a/internal/schema/schema_reference.go +++ b/internal/schema/schema_reference.go @@ -31,3 +31,12 @@ func (r *Reference) ColNames(ft Table) []string { return fknames } + +// ExportedGoName returns the string that will be used for naming Exported types, functions, etc in generated Go code +func (r *Reference) ExportedGoName() string { + if r.GoName != "" { + return pascal(r.GoName) + } + + return pascal(r.TableName) +} diff --git a/internal/schema/schema_reference_test.go b/internal/schema/schema_reference_test.go index 95a108e..475b92a 100644 --- a/internal/schema/schema_reference_test.go +++ b/internal/schema/schema_reference_test.go @@ -72,3 +72,48 @@ func TestReference_ColNames(t *testing.T) { }) } } + +func TestReference_ExportedGoName(t *testing.T) { + type fields struct { + GoName string + TableName string + } + tests := []struct { + name string + fields fields + want string + }{ + { + name: "from table name", + fields: fields{ + TableName: "tableName", + }, + want: "TableName", + }, + { + name: "from goname", + fields: fields{ + GoName: "goTable", + }, + want: "GoTable", + }, + { + name: "from table name with underscore", + fields: fields{ + GoName: "table_name", + }, + want: "TableName", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + r := &Reference{ + GoName: tt.fields.GoName, + TableName: tt.fields.TableName, + } + if got := r.ExportedGoName(); got != tt.want { + t.Errorf("ExportedGoName() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/internal/schema/schema_unmarshal.go b/internal/schema/schema_unmarshal.go index 6d775d4..edef4e9 100644 --- a/internal/schema/schema_unmarshal.go +++ b/internal/schema/schema_unmarshal.go @@ -151,6 +151,8 @@ func (r *Reference) UnmarshalYAML(value *yaml.Node) (err error) { err = value.Content[i+1].Decode(&r.OnDelete) case "on_update": err = value.Content[i+1].Decode(&r.OnUpdate) + case "go_name": + err = value.Content[i+1].Decode(&r.GoName) } if err != nil {