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

internal/fwserver: Ensure Attribute and Block Plan Modification Returns Custom Value Type Implementations When Using Custom Type NestedAttributeObject/NestedBlockObject #823

Merged
merged 6 commits into from
Aug 17, 2023

Conversation

bendbennett
Copy link
Contributor

@bendbennett bendbennett commented Aug 15, 2023

Closes: #767
Closes: #821
Reference: #768

This PR addresses diagnostic errors arising from using a CustomType associated with a NestedObject within a ListNestedAttribute, ListNestedBlock, MapNestedAttribute, SetNestedAttribute or SetNestedBlock. The diagnostic error generated is Invalid ... Element Type. Further details can be found in #821 and #767.

Previously, before logic updates:

=== NAME  TestAttributeModifyPlan/attribute-map-nested-nested-custom-nested-object
    attribute_plan_modification_test.go:3285: Unexpected response (-wanted, +got):   fwserver.ModifyAttributePlanResponse{
                AttributePlan: s`{"outer":{"nested_map_nested_attribute":{"inner":{"nested_attr":`...,
        -       Diagnostics:   nil,
        +       Diagnostics: diag.Diagnostics{
        +               diag.ErrorDiagnostic{
        +                       detail:  "While creating a Map value, an invalid element was detected. A M"...,
        +                       summary: "Invalid Map Element Type",
        +               },
        +               diag.ErrorDiagnostic{
        +                       detail:  "While creating a Map value, an invalid element was detected. A M"...,
        +                       summary: "Invalid Map Element Type",
        +               },
        +       },
                RequiresReplace: s"[]",
                Private:         nil,
          }
=== NAME  TestAttributeModifyPlan/attribute-map-nested-custom-nested-object
    attribute_plan_modification_test.go:3285: Unexpected response (-wanted, +got):   fwserver.ModifyAttributePlanResponse{
                AttributePlan: s`{"testkey":{"nested_attr":"testvalue"}}`,
        -       Diagnostics:   nil,
        +       Diagnostics: diag.Diagnostics{
        +               diag.ErrorDiagnostic{
        +                       detail:  "While creating a Map value, an invalid element was detected. A M"...,
        +                       summary: "Invalid Map Element Type",
        +               },
        +       },
                RequiresReplace: s"[]",
                Private:         nil,
          }
=== NAME  TestAttributeModifyPlan/attribute-set-nested-custom-nested-object
    attribute_plan_modification_test.go:3285: Unexpected response (-wanted, +got):   fwserver.ModifyAttributePlanResponse{
                AttributePlan: s`[{"nested_attr":"testvalue"}]`,
        -       Diagnostics:   nil,
        +       Diagnostics: diag.Diagnostics{
        +               diag.ErrorDiagnostic{
        +                       detail:  "While creating a Set value, an invalid element was detected. A S"...,
        +                       summary: "Invalid Set Element Type",
        +               },
        +       },
                RequiresReplace: s"[]",
                Private:         nil,
          }
=== NAME  TestAttributeModifyPlan/attribute-list-nested-nested-custom-nested-object
    attribute_plan_modification_test.go:3285: Unexpected response (-wanted, +got):   fwserver.ModifyAttributePlanResponse{
                AttributePlan: s`[{"nested_list_nested_attribute":[{"nested_attr":"testvalue"}]}]`,
        -       Diagnostics:   nil,
        +       Diagnostics: diag.Diagnostics{
        +               diag.ErrorDiagnostic{
        +                       detail:  "While creating a List value, an invalid element was detected. A "...,
        +                       summary: "Invalid List Element Type",
        +               },
        +               diag.ErrorDiagnostic{
        +                       detail:  "While creating a List value, an invalid element was detected. A "...,
        +                       summary: "Invalid List Element Type",
        +               },
        +       },
                RequiresReplace: s"[]",
                Private:         nil,
          }
=== NAME  TestAttributeModifyPlan/attribute-list-nested-custom-nested-object
    attribute_plan_modification_test.go:3285: Unexpected response (-wanted, +got):   fwserver.ModifyAttributePlanResponse{
                AttributePlan: s`[{"nested_attr":"testvalue"}]`,
        -       Diagnostics:   nil,
        +       Diagnostics: diag.Diagnostics{
        +               diag.ErrorDiagnostic{
        +                       detail:  "While creating a List value, an invalid element was detected. A "...,
        +                       summary: "Invalid List Element Type",
        +               },
        +       },
                RequiresReplace: s"[]",
                Private:         nil,
          }
