I’m currently trying to implement read
and hitting my head against a wall.
I have an attribute allowed_values
which is of type types.SetType()
. I just want to add it to the state, but it seems I do it the wrong way, as it’s throwing a nil pointer exception.
I’m currently doing:
// Schema & structs
"allowed_values": {
Optional: true,
Type: types.SetType{
ElemType: types.StringType,
},
}
type DeploymentTypeParametersModel struct {
Type types.String `tfsdk:"type"`
Name types.String `tfsdk:"name"`
ReadOnly types.Bool `tfsdk:"read_only"`
Required types.Bool `tfsdk:"required"`
AllowedValues types.Set `tfsdk:"allowed_values"`
IsIdentifier types.Bool `tfsdk:"is_identifier"`
Default types.String `tfsdk:"default"`
}
if v.AllowedValues != nil {
if len(v.AllowedValues) > 0 {
elems := make([]attr.Value, 0)
for _, elem := range v.AllowedValues {
tfval := tftypes.NewValue(tftypes.String, elem)
set := types.SetType{
ElemType: types.StringType,
}
val, err := set.ValueFromTerraform(ctx, tfval)
if err != nil {
resp.Diagnostics.AddError(
"GetDeploymentType() failed",
fmt.Sprintf("Underlying error: %v", err),
)
return
}
elems = append(elems, val)
}
p.AllowedValues = types.Set{
ElemType: types.StringType,
Elems: elems,
}
}
}
What is the right way to create a new set from a []string
?