Skip to content

Commit

Permalink
Merge pull request #22 from yoyo-project/task/reference-goname
Browse files Browse the repository at this point in the history
Adds GoName to references for better query gen
  • Loading branch information
dotvezz authored Jul 2, 2021
2 parents 71c60a5 + 54018be commit 69b62dd
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 78 deletions.
1 change: 1 addition & 0 deletions example/yoyo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ schema:
- favorite_color
references:
city:
go_name: Hometown
has_one: true
required: false
154 changes: 77 additions & 77 deletions example/yoyo/repositories/query/person/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}}
}
Expand Down Expand Up @@ -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,
},
}}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/repository/generate_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
1 change: 1 addition & 0 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions internal/schema/schema_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()

Expand All @@ -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"`
Expand Down
9 changes: 9 additions & 0 deletions internal/schema/schema_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading

0 comments on commit 69b62dd

Please sign in to comment.