Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardfeng-db committed Aug 19, 2024
1 parent ead8117 commit ad850a7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 29 deletions.
3 changes: 2 additions & 1 deletion common/force_send_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"reflect"
"strings"

"github.com/databricks/terraform-provider-databricks/internal/reflect_utils"
"golang.org/x/exp/slices"
)

Expand All @@ -29,7 +30,7 @@ func SetForceSendFields(req any, d attributeGetter, fields []string) {
if !ok {
panic(fmt.Errorf("request argument to setForceSendFields must have ForceSendFields field of type []string (got %s)", forceSendFieldsField.Type()))
}
fs := ListAllFields(rv)
fs := reflect_utils.ListAllFields(rv)
for _, fieldName := range fields {
found := false
var structField reflect.StructField
Expand Down
27 changes: 3 additions & 24 deletions common/reflect_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"strings"

"github.com/databricks/terraform-provider-databricks/internal/reflect_utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -382,28 +383,6 @@ func diffSuppressor(fieldName string, v *schema.Schema) func(k, old, new string,
}
}

type Field struct {
Sf reflect.StructField
V reflect.Value
}

func ListAllFields(v reflect.Value) []Field {
t := v.Type()
fields := make([]Field, 0, v.NumField())
for i := 0; i < v.NumField(); i++ {
f := t.Field(i)
if f.Anonymous {
fields = append(fields, ListAllFields(v.Field(i))...)
} else {
fields = append(fields, Field{
Sf: f,
V: v.Field(i),
})
}
}
return fields
}

func typeToSchema(v reflect.Value, aliases map[string]map[string]string, tc trackingContext) map[string]*schema.Schema {
if rpStruct, ok := resourceProviderRegistry[getNonPointerType(v.Type())]; ok {
return resourceProviderStructToSchemaInternal(rpStruct, tc.pathCtx).GetSchemaMap()
Expand All @@ -418,7 +397,7 @@ func typeToSchema(v reflect.Value, aliases map[string]map[string]string, tc trac
panic(fmt.Errorf("Schema value of Struct is expected, but got %s: %#v", reflectKind(rk), v))
}
tc = tc.visit(v)
fields := ListAllFields(v)
fields := reflect_utils.ListAllFields(v)
for _, field := range fields {
typeField := field.Sf
if tc.depthExceeded(typeField) {
Expand Down Expand Up @@ -597,7 +576,7 @@ func iterFields(rv reflect.Value, path []string, s map[string]*schema.Schema, al
return fmt.Errorf("%s: got invalid reflect value %#v", path, rv)
}
isGoSDK := isGoSdk(rv)
fields := ListAllFields(rv)
fields := reflect_utils.ListAllFields(rv)
for _, field := range fields {
typeField := field.Sf
fieldName := chooseFieldNameWithAliases(typeField, rv.Type(), aliases)
Expand Down
25 changes: 25 additions & 0 deletions internal/reflect_utils/reflect_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package reflect_utils

import "reflect"

type Field struct {
Sf reflect.StructField
V reflect.Value
}

func ListAllFields(v reflect.Value) []Field {
t := v.Type()
fields := make([]Field, 0, v.NumField())
for i := 0; i < v.NumField(); i++ {
f := t.Field(i)
if f.Anonymous {
fields = append(fields, ListAllFields(v.Field(i))...)
} else {
fields = append(fields, Field{
Sf: f,
V: v.Field(i),
})
}
}
return fields
}
5 changes: 3 additions & 2 deletions pluginframework/common/go_to_tf.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"reflect"

"github.com/databricks/databricks-sdk-go/logger"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/databricks/terraform-provider-databricks/internal/reflect_utils"
)

// Converts a gosdk struct into a tfsdk struct, with the folowing rules.
Expand Down Expand Up @@ -54,7 +55,7 @@ func GoSdkToTfSdkStruct(gosdk interface{}, tfsdk interface{}, ctx context.Contex
forceSendFieldVal = forceSendField.Interface().([]string)
}

for _, field := range common.ListAllFields(srcVal) {
for _, field := range reflect_utils.ListAllFields(srcVal) {
srcField := field.V
srcFieldName := field.Sf.Name

Expand Down
5 changes: 3 additions & 2 deletions pluginframework/common/tf_to_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"reflect"

"github.com/databricks/databricks-sdk-go/logger"
"github.com/databricks/terraform-provider-databricks/common"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/databricks/terraform-provider-databricks/internal/reflect_utils"
)

// Converts a tfsdk struct into a gosdk struct, with the folowing rules.
Expand Down Expand Up @@ -45,7 +46,7 @@ func TfSdkToGoSdkStruct(tfsdk interface{}, gosdk interface{}, ctx context.Contex

forceSendFieldsField := destVal.FieldByName("ForceSendFields")

allFields := common.ListAllFields(srcVal)
allFields := reflect_utils.ListAllFields(srcVal)
for _, field := range allFields {
srcField := field.V
srcFieldName := field.Sf.Name
Expand Down

0 comments on commit ad850a7

Please sign in to comment.