Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix schema path elem traversal #1757

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions pkg/tfbridge/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ func propertyPathToSchemaPathInner(
}

// Detect single-nested blocks (object types).
if res, isRes := schema.Elem().(shim.Resource); schema.Type() == shim.TypeMap && isRes {
return propertyPathToSchemaPath(basePath, propertyPath, res.Schema(), schemaInfo.Fields)
//
// This is the case where (schema & schema.Elem) ~ {x: T}.
if res, isRes := util.CastToTypeObject(schema); isRes {
return propertyPathToSchemaPath(basePath, propertyPath, res, schemaInfo.Fields)
VenelinMartinov marked this conversation as resolved.
Show resolved Hide resolved
}

// Detect collections.
Expand All @@ -108,8 +110,16 @@ func propertyPathToSchemaPathInner(
elemPP = propertyPath[1:]
}
switch e := schema.Elem().(type) {

// (schema, schemaInfo) represents a list or set nested object (schema ~
// List[{x: T}] or schema ~ Set[{x: T}]), so we traverse the inner object
// with the associated .Elem.Fields.
case shim.Resource: // object element type
return propertyPathToSchemaPath(basePath.Element(), elemPP, e.Schema(), schemaInfo.Fields)
elem := schemaInfo.Elem
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, very annoying omission. For this case schemaInfo.Elem.Fields makes so much more sense.

if elem == nil {
elem = &SchemaInfo{}
}
return propertyPathToSchemaPath(basePath.Element(), elemPP, e.Schema(), elem.Fields)
case shim.Schema: // non-object element type
return propertyPathToSchemaPathInner(basePath.Element(), elemPP, e, schemaInfo.Elem)
case nil: // unknown element type
Expand Down Expand Up @@ -219,8 +229,16 @@ func schemaPathToPropertyPathInner(
basePath = append(basePath, "*")
}
switch e := schema.Elem().(type) {

// (schema, schemaInfo) represents a list or set nested object (schema ~
// List[{x: T}] or schema ~ Set[{x: T}]), so we traverse the inner object
// with the associated .Elem.Fields.
case shim.Resource: // object element type
return schemaPathToPropertyPath(basePath, schemaPath[1:], e.Schema(), schemaInfo.Fields)
elem := schemaInfo.Elem
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this also need a test? Looks like the tests only cover the other direction.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is bi-directional. Both changes are covered.


I actually only noticed that this needed to be fixed because the test I added was still failing.

if elem == nil {
elem = &SchemaInfo{}
}
return schemaPathToPropertyPath(basePath, schemaPath[1:], e.Schema(), elem.Fields)
case shim.Schema: // non-object element type
return schemaPathToPropertyPathInner(basePath, schemaPath[1:], e, schemaInfo.Elem)
case nil: // unknown element type
Expand Down
17 changes: 17 additions & 0 deletions pkg/tfbridge/walk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func TestPropertyPathToSchemaPath(t *testing.T) {
Type: shim.TypeMap,
Elem: xySchema,
}).Shim(),
"list_obj": (&schema.Schema{
Type: shim.TypeList,
Elem: xySchema,
}).Shim(),
}

schemaInfos := map[string]*SchemaInfo{
Expand All @@ -79,6 +83,14 @@ func TestPropertyPathToSchemaPath(t *testing.T) {
"list_str_named": {
Name: "listStr",
},
"list_obj": {
Name: "listObj",
Elem: &SchemaInfo{
Fields: map[string]*SchemaInfo{
"x_prop": {Name: "xOverride"},
},
},
},
}

type testCase struct {
Expand Down Expand Up @@ -164,6 +176,11 @@ func TestPropertyPathToSchemaPath(t *testing.T) {
pp: []any{"flatListViaSchemaInfo", "xProp"},
expected: walk.NewSchemaPath().GetAttr("flat_list_via_schema_info").Element().GetAttr("x_prop"),
},
{
name: "override list nested object property",
pp: resource.PropertyPath{"listObj", 0, "xOverride"},
expected: walk.NewSchemaPath().GetAttr("list_obj").Element().GetAttr("x_prop"),
},
}

for _, tc := range cases {
Expand Down