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

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

Open
AgustinBettati opened this issue Oct 25, 2023 · 0 comments
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@AgustinBettati
Copy link

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.

@bendbennett bendbennett added documentation Improvements or additions to documentation enhancement New feature or request labels Oct 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants