Issue while saving ListNestedAttribute in state file

I am using below schema for the resource.

"demo_attribute": schema.ListNestedAttribute{
	Optional:            true,
	Computed:            true,
	Description:         "",
	MarkdownDescription: "",
	NestedObject: schema.NestedAttributeObject{
		Attributes: map[string]schema.Attribute{
			"id": schema.StringAttribute{
				Description:         "",
				MarkdownDescription: "",
				Optional:            true,
				Computed:            true,
			},
			"name": schema.StringAttribute{
				Description:         "",
				MarkdownDescription: "",
				Optional:            true,
				Computed:            true,
			},
			"port": schema.Int64Attribute{
				Description:         "",
				MarkdownDescription: "",
				Optional:            true,
				Computed:            true,
			},
			"list1": schema.SetAttribute{
				Description:         "",
				MarkdownDescription: "",
				ElementType:         types.StringType,
				Required:            true,
			},
			"list2": schema.SetAttribute{
				Description:         "",
				MarkdownDescription: "",
				ElementType:         types.StringType,
				Optional:            true,
				Computed:            true,
			},
			"role": schema.StringAttribute{
				Required:            true,
				Description:         "",
				MarkdownDescription: "",
			},
		},
	},
},

The model structure is as follows:

type DemoResourceModel struct {
	DemoAttribute         types.List   `tfsdk:"demo_attribute"`
}

type Demo struct {
	ID            types.String `tfsdk:"id"`
	Name          types.String `tfsdk:"name"`
	Port          types.Int64  `tfsdk:"port"`
	List1         types.Set    `tfsdk:"list1"`
	List2		  types.Set    `tfsdk:"list2"`
	Role          types.String `tfsdk:"role"`
}

I am facing an issue while saving the List1 and List2 attributes in the state. Is there any way to save such a structure in the state?

Hi @akashgs,

To set a types.Set attribute in the state, you would need to convert a Go string slice containing your data to types.Set:

var list1data []string
var list1Attr types.List
var convertDiags diag.Diagnostics

list1Attr, convertDiags = types.SetValueFrom(ctx, types.string, list1data)
if convertDiags.HasError() {
	resp.Diagnostics.Append(convertDiags...)
	return
}

Then you would set the List1 field in your Demo struct, convert the Go type ([]Data) into the correct Framework type (types.List) and set the state:

//Get the demo_attribute from Plan
var demoData []Demo

resp.Diagnostics.Append(plan.Demo.ElementsAs(ctx, &demoData, false)...)
if resp.Diagnostics.HasError() {
	return
}

demoData[0].list1 = list1Attr
//populate any other demoData fields ...

//convert demoData to types.List and set the plan.DemoAttribute field
plan.DemoAttribute, convertDiags = types.ListValueFrom(ctx, plan.DemoAttribute.ElementType(ctx), &demoData)
if convertDiags.HasError() {
	resp.Diagnostics.Append(convertDiags...)
	return
}

//Set the state
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)

Thanks for the response @SBGoods