Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardfeng-db committed Mar 7, 2024
1 parent 694f748 commit 8ea0273
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
9 changes: 4 additions & 5 deletions common/reflect_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ func reflectKind(k reflect.Kind) string {
return n
}

func chooseFieldNameWithAliases(typeField reflect.StructField, parentTypeName string, aliases map[string]map[string]string) string {
func chooseFieldNameWithAliases(typeField reflect.StructField, parentType reflect.Type, aliases map[string]map[string]string) string {
parentTypeName := getNameForType(parentType)
fieldNameWithAliasTag := chooseFieldName(typeField)
// If nothing in the aliases map, return the field name from plain chooseFieldName method.
if len(aliases) == 0 {
Expand Down Expand Up @@ -299,7 +300,6 @@ func listAllFields(v reflect.Value) []field {
}

func typeToSchema(v reflect.Value, aliases map[string]map[string]string, rt recursionTrackingContext) map[string]*schema.Schema {
typeName := getNameForType(v.Type())
scm := map[string]*schema.Schema{}
rk := v.Kind()
if rk == reflect.Ptr {
Expand All @@ -321,7 +321,7 @@ func typeToSchema(v reflect.Value, aliases map[string]map[string]string, rt recu
}
tfTag := typeField.Tag.Get("tf")

fieldName := chooseFieldNameWithAliases(typeField, typeName, aliases)
fieldName := chooseFieldNameWithAliases(typeField, v.Type(), aliases)
if fieldName == "-" {
continue
}
Expand Down Expand Up @@ -488,10 +488,9 @@ func iterFields(rv reflect.Value, path []string, s map[string]*schema.Schema, al
}
isGoSDK := isGoSdk(rv)
fields := listAllFields(rv)
parentTypeName := getNameForType(rv.Type())
for _, field := range fields {
typeField := field.sf
fieldName := chooseFieldNameWithAliases(typeField, parentTypeName, aliases)
fieldName := chooseFieldNameWithAliases(typeField, rv.Type(), aliases)
if fieldName == "-" {
continue
}
Expand Down
5 changes: 4 additions & 1 deletion common/reflect_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ func TestChooseFieldName(t *testing.T) {
}

func TestChooseFieldNameWithAliasesMap(t *testing.T) {
type Bar struct {
Foo string `json:"foo,omitempty"`
}
assert.Equal(t, "foo", chooseFieldNameWithAliases(reflect.StructField{
Tag: `json:"bar"`,
}, "Bar", map[string]map[string]string{"Bar": {"bar": "foo"}}))
}, reflect.ValueOf(Bar{}).Type(), map[string]map[string]string{"Bar": {"bar": "foo"}}))
}

type testSliceItem struct {
Expand Down

0 comments on commit 8ea0273

Please sign in to comment.