Skip to content

Commit

Permalink
Merge pull request 99designs#370 from rodrigo-brito/fix-underscore
Browse files Browse the repository at this point in the history
Underscore on field name finder
  • Loading branch information
vektah authored Oct 11, 2018
2 parents 91cc339 + e1639a2 commit 10dc43e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions codegen/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestGenerateServer(t *testing.T) {
}
type User {
id: Int
fist_name: String
}
`
serverFilename := "gen/" + name + "/server/server.go"
Expand Down
10 changes: 8 additions & 2 deletions codegen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ func findMethod(typ *types.Named, name string) *types.Func {
return nil
}

func equalFieldName(source, target string) bool {
source = strings.Replace(source, "_", "", -1)
target = strings.Replace(target, "_", "", -1)
return strings.EqualFold(source, target)
}

// findField attempts to match the name to a struct field with the following
// priorites:
// 1. If struct tag is passed then struct tag has highest priority
Expand All @@ -120,7 +126,7 @@ func findField(typ *types.Struct, name, structTag string) (*types.Var, error) {
if structTag != "" {
tags := reflect.StructTag(typ.Tag(i))
if val, ok := tags.Lookup(structTag); ok {
if strings.EqualFold(val, name) {
if equalFieldName(val, name) {
if foundField != nil && foundFieldWasTag {
return nil, errors.Errorf("tag %s is ambigious; multiple fields have the same tag value of %s", structTag, val)
}
Expand Down Expand Up @@ -156,7 +162,7 @@ func findField(typ *types.Struct, name, structTag string) (*types.Var, error) {
continue
}

if strings.EqualFold(field.Name(), name) && foundField == nil {
if equalFieldName(field.Name(), name) && foundField == nil { // aqui!
foundField = field
}
}
Expand Down
27 changes: 25 additions & 2 deletions codegen/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Embed struct {
Test string
}
`
scope, err := parseScope(input, "test")
scope, err := parseScope(t, input, "test")
require.NoError(t, err)

std := scope.Lookup("Std").Type().Underlying().(*types.Struct)
Expand Down Expand Up @@ -82,7 +82,8 @@ type Embed struct {
}
}

func parseScope(input interface{}, packageName string) (*types.Scope, error) {
func parseScope(t *testing.T, input interface{}, packageName string) (*types.Scope, error) {
t.Helper()
// test setup to parse the types
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "test.go", input, 0)
Expand All @@ -98,3 +99,25 @@ func parseScope(input interface{}, packageName string) (*types.Scope, error) {

return pkg.Scope(), nil
}

func TestEqualFieldName(t *testing.T) {
tt := []struct {
Name string
Source string
Target string
Expected bool
}{
{Name: "words with same case", Source: "test", Target: "test", Expected: true},
{Name: "words different case", Source: "test", Target: "tEsT", Expected: true},
{Name: "different words", Source: "foo", Target: "bar", Expected: false},
{Name: "separated with underscore", Source: "the_test", Target: "TheTest", Expected: true},
{Name: "empty values", Source: "", Target: "", Expected: true},
}

for _, tc := range tt {
t.Run(tc.Name, func(t *testing.T) {
result := equalFieldName(tc.Source, tc.Target)
require.Equal(t, tc.Expected, result)
})
}
}

0 comments on commit 10dc43e

Please sign in to comment.