Skip to content

Commit

Permalink
Rename polymorphic type
Browse files Browse the repository at this point in the history
Signed-off-by: Shawn Wang <shawn.wang@broadcom.com>
  • Loading branch information
wsquan171 committed Jun 10, 2024
1 parent d46401d commit 40070d3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 55 deletions.
48 changes: 24 additions & 24 deletions nsxt/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
var logger = log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile)

const (
PolymorphicTypeFlat = "flat"
PolymorphicTypeWrapped = "wrapped"
PolymorphicTypeFlatten = "flatten"
PolymorphicTypeNested = "nested"
)

type Metadata struct {
Expand All @@ -35,10 +35,10 @@ type Metadata struct {
// SDK vapi binding type for converting polymorphic structs
BindingType vapiBindings_.BindingType
// SDK resource type to match and filter for a schema key
// Only applicable to PolymorphicTypeFlat schema
// Only applicable to PolymorphicTypeFlatten schema
ResourceType string
// Map from schema key of polymorphic attr to this SDK resource type
// Only applicable to PolymorphicTypeWrapped schema
// Only applicable to PolymorphicTypeNested schema
ResourceTypeMap map[string]string
// Type identifier name for both SDK and JSON (StructValue field name)
TypeIdentifier TypeIdentifier
Expand Down Expand Up @@ -168,10 +168,10 @@ func StructToSchema(elem reflect.Value, d *schema.ResourceData, metadata map[str
childElem := elem.FieldByName(item.Metadata.SdkFieldName)
var nestedVal interface{}
switch item.Metadata.PolymorphicType {
case PolymorphicTypeWrapped:
nestedVal, err = polyStructToWrappedSchema(ctx, childElem, item)
case PolymorphicTypeFlat:
nestedVal, err = polyStructToFlatSchema(ctx, childElem, key, item)
case PolymorphicTypeNested:
nestedVal, err = polyStructToNestedSchema(ctx, childElem, item)
case PolymorphicTypeFlatten:
nestedVal, err = polyStructToFlattenSchema(ctx, childElem, key, item)
default:
err = fmt.Errorf("%s unknown polymorphic type %s", ctx,
item.Metadata.PolymorphicType)
Expand Down Expand Up @@ -287,10 +287,10 @@ func SchemaToStruct(elem reflect.Value, d *schema.ResourceData, metadata map[str
if len(item.Metadata.PolymorphicType) > 0 {
itemList := getItemListForSchemaToStruct(d, item.Metadata.SchemaType, key, parent, parentMap)
switch item.Metadata.PolymorphicType {
case PolymorphicTypeWrapped:
err = polyWrappedSchemaToStruct(ctx, elem, itemList, item)
case PolymorphicTypeFlat:
err = polyFlatSchemaToStruct(ctx, elem, key, itemList, item)
case PolymorphicTypeNested:
err = polyNestedSchemaToStruct(ctx, elem, itemList, item)
case PolymorphicTypeFlatten:
err = polyFlattenSchemaToStruct(ctx, elem, key, itemList, item)
default:
err = fmt.Errorf("%s unknown polymorphic type %s", ctx,
item.Metadata.PolymorphicType)
Expand Down Expand Up @@ -437,9 +437,9 @@ func getResourceTypeFromStructValue(ctx string, value data.StructValue, identifi
return "", err
}

func polyStructToWrappedSchema(ctx string, elem reflect.Value, item *ExtendedSchema) (ret []map[string]interface{}, err error) {
if item.Metadata.PolymorphicType != PolymorphicTypeWrapped {
err = fmt.Errorf("%s polyStructToWrappedSchema called on non-wrapped polymorphic attr", ctx)
func polyStructToNestedSchema(ctx string, elem reflect.Value, item *ExtendedSchema) (ret []map[string]interface{}, err error) {
if item.Metadata.PolymorphicType != PolymorphicTypeNested {
err = fmt.Errorf("%s polyStructToNestedSchema called on non-wrapped polymorphic attr", ctx)
logger.Printf("[ERROR] %v", err)
return
}
Expand Down Expand Up @@ -508,9 +508,9 @@ func polyStructToWrappedSchema(ctx string, elem reflect.Value, item *ExtendedSch
return
}

func polyWrappedSchemaToStruct(ctx string, elem reflect.Value, dataList []interface{}, item *ExtendedSchema) (err error) {
if item.Metadata.PolymorphicType != PolymorphicTypeWrapped {
err = fmt.Errorf("%s polyWrappedSchemaToStruct called on non-wrapped polymorphic attr", ctx)
func polyNestedSchemaToStruct(ctx string, elem reflect.Value, dataList []interface{}, item *ExtendedSchema) (err error) {
if item.Metadata.PolymorphicType != PolymorphicTypeNested {
err = fmt.Errorf("%s polyNestedSchemaToStruct called on non-wrapped polymorphic attr", ctx)
logger.Printf("[ERROR] %v", err)
return
}
Expand Down Expand Up @@ -584,9 +584,9 @@ func polyWrappedSchemaToStruct(ctx string, elem reflect.Value, dataList []interf
return
}

func polyStructToFlatSchema(ctx string, elem reflect.Value, key string, item *ExtendedSchema) (ret []map[string]interface{}, err error) {
if item.Metadata.PolymorphicType != PolymorphicTypeFlat {
err = fmt.Errorf("%s polyStructToFlatSchema called on non-flat polymorphic attr", ctx)
func polyStructToFlattenSchema(ctx string, elem reflect.Value, key string, item *ExtendedSchema) (ret []map[string]interface{}, err error) {
if item.Metadata.PolymorphicType != PolymorphicTypeFlatten {
err = fmt.Errorf("%s polyStructToFlattenSchema called on non-flat polymorphic attr", ctx)
logger.Printf("[ERROR] %v", err)
return
}
Expand Down Expand Up @@ -629,9 +629,9 @@ func polyStructToFlatSchema(ctx string, elem reflect.Value, key string, item *Ex
return
}

func polyFlatSchemaToStruct(ctx string, elem reflect.Value, key string, dataList []interface{}, item *ExtendedSchema) (err error) {
if item.Metadata.PolymorphicType != PolymorphicTypeFlat {
err = fmt.Errorf("%s polyFlatSchemaToStruct called on non-flat polymorphic attr", ctx)
func polyFlattenSchemaToStruct(ctx string, elem reflect.Value, key string, dataList []interface{}, item *ExtendedSchema) (err error) {
if item.Metadata.PolymorphicType != PolymorphicTypeFlatten {
err = fmt.Errorf("%s polyFlattenSchemaToStruct called on non-flat polymorphic attr", ctx)
logger.Printf("[ERROR] %v", err)
return
}
Expand Down
62 changes: 31 additions & 31 deletions nsxt/metadata/metadata_poly_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type testPolyListStruct struct {
PolyList []*data.StructValue
}

func testPolyStructWrappedSchema(t string) map[string]*schema.Schema {
func testPolyStructNestedSchema(t string) map[string]*schema.Schema {
schemaType := schema.TypeList
maxItems := 0
if t == "set" {
Expand Down Expand Up @@ -143,7 +143,7 @@ func testPolyStructWrappedSchema(t string) map[string]*schema.Schema {
}
}

func testPolyStructWrappedExtSchema(t, sdkName string) map[string]*ExtendedSchema {
func testPolyStructNestedExtSchema(t, sdkName string) map[string]*ExtendedSchema {
typeIdentier := TypeIdentifier{
SdkName: "Type",
APIFieldName: "type",
Expand Down Expand Up @@ -207,7 +207,7 @@ func testPolyStructWrappedExtSchema(t, sdkName string) map[string]*ExtendedSchem
Metadata: Metadata{
SchemaType: t,
SdkFieldName: sdkName,
PolymorphicType: PolymorphicTypeWrapped,
PolymorphicType: PolymorphicTypeNested,
ResourceTypeMap: map[string]string{
"cat": "FakeCat",
"coffee": "FakeCoffee",
Expand All @@ -218,7 +218,7 @@ func testPolyStructWrappedExtSchema(t, sdkName string) map[string]*ExtendedSchem
}
}

func TestPolyStructToWrappedSchema(t *testing.T) {
func TestPolyStructToNestedSchema(t *testing.T) {
t.Run("cat struct", func(t *testing.T) {
name := "matcha"
rType := "FakeCat"
Expand All @@ -234,10 +234,10 @@ func TestPolyStructToWrappedSchema(t *testing.T) {
assert.Nil(t, errors, "unexpected error calling ConvertToGolang")
obj.PolyStruct = dv.(*data.StructValue)
d := schema.TestResourceDataRaw(
t, testPolyStructWrappedSchema("struct"), map[string]interface{}{})
t, testPolyStructNestedSchema("struct"), map[string]interface{}{})

elem := reflect.ValueOf(&obj).Elem()
err := StructToSchema(elem, d, testPolyStructWrappedExtSchema("struct", "PolyStruct"), "", nil)
err := StructToSchema(elem, d, testPolyStructNestedExtSchema("struct", "PolyStruct"), "", nil)
assert.NoError(t, err, "unexpected error calling StructToSchema")
assert.Len(t, d.Get("poly_struct"), 1)
polyData := d.Get("poly_struct").([]interface{})[0].(map[string]interface{})
Expand All @@ -264,10 +264,10 @@ func TestPolyStructToWrappedSchema(t *testing.T) {
assert.Nil(t, errors, "unexpected error calling ConvertToGolang")
obj.PolyStruct = dv.(*data.StructValue)
d := schema.TestResourceDataRaw(
t, testPolyStructWrappedSchema("struct"), map[string]interface{}{})
t, testPolyStructNestedSchema("struct"), map[string]interface{}{})

elem := reflect.ValueOf(&obj).Elem()
err := StructToSchema(elem, d, testPolyStructWrappedExtSchema("struct", "PolyStruct"), "", nil)
err := StructToSchema(elem, d, testPolyStructNestedExtSchema("struct", "PolyStruct"), "", nil)
assert.NoError(t, err, "unexpected error calling StructToSchema")
assert.Len(t, d.Get("poly_struct"), 1)
polyData := d.Get("poly_struct").([]interface{})[0].(map[string]interface{})
Expand Down Expand Up @@ -307,10 +307,10 @@ func TestPolyStructToWrappedSchema(t *testing.T) {
assert.Nil(t, errors, "unexpected error calling ConvertToGolang")
obj.PolyList[1] = dv.(*data.StructValue)
d := schema.TestResourceDataRaw(
t, testPolyStructWrappedSchema("struct"), map[string]interface{}{})
t, testPolyStructNestedSchema("struct"), map[string]interface{}{})

elem := reflect.ValueOf(&obj).Elem()
err := StructToSchema(elem, d, testPolyStructWrappedExtSchema("list", "PolyList"), "", nil)
err := StructToSchema(elem, d, testPolyStructNestedExtSchema("list", "PolyList"), "", nil)
assert.NoError(t, err, "unexpected error calling StructToSchema")
assert.Len(t, d.Get("poly_struct"), 2)
// idx 0: coffee
Expand Down Expand Up @@ -359,10 +359,10 @@ func TestPolyStructToWrappedSchema(t *testing.T) {
assert.Nil(t, errors, "unexpected error calling ConvertToGolang")
obj.PolyList[0] = dv.(*data.StructValue)
d := schema.TestResourceDataRaw(
t, testPolyStructWrappedSchema("struct"), map[string]interface{}{})
t, testPolyStructNestedSchema("struct"), map[string]interface{}{})

elem := reflect.ValueOf(&obj).Elem()
err := StructToSchema(elem, d, testPolyStructWrappedExtSchema("set", "PolyList"), "", nil)
err := StructToSchema(elem, d, testPolyStructNestedExtSchema("set", "PolyList"), "", nil)
assert.NoError(t, err, "unexpected error calling StructToSchema")
assert.Len(t, d.Get("poly_struct"), 2)
for _, v := range d.Get("poly_struct").([]interface{}) {
Expand All @@ -386,10 +386,10 @@ func TestPolyStructToWrappedSchema(t *testing.T) {
})
}

func TestWrappedSchemaToPolyStruct(t *testing.T) {
func TestNestedSchemaToPolyStruct(t *testing.T) {
t.Run("cat struct", func(t *testing.T) {
d := schema.TestResourceDataRaw(
t, testPolyStructWrappedSchema("struct"), map[string]interface{}{
t, testPolyStructNestedSchema("struct"), map[string]interface{}{
"poly_struct": []interface{}{
map[string]interface{}{
"cat": []interface{}{
Expand All @@ -404,7 +404,7 @@ func TestWrappedSchemaToPolyStruct(t *testing.T) {

obj := testPolyStruct{}
elem := reflect.ValueOf(&obj).Elem()
err := SchemaToStruct(elem, d, testPolyStructWrappedExtSchema("struct", "PolyStruct"), "", nil)
err := SchemaToStruct(elem, d, testPolyStructNestedExtSchema("struct", "PolyStruct"), "", nil)
assert.NoError(t, err, "unexpected error calling SchemaToStruct")

converter := vapiBindings_.NewTypeConverter()
Expand All @@ -417,7 +417,7 @@ func TestWrappedSchemaToPolyStruct(t *testing.T) {

t.Run("coffee struct", func(t *testing.T) {
d := schema.TestResourceDataRaw(
t, testPolyStructWrappedSchema("struct"), map[string]interface{}{
t, testPolyStructNestedSchema("struct"), map[string]interface{}{
"poly_struct": []interface{}{
map[string]interface{}{
"coffee": []interface{}{
Expand All @@ -432,7 +432,7 @@ func TestWrappedSchemaToPolyStruct(t *testing.T) {

obj := testPolyStruct{}
elem := reflect.ValueOf(&obj).Elem()
err := SchemaToStruct(elem, d, testPolyStructWrappedExtSchema("struct", "PolyStruct"), "", nil)
err := SchemaToStruct(elem, d, testPolyStructNestedExtSchema("struct", "PolyStruct"), "", nil)
assert.NoError(t, err, "unexpected error calling SchemaToStruct")

converter := vapiBindings_.NewTypeConverter()
Expand All @@ -445,7 +445,7 @@ func TestWrappedSchemaToPolyStruct(t *testing.T) {

t.Run("mixed list", func(t *testing.T) {
d := schema.TestResourceDataRaw(
t, testPolyStructWrappedSchema("list"), map[string]interface{}{
t, testPolyStructNestedSchema("list"), map[string]interface{}{
"poly_struct": []interface{}{
map[string]interface{}{
"coffee": []interface{}{
Expand All @@ -468,7 +468,7 @@ func TestWrappedSchemaToPolyStruct(t *testing.T) {

obj := testPolyListStruct{}
elem := reflect.ValueOf(&obj).Elem()
err := SchemaToStruct(elem, d, testPolyStructWrappedExtSchema("list", "PolyList"), "", nil)
err := SchemaToStruct(elem, d, testPolyStructNestedExtSchema("list", "PolyList"), "", nil)
assert.NoError(t, err, "unexpected error calling SchemaToStruct")

converter := vapiBindings_.NewTypeConverter()
Expand All @@ -487,7 +487,7 @@ func TestWrappedSchemaToPolyStruct(t *testing.T) {

t.Run("mixed set", func(t *testing.T) {
d := schema.TestResourceDataRaw(
t, testPolyStructWrappedSchema("set"), map[string]interface{}{
t, testPolyStructNestedSchema("set"), map[string]interface{}{
"poly_struct": []interface{}{
map[string]interface{}{
"cat": []interface{}{
Expand All @@ -510,7 +510,7 @@ func TestWrappedSchemaToPolyStruct(t *testing.T) {

obj := testPolyListStruct{}
elem := reflect.ValueOf(&obj).Elem()
err := SchemaToStruct(elem, d, testPolyStructWrappedExtSchema("set", "PolyList"), "", nil)
err := SchemaToStruct(elem, d, testPolyStructNestedExtSchema("set", "PolyList"), "", nil)
assert.NoError(t, err, "unexpected error calling SchemaToStruct")

converter := vapiBindings_.NewTypeConverter()
Expand All @@ -533,7 +533,7 @@ func TestWrappedSchemaToPolyStruct(t *testing.T) {
})
}

func testPolyStructFlatSchema() map[string]*schema.Schema {
func testPolyStructFlattenSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"cat": {
Type: schema.TypeList,
Expand Down Expand Up @@ -564,7 +564,7 @@ func testPolyStructFlatSchema() map[string]*schema.Schema {
}
}

func testPolyStructFlatExtSchema(sdkName string) map[string]*ExtendedSchema {
func testPolyStructFlattenExtSchema(sdkName string) map[string]*ExtendedSchema {
typeIdentifier := TypeIdentifier{
SdkName: "Type",
APIFieldName: "type",
Expand All @@ -586,7 +586,7 @@ func testPolyStructFlatExtSchema(sdkName string) map[string]*ExtendedSchema {
ReflectType: reflect.TypeOf(testCatStruct{}),
BindingType: testCatStructBindingType(),
TypeIdentifier: typeIdentifier,
PolymorphicType: PolymorphicTypeFlat,
PolymorphicType: PolymorphicTypeFlatten,
ResourceType: "FakeCat",
SdkFieldName: sdkName,
},
Expand All @@ -606,15 +606,15 @@ func testPolyStructFlatExtSchema(sdkName string) map[string]*ExtendedSchema {
ReflectType: reflect.TypeOf(testCoffeeStruct{}),
BindingType: testCoffeeStructBindingType(),
TypeIdentifier: typeIdentifier,
PolymorphicType: PolymorphicTypeFlat,
PolymorphicType: PolymorphicTypeFlatten,
ResourceType: "FakeCoffee",
SdkFieldName: sdkName,
},
},
}
}

func TestPolyStructToFlatSchema(t *testing.T) {
func TestPolyStructToFlattenSchema(t *testing.T) {
t.Run("mixed list", func(t *testing.T) {
catName := "oolong"
coffeeName := "mocha"
Expand Down Expand Up @@ -643,10 +643,10 @@ func TestPolyStructToFlatSchema(t *testing.T) {
assert.Nil(t, errors, "unexpected error calling ConvertToGolang")
obj.PolyList[1] = dv.(*data.StructValue)
d := schema.TestResourceDataRaw(
t, testPolyStructFlatSchema(), map[string]interface{}{})
t, testPolyStructFlattenSchema(), map[string]interface{}{})

elem := reflect.ValueOf(&obj).Elem()
err := StructToSchema(elem, d, testPolyStructFlatExtSchema("PolyList"), "", nil)
err := StructToSchema(elem, d, testPolyStructFlattenExtSchema("PolyList"), "", nil)
assert.NoError(t, err, "unexpected error calling StructToSchema")

assert.Len(t, d.Get("coffee"), 1)
Expand All @@ -663,10 +663,10 @@ func TestPolyStructToFlatSchema(t *testing.T) {
})
}

func TestFlatSchemaToPolyStruct(t *testing.T) {
func TestFlattenSchemaToPolyStruct(t *testing.T) {
t.Run("mixed list", func(t *testing.T) {
d := schema.TestResourceDataRaw(
t, testPolyStructFlatSchema(), map[string]interface{}{
t, testPolyStructFlattenSchema(), map[string]interface{}{
"coffee": []interface{}{
map[string]interface{}{
"name": "mocha",
Expand All @@ -683,7 +683,7 @@ func TestFlatSchemaToPolyStruct(t *testing.T) {

obj := testPolyListStruct{}
elem := reflect.ValueOf(&obj).Elem()
err := SchemaToStruct(elem, d, testPolyStructFlatExtSchema("PolyList"), "", nil)
err := SchemaToStruct(elem, d, testPolyStructFlattenExtSchema("PolyList"), "", nil)
assert.NoError(t, err, "unexpected error calling SchemaToStruct")

converter := vapiBindings_.NewTypeConverter()
Expand Down

0 comments on commit 40070d3

Please sign in to comment.