Set a dynamic List based value of different property

Hi !
I’m trying to figure out if this thing is possiable :

I have a schema, and I want to set the ElementType of the ‘default’ key based on the type value. For example, if the type is a string, I want to set it as types.String . If it’s a number, I want to set it as types.Int64 , and so on.

									MarkdownDescription: "The items of the array property",
									Optional:            true,
									Attributes: map[string]schema.Attribute{
										"type": schema.StringAttribute{
											MarkdownDescription: "The type of the items",
											Required:            true,
											// Validators:          []validator.String{stringvalidator.OneOf("STRING", "BOOLEAN", "INTEGER", "FLOAT", "OBJECT", "ARRAY")},
										},
										"format": schema.StringAttribute{
											MarkdownDescription: "The format of the items",
											Optional:            true,
										},
										"default": schema.ListAttribute{
											MarkdownDescription: "The default of the items",
											Computed:            true,
										},
									},
								}`

Terraform’s schemas are statically defined - they cannot vary depending on other fields in a particular configuration.

You would need to use different attribute names, if you wanted them to have different types.

That’s a bummer, so it’s mean that can’t create array that can be with mix types?

I want to allow somehow to accept [1,2,3] and ["1,"2,"3]

You’d be fighting the typical design goals of Terraform. Why?

Because i’m trying to simulate JSON schema, and I have an API that can get array with keyword items, and accept each type such as JSON schema

https://json-schema.org/understanding-json-schema/reference/array.html

The Terraform schema system doesn’t need to apply to the APIs you call from within your Terraform provider.

It only needs to be used to define the structure of the user’s configuration input to your provider, and the results returned to Terraform for the user to reference elsewhere in their configuration.

1 Like