=== NAME  TestAttributeModifyPlan/attribute-set-nested-nested-custom-nested-object
    attribute_plan_modification_test.go:3285: Unexpected response (-wanted, +got):   fwserver.ModifyAttributePlanResponse{
                AttributePlan: s`[{"nested_set_nested_attribute":[{"nested_attr":"testvalue"}]}]`,
        -       Diagnostics:   nil,
        +       Diagnostics: diag.Diagnostics{
        +               diag.ErrorDiagnostic{
        +                       detail:  "While creating a Set value, an invalid element was detected. A S"...,
        +                       summary: "Invalid Set Element Type",
        +               },
        +               diag.ErrorDiagnostic{
        +                       detail:  "While creating a Set value, an invalid element was detected. A S"...,
        +                       summary: "Invalid Set Element Type",
        +               },
        +       },
                RequiresReplace: s"[]",
                Private:         nil,
          }
=== NAME  TestBlockModifyPlan/block-set-nested-nested-custom-nested-object
    block_plan_modification_test.go:4040: Error: Invalid Set Element Type
        While creating a Set value, an invalid element was detected. A Set must use the single, given element type. This is always an issue with the provider and should be reported to the provider developers.
        
        Set Element Type: NestedObjectCustomType
        Set Index (0) Element Type: types.ObjectType["nested_attr":basetypes.StringType]
=== NAME  TestBlockModifyPlan/block-set-nested-custom-nested-object
    block_plan_modification_test.go:4040: Error: Invalid Set Element Type
        While creating a Set value, an invalid element was detected. A Set must use the single, given element type. This is always an issue with the provider and should be reported to the provider developers.
        
        Set Element Type: NestedObjectCustomType
        Set Index (0) Element Type: types.ObjectType["nested_attr":basetypes.StringType]
=== NAME  TestBlockModifyPlan/block-list-nested-nested-custom-nested-object
    block_plan_modification_test.go:4040: Error: Invalid List Element Type
        While creating a List value, an invalid element was detected. A List must use the single, given element type. This is always an issue with the provider and should be reported to the provider developers.
        
        List Element Type: NestedObjectCustomType
        List Index (0) Element Type: types.ObjectType["nested_attr":basetypes.StringType]
    block_plan_modification_test.go:4040: Error: Invalid List Element Type
        While creating a List value, an invalid element was detected. A List must use the single, given element type. This is always an issue with the provider and should be reported to the provider developers.
        
        List Element Type: ListNestedObjectCustomType
        List Index (0) Element Type: types.ObjectType["nested_list_nested":types.ListType[NestedObjectCustomType]]
    block_plan_modification_test.go:4042: Unexpected response (+wanted, -got):   fwserver.ModifyAttributePlanResponse{
                AttributePlan: s`[{"nested_list_nested":[{"nested_attr":"testvalue"}]}]`,
        -       Diagnostics:   nil,
        +       Diagnostics: diag.Diagnostics{
        +               diag.ErrorDiagnostic{
        +                       detail:  "While creating a List value, an invalid element was detected. A "...,
        +                       summary: "Invalid List Element Type",
        +               },
        +               diag.ErrorDiagnostic{
        +                       detail:  "While creating a List value, an invalid element was detected. A "...,
        +                       summary: "Invalid List Element Type",
        +               },
        +       },
                RequiresReplace: s"[]",
                Private:         nil,
          }
=== NAME  TestBlockModifyPlan/block-set-nested-custom-nested-object
    block_plan_modification_test.go:4042: Unexpected response (+wanted, -got):   fwserver.ModifyAttributePlanResponse{
                AttributePlan: s`[{"nested_attr":"testvalue"}]`,
        -       Diagnostics:   nil,
        +       Diagnostics: diag.Diagnostics{
        +               diag.ErrorDiagnostic{
        +                       detail:  "While creating a Set value, an invalid element was detected. A S"...,
        +                       summary: "Invalid Set Element Type",
        +               },
        +       },
                RequiresReplace: s"[]",
                Private:         nil,
          }
=== NAME  TestBlockModifyPlan/block-set-nested-nested-custom-nested-object
    block_plan_modification_test.go:4040: Error: Invalid Set Element Type
        While creating a Set value, an invalid element was detected. A Set must use the single, given element type. This is always an issue with the provider and should be reported to the provider developers.
        
        Set Element Type: SetNestedObjectCustomType
        Set Index (0) Element Type: types.ObjectType["nested_set_nested":types.SetType[NestedObjectCustomType]]
=== NAME  TestBlockModifyPlan/block-set-nested-nested-custom-nested-object
    block_plan_modification_test.go:4042: Unexpected response (+wanted, -got):   fwserver.ModifyAttributePlanResponse{
                AttributePlan: s`[{"nested_set_nested":[{"nested_attr":"testvalue"}]}]`,
        -       Diagnostics:   nil,
        +       Diagnostics: diag.Diagnostics{
        +               diag.ErrorDiagnostic{
        +                       detail:  "While creating a Set value, an invalid element was detected. A S"...,
        +                       summary: "Invalid Set Element Type",
        +               },
        +               diag.ErrorDiagnostic{
        +                       detail:  "While creating a Set value, an invalid element was detected. A S"...,
        +                       summary: "Invalid Set Element Type",
        +               },
        +       },
                RequiresReplace: s"[]",
                Private:         nil,
          }
=== NAME  TestBlockModifyPlan/block-list-nested-custom-nested-object
    block_plan_modification_test.go:4040: Error: Invalid List Element Type
        While creating a List value, an invalid element was detected. A List must use the single, given element type. This is always an issue with the provider and should be reported to the provider developers.
        
        List Element Type: NestedObjectCustomType
        List Index (0) Element Type: types.ObjectType["nested_attr":basetypes.StringType]
    block_plan_modification_test.go:4042: Unexpected response (+wanted, -got):   fwserver.ModifyAttributePlanResponse{
                AttributePlan: s`[{"nested_attr":"testvalue"}]`,
        -       Diagnostics:   nil,
        +       Diagnostics: diag.Diagnostics{
        +               diag.ErrorDiagnostic{
        +                       detail:  "While creating a List value, an invalid element was detected. A "...,
        +                       summary: "Invalid List Element Type",
        +               },
        +       },
                RequiresReplace: s"[]",
                Private:         nil,
          }

…ype implementations for list, map, set nested attributes using a nested object with a custom type (#821)

Reference: #768
Reference: #767
@bendbennett bendbennett added the bug Something isn't working label Aug 15, 2023
@bendbennett bendbennett added this to the v1.3.5 milestone Aug 15, 2023
Copy link
Member

@austinvalle austinvalle left a comment

Choose a reason for hiding this comment

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

lgtm 🎸

Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

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

Nice work! One minor testing thing I noticed, otherwise looks good to me 🚀

Comment on lines 90 to 94
basetypes.ObjectType{
AttrTypes: map[string]attr.Type{
"nested_attr": types.StringType,
},
},
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry I might be being dense, should this be c.ObjectValue.Type(ctx) so it matches up with how the type was created?

Similarly with the other custom types below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perhaps I've misunderstood, but I don't think we can use the return value of c.ObjectValue.Type(ctx) as the type is attr.Type and NestedObjectCustomType expects the embedded type to be basetypes.ObjectType:

func (c NestedObjectCustomValue) Type(ctx context.Context) attr.Type {
	return NestedObjectCustomType{
		c.ObjectValue.Type(ctx), // wrong type, expects `basetypes.ObjectType`, gets `attr.Type`
	}
}

However, we could use a call to obtain the attributes for the embedded basetypes.ObjectType, for instance:

func (c NestedObjectCustomValue) Type(ctx context.Context) attr.Type {
	return NestedObjectCustomType{
		basetypes.ObjectType{
			AttrTypes: c.AttributeTypes(ctx),
		},
	}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah yes, you are correct -- I was just surprised by the hardcoded attribute names/types in there.

If this works:

func (c NestedObjectCustomValue) Type(ctx context.Context) attr.Type {
	return NestedObjectCustomType{
		basetypes.ObjectType{
			AttrTypes: c.AttributeTypes(ctx),
		},
	}
}

I'd highly suggest it, so anyone can implement test schemas without hardcoded schema implementation details. 👍

@bendbennett bendbennett merged commit 4ee15c8 into main Aug 17, 2023
@bendbennett bendbennett deleted the bendbendbennett/issues-821 branch August 17, 2023 15:45
@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
3 participants