Skip to content

Understanding correct usage of generated methods for nested object typesΒ #80

@AgustinBettati

Description

@AgustinBettati

Provider code spec used for generation

"schema": {
    "blocks": [
        {
            "name": "roles",
            "set_nested": {
                "nested_object": {
                    "attributes": [
                        {
                            "name": "database_name",
                            "string": {
                                "computed_optional_required": "required"
                            }
                        },
                        {
                            "name": "role_name",
                            "string": {
                                "computed_optional_required": "required"
                            }
                        }
                    ]
                }
            }
        }
    ]
}

Description of scenario

Within my provider implementation, I am struggling to understand the correct usage of autogenerated methods to define the Set value for roles in my model.

As an example, I would have assumed that the following code would be valid:

role := autogen.RolesValue{
	DatabaseName: types.StringValue(v.DatabaseName),
        RoleName:     types.StringValue(v.RoleName),
}
rolesSet, diagnostic := types.SetValueFrom(ctx, autogen.RolesValue{}.Type(ctx), []autogen.RolesValue{role})
... // continue by defining rolesSet in model

However, this code ends up with rolesSet having a single element with all its attributes as Null. Doing some further investigation I found that the generate RolesValue has a private state field that needs the value ValueStateKnown for attribute values to be defined correctly in the set.

To get my code to work as expected (having the state field as known), I made use of the following functions:

role := autogen.RolesValue{
	DatabaseName: types.StringValue(v.DatabaseName),
        RoleName:     types.StringValue(v.RoleName),
}
objVal, _ := value.ToObjectValue(ctx)
newRoleValue, _ := autogen.NewRolesValue(objVal.AttributeTypes(ctx), objVal.Attributes())
rolesSet, diagnostic := types.SetValueFrom(ctx, autogen.RolesValue{}.Type(ctx), []autogen.RolesValue{newRoleValue})
... // continue by defining rolesSet in model

Possible outcomes

  • Improve current implementation so that there is more straight forward option for creating a nested object type with the known state field, avoiding the intermediate ToObjectValue creation.
  • Improve current documentation in nested blocks providing an example of how the auotgenerated methods are expected to be used, and the need for the private state field.

Please let me know if there is something I am missing for my findings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions