How to declare a jsontypes.NormalizedType inside "*AttributeTypes" function?

Hello,

I am building a provider with a model struct using the jsontypes.Normalized. I have to convert the struct to, and from, types.Object. Hence I wrote the function that expose the types. However I am unable to use jsontypes.NormalizedTypes for the “form” attribute:

# github.com/davidfischer-ch/terraform-provider-aria/internal/provider
internal\provider\custom_form_model.go:123:18: jsontypes.NormalizedType (type) is not an expression
FAIL    github.com/davidfischer-ch/terraform-provider-aria/internal/provider [build failed]
FAIL
+ popd

The relevant sections of code :

// CustomFormModel describes the resource data model.
type CustomFormModel struct {
	Id         types.String         `tfsdk:"id"`
	Name       types.String         `tfsdk:"name"`
	Type       types.String         `tfsdk:"type"`
	Form       jsontypes.Normalized `tfsdk:"form"`
	FormFormat types.String         `tfsdk:"form_format"`
	Styles     types.String         `tfsdk:"styles"`
	SourceId   types.String         `tfsdk:"source_id"`
	SourceType types.String         `tfsdk:"source_type"`
	Tenant     types.String         `tfsdk:"tenant"`
	Status     types.String         `tfsdk:"status"`
}


// Used to convert structure to a types.Object.
func CustomFormModelAttributeTypes() map[string]attr.Type {
	return map[string]attr.Type{
		"id":          types.StringType,
		"name":        types.StringType,
		"type":        types.StringType,
		"form":        jsontypes.NormalizedType,
		"form_format": types.StringType,
		"styles":      types.StringType,
		"source_id":   types.StringType,
		"source_type": types.StringType,
		"tenant":      types.StringType,
		"status":      types.StringType,
	}
}

I do not see what I have to do in order for this code to compile.
I previously declared form as types.StringType however Terraform complained that the response, when saving in state the object, was wrong (String vs Normalized) … I think its not the path.

Thank you for any guidance.

Best Regards,

David

Hi @davidfischer-ch :wave:t6:,

Sorry that you’re running into trouble here. If you only need the map[string]attr.Type for type conversion to and from a types.Object framework type, you likely don’t need to implement the CustomFormModelAttributeTypes() yourself. The framework provides the ElementType(ctx) method to get the element types that you can use instead.

func (d *dataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
	var model CustomFormModel

	resp.Diagnostics.Append(req.Config.Get(ctx, &model)...)
	if resp.Diagnostics.HasError() {
		return
	}

	var convertDiags diag.Diagnostics
	objectValue, convertDiags = types.ObjectValueFrom(ctx, model.Form.ElementType(ctx), forms)
	if convertDiags.HasError() {
		resp.Diagnostics.Append(convertDiags...)
		return
	}

}

And this conversion should work with the jsontypes.Normalized definition in your struct as-is.

Hope this helps!

Awesome @SBGoods , I will try this later (I opened a ticket on the provider I am working on).

I found the reason : I have to declare jsontypes.NormalizedType{}, the fact that otherwise using types.StringType is working properly was misleading.

I found the response here : terraform-plugin-framework/types/string_type.go at v1.11.0 · hashicorp/terraform-plugin-framework · GitHubvar StringType = basetypes.StringType{}.

The API is kind of misleading for that case.

Definitely, I will try your solution to make my code DRY!

Thank your for your time.

Best Regads,

David