diff --git a/codegen/codegen_test.go b/codegen/codegen_test.go index 20a28179168..90bfbc97a8d 100644 --- a/codegen/codegen_test.go +++ b/codegen/codegen_test.go @@ -16,6 +16,7 @@ func TestGenerateServer(t *testing.T) { } type User { id: Int + fist_name: String } ` serverFilename := "gen/" + name + "/server/server.go" diff --git a/codegen/util.go b/codegen/util.go index 12530cb0b6a..2ecd7c9e750 100644 --- a/codegen/util.go +++ b/codegen/util.go @@ -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 @@ -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) } @@ -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 } } diff --git a/codegen/util_test.go b/codegen/util_test.go index aedfda04694..47bbe2efdac 100644 --- a/codegen/util_test.go +++ b/codegen/util_test.go @@ -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) @@ -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) @@ -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) + }) + } +}