Hi,
I’m experiencing a bit of a problem with type conversion which hopefully someone could help clarifying
applicationResponseSpec.GithubAuthRoles JSON response produces object with the data
Schema
type GithubAuthRoleV1 struct {
// Organization Name of the organization that owns the repository
Organization GithubAuthRoleV1Organization `json:"organization"`
// Repository Name of the github repository (case sensitive)
Repository string `json:"repository"`
}
type GithubAuthRoleV1Organization string
and the data
{
Organization: "ABC",
Repository: "abc-123"
}
I have generated the code with a terraform framework generator which resulted in a Schema
"spec": schema.SingleNestedAttribute{
Attributes: map[string]schema.Attribute{
"github_auth_roles": schema.ListNestedAttribute{
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"organization": schema.StringAttribute{
Required: true,
Description: "Name of the organization that owns the repository",
MarkdownDescription: "Name of the organization that owns the repository",
Validators: []validator.String{
stringvalidator.OneOf(
"ABC",
),
stringvalidator.LengthAtLeast(1),
},
},
"repository": schema.StringAttribute{
Required: true,
Description: "Name of the github repository (case sensitive)",
MarkdownDescription: "Name of the github repository (case sensitive)",
Validators: []validator.String{
stringvalidator.LengthAtLeast(1),
},
},
},
CustomType: GithubAuthRolesType{
ObjectType: types.ObjectType{
AttrTypes: GithubAuthRolesValue{}.AttributeTypes(ctx),
},
},
},
Optional: true,
Description: "List of github authentication roles to create",
MarkdownDescription: "List of github authentication roles to create",
},
And a model
type GithubAuthRolesValue struct {
Organization basetypes.StringValue `tfsdk:"organization"`
Repository basetypes.StringValue `tfsdk:"repository"`
state attr.ValueState
}
When I do a conversion like that
if applicationResponseSpec.GithubAuthRoles != nil {
githubAuthRoles := make([]resource_mss_application.GithubAuthRolesValue, len(*applicationResponseSpec.GithubAuthRoles))
for i, githubAuthRole := range *applicationResponseSpec.GithubAuthRoles {
githubAuthRoles[i] = resource_mss_application.GithubAuthRolesValue{
Organization: basetypes.NewStringValue(string(githubAuthRole.Organization)),
Repository: basetypes.NewStringValue(githubAuthRole.Repository),
}
}
githubAuthRolesList, diagerr := types.ListValueFrom(ctx, data.Spec.GithubAuthRoles.ElementType(ctx), githubAuthRoles)
diag.Append(diagerr...)
data.Spec.GithubAuthRoles = githubAuthRolesList
}
It results in githubAuthRolesList being a list of 1 element (which is expected) with Organization and Repository set to “” instead of the actual values from the githubAuthRoles (which is not the desired outcome)
I’ve seen this working in various examples but can’t figure out what is wrong in this particular case.
