Skip to content

Commit

Permalink
Merge pull request #166 from erizocosmico/fix/ctor-type
Browse files Browse the repository at this point in the history
fix type name recognition on ctor args and return values
  • Loading branch information
Miguel Molina committed Jun 7, 2017
2 parents da6ce02 + b9327c5 commit 4bf11e1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
17 changes: 8 additions & 9 deletions generator/template.go
Expand Up @@ -588,7 +588,7 @@ func (td *TemplateData) genFindBy(buf *bytes.Buffer, parent *Model, fields []*Fi
}

func writeFindByTpl(buf *bytes.Buffer, parent *Model, name string, f *Field, tpl string) {
findableTypeName, ok := findableTypeName(f)
findableTypeName, ok := f.typeName()
if !ok {
return
}
Expand All @@ -598,16 +598,15 @@ func writeFindByTpl(buf *bytes.Buffer, parent *Model, name string, f *Field, tpl
buf.WriteString(fmt.Sprintf(tpl, name, query, findableTypeName, model))
}

// findableTypeName returns the short name of the type that can be used to search
// for the passed Field, in an autogenerated 'FindBy';
// the second value returned is true if it was found a valid type
func findableTypeName(f *Field) (string, bool) {
analyzedType := f.Node.Type()
// findableTypeName returns the correct go type name with its qualifier for
// the given type. It returns such name along with a boolean reporting whether
// such type was found or not.
func findableTypeName(typ types.Type, pkg *types.Package) (string, bool) {
collectionAlreadyScanned := false
for {
valid, deepest := lookupValid(f.Node.Pkg(), analyzedType)
valid, deepest := lookupValid(pkg, typ)
if valid != nil {
return shortName(f.Node.Pkg(), analyzedType), true
return shortName(pkg, typ), true
}

if collectionAlreadyScanned {
Expand All @@ -619,7 +618,7 @@ func findableTypeName(f *Field) (string, bool) {
break
}

analyzedType = singular
typ = singular
collectionAlreadyScanned = true
}

Expand Down
2 changes: 1 addition & 1 deletion generator/template_test.go
Expand Up @@ -544,7 +544,7 @@ func (s *ProcessorSuite) assertFindableTypeName(f *Field) {
return
}

findableTypeName, ok := findableTypeName(f)
findableTypeName, ok := findableTypeName(f.Node.Type(), f.Node.Pkg())
if expected := f.Tag.Get("findable"); expected != "" {
relativeFindableTypeName := getRelativeTypeName(findableTypeName, "foo")
s.True(ok, fmt.Sprintf("Could not be found the findable type name of '%s' %s", f.Name, f.Node.Type()))
Expand Down
24 changes: 19 additions & 5 deletions generator/types.go
Expand Up @@ -309,11 +309,16 @@ func (m *Model) CtorArgs() string {
paramsLen := sig.Params().Len()
for i := 0; i < paramsLen; i++ {
param := sig.Params().At(i)
typeName := typeString(param.Type(), m.Package)

// TODO: refactor findableTypeName so this is not needed
// or split into two functions
typeName, ok := findableTypeName(param.Type(), m.Package)
if !ok {
typeName = typeString(param.Type(), m.Package)
}

if paramsLen == i+1 && sig.Variadic() {
// since it's variadic, type name is []T instead of T
// so we gotta get rid of the []
typeName = "..." + typeName[2:]
typeName = "..." + typeName
}
paramName := param.Name()
if paramName == "s" {
Expand Down Expand Up @@ -360,7 +365,12 @@ func (m *Model) CtorReturns() string {

for i := 0; i < sig.Results().Len(); i++ {
res := sig.Results().At(i)
typeName := typeString(res.Type(), m.Package)
// TODO: refactor findableTypeName so this is not needed
// or split into two functions
typeName, ok := findableTypeName(res.Type(), m.Package)
if !ok {
typeName = typeString(res.Type(), m.Package)
}
if isTypeOrPtrTo(res.Type(), m.Node) {
ret = append(ret, "record "+typeName)
} else if isBuiltinError(res.Type()) && !hasError {
Expand Down Expand Up @@ -727,6 +737,10 @@ func (f *Field) Address() string {
return f.wrapAddress(name, casted)
}

func (f *Field) typeName() (string, bool) {
return findableTypeName(f.Node.Type(), f.Node.Pkg())
}

func (f *Field) wrapAddress(ptr string, casted bool) string {
if f.IsJSON {
return fmt.Sprintf("types.JSON(%s)", ptr)
Expand Down
10 changes: 5 additions & 5 deletions generator/types_test.go
Expand Up @@ -212,7 +212,7 @@ import (
type User struct {
kallax.Model ` + "`table:\"users\"`" + `
ID int64 ` + "`pk:\"autoincr\"`" + `
ID kallax.ULID ` + "`pk:\"\"`" + `
Username string
Email string
Password Password
Expand All @@ -221,11 +221,11 @@ type User struct {
Settings *Settings
}
func newUser(username, email string) (*User, error) {
func newUser(id kallax.ULID, username, email string) (*User, error) {
if strings.Contains(email, "@spam.org") {
return nil, errors.New("kallax: is spam!")
}
return &User{Username: username, Email: email}, nil
return &User{ID: id, Username: username, Email: email}, nil
}
type Email struct {
Expand Down Expand Up @@ -302,8 +302,8 @@ func (s *ModelSuite) TestModel() {
}

func (s *ModelSuite) TestCtor() {
s.Equal("username string, email string", s.model.CtorArgs())
s.Equal("username, email", s.model.CtorArgVars())
s.Equal("id kallax.ULID, username string, email string", s.model.CtorArgs())
s.Equal("id, username, email", s.model.CtorArgVars())
s.Equal("(record *User, err error)", s.model.CtorReturns())
s.Equal("record, err", s.model.CtorRetVars())
}
Expand Down

0 comments on commit 4bf11e1

Please sign in to comment.