hashicorp / hashicorp/terraform-plugin-codegen-framework
Understanding correct usage of generated methods for nested object types
- Dominant language
- Go
- Stars
- 57
- Forks
- 29
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 1
Description
**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](https://developer.hashicorp.com/terraform/plugin/code-generation/framework-generator#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.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.