From 0c2e085acd3e6ebcfc248a56eebcf32dc6a64f51 Mon Sep 17 00:00:00 2001 From: Sean Conroy Date: Fri, 29 Aug 2025 16:36:52 +0100 Subject: [PATCH] Resolve list version issue --- gen/templates/generic/model.go | 1250 +++++++++-------- .../model_sdwan_centralized_policy.go | 46 +- .../model_sdwan_configuration_group.go | 19 + ...l_sdwan_mesh_topology_policy_definition.go | 16 +- internal/provider/model_sdwan_policy_group.go | 19 + .../model_sdwan_route_policy_definition.go | 16 +- ..._sdwan_vpn_membership_policy_definition.go | 16 +- .../resource_sdwan_configuration_group.go | 13 + .../provider/resource_sdwan_policy_group.go | 13 + 9 files changed, 803 insertions(+), 605 deletions(-) diff --git a/gen/templates/generic/model.go b/gen/templates/generic/model.go index 54b1a83c1..b51c30498 100644 --- a/gen/templates/generic/model.go +++ b/gen/templates/generic/model.go @@ -21,25 +21,25 @@ package provider // Section below is generated&owned by "gen/generator.go". //template:begin imports import ( - "context" - "fmt" - "net/url" - "strconv" + "context" + "fmt" + "net/url" + "strconv" - "github.com/hashicorp/terraform-plugin-framework/types" - "github.com/hashicorp/terraform-plugin-log/tflog" - "github.com/CiscoDevNet/terraform-provider-sdwan/internal/provider/helpers" - "github.com/tidwall/gjson" - "github.com/tidwall/sjson" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-log/tflog" + "github.com/CiscoDevNet/terraform-provider-sdwan/internal/provider/helpers" + "github.com/tidwall/gjson" + "github.com/tidwall/sjson" ) // End of section. //template:end imports // Section below is generated&owned by "gen/generator.go". //template:begin types {{- $name := camelCase .Name}} type {{camelCase .Name}} struct { - Id types.String `tfsdk:"id"` + Id types.String `tfsdk:"id"` {{- if .HasVersion}} - Version types.Int64 `tfsdk:"version"` + Version types.Int64 `tfsdk:"version"` {{- end}} {{- if .TypeValue}} Type types.String `tfsdk:"type"` @@ -47,13 +47,13 @@ Type types.String `tfsdk:"type"` {{- range .Attributes}} {{- if not .Value}} {{- if isNestedListSet .}} - {{toGoName .TfName}} []{{$name}}{{toGoName .TfName}} `tfsdk:"{{.TfName}}"` + {{toGoName .TfName}} []{{$name}}{{toGoName .TfName}} `tfsdk:"{{.TfName}}"` {{- else if eq .Type "Versions"}} - {{toGoName .TfName}} types.List `tfsdk:"{{.TfName}}"` + {{toGoName .TfName}} types.List `tfsdk:"{{.TfName}}"` {{- else if eq .Type "Version"}} - {{toGoName .TfName}} types.Int64 `tfsdk:"{{.TfName}}"` + {{toGoName .TfName}} types.Int64 `tfsdk:"{{.TfName}}"` {{- else}} - {{toGoName .TfName}} types.{{.Type}} `tfsdk:"{{.TfName}}"` + {{toGoName .TfName}} types.{{.Type}} `tfsdk:"{{.TfName}}"` {{- end}} {{- end}} {{- end}} @@ -68,13 +68,13 @@ type {{$name}}{{toGoName .TfName}} struct { {{- range .Attributes}} {{- if not .Value}} {{- if isNestedListSet .}} - {{toGoName .TfName}} []{{$name}}{{$childName}}{{toGoName .TfName}} `tfsdk:"{{.TfName}}"` + {{toGoName .TfName}} []{{$name}}{{$childName}}{{toGoName .TfName}} `tfsdk:"{{.TfName}}"` {{- else if eq .Type "Versions"}} - {{toGoName .TfName}} types.List `tfsdk:"{{.TfName}}"` + {{toGoName .TfName}} types.List `tfsdk:"{{.TfName}}"` {{- else if eq .Type "Version"}} - {{toGoName .TfName}} types.Int64 `tfsdk:"{{.TfName}}"` + {{toGoName .TfName}} types.Int64 `tfsdk:"{{.TfName}}"` {{- else}} - {{toGoName .TfName}} types.{{.Type}} `tfsdk:"{{.TfName}}"` + {{toGoName .TfName}} types.{{.Type}} `tfsdk:"{{.TfName}}"` {{- end}} {{- end}} {{- end}} @@ -95,13 +95,13 @@ type {{$name}}{{$childName}}{{toGoName .TfName}} struct { {{- range .Attributes}} {{- if not .Value}} {{- if isNestedListSet .}} - {{toGoName .TfName}} []{{$name}}{{$childName}}{{$childChildName}}{{toGoName .TfName}} `tfsdk:"{{.TfName}}"` + {{toGoName .TfName}} []{{$name}}{{$childName}}{{$childChildName}}{{toGoName .TfName}} `tfsdk:"{{.TfName}}"` {{- else if eq .Type "Versions"}} - {{toGoName .TfName}} types.List `tfsdk:"{{.TfName}}"` + {{toGoName .TfName}} types.List `tfsdk:"{{.TfName}}"` {{- else if eq .Type "Version"}} - {{toGoName .TfName}} types.Int64 `tfsdk:"{{.TfName}}"` + {{toGoName .TfName}} types.Int64 `tfsdk:"{{.TfName}}"` {{- else}} - {{toGoName .TfName}} types.{{.Type}} `tfsdk:"{{.TfName}}"` + {{toGoName .TfName}} types.{{.Type}} `tfsdk:"{{.TfName}}"` {{- end}} {{- end}} {{- end}} @@ -128,11 +128,11 @@ type {{$name}}{{$childName}}{{$childChildName}}{{toGoName .TfName}} struct { {{- range .Attributes}} {{- if not .Value}} {{- if eq .Type "Versions"}} - {{toGoName .TfName}} types.List `tfsdk:"{{.TfName}}"` + {{toGoName .TfName}} types.List `tfsdk:"{{.TfName}}"` {{- else if eq .Type "Version"}} - {{toGoName .TfName}} types.Int64 `tfsdk:"{{.TfName}}"` + {{toGoName .TfName}} types.Int64 `tfsdk:"{{.TfName}}"` {{- else}} - {{toGoName .TfName}} types.{{.Type}} `tfsdk:"{{.TfName}}"` + {{toGoName .TfName}} types.{{.Type}} `tfsdk:"{{.TfName}}"` {{- end}} {{- end}} {{- end}} @@ -150,609 +150,661 @@ type {{$name}}{{$childName}}{{$childChildName}}{{toGoName .TfName}} struct { // Section below is generated&owned by "gen/generator.go". //template:begin getPath func (data {{camelCase .Name}}) getPath() string { - {{- if hasReference .Attributes}} - return fmt.Sprintf("{{.RestEndpoint}}"{{range .Attributes}}{{if .Reference}}, url.QueryEscape(data.{{toGoName .TfName}}.Value{{.Type}}()){{end}}{{end}}) - {{- else}} - return "{{.RestEndpoint}}" - {{- end}} + {{- if hasReference .Attributes}} + return fmt.Sprintf("{{.RestEndpoint}}"{{range .Attributes}}{{if .Reference}}, url.QueryEscape(data.{{toGoName .TfName}}.Value{{.Type}}()){{end}}{{end}}) + {{- else}} + return "{{.RestEndpoint}}" + {{- end}} } // End of section. //template:end getPath // Section below is generated&owned by "gen/generator.go". //template:begin toBody func (data {{camelCase .Name}}) toBody(ctx context.Context) string { - body := "" - {{- range .Attributes}} - {{- if .Value}} - if true{{if ne .ConditionalAttribute.Name ""}} && data.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(data.{{toGoName .ConditionalListLength}}) > 0{{end}} { - body, _ = sjson.Set(body, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", {{if eq .Type "String"}}"{{end}}{{.Value}}{{if eq .Type "String"}}"{{end}}) - } - {{- else if and (not .TfOnly) (not .Reference)}} - {{- if or (eq .Type "String") (eq .Type "Int64") (eq .Type "Float64")}} - if {{if not .AlwaysInclude}}!data.{{toGoName .TfName}}.IsNull(){{else}}true{{end}}{{if ne .ConditionalAttribute.Name ""}} && data.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(data.{{toGoName .ConditionalListLength}}) > 0{{end}} { - body, _ = sjson.Set(body, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", {{if .ModelTypeString}}fmt.Sprint({{end}}data.{{toGoName .TfName}}.Value{{.Type}}(){{if .ModelTypeString}}){{end}}) - } - {{- else if eq .Type "Bool"}} - if {{if not .AlwaysInclude}}!data.{{toGoName .TfName}}.IsNull(){{else}}true{{end}}{{if ne .ConditionalAttribute.Name ""}} && data.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(data.{{toGoName .ConditionalListLength}}) > 0{{end}} { - if {{.BoolEmptyString}} && data.{{toGoName .TfName}}.Value{{.Type}}() { - body, _ = sjson.Set(body, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", "") - }{{if not .BoolEmptyString}} else { - body, _ = sjson.Set(body, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", {{if .ModelTypeString}}fmt.Sprint({{end}}data.{{toGoName .TfName}}.Value{{.Type}}(){{if .ModelTypeString}}){{end}}) - } - {{- end}} - } - {{- else if isListSet .}} - if {{if not .AlwaysInclude}}!data.{{toGoName .TfName}}.IsNull(){{else}}true{{end}}{{if ne .ConditionalAttribute.Name ""}} && data.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(data.{{toGoName .ConditionalListLength}}) > 0{{end}} { - var values []{{if isStringListSet .}}string{{else if isInt64ListSet .}}int64{{end}} - data.{{toGoName .TfName}}.ElementsAs(ctx, &values, false) - body, _ = sjson.Set(body, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", values) - } - {{- else if isNestedListSet .}} - if true{{if ne .ConditionalAttribute.Name ""}} && data.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(data.{{toGoName .ConditionalListLength}}) > 0{{end}} { - body, _ = sjson.Set(body, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", []interface{}{}) - for _, item := range data.{{toGoName .TfName}} { - itemBody := "" - {{- range .Attributes}} - {{- if .Value}} - if true{{if ne .ConditionalAttribute.Name ""}} && item.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(item.{{toGoName .ConditionalListLength}}) > 0{{end}} { - itemBody, _ = sjson.Set(itemBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", {{if eq .Type "String"}}"{{end}}{{.Value}}{{if eq .Type "String"}}"{{end}}) - } - {{- else if and (not .TfOnly) (not .Reference)}} - {{- if or (eq .Type "String") (eq .Type "Int64") (eq .Type "Float64")}} - if {{if not .AlwaysInclude}}!item.{{toGoName .TfName}}.IsNull(){{else}}true{{end}}{{if ne .ConditionalAttribute.Name ""}} && item.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(item.{{toGoName .ConditionalListLength}}) > 0{{end}} { - itemBody, _ = sjson.Set(itemBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", {{if .ModelTypeString}}fmt.Sprint({{end}}item.{{toGoName .TfName}}.Value{{.Type}}(){{if .ModelTypeString}}){{end}}) - } - {{- else if eq .Type "Bool"}} - if {{if not .AlwaysInclude}}!item.{{toGoName .TfName}}.IsNull(){{else}}true{{end}}{{if ne .ConditionalAttribute.Name ""}} && item.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(item.{{toGoName .ConditionalListLength}}) > 0{{end}} { - if {{.BoolEmptyString}} && item.{{toGoName .TfName}}.Value{{.Type}}() { - itemBody, _ = sjson.Set(itemBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", "") - }{{if not .BoolEmptyString}} else { - itemBody, _ = sjson.Set(itemBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", {{if .ModelTypeString}}fmt.Sprint({{end}}item.{{toGoName .TfName}}.Value{{.Type}}(){{if .ModelTypeString}}){{end}}) - } - {{- end}} - } - {{- else if isListSet .}} - if {{if not .AlwaysInclude}}!item.{{toGoName .TfName}}.IsNull(){{else}}true{{end}}{{if ne .ConditionalAttribute.Name ""}} && item.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(item.{{toGoName .ConditionalListLength}}) > 0{{end}} { - var values []{{if isStringListSet .}}string{{else if isInt64ListSet .}}int64{{end}} - item.{{toGoName .TfName}}.ElementsAs(ctx, &values, false) - itemBody, _ = sjson.Set(itemBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", values) - } - {{- else if isNestedListSet .}} - if true{{if ne .ConditionalAttribute.Name ""}} && item.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(item.{{toGoName .ConditionalListLength}}) > 0{{end}} { - itemBody, _ = sjson.Set(itemBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", []interface{}{}) - for _, childItem := range item.{{toGoName .TfName}} { - itemChildBody := "" - {{- range .Attributes}} - {{- if .Value}} - if true{{if ne .ConditionalAttribute.Name ""}} && childItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(childItem.{{toGoName .ConditionalListLength}}) > 0{{end}} { - itemChildBody, _ = sjson.Set(itemChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", {{if eq .Type "String"}}"{{end}}{{.Value}}{{if eq .Type "String"}}"{{end}}) - } - {{- else if and (not .TfOnly) (not .Reference)}} - {{- if or (eq .Type "String") (eq .Type "Int64") (eq .Type "Float64")}} - if {{if not .AlwaysInclude}}!childItem.{{toGoName .TfName}}.IsNull(){{else}}true{{end}}{{if ne .ConditionalAttribute.Name ""}} && childItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(childItem.{{toGoName .ConditionalListLength}}) > 0{{end}} { - itemChildBody, _ = sjson.Set(itemChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", {{if .ModelTypeString}}fmt.Sprint({{end}}childItem.{{toGoName .TfName}}.Value{{.Type}}(){{if .ModelTypeString}}){{end}}) - } - {{- else if eq .Type "Bool"}} - if {{if not .AlwaysInclude}}!childItem.{{toGoName .TfName}}.IsNull(){{else}}true{{end}}{{if ne .ConditionalAttribute.Name ""}} && childItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(childItem.{{toGoName .ConditionalListLength}}) > 0{{end}} { - if {{.BoolEmptyString}} && childItem.{{toGoName .TfName}}.Value{{.Type}}() { - itemChildBody, _ = sjson.Set(itemChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", "") - }{{if not .BoolEmptyString}} else { - itemChildBody, _ = sjson.Set(itemChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", {{if .ModelTypeString}}fmt.Sprint({{end}}childItem.{{toGoName .TfName}}.Value{{.Type}}(){{if .ModelTypeString}}){{end}}) - } - {{- end}} - } - {{- else if isListSet .}} - if {{if not .AlwaysInclude}}!childItem.{{toGoName .TfName}}.IsNull(){{else}}true{{end}}{{if ne .ConditionalAttribute.Name ""}} && childItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(childItem.{{toGoName .ConditionalListLength}}) > 0{{end}} { - var values []{{if isStringListSet .}}string{{else if isInt64ListSet .}}int64{{end}} - childItem.{{toGoName .TfName}}.ElementsAs(ctx, &values, false) - itemChildBody, _ = sjson.Set(itemChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", values) - } - {{- else if isNestedListSet .}} - if true{{if ne .ConditionalAttribute.Name ""}} && childItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(childItem.{{toGoName .ConditionalListLength}}) > 0{{end}} { - itemChildBody, _ = sjson.Set(itemChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", []interface{}{}) - for _, childChildItem := range childItem.{{toGoName .TfName}} { - itemChildChildBody := "" - {{- range .Attributes}} - {{- if .Value}} - if true{{if ne .ConditionalAttribute.Name ""}} && childChildItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(childChildItem.{{toGoName .ConditionalListLength}}) > 0{{end}} { - itemChildChildBody, _ = sjson.Set(itemChildChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", {{if eq .Type "String"}}"{{end}}{{.Value}}{{if eq .Type "String"}}"{{end}}) - } - {{- else if and (not .TfOnly) (not .Reference)}} - {{- if or (eq .Type "String") (eq .Type "Int64") (eq .Type "Float64")}} - if {{if not .AlwaysInclude}}!childChildItem.{{toGoName .TfName}}.IsNull(){{else}}true{{end}}{{if ne .ConditionalAttribute.Name ""}} && childChildItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(childChildItem.{{toGoName .ConditionalListLength}}) > 0{{end}} { - itemChildChildBody, _ = sjson.Set(itemChildChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", {{if .ModelTypeString}}fmt.Sprint({{end}}childChildItem.{{toGoName .TfName}}.Value{{.Type}}(){{if .ModelTypeString}}){{end}}) - } - {{- else if eq .Type "Bool"}} - if {{if not .AlwaysInclude}}!childChildItem.{{toGoName .TfName}}.IsNull(){{else}}true{{end}}{{if ne .ConditionalAttribute.Name ""}} && childChildItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(childChildItem.{{toGoName .ConditionalListLength}}) > 0{{end}} { - if {{.BoolEmptyString}} && childChildItem.{{toGoName .TfName}}.Value{{.Type}}() { - itemChildChildBody, _ = sjson.Set(itemChildChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", "") - }{{if not .BoolEmptyString}} else { - itemChildChildBody, _ = sjson.Set(itemChildChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", {{if .ModelTypeString}}fmt.Sprint({{end}}childChildItem.{{toGoName .TfName}}.Value{{.Type}}(){{if .ModelTypeString}}){{end}}) - } - {{- end}} - } - {{- else if isListSet .}} - if {{if not .AlwaysInclude}}!childChildItem.{{toGoName .TfName}}.IsNull(){{else}}true{{end}}{{if ne .ConditionalAttribute.Name ""}} && childChildItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(childChildItem.{{toGoName .ConditionalListLength}}) > 0{{end}} { - var values []{{if isStringListSet .}}string{{else if isInt64ListSet .}}int64{{end}} - childChildItem.{{toGoName .TfName}}.ElementsAs(ctx, &values, false) - itemChildChildBody, _ = sjson.Set(itemChildChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", values) - } - {{- end}} - {{- end}} - {{- end}} - itemChildBody, _ = sjson.SetRaw(itemChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}.-1", itemChildChildBody) - } - } - {{- end}} - {{- end}} - {{- end}} - itemBody, _ = sjson.SetRaw(itemBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}.-1", itemChildBody) - } - } - {{- end}} - {{- end}} - {{- end}} - body, _ = sjson.SetRaw(body, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}.-1", itemBody) - } - } - {{- end}} - {{- end}} - {{- end}} - return body + body := "" + {{- range .Attributes}} + {{- if .Value}} + if true{{if ne .ConditionalAttribute.Name ""}} && data.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(data.{{toGoName .ConditionalListLength}}) > 0{{end}} { + body, _ = sjson.Set(body, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", {{if eq .Type "String"}}"{{end}}{{.Value}}{{if eq .Type "String"}}"{{end}}) + } + {{- else if and (not .TfOnly) (not .Reference)}} + {{- if or (eq .Type "String") (eq .Type "Int64") (eq .Type "Float64")}} + if {{if not .AlwaysInclude}}!data.{{toGoName .TfName}}.IsNull(){{else}}true{{end}}{{if ne .ConditionalAttribute.Name ""}} && data.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(data.{{toGoName .ConditionalListLength}}) > 0{{end}} { + body, _ = sjson.Set(body, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", {{if .ModelTypeString}}fmt.Sprint({{end}}data.{{toGoName .TfName}}.Value{{.Type}}(){{if .ModelTypeString}}){{end}}) + } + {{- else if eq .Type "Bool"}} + if {{if not .AlwaysInclude}}!data.{{toGoName .TfName}}.IsNull(){{else}}true{{end}}{{if ne .ConditionalAttribute.Name ""}} && data.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(data.{{toGoName .ConditionalListLength}}) > 0{{end}} { + if {{.BoolEmptyString}} && data.{{toGoName .TfName}}.Value{{.Type}}() { + body, _ = sjson.Set(body, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", "") + }{{if not .BoolEmptyString}} else { + body, _ = sjson.Set(body, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", {{if .ModelTypeString}}fmt.Sprint({{end}}data.{{toGoName .TfName}}.Value{{.Type}}(){{if .ModelTypeString}}){{end}}) + } + {{- end}} + } + {{- else if isListSet .}} + if {{if not .AlwaysInclude}}!data.{{toGoName .TfName}}.IsNull(){{else}}true{{end}}{{if ne .ConditionalAttribute.Name ""}} && data.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(data.{{toGoName .ConditionalListLength}}) > 0{{end}} { + var values []{{if isStringListSet .}}string{{else if isInt64ListSet .}}int64{{end}} + data.{{toGoName .TfName}}.ElementsAs(ctx, &values, false) + body, _ = sjson.Set(body, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", values) + } + {{- else if isNestedListSet .}} + if true{{if ne .ConditionalAttribute.Name ""}} && data.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(data.{{toGoName .ConditionalListLength}}) > 0{{end}} { + body, _ = sjson.Set(body, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", []interface{}{}) + for _, item := range data.{{toGoName .TfName}} { + itemBody := "" + {{- range .Attributes}} + {{- if .Value}} + if true{{if ne .ConditionalAttribute.Name ""}} && item.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(item.{{toGoName .ConditionalListLength}}) > 0{{end}} { + itemBody, _ = sjson.Set(itemBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", {{if eq .Type "String"}}"{{end}}{{.Value}}{{if eq .Type "String"}}"{{end}}) + } + {{- else if and (not .TfOnly) (not .Reference)}} + {{- if or (eq .Type "String") (eq .Type "Int64") (eq .Type "Float64")}} + if {{if not .AlwaysInclude}}!item.{{toGoName .TfName}}.IsNull(){{else}}true{{end}}{{if ne .ConditionalAttribute.Name ""}} && item.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(item.{{toGoName .ConditionalListLength}}) > 0{{end}} { + itemBody, _ = sjson.Set(itemBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", {{if .ModelTypeString}}fmt.Sprint({{end}}item.{{toGoName .TfName}}.Value{{.Type}}(){{if .ModelTypeString}}){{end}}) + } + {{- else if eq .Type "Bool"}} + if {{if not .AlwaysInclude}}!item.{{toGoName .TfName}}.IsNull(){{else}}true{{end}}{{if ne .ConditionalAttribute.Name ""}} && item.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(item.{{toGoName .ConditionalListLength}}) > 0{{end}} { + if {{.BoolEmptyString}} && item.{{toGoName .TfName}}.Value{{.Type}}() { + itemBody, _ = sjson.Set(itemBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", "") + }{{if not .BoolEmptyString}} else { + itemBody, _ = sjson.Set(itemBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", {{if .ModelTypeString}}fmt.Sprint({{end}}item.{{toGoName .TfName}}.Value{{.Type}}(){{if .ModelTypeString}}){{end}}) + } + {{- end}} + } + {{- else if isListSet .}} + if {{if not .AlwaysInclude}}!item.{{toGoName .TfName}}.IsNull(){{else}}true{{end}}{{if ne .ConditionalAttribute.Name ""}} && item.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(item.{{toGoName .ConditionalListLength}}) > 0{{end}} { + var values []{{if isStringListSet .}}string{{else if isInt64ListSet .}}int64{{end}} + item.{{toGoName .TfName}}.ElementsAs(ctx, &values, false) + itemBody, _ = sjson.Set(itemBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", values) + } + {{- else if isNestedListSet .}} + if true{{if ne .ConditionalAttribute.Name ""}} && item.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(item.{{toGoName .ConditionalListLength}}) > 0{{end}} { + itemBody, _ = sjson.Set(itemBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", []interface{}{}) + for _, childItem := range item.{{toGoName .TfName}} { + itemChildBody := "" + {{- range .Attributes}} + {{- if .Value}} + if true{{if ne .ConditionalAttribute.Name ""}} && childItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(childItem.{{toGoName .ConditionalListLength}}) > 0{{end}} { + itemChildBody, _ = sjson.Set(itemChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", {{if eq .Type "String"}}"{{end}}{{.Value}}{{if eq .Type "String"}}"{{end}}) + } + {{- else if and (not .TfOnly) (not .Reference)}} + {{- if or (eq .Type "String") (eq .Type "Int64") (eq .Type "Float64")}} + if {{if not .AlwaysInclude}}!childItem.{{toGoName .TfName}}.IsNull(){{else}}true{{end}}{{if ne .ConditionalAttribute.Name ""}} && childItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(childItem.{{toGoName .ConditionalListLength}}) > 0{{end}} { + itemChildBody, _ = sjson.Set(itemChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", {{if .ModelTypeString}}fmt.Sprint({{end}}childItem.{{toGoName .TfName}}.Value{{.Type}}(){{if .ModelTypeString}}){{end}}) + } + {{- else if eq .Type "Bool"}} + if {{if not .AlwaysInclude}}!childItem.{{toGoName .TfName}}.IsNull(){{else}}true{{end}}{{if ne .ConditionalAttribute.Name ""}} && childItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(childItem.{{toGoName .ConditionalListLength}}) > 0{{end}} { + if {{.BoolEmptyString}} && childItem.{{toGoName .TfName}}.Value{{.Type}}() { + itemChildBody, _ = sjson.Set(itemChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", "") + }{{if not .BoolEmptyString}} else { + itemChildBody, _ = sjson.Set(itemChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", {{if .ModelTypeString}}fmt.Sprint({{end}}childItem.{{toGoName .TfName}}.Value{{.Type}}(){{if .ModelTypeString}}){{end}}) + } + {{- end}} + } + {{- else if isListSet .}} + if {{if not .AlwaysInclude}}!childItem.{{toGoName .TfName}}.IsNull(){{else}}true{{end}}{{if ne .ConditionalAttribute.Name ""}} && childItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(childItem.{{toGoName .ConditionalListLength}}) > 0{{end}} { + var values []{{if isStringListSet .}}string{{else if isInt64ListSet .}}int64{{end}} + childItem.{{toGoName .TfName}}.ElementsAs(ctx, &values, false) + itemChildBody, _ = sjson.Set(itemChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", values) + } + {{- else if isNestedListSet .}} + if true{{if ne .ConditionalAttribute.Name ""}} && childItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(childItem.{{toGoName .ConditionalListLength}}) > 0{{end}} { + itemChildBody, _ = sjson.Set(itemChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", []interface{}{}) + for _, childChildItem := range childItem.{{toGoName .TfName}} { + itemChildChildBody := "" + {{- range .Attributes}} + {{- if .Value}} + if true{{if ne .ConditionalAttribute.Name ""}} && childChildItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(childChildItem.{{toGoName .ConditionalListLength}}) > 0{{end}} { + itemChildChildBody, _ = sjson.Set(itemChildChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", {{if eq .Type "String"}}"{{end}}{{.Value}}{{if eq .Type "String"}}"{{end}}) + } + {{- else if and (not .TfOnly) (not .Reference)}} + {{- if or (eq .Type "String") (eq .Type "Int64") (eq .Type "Float64")}} + if {{if not .AlwaysInclude}}!childChildItem.{{toGoName .TfName}}.IsNull(){{else}}true{{end}}{{if ne .ConditionalAttribute.Name ""}} && childChildItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(childChildItem.{{toGoName .ConditionalListLength}}) > 0{{end}} { + itemChildChildBody, _ = sjson.Set(itemChildChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", {{if .ModelTypeString}}fmt.Sprint({{end}}childChildItem.{{toGoName .TfName}}.Value{{.Type}}(){{if .ModelTypeString}}){{end}}) + } + {{- else if eq .Type "Bool"}} + if {{if not .AlwaysInclude}}!childChildItem.{{toGoName .TfName}}.IsNull(){{else}}true{{end}}{{if ne .ConditionalAttribute.Name ""}} && childChildItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(childChildItem.{{toGoName .ConditionalListLength}}) > 0{{end}} { + if {{.BoolEmptyString}} && childChildItem.{{toGoName .TfName}}.Value{{.Type}}() { + itemChildChildBody, _ = sjson.Set(itemChildChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", "") + }{{if not .BoolEmptyString}} else { + itemChildChildBody, _ = sjson.Set(itemChildChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", {{if .ModelTypeString}}fmt.Sprint({{end}}childChildItem.{{toGoName .TfName}}.Value{{.Type}}(){{if .ModelTypeString}}){{end}}) + } + {{- end}} + } + {{- else if isListSet .}} + if {{if not .AlwaysInclude}}!childChildItem.{{toGoName .TfName}}.IsNull(){{else}}true{{end}}{{if ne .ConditionalAttribute.Name ""}} && childChildItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{else if ne .ConditionalListLength ""}} && len(childChildItem.{{toGoName .ConditionalListLength}}) > 0{{end}} { + var values []{{if isStringListSet .}}string{{else if isInt64ListSet .}}int64{{end}} + childChildItem.{{toGoName .TfName}}.ElementsAs(ctx, &values, false) + itemChildChildBody, _ = sjson.Set(itemChildChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}", values) + } + {{- end}} + {{- end}} + {{- end}} + itemChildBody, _ = sjson.SetRaw(itemChildBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}.-1", itemChildChildBody) + } + } + {{- end}} + {{- end}} + {{- end}} + itemBody, _ = sjson.SetRaw(itemBody, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}.-1", itemChildBody) + } + } + {{- end}} + {{- end}} + {{- end}} + body, _ = sjson.SetRaw(body, "{{range .DataPath}}{{.}}.{{end}}{{.ModelName}}.-1", itemBody) + } + } + {{- end}} + {{- end}} + {{- end}} + return body } // End of section. //template:end toBody // Section below is generated&owned by "gen/generator.go". //template:begin fromBody func (data *{{camelCase .Name}}) fromBody(ctx context.Context, res gjson.Result) { - {{- if hasVersionAttribute .Attributes}} - state := *data - {{- end}} - {{- range .Attributes}} - {{- if and (not .TfOnly) (not .Value) (not .Reference)}} - {{- $cname := toGoName .TfName}} - {{- if eq .Type "String"}} - if value := res.Get("{{getResponseModelPath .}}"); value.Exists(){{if ne .ConditionalAttribute.Name ""}} && data.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && value.String() != ""{{end}} { - data.{{toGoName .TfName}} = types.StringValue(value.String()) - } else { - data.{{toGoName .TfName}} = types.StringNull() - } - {{- else if eq .Type "Int64"}} - if value := res.Get("{{getResponseModelPath .}}"); value.Exists(){{if ne .ConditionalAttribute.Name ""}} && data.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && value.String() != ""{{end}} { - data.{{toGoName .TfName}} = types.Int64Value(value.Int()) - } else { - data.{{toGoName .TfName}} = types.Int64Null() - } - {{- else if eq .Type "Float64"}} - if value := res.Get("{{getResponseModelPath .}}"); value.Exists(){{if ne .ConditionalAttribute.Name ""}} && data.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && value.String() != ""{{end}} { - data.{{toGoName .TfName}} = types.Float64Value(value.Float()) - } else { - data.{{toGoName .TfName}} = types.Float64Null() - } - {{- else if eq .Type "Bool"}} - if value := res.Get("{{getResponseModelPath .}}"); value.Exists(){{if ne .ConditionalAttribute.Name ""}} && data.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && value.String() != ""{{end}} { - if {{.BoolEmptyString}} && value.String() == "" { - data.{{toGoName .TfName}} = types.BoolValue(true) - }{{if not .BoolEmptyString}} else { - data.{{toGoName .TfName}} = types.BoolValue(value.Bool()) - } - {{- end}} - } else { - data.{{toGoName .TfName}} = types.BoolNull() - } - {{- else if isListSet .}} - if value := res.Get("{{getResponseModelPath .}}"); value.Exists(){{if ne .ConditionalAttribute.Name ""}} && data.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && value.String() != ""{{end}} { - data.{{toGoName .TfName}} = helpers.Get{{.ElementType}}{{.Type}}(value.Array()) - } else { - data.{{toGoName .TfName}} = types.{{.Type}}Null(types.{{.ElementType}}Type) - } - {{- else if isNestedListSet .}} - if value := res.Get("{{getResponseModelPath .}}"); value.Exists() && len(value.Array()) > 0{{if ne .ConditionalAttribute.Name ""}} && data.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}} { - data.{{toGoName .TfName}} = make([]{{$name}}{{toGoName .TfName}}, 0) - value.ForEach(func(k, v gjson.Result) bool { - item := {{$name}}{{toGoName .TfName}}{} - {{- range .Attributes}} - {{- $ccname := toGoName .TfName}} - {{- if and (not .TfOnly) (not .Value) (not .Reference)}} - {{- if eq .Type "String"}} - if cValue := v.Get("{{getResponseModelPath .}}"); cValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && item.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && cValue.String() != ""{{end}} { - item.{{toGoName .TfName}} = types.StringValue(cValue.String()) - } else { - item.{{toGoName .TfName}} = types.StringNull() - } - {{- else if eq .Type "Int64"}} - if cValue := v.Get("{{getResponseModelPath .}}"); cValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && item.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && cValue.String() != ""{{end}} { - item.{{toGoName .TfName}} = types.Int64Value(cValue.Int()) - } else { - item.{{toGoName .TfName}} = types.Int64Null() - } - {{- else if eq .Type "Float64"}} - if cValue := v.Get("{{getResponseModelPath .}}"); cValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && item.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && cValue.String() != ""{{end}} { - item.{{toGoName .TfName}} = types.Float64Value(cValue.Float()) - } else { - item.{{toGoName .TfName}} = types.Float64Null() - } - {{- else if eq .Type "Bool"}} - if cValue := v.Get("{{getResponseModelPath .}}"); cValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && item.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && cValue.String() != ""{{end}} { - if {{.BoolEmptyString}} && cValue.String() == "" { - item.{{toGoName .TfName}} = types.BoolValue(true) - }{{if not .BoolEmptyString}} else { - item.{{toGoName .TfName}} = types.BoolValue(cValue.Bool()) - } - {{- end}} - } else { - item.{{toGoName .TfName}} = types.BoolNull() - } - {{- else if isListSet .}} - if cValue := v.Get("{{getResponseModelPath .}}"); cValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && item.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && cValue.String() != ""{{end}} { - item.{{toGoName .TfName}} = helpers.Get{{.ElementType}}{{.Type}}(cValue.Array()) - } else { - item.{{toGoName .TfName}} = types.{{.Type}}Null(types.{{.ElementType}}Type) - } - {{- else if isNestedListSet .}} - if cValue := v.Get("{{getResponseModelPath .}}"); cValue.Exists() && len(cValue.Array()) > 0{{if ne .ConditionalAttribute.Name ""}} && item.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}} { - item.{{toGoName .TfName}} = make([]{{$name}}{{$cname}}{{toGoName .TfName}}, 0) - cValue.ForEach(func(ck, cv gjson.Result) bool { - cItem := {{$name}}{{$cname}}{{toGoName .TfName}}{} - {{- range .Attributes}} - {{- if and (not .TfOnly) (not .Value) (not .Reference)}} - {{- if eq .Type "String"}} - if ccValue := cv.Get("{{getResponseModelPath .}}"); ccValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && cItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && ccValue.String() != ""{{end}} { - cItem.{{toGoName .TfName}} = types.StringValue(ccValue.String()) - } else { - cItem.{{toGoName .TfName}} = types.StringNull() - } - {{- else if eq .Type "Int64"}} - if ccValue := cv.Get("{{getResponseModelPath .}}"); ccValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && cItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && ccValue.String() != ""{{end}} { - cItem.{{toGoName .TfName}} = types.Int64Value(ccValue.Int()) - } else { - cItem.{{toGoName .TfName}} = types.Int64Null() - } - {{- else if eq .Type "Float64"}} - if ccValue := cv.Get("{{getResponseModelPath .}}"); ccValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && cItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && ccValue.String() != ""{{end}} { - cItem.{{toGoName .TfName}} = types.Float64Value(ccValue.Float()) - } else { - cItem.{{toGoName .TfName}} = types.Float64Null() - } - {{- else if eq .Type "Bool"}} - if ccValue := cv.Get("{{getResponseModelPath .}}"); ccValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && cItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && ccValue.String() != ""{{end}} { - if {{.BoolEmptyString}} && ccValue.String() == "" { - cItem.{{toGoName .TfName}} = types.BoolValue(true) - }{{if not .BoolEmptyString}} else { - cItem.{{toGoName .TfName}} = types.BoolValue(ccValue.Bool()) - } - {{- end}} - } else { - cItem.{{toGoName .TfName}} = types.BoolNull() - } - {{- else if isListSet .}} - if ccValue := cv.Get("{{getResponseModelPath .}}"); ccValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && cItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && ccValue.String() != ""{{end}} { - cItem.{{toGoName .TfName}} = helpers.Get{{.ElementType}}{{.Type}}(ccValue.Array()) - } else { - cItem.{{toGoName .TfName}} = types.{{.Type}}Null(types.{{.ElementType}}Type) - } - {{- else if isNestedListSet .}} - if ccValue := cv.Get("{{getResponseModelPath .}}"); ccValue.Exists() && len(ccValue.Array()) > 0{{if ne .ConditionalAttribute.Name ""}} && cItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}} { - cItem.{{toGoName .TfName}} = make([]{{$name}}{{$cname}}{{$ccname}}{{toGoName .TfName}}, 0) - ccValue.ForEach(func(cck, ccv gjson.Result) bool { - ccItem := {{$name}}{{$cname}}{{$ccname}}{{toGoName .TfName}}{} - {{- range .Attributes}} - {{- if and (not .TfOnly) (not .Value) (not .Reference)}} - {{- if eq .Type "String"}} - if cccValue := ccv.Get("{{getResponseModelPath .}}"); cccValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && ccItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && cccValue.String() != ""{{end}} { - ccItem.{{toGoName .TfName}} = types.StringValue(cccValue.String()) - } else { - ccItem.{{toGoName .TfName}} = types.StringNull() - } - {{- else if eq .Type "Int64"}} - if cccValue := ccv.Get("{{getResponseModelPath .}}"); cccValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && ccItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && cccValue.String() != ""{{end}} { - ccItem.{{toGoName .TfName}} = types.Int64Value(cccValue.Int()) - } else { - ccItem.{{toGoName .TfName}} = types.Int64Null() - } - {{- else if eq .Type "Float64"}} - if cccValue := ccv.Get("{{getResponseModelPath .}}"); cccValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && ccItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && cccValue.String() != ""{{end}} { - ccItem.{{toGoName .TfName}} = types.Float64Value(cccValue.Float()) - } else { - ccItem.{{toGoName .TfName}} = types.Float64Null() - } - {{- else if eq .Type "Bool"}} - if cccValue := ccv.Get("{{getResponseModelPath .}}"); cccValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && ccItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && cccValue.String() != ""{{end}} { - if {{.BoolEmptyString}} && cccValue.String() == "" { - ccItem.{{toGoName .TfName}} = types.BoolValue(true) - }{{if not .BoolEmptyString}} else { - ccItem.{{toGoName .TfName}} = types.BoolValue(cccValue.Bool()) - } - {{- end}} - } else { - ccItem.{{toGoName .TfName}} = types.BoolNull() - } - {{- else if isListSet .}} - if cccValue := ccv.Get("{{getResponseModelPath .}}"); cccValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && ccItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && cccValue.String() != ""{{end}} { - ccItem.{{toGoName .TfName}} = helpers.Get{{.ElementType}}{{.Type}}(cccValue.Array()) - } else { - ccItem.{{toGoName .TfName}} = types.{{.Type}}Null(types.{{.ElementType}}Type) - } - {{- end}} - {{- end}} - {{- end}} - cItem.{{toGoName .TfName}} = append(cItem.{{toGoName .TfName}}, ccItem) - return true - }) - } else { - if len(cItem.{{toGoName .TfName}}) > 0 { - cItem.{{toGoName .TfName}} = []{{$name}}{{$cname}}{{$ccname}}{{toGoName .TfName}}{} - } - } - {{- end}} - {{- end}} - {{- end}} - item.{{toGoName .TfName}} = append(item.{{toGoName .TfName}}, cItem) - return true - }) - } else { - if len(item.{{toGoName .TfName}}) > 0 { - item.{{toGoName .TfName}} = []{{$name}}{{$cname}}{{toGoName .TfName}}{} - } - } - {{- end}} - {{- end}} - {{- end}} - data.{{toGoName .TfName}} = append(data.{{toGoName .TfName}}, item) - return true - }) - } else { - if len(data.{{toGoName .TfName}}) > 0 { - data.{{toGoName .TfName}} = []{{$name}}{{toGoName .TfName}}{} - } - } - {{- end}} - {{- end}} - {{- end}} - {{- if hasVersionAttribute .Attributes}} - data.updateVersions(ctx, &state) - {{- end}} + {{- if hasVersionAttribute .Attributes}} + state := *data + {{- end}} + {{- range .Attributes}} + {{- if and (not .TfOnly) (not .Value) (not .Reference)}} + {{- $cname := toGoName .TfName}} + {{- if eq .Type "String"}} + if value := res.Get("{{getResponseModelPath .}}"); value.Exists(){{if ne .ConditionalAttribute.Name ""}} && data.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && value.String() != ""{{end}} { + data.{{toGoName .TfName}} = types.StringValue(value.String()) + } else { + data.{{toGoName .TfName}} = types.StringNull() + } + {{- else if eq .Type "Int64"}} + if value := res.Get("{{getResponseModelPath .}}"); value.Exists(){{if ne .ConditionalAttribute.Name ""}} && data.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && value.String() != ""{{end}} { + data.{{toGoName .TfName}} = types.Int64Value(value.Int()) + } else { + data.{{toGoName .TfName}} = types.Int64Null() + } + {{- else if eq .Type "Float64"}} + if value := res.Get("{{getResponseModelPath .}}"); value.Exists(){{if ne .ConditionalAttribute.Name ""}} && data.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && value.String() != ""{{end}} { + data.{{toGoName .TfName}} = types.Float64Value(value.Float()) + } else { + data.{{toGoName .TfName}} = types.Float64Null() + } + {{- else if eq .Type "Bool"}} + if value := res.Get("{{getResponseModelPath .}}"); value.Exists(){{if ne .ConditionalAttribute.Name ""}} && data.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && value.String() != ""{{end}} { + if {{.BoolEmptyString}} && value.String() == "" { + data.{{toGoName .TfName}} = types.BoolValue(true) + }{{if not .BoolEmptyString}} else { + data.{{toGoName .TfName}} = types.BoolValue(value.Bool()) + } + {{- end}} + } else { + data.{{toGoName .TfName}} = types.BoolNull() + } + {{- else if isListSet .}} + if value := res.Get("{{getResponseModelPath .}}"); value.Exists(){{if ne .ConditionalAttribute.Name ""}} && data.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && value.String() != ""{{end}} { + data.{{toGoName .TfName}} = helpers.Get{{.ElementType}}{{.Type}}(value.Array()) + } else { + data.{{toGoName .TfName}} = types.{{.Type}}Null(types.{{.ElementType}}Type) + } + {{- else if isNestedListSet .}} + if value := res.Get("{{getResponseModelPath .}}"); value.Exists() && len(value.Array()) > 0{{if ne .ConditionalAttribute.Name ""}} && data.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}} { + data.{{toGoName .TfName}} = make([]{{$name}}{{toGoName .TfName}}, 0) + value.ForEach(func(k, v gjson.Result) bool { + item := {{$name}}{{toGoName .TfName}}{} + {{- range .Attributes}} + {{- $ccname := toGoName .TfName}} + {{- if and (not .TfOnly) (not .Value) (not .Reference)}} + {{- if eq .Type "String"}} + if cValue := v.Get("{{getResponseModelPath .}}"); cValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && item.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && cValue.String() != ""{{end}} { + item.{{toGoName .TfName}} = types.StringValue(cValue.String()) + } else { + item.{{toGoName .TfName}} = types.StringNull() + } + {{- else if eq .Type "Int64"}} + if cValue := v.Get("{{getResponseModelPath .}}"); cValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && item.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && cValue.String() != ""{{end}} { + item.{{toGoName .TfName}} = types.Int64Value(cValue.Int()) + } else { + item.{{toGoName .TfName}} = types.Int64Null() + } + {{- else if eq .Type "Float64"}} + if cValue := v.Get("{{getResponseModelPath .}}"); cValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && item.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && cValue.String() != ""{{end}} { + item.{{toGoName .TfName}} = types.Float64Value(cValue.Float()) + } else { + item.{{toGoName .TfName}} = types.Float64Null() + } + {{- else if eq .Type "Bool"}} + if cValue := v.Get("{{getResponseModelPath .}}"); cValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && item.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && cValue.String() != ""{{end}} { + if {{.BoolEmptyString}} && cValue.String() == "" { + item.{{toGoName .TfName}} = types.BoolValue(true) + }{{if not .BoolEmptyString}} else { + item.{{toGoName .TfName}} = types.BoolValue(cValue.Bool()) + } + {{- end}} + } else { + item.{{toGoName .TfName}} = types.BoolNull() + } + {{- else if isListSet .}} + if cValue := v.Get("{{getResponseModelPath .}}"); cValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && item.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && cValue.String() != ""{{end}} { + item.{{toGoName .TfName}} = helpers.Get{{.ElementType}}{{.Type}}(cValue.Array()) + } else { + item.{{toGoName .TfName}} = types.{{.Type}}Null(types.{{.ElementType}}Type) + } + {{- else if isNestedListSet .}} + if cValue := v.Get("{{getResponseModelPath .}}"); cValue.Exists() && len(cValue.Array()) > 0{{if ne .ConditionalAttribute.Name ""}} && item.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}} { + item.{{toGoName .TfName}} = make([]{{$name}}{{$cname}}{{toGoName .TfName}}, 0) + cValue.ForEach(func(ck, cv gjson.Result) bool { + cItem := {{$name}}{{$cname}}{{toGoName .TfName}}{} + {{- range .Attributes}} + {{- if and (not .TfOnly) (not .Value) (not .Reference)}} + {{- if eq .Type "String"}} + if ccValue := cv.Get("{{getResponseModelPath .}}"); ccValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && cItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && ccValue.String() != ""{{end}} { + cItem.{{toGoName .TfName}} = types.StringValue(ccValue.String()) + } else { + cItem.{{toGoName .TfName}} = types.StringNull() + } + {{- else if eq .Type "Int64"}} + if ccValue := cv.Get("{{getResponseModelPath .}}"); ccValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && cItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && ccValue.String() != ""{{end}} { + cItem.{{toGoName .TfName}} = types.Int64Value(ccValue.Int()) + } else { + cItem.{{toGoName .TfName}} = types.Int64Null() + } + {{- else if eq .Type "Float64"}} + if ccValue := cv.Get("{{getResponseModelPath .}}"); ccValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && cItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && ccValue.String() != ""{{end}} { + cItem.{{toGoName .TfName}} = types.Float64Value(ccValue.Float()) + } else { + cItem.{{toGoName .TfName}} = types.Float64Null() + } + {{- else if eq .Type "Bool"}} + if ccValue := cv.Get("{{getResponseModelPath .}}"); ccValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && cItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && ccValue.String() != ""{{end}} { + if {{.BoolEmptyString}} && ccValue.String() == "" { + cItem.{{toGoName .TfName}} = types.BoolValue(true) + }{{if not .BoolEmptyString}} else { + cItem.{{toGoName .TfName}} = types.BoolValue(ccValue.Bool()) + } + {{- end}} + } else { + cItem.{{toGoName .TfName}} = types.BoolNull() + } + {{- else if isListSet .}} + if ccValue := cv.Get("{{getResponseModelPath .}}"); ccValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && cItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && ccValue.String() != ""{{end}} { + cItem.{{toGoName .TfName}} = helpers.Get{{.ElementType}}{{.Type}}(ccValue.Array()) + } else { + cItem.{{toGoName .TfName}} = types.{{.Type}}Null(types.{{.ElementType}}Type) + } + {{- else if isNestedListSet .}} + if ccValue := cv.Get("{{getResponseModelPath .}}"); ccValue.Exists() && len(ccValue.Array()) > 0{{if ne .ConditionalAttribute.Name ""}} && cItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}} { + cItem.{{toGoName .TfName}} = make([]{{$name}}{{$cname}}{{$ccname}}{{toGoName .TfName}}, 0) + ccValue.ForEach(func(cck, ccv gjson.Result) bool { + ccItem := {{$name}}{{$cname}}{{$ccname}}{{toGoName .TfName}}{} + {{- range .Attributes}} + {{- if and (not .TfOnly) (not .Value) (not .Reference)}} + {{- if eq .Type "String"}} + if cccValue := ccv.Get("{{getResponseModelPath .}}"); cccValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && ccItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && cccValue.String() != ""{{end}} { + ccItem.{{toGoName .TfName}} = types.StringValue(cccValue.String()) + } else { + ccItem.{{toGoName .TfName}} = types.StringNull() + } + {{- else if eq .Type "Int64"}} + if cccValue := ccv.Get("{{getResponseModelPath .}}"); cccValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && ccItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && cccValue.String() != ""{{end}} { + ccItem.{{toGoName .TfName}} = types.Int64Value(cccValue.Int()) + } else { + ccItem.{{toGoName .TfName}} = types.Int64Null() + } + {{- else if eq .Type "Float64"}} + if cccValue := ccv.Get("{{getResponseModelPath .}}"); cccValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && ccItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && cccValue.String() != ""{{end}} { + ccItem.{{toGoName .TfName}} = types.Float64Value(cccValue.Float()) + } else { + ccItem.{{toGoName .TfName}} = types.Float64Null() + } + {{- else if eq .Type "Bool"}} + if cccValue := ccv.Get("{{getResponseModelPath .}}"); cccValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && ccItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && cccValue.String() != ""{{end}} { + if {{.BoolEmptyString}} && cccValue.String() == "" { + ccItem.{{toGoName .TfName}} = types.BoolValue(true) + }{{if not .BoolEmptyString}} else { + ccItem.{{toGoName .TfName}} = types.BoolValue(cccValue.Bool()) + } + {{- end}} + } else { + ccItem.{{toGoName .TfName}} = types.BoolNull() + } + {{- else if isListSet .}} + if cccValue := ccv.Get("{{getResponseModelPath .}}"); cccValue.Exists(){{if ne .ConditionalAttribute.Name ""}} && ccItem.{{toGoName .ConditionalAttribute.Name}}.ValueString() == "{{.ConditionalAttribute.Value}}"{{end}}{{if .AlwaysInclude}} && cccValue.String() != ""{{end}} { + ccItem.{{toGoName .TfName}} = helpers.Get{{.ElementType}}{{.Type}}(cccValue.Array()) + } else { + ccItem.{{toGoName .TfName}} = types.{{.Type}}Null(types.{{.ElementType}}Type) + } + {{- end}} + {{- end}} + {{- end}} + cItem.{{toGoName .TfName}} = append(cItem.{{toGoName .TfName}}, ccItem) + return true + }) + } else { + if len(cItem.{{toGoName .TfName}}) > 0 { + cItem.{{toGoName .TfName}} = []{{$name}}{{$cname}}{{$ccname}}{{toGoName .TfName}}{} + } + } + {{- end}} + {{- end}} + {{- end}} + item.{{toGoName .TfName}} = append(item.{{toGoName .TfName}}, cItem) + return true + }) + } else { + if len(item.{{toGoName .TfName}}) > 0 { + item.{{toGoName .TfName}} = []{{$name}}{{$cname}}{{toGoName .TfName}}{} + } + } + {{- end}} + {{- end}} + {{- end}} + data.{{toGoName .TfName}} = append(data.{{toGoName .TfName}}, item) + return true + }) + } else { + if len(data.{{toGoName .TfName}}) > 0 { + data.{{toGoName .TfName}} = []{{$name}}{{toGoName .TfName}}{} + } + } + {{- end}} + {{- end}} + {{- end}} + {{- if hasVersionAttribute .Attributes}} + data.updateVersions(ctx, &state) + {{- end}} } // End of section. //template:end fromBody // Section below is generated&owned by "gen/generator.go". //template:begin hasChanges func (data *{{camelCase .Name}}) hasChanges(ctx context.Context, state *{{camelCase .Name}}) bool { - hasChanges := false - {{- range .Attributes}} - {{- $name := toGoName .TfName}} - {{- if and (not .Value) (not .TfOnly)}} - {{- if not (isNestedListSet .)}} - if !data.{{toGoName .TfName}}.Equal(state.{{toGoName .TfName}}) { - hasChanges = true - } - {{- else}} - if len(data.{{toGoName .TfName}}) != len(state.{{toGoName .TfName}}) { - hasChanges = true - } else { - for i := range data.{{toGoName .TfName}} { - {{- range .Attributes}} - {{- $cname := toGoName .TfName}} - {{- if and (not .Value) (not .TfOnly)}} - {{- if not (isNestedListSet .)}} - if !data.{{$name}}[i].{{toGoName .TfName}}.Equal(state.{{$name}}[i].{{toGoName .TfName}}) { - hasChanges = true - } - {{- else}} - if len(data.{{$name}}[i].{{toGoName .TfName}}) != len(state.{{$name}}[i].{{toGoName .TfName}}) { - hasChanges = true - } else { - for ii := range data.{{$name}}[i].{{toGoName .TfName}} { - {{- range .Attributes}} - {{- $ccname := toGoName .TfName}} - {{- if and (not .Value) (not .TfOnly)}} - {{- if not (isNestedListSet .)}} - if !data.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}}.Equal(state.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}}) { - hasChanges = true - } - {{- else}} - if len(data.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}}) != len(state.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}}) { - hasChanges = true - } else { - for iii := range data.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}} { - {{- range .Attributes}} - {{- if and (not .Value) (not .TfOnly)}} - if !data.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toGoName .TfName}}.Equal(state.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toGoName .TfName}}) { - hasChanges = true - } - {{- end}} - {{- end}} - } - } - {{- end}} - {{- end}} - {{- end}} - } - } - {{- end}} - {{- end}} - {{- end}} - } - } - {{- end}} - {{- end}} - {{- end}} - return hasChanges + hasChanges := false + {{- range .Attributes}} + {{- $name := toGoName .TfName}} + {{- if and (not .Value) (not .TfOnly)}} + {{- if not (isNestedListSet .)}} + if !data.{{toGoName .TfName}}.Equal(state.{{toGoName .TfName}}) { + hasChanges = true + } + {{- else}} + if len(data.{{toGoName .TfName}}) != len(state.{{toGoName .TfName}}) { + hasChanges = true + } else { + for i := range data.{{toGoName .TfName}} { + {{- range .Attributes}} + {{- $cname := toGoName .TfName}} + {{- if and (not .Value) (not .TfOnly)}} + {{- if not (isNestedListSet .)}} + if !data.{{$name}}[i].{{toGoName .TfName}}.Equal(state.{{$name}}[i].{{toGoName .TfName}}) { + hasChanges = true + } + {{- else}} + if len(data.{{$name}}[i].{{toGoName .TfName}}) != len(state.{{$name}}[i].{{toGoName .TfName}}) { + hasChanges = true + } else { + for ii := range data.{{$name}}[i].{{toGoName .TfName}} { + {{- range .Attributes}} + {{- $ccname := toGoName .TfName}} + {{- if and (not .Value) (not .TfOnly)}} + {{- if not (isNestedListSet .)}} + if !data.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}}.Equal(state.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}}) { + hasChanges = true + } + {{- else}} + if len(data.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}}) != len(state.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}}) { + hasChanges = true + } else { + for iii := range data.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}} { + {{- range .Attributes}} + {{- if and (not .Value) (not .TfOnly)}} + if !data.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toGoName .TfName}}.Equal(state.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toGoName .TfName}}) { + hasChanges = true + } + {{- end}} + {{- end}} + } + } + {{- end}} + {{- end}} + {{- end}} + } + } + {{- end}} + {{- end}} + {{- end}} + } + } + {{- end}} + {{- end}} + {{- end}} + return hasChanges } // End of section. //template:end hasChanges // Section below is generated&owned by "gen/generator.go". //template:begin updateVersions {{if hasVersionAttribute .Attributes}} func (data *{{camelCase .Name}}) updateVersions(ctx context.Context, state *{{camelCase .Name}}) { - {{- range .Attributes}} - {{- $name := toGoName .TfName}} - {{- if or (eq .Type "Version") (eq .Type "Versions")}} - data.{{toGoName .TfName}} = state.{{toGoName .TfName}} - {{- else if and (isNestedListSet .) (hasVersionAttribute .Attributes)}} - for i := range data.{{toGoName .TfName}} { - dataKeys := [...]string{ {{range .Attributes}}{{if .Id}}fmt.Sprintf("%v", data.{{$name}}[i].{{toGoName .TfName}}.{{if isStringListSet .}}String{{else}}Value{{.Type}}{{end}}()), {{end}}{{end}} } - stateIndex := -1 - for j := range state.{{toGoName .TfName}} { - stateKeys := [...]string{ {{range .Attributes}}{{if .Id}}fmt.Sprintf("%v", state.{{$name}}[j].{{toGoName .TfName}}.{{if isStringListSet .}}String{{else}}Value{{.Type}}{{end}}()), {{end}}{{end}} } - if dataKeys == stateKeys { - stateIndex = j + {{- range .Attributes}} + {{- $name := toGoName .TfName}} + {{- if or (eq .Type "Version") (eq .Type "Versions")}} + data.{{toGoName .TfName}} = state.{{toGoName .TfName}} + {{- else if and (isNestedListSet .) (hasVersionAttribute .Attributes)}} + for i := range data.{{toGoName .TfName}} { + dataKeys := [...]string{ {{range .Attributes}}{{if .Id}}fmt.Sprintf("%v", data.{{$name}}[i].{{toGoName .TfName}}.{{if isStringListSet .}}String{{else}}Value{{.Type}}{{end}}()), {{end}}{{end}} } + stateIndex := -1 + for j := range state.{{toGoName .TfName}} { + stateKeys := [...]string{ {{range .Attributes}}{{if .Id}}fmt.Sprintf("%v", state.{{$name}}[j].{{toGoName .TfName}}.{{if isStringListSet .}}String{{else}}Value{{.Type}}{{end}}()), {{end}}{{end}} } + if dataKeys == stateKeys { + stateIndex = j break - } - } - {{- range .Attributes}} - {{- $cname := toGoName .TfName}} - {{- if eq .Type "Version"}} - if stateIndex > -1 { - data.{{$name}}[i].{{toGoName .TfName}} = state.{{$name}}[stateIndex].{{toGoName .TfName}} - } else { - data.{{$name}}[i].{{toGoName .TfName}} = types.Int64Null() - } - {{- else if eq .Type "Versions"}} - if stateIndex > -1 && !state.{{$name}}[stateIndex].{{toGoName .TfName}}.IsNull() { - data.{{$name}}[i].{{toGoName .TfName}} = state.{{$name}}[stateIndex].{{toGoName .TfName}} - } else { - data.{{$name}}[i].{{toGoName .TfName}} = types.ListNull(types.StringType) - } - {{- else if and (isNestedListSet .) (hasVersionAttribute .Attributes)}} - for ii := range data.{{$name}}[i].{{toGoName .TfName}} { - cDataKeys := [...]string{ {{range .Attributes}}{{if .Id}}fmt.Sprintf("%v", data.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}}.{{if isStringListSet .}}String{{else}}Value{{.Type}}{{end}}()), {{end}}{{end}} } - cStateIndex := -1 - if stateIndex > -1 { - for jj := range state.{{$name}}[stateIndex].{{toGoName .TfName}} { - cStateKeys := [...]string{ {{range .Attributes}}{{if .Id}}fmt.Sprintf("%v", state.{{$name}}[stateIndex].{{$cname}}[jj].{{toGoName .TfName}}.{{if isStringListSet .}}String{{else}}Value{{.Type}}{{end}}()), {{end}}{{end}} } - if cDataKeys == cStateKeys { - cStateIndex = jj - break - } - } - } - {{- range .Attributes}} - {{- $ccname := toGoName .TfName}} - {{- if eq .Type "Version"}} - if cStateIndex > -1 { - data.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}} = state.{{$name}}[stateIndex].{{$cname}}[cStateIndex].{{toGoName .TfName}} - } else { - data.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}} = types.Int64Null() - } - {{- else if eq .Type "Versions"}} - if cStateIndex > -1 && !state.{{$name}}[stateIndex].{{$cname}}[cStateIndex].{{toGoName .TfName}}.IsNull() { - data.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}} = state.{{$name}}[stateIndex].{{$cname}}[cStateIndex].{{toGoName .TfName}} - } else { - data.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}} = types.ListNull(types.StringType) - } - {{- else if and (isNestedListSet .) (hasVersionAttribute .Attributes)}} - for iii := range data.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}} { - ccDataKeys := [...]string{ {{range .Attributes}}{{if .Id}}fmt.Sprintf("%v", data.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toGoName .TfName}}.{{if isStringListSet .}}String{{else}}Value{{.Type}}{{end}}()), {{end}}{{end}} } - ccStateIndex := -1 - if cStateIndex > -1 { - for jjj := range state.{{$name}}[stateIndex].{{$cname}}[cStateIndex].{{toGoName .TfName}} { - ccStateKeys := [...]string{ {{range .Attributes}}{{if .Id}}fmt.Sprintf("%v", state.{{$name}}[stateIndex].{{$cname}}[cStateIndex].{{$ccname}}[jjj].{{toGoName .TfName}}.{{if isStringListSet .}}String{{else}}Value{{.Type}}{{end}}()), {{end}}{{end}} } - if ccDataKeys == ccStateKeys { - ccStateIndex = jjj - break - } - } - } - {{- range .Attributes}} - {{- if eq .Type "Version"}} - if ccStateIndex > -1 { - data.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toGoName .TfName}} = state.{{$name}}[stateIndex].{{$cname}}[cStateIndex].{{$ccname}}[ccStateIndex].{{toGoName .TfName}} - } else { - data.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toGoName .TfName}} = types.Int64Null() - } - {{- else if eq .Type "Versions"}} - if ccStateIndex > -1 && !state.{{$name}}[stateIndex].{{$cname}}[cStateIndex].{{$ccname}}[ccStateIndex].{{toGoName .TfName}}.IsNull() { - data.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toGoName .TfName}} = state.{{$name}}[stateIndex].{{$cname}}[cStateIndex].{{$ccname}}[ccStateIndex].{{toGoName .TfName}} - } else { - data.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toGoName .TfName}} = types.ListNull(types.StringType) - } - {{- end}} - {{- end}} - } - {{- end}} - {{- end}} - } - {{- end}} - {{- end}} - } - {{- end}} - {{- end}} + } + } + {{- range .Attributes}} + {{- $cname := toGoName .TfName}} + {{- if eq .Type "Version"}} + if stateIndex > -1 { + data.{{$name}}[i].{{toGoName .TfName}} = state.{{$name}}[stateIndex].{{toGoName .TfName}} + } else { + data.{{$name}}[i].{{toGoName .TfName}} = types.Int64Null() + } + {{- else if eq .Type "Versions"}} + if stateIndex > -1 && !state.{{$name}}[stateIndex].{{toGoName .TfName}}.IsNull() { + data.{{$name}}[i].{{toGoName .TfName}} = state.{{$name}}[stateIndex].{{toGoName .TfName}} + } else { + data.{{$name}}[i].{{toGoName .TfName}} = types.ListNull(types.StringType) + } + {{- else if and (isNestedListSet .) (hasVersionAttribute .Attributes)}} + for ii := range data.{{$name}}[i].{{toGoName .TfName}} { + cDataKeys := [...]string{ {{range .Attributes}}{{if .Id}}fmt.Sprintf("%v", data.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}}.{{if isStringListSet .}}String{{else}}Value{{.Type}}{{end}}()), {{end}}{{end}} } + cStateIndex := -1 + if stateIndex > -1 { + for jj := range state.{{$name}}[stateIndex].{{toGoName .TfName}} { + cStateKeys := [...]string{ {{range .Attributes}}{{if .Id}}fmt.Sprintf("%v", state.{{$name}}[stateIndex].{{$cname}}[jj].{{toGoName .TfName}}.{{if isStringListSet .}}String{{else}}Value{{.Type}}{{end}}()), {{end}}{{end}} } + if cDataKeys == cStateKeys { + cStateIndex = jj + break + } + } + } + {{- range .Attributes}} + {{- $ccname := toGoName .TfName}} + {{- if eq .Type "Version"}} + if cStateIndex > -1 { + data.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}} = state.{{$name}}[stateIndex].{{$cname}}[cStateIndex].{{toGoName .TfName}} + } else { + data.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}} = types.Int64Null() + } + {{- else if eq .Type "Versions"}} + if cStateIndex > -1 && !state.{{$name}}[stateIndex].{{$cname}}[cStateIndex].{{toGoName .TfName}}.IsNull() { + data.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}} = state.{{$name}}[stateIndex].{{$cname}}[cStateIndex].{{toGoName .TfName}} + } else { + data.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}} = types.ListNull(types.StringType) + } + {{- else if and (isNestedListSet .) (hasVersionAttribute .Attributes)}} + for iii := range data.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}} { + ccDataKeys := [...]string{ {{range .Attributes}}{{if .Id}}fmt.Sprintf("%v", data.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toGoName .TfName}}.{{if isStringListSet .}}String{{else}}Value{{.Type}}{{end}}()), {{end}}{{end}} } + ccStateIndex := -1 + if cStateIndex > -1 { + for jjj := range state.{{$name}}[stateIndex].{{$cname}}[cStateIndex].{{toGoName .TfName}} { + ccStateKeys := [...]string{ {{range .Attributes}}{{if .Id}}fmt.Sprintf("%v", state.{{$name}}[stateIndex].{{$cname}}[cStateIndex].{{$ccname}}[jjj].{{toGoName .TfName}}.{{if isStringListSet .}}String{{else}}Value{{.Type}}{{end}}()), {{end}}{{end}} } + if ccDataKeys == ccStateKeys { + ccStateIndex = jjj + break + } + } + } + {{- range .Attributes}} + {{- if eq .Type "Version"}} + if ccStateIndex > -1 { + data.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toGoName .TfName}} = state.{{$name}}[stateIndex].{{$cname}}[cStateIndex].{{$ccname}}[ccStateIndex].{{toGoName .TfName}} + } else { + data.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toGoName .TfName}} = types.Int64Null() + } + {{- else if eq .Type "Versions"}} + if ccStateIndex > -1 && !state.{{$name}}[stateIndex].{{$cname}}[cStateIndex].{{$ccname}}[ccStateIndex].{{toGoName .TfName}}.IsNull() { + data.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toGoName .TfName}} = state.{{$name}}[stateIndex].{{$cname}}[cStateIndex].{{$ccname}}[ccStateIndex].{{toGoName .TfName}} + } else { + data.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toGoName .TfName}} = types.ListNull(types.StringType) + } + {{- end}} + {{- end}} + } + {{- end}} + {{- end}} + } + {{- end}} + {{- end}} + } + {{- end}} + {{- end}} } {{end}} // End of section. //template:end updateVersions // Section below is generated&owned by "gen/generator.go". //template:begin processImport func (data *{{camelCase .Name}}) processImport(ctx context.Context) { - {{- if .HasVersion}} - data.Version = types.Int64Value(0) - {{- end}} - {{- if .TypeValue}} - data.Type = types.StringValue("{{ .TypeValue }}") - {{- end}} - {{- range .Attributes}} - {{- $name := toGoName .TfName}} - {{- if not .Value}} - {{- if .Value}} - data.{{toGoName .TfName}} = {{if eq .Type "String"}}types.StringValue("{{ .Value }}"){{else if eq .Type "Bool"}}types.BoolValue({{ .Value }}){{else if eq .Type "List"}}{{ .Value }}{{else}}types.ListNull({{ .Value }}){{end}} - {{- end}} - {{- if eq .Type "Version"}} - if data.{{toVersionName .TfName}} != types.StringNull() { - data.{{toGoName .TfName}} = types.Int64Value(0) - } - {{- else if eq .Type "Versions"}} - if !data.{{toVersionName .TfName}}.IsNull() { - data.{{toGoName .TfName}} = types.ListNull(types.StringType) - } - {{- else if and (isNestedListSet .) (hasVersionAttribute .Attributes)}} - for i := range data.{{toGoName .TfName}} { + {{- if .HasVersion}} + data.Version = types.Int64Value(0) + {{- end}} + {{- if .TypeValue}} + data.Type = types.StringValue("{{ .TypeValue }}") + {{- end}} + {{- range .Attributes}} + {{- $name := toGoName .TfName}} + {{- if not .Value}} + {{- if .Value}} + data.{{toGoName .TfName}} = {{if eq .Type "String"}}types.StringValue("{{ .Value }}"){{else if eq .Type "Bool"}}types.BoolValue({{ .Value }}){{else if eq .Type "List"}}{{ .Value }}{{else}}types.ListNull({{ .Value }}){{end}} + {{- end}} + {{- if eq .Type "Version"}} + if data.{{toVersionName .TfName}} != types.StringNull() { + data.{{toGoName .TfName}} = types.Int64Value(0) + } + {{- else if eq .Type "Versions"}} + if !data.{{toVersionName .TfName}}.IsNull() { + var elements{{toGoName .TfName}} []attr.Value + var count{{toGoName .TfName}} int = 0 + if !(data.{{toVersionName .TfName}}.IsNull() || data.{{toVersionName .TfName}}.IsUnknown()) { + count{{toGoName .TfName}} = len(data.{{toVersionName .TfName}}.Elements()) + } + if count{{toGoName .TfName}} > 0 { + elements{{toGoName .TfName}} = make([]attr.Value, count{{toGoName .TfName}}) + for i := 0; i < count{{toGoName .TfName}}; i++ { + elements{{toGoName .TfName}}[i] = types.StringValue("0") + } + data.{{toGoName .TfName}} = types.ListValueMust(types.StringType, elements{{toGoName .TfName}}) + } else { + data.{{toGoName .TfName}} = types.ListNull(types.StringType) + } + } + {{- else if and (isNestedListSet .) (hasVersionAttribute .Attributes)}} + for i := range data.{{toGoName .TfName}} { - {{- range .Attributes}} - {{- $cname := toGoName .TfName}} - {{- if not .Value}} - {{- if eq .Type "Version"}} - if data.{{$name}}[i].{{toVersionName .TfName}} != types.StringNull() { - data.{{$name}}[i].{{toGoName .TfName}} = types.Int64Value(0) - } - {{- else if eq .Type "Versions"}} - if !data.{{$name}}[i].{{toVersionName .TfName}}.IsNull() { - data.{{$name}}[i].{{toGoName .TfName}} = types.ListNull(types.StringType) - } - {{- else if and (isNestedListSet .) (hasVersionAttribute .Attributes)}} - for ii := range data.{{$name}}[i].{{toGoName .TfName}} { + {{- range .Attributes}} + {{- $cname := toGoName .TfName}} + {{- if not .Value}} + {{- if eq .Type "Version"}} + if data.{{$name}}[i].{{toVersionName .TfName}} != types.StringNull() { + data.{{$name}}[i].{{toGoName .TfName}} = types.Int64Value(0) + } + {{- else if eq .Type "Versions"}} + if !data.{{$name}}[i].{{toVersionName .TfName}}.IsNull() { + var elements{{toGoName .TfName}} []attr.Value + var count{{toGoName .TfName}} int = 0 + if !(data.{{$name}}[i].{{toVersionName .TfName}}.IsNull() || data.{{$name}}[i].{{toVersionName .TfName}}.IsUnknown()) { + count{{toGoName .TfName}} = len(data.{{$name}}[i].{{toVersionName .TfName}}.Elements()) + } + if count{{toGoName .TfName}} > 0 { + elements{{toGoName .TfName}} = make([]attr.Value, count{{toGoName .TfName}}) + for i := 0; i < count{{toGoName .TfName}}; i++ { + elements{{toGoName .TfName}}[i] = types.StringValue("0") + } + data.{{$name}}[i].{{toGoName .TfName}} = types.ListValueMust(types.StringType, elements{{toGoName .TfName}}) + } else { + data.{{$name}}[i].{{toGoName .TfName}} = types.ListNull(types.StringType) + } + } + {{- else if and (isNestedListSet .) (hasVersionAttribute .Attributes)}} + for ii := range data.{{$name}}[i].{{toGoName .TfName}} { - {{- range .Attributes}} - {{- $ccname := toGoName .TfName}} - {{- if not .Value}} - {{- if eq .Type "Version"}} - if data.{{$name}}[i].{{$cname}}[ii].{{toVersionName .TfName}} != types.StringNull() { - data.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}} = types.Int64Value(0) - } - {{- else if eq .Type "Versions"}} - if !data.{{$name}}[i].{{$cname}}[ii].{{toVersionName .TfName}}.IsNull() { - data.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}} = types.ListNull(types.StringType) - } - {{- else if and (isNestedListSet .) (hasVersionAttribute .Attributes)}} - for iii := range data.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}} { + {{- range .Attributes}} + {{- $ccname := toGoName .TfName}} + {{- if not .Value}} + {{- if eq .Type "Version"}} + if data.{{$name}}[i].{{$cname}}[ii].{{toVersionName .TfName}} != types.StringNull() { + data.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}} = types.Int64Value(0) + } + {{- else if eq .Type "Versions"}} + if !data.{{$name}}[i].{{$cname}}[ii].{{toVersionName .TfName}}.IsNull() { + var elements{{toGoName .TfName}} []attr.Value + var count{{toGoName .TfName}} int = 0 + if !(data.{{$name}}[i].{{$cname}}[ii].{{toVersionName .TfName}}.IsNull() || data.{{$name}}[i].{{$cname}}[ii].{{toVersionName .TfName}}.IsUnknown()) { + count{{toGoName .TfName}} = len(data.{{$name}}[i].{{$cname}}[ii].{{toVersionName .TfName}}.Elements()) + } + if count{{toGoName .TfName}} > 0 { + elements{{toGoName .TfName}} = make([]attr.Value, count{{toGoName .TfName}}) + for i := 0; i < count{{toGoName .TfName}}; i++ { + elements{{toGoName .TfName}}[i] = types.StringValue("0") + } + data.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}} = types.ListValueMust(types.StringType, elements{{toGoName .TfName}}) + } else { + data.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}} = types.ListNull(types.StringType) + } + } + {{- else if and (isNestedListSet .) (hasVersionAttribute .Attributes)}} + for iii := range data.{{$name}}[i].{{$cname}}[ii].{{toGoName .TfName}} { - {{- range .Attributes}} - {{- if eq .Type "Version"}} - if data.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toVersionName .TfName}} != types.StringNull() { - data.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toGoName .TfName}} = types.Int64Value(0) - } - {{- else if eq .Type "Versions"}} - if !data.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toVersionName .TfName}}.IsNull() { - data.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toGoName .TfName}} = types.ListNull(types.StringType) - } - {{- end}} - {{- end}} - } - {{- end}} - {{- end}} - {{- end}} - } - {{- end}} - {{- end}} - {{- end}} - } - {{- end}} - {{- end}} - {{- end}} + {{- range .Attributes}} + {{- if eq .Type "Version"}} + if data.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toVersionName .TfName}} != types.StringNull() { + data.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toGoName .TfName}} = types.Int64Value(0) + } + {{- else if eq .Type "Versions"}} + if !data.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toVersionName .TfName}}.IsNull() { + var elements{{toGoName .TfName}} []attr.Value + var count{{toGoName .TfName}} int = 0 + if !(data.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toVersionName .TfName}}.IsNull() || data.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toVersionName .TfName}}.IsUnknown()) { + count{{toGoName .TfName}} = len(data.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toVersionName .TfName}}.Elements()) + } + if count{{toGoName .TfName}} > 0 { + elements{{toGoName .TfName}} = make([]attr.Value, count{{toGoName .TfName}}) + for i := 0; i < count{{toGoName .TfName}}; i++ { + elements{{toGoName .TfName}}[i] = types.StringValue("0") + } + data.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toGoName .TfName}} = types.ListValueMust(types.StringType, elements{{toGoName .TfName}}) + } else { + data.{{$name}}[i].{{$cname}}[ii].{{$ccname}}[iii].{{toGoName .TfName}} = types.ListNull(types.StringType) + } + } + {{- end}} + {{- end}} + } + {{- end}} + {{- end}} + {{- end}} + } + {{- end}} + {{- end}} + {{- end}} + } + {{- end}} + {{- end}} + {{- end}} } // End of section. //template:end processImport \ No newline at end of file diff --git a/internal/provider/model_sdwan_centralized_policy.go b/internal/provider/model_sdwan_centralized_policy.go index a1d629a3a..97df5721f 100644 --- a/internal/provider/model_sdwan_centralized_policy.go +++ b/internal/provider/model_sdwan_centralized_policy.go @@ -23,6 +23,7 @@ import ( "fmt" "github.com/CiscoDevNet/terraform-provider-sdwan/internal/provider/helpers" + "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/tidwall/gjson" "github.com/tidwall/sjson" @@ -310,13 +311,52 @@ func (data *CentralizedPolicy) processImport(ctx context.Context) { } for ii := range data.Definitions[i].Entries { if !data.Definitions[i].Entries[ii].SiteListIds.IsNull() { - data.Definitions[i].Entries[ii].SiteListVersions = types.ListNull(types.StringType) + var elementsSiteListVersions []attr.Value + var countSiteListVersions int = 0 + if !(data.Definitions[i].Entries[ii].SiteListIds.IsNull() || data.Definitions[i].Entries[ii].SiteListIds.IsUnknown()) { + countSiteListVersions = len(data.Definitions[i].Entries[ii].SiteListIds.Elements()) + } + if countSiteListVersions > 0 { + elementsSiteListVersions = make([]attr.Value, countSiteListVersions) + for i := 0; i < countSiteListVersions; i++ { + elementsSiteListVersions[i] = types.StringValue("0") + } + data.Definitions[i].Entries[ii].SiteListVersions = types.ListValueMust(types.StringType, elementsSiteListVersions) + } else { + data.Definitions[i].Entries[ii].SiteListVersions = types.ListNull(types.StringType) + } } if !data.Definitions[i].Entries[ii].VpnListIds.IsNull() { - data.Definitions[i].Entries[ii].VpnListVersions = types.ListNull(types.StringType) + var elementsVpnListVersions []attr.Value + var countVpnListVersions int = 0 + if !(data.Definitions[i].Entries[ii].VpnListIds.IsNull() || data.Definitions[i].Entries[ii].VpnListIds.IsUnknown()) { + countVpnListVersions = len(data.Definitions[i].Entries[ii].VpnListIds.Elements()) + } + if countVpnListVersions > 0 { + elementsVpnListVersions = make([]attr.Value, countVpnListVersions) + for i := 0; i < countVpnListVersions; i++ { + elementsVpnListVersions[i] = types.StringValue("0") + } + data.Definitions[i].Entries[ii].VpnListVersions = types.ListValueMust(types.StringType, elementsVpnListVersions) + } else { + data.Definitions[i].Entries[ii].VpnListVersions = types.ListNull(types.StringType) + } } if !data.Definitions[i].Entries[ii].RegionListIds.IsNull() { - data.Definitions[i].Entries[ii].RegionListVersions = types.ListNull(types.StringType) + var elementsRegionListVersions []attr.Value + var countRegionListVersions int = 0 + if !(data.Definitions[i].Entries[ii].RegionListIds.IsNull() || data.Definitions[i].Entries[ii].RegionListIds.IsUnknown()) { + countRegionListVersions = len(data.Definitions[i].Entries[ii].RegionListIds.Elements()) + } + if countRegionListVersions > 0 { + elementsRegionListVersions = make([]attr.Value, countRegionListVersions) + for i := 0; i < countRegionListVersions; i++ { + elementsRegionListVersions[i] = types.StringValue("0") + } + data.Definitions[i].Entries[ii].RegionListVersions = types.ListValueMust(types.StringType, elementsRegionListVersions) + } else { + data.Definitions[i].Entries[ii].RegionListVersions = types.ListNull(types.StringType) + } } } } diff --git a/internal/provider/model_sdwan_configuration_group.go b/internal/provider/model_sdwan_configuration_group.go index 9e982c0d4..66b4747e5 100644 --- a/internal/provider/model_sdwan_configuration_group.go +++ b/internal/provider/model_sdwan_configuration_group.go @@ -568,3 +568,22 @@ func (data ConfigurationGroup) hasFeatureVersionChanges(ctx context.Context, sta } return false } + +func (data ConfigurationGroup) processImport(ctx context.Context) { + var elementsFeatureList []attr.Value + var countFeatureProfile int = 0 + + if !(data.FeatureProfileIds.IsNull() || data.FeatureProfileIds.IsUnknown()) { + countFeatureProfile = len(data.FeatureProfileIds.Elements()) + } + + if countFeatureProfile > 0 { + elementsFeatureList = make([]attr.Value, countFeatureProfile) + for i := 0; i < countFeatureProfile; i++ { + elementsFeatureList[i] = types.Int64Value(0) + } + data.FeatureVersions = types.ListValueMust(types.Int64Type, elementsFeatureList) + } else { + data.FeatureVersions = types.ListNull(types.StringType) + } +} diff --git a/internal/provider/model_sdwan_mesh_topology_policy_definition.go b/internal/provider/model_sdwan_mesh_topology_policy_definition.go index ea468407a..bf3c6c1f1 100644 --- a/internal/provider/model_sdwan_mesh_topology_policy_definition.go +++ b/internal/provider/model_sdwan_mesh_topology_policy_definition.go @@ -23,6 +23,7 @@ import ( "fmt" "github.com/CiscoDevNet/terraform-provider-sdwan/internal/provider/helpers" + "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/tidwall/gjson" "github.com/tidwall/sjson" @@ -199,7 +200,20 @@ func (data *MeshTopologyPolicyDefinition) processImport(ctx context.Context) { } for i := range data.Regions { if !data.Regions[i].SiteListIds.IsNull() { - data.Regions[i].SiteListVersions = types.ListNull(types.StringType) + var elementsSiteListVersions []attr.Value + var countSiteListVersions int = 0 + if !(data.Regions[i].SiteListIds.IsNull() || data.Regions[i].SiteListIds.IsUnknown()) { + countSiteListVersions = len(data.Regions[i].SiteListIds.Elements()) + } + if countSiteListVersions > 0 { + elementsSiteListVersions = make([]attr.Value, countSiteListVersions) + for i := 0; i < countSiteListVersions; i++ { + elementsSiteListVersions[i] = types.StringValue("0") + } + data.Regions[i].SiteListVersions = types.ListValueMust(types.StringType, elementsSiteListVersions) + } else { + data.Regions[i].SiteListVersions = types.ListNull(types.StringType) + } } } } diff --git a/internal/provider/model_sdwan_policy_group.go b/internal/provider/model_sdwan_policy_group.go index ca2725352..7dbce9bda 100644 --- a/internal/provider/model_sdwan_policy_group.go +++ b/internal/provider/model_sdwan_policy_group.go @@ -410,3 +410,22 @@ func (data PolicyGroup) hasPolicyVersionChanges(ctx context.Context, state *Poli } return false } + +func (data PolicyGroup) processImport(ctx context.Context) { + var elementsFeatureList []attr.Value + var countFeatureProfile int = 0 + + if !(data.FeatureProfileIds.IsNull() || data.FeatureProfileIds.IsUnknown()) { + countFeatureProfile = len(data.FeatureProfileIds.Elements()) + } + + if countFeatureProfile > 0 { + elementsFeatureList = make([]attr.Value, countFeatureProfile) + for i := 0; i < countFeatureProfile; i++ { + elementsFeatureList[i] = types.Int64Value(0) + } + data.PolicyVersions = types.ListValueMust(types.Int64Type, elementsFeatureList) + } else { + data.PolicyVersions = types.ListNull(types.StringType) + } +} diff --git a/internal/provider/model_sdwan_route_policy_definition.go b/internal/provider/model_sdwan_route_policy_definition.go index 200d3ff72..c77717ffe 100644 --- a/internal/provider/model_sdwan_route_policy_definition.go +++ b/internal/provider/model_sdwan_route_policy_definition.go @@ -23,6 +23,7 @@ import ( "fmt" "github.com/CiscoDevNet/terraform-provider-sdwan/internal/provider/helpers" + "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/tidwall/gjson" "github.com/tidwall/sjson" @@ -743,7 +744,20 @@ func (data *RoutePolicyDefinition) processImport(ctx context.Context) { data.Sequences[i].MatchEntries[ii].AsPathListVersion = types.Int64Value(0) } if !data.Sequences[i].MatchEntries[ii].CommunityListIds.IsNull() { - data.Sequences[i].MatchEntries[ii].CommunityListVersions = types.ListNull(types.StringType) + var elementsCommunityListVersions []attr.Value + var countCommunityListVersions int = 0 + if !(data.Sequences[i].MatchEntries[ii].CommunityListIds.IsNull() || data.Sequences[i].MatchEntries[ii].CommunityListIds.IsUnknown()) { + countCommunityListVersions = len(data.Sequences[i].MatchEntries[ii].CommunityListIds.Elements()) + } + if countCommunityListVersions > 0 { + elementsCommunityListVersions = make([]attr.Value, countCommunityListVersions) + for i := 0; i < countCommunityListVersions; i++ { + elementsCommunityListVersions[i] = types.StringValue("0") + } + data.Sequences[i].MatchEntries[ii].CommunityListVersions = types.ListValueMust(types.StringType, elementsCommunityListVersions) + } else { + data.Sequences[i].MatchEntries[ii].CommunityListVersions = types.ListNull(types.StringType) + } } if data.Sequences[i].MatchEntries[ii].ExpandedCommunityListId != types.StringNull() { data.Sequences[i].MatchEntries[ii].ExpandedCommunityListVersion = types.Int64Value(0) diff --git a/internal/provider/model_sdwan_vpn_membership_policy_definition.go b/internal/provider/model_sdwan_vpn_membership_policy_definition.go index 746b8470b..7b9b38133 100644 --- a/internal/provider/model_sdwan_vpn_membership_policy_definition.go +++ b/internal/provider/model_sdwan_vpn_membership_policy_definition.go @@ -23,6 +23,7 @@ import ( "fmt" "github.com/CiscoDevNet/terraform-provider-sdwan/internal/provider/helpers" + "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/tidwall/gjson" "github.com/tidwall/sjson" @@ -191,7 +192,20 @@ func (data *VPNMembershipPolicyDefinition) processImport(ctx context.Context) { data.Sites[i].SiteListVersion = types.Int64Value(0) } if !data.Sites[i].VpnListIds.IsNull() { - data.Sites[i].VpnListVersions = types.ListNull(types.StringType) + var elementsVpnListVersions []attr.Value + var countVpnListVersions int = 0 + if !(data.Sites[i].VpnListIds.IsNull() || data.Sites[i].VpnListIds.IsUnknown()) { + countVpnListVersions = len(data.Sites[i].VpnListIds.Elements()) + } + if countVpnListVersions > 0 { + elementsVpnListVersions = make([]attr.Value, countVpnListVersions) + for i := 0; i < countVpnListVersions; i++ { + elementsVpnListVersions[i] = types.StringValue("0") + } + data.Sites[i].VpnListVersions = types.ListValueMust(types.StringType, elementsVpnListVersions) + } else { + data.Sites[i].VpnListVersions = types.ListNull(types.StringType) + } } } } diff --git a/internal/provider/resource_sdwan_configuration_group.go b/internal/provider/resource_sdwan_configuration_group.go index a775e54c1..5ca056d41 100644 --- a/internal/provider/resource_sdwan_configuration_group.go +++ b/internal/provider/resource_sdwan_configuration_group.go @@ -264,6 +264,8 @@ func (r *ConfigurationGroupResource) Create(ctx context.Context, req resource.Cr diags = resp.State.Set(ctx, &plan) resp.Diagnostics.Append(diags...) + + helpers.SetFlagImporting(ctx, false, resp.Private, &resp.Diagnostics) } func (r *ConfigurationGroupResource) Deploy(ctx context.Context, plan ConfigurationGroup, state *ConfigurationGroup, diag *diag.Diagnostics, deleteOnError bool) { @@ -350,6 +352,15 @@ func (r *ConfigurationGroupResource) Read(ctx context.Context, req resource.Read state.fromBodyConfigGroup(ctx, res) + imp, diags := helpers.IsFlagImporting(ctx, req) + if resp.Diagnostics.Append(diags...); resp.Diagnostics.HasError() { + return + } + + if imp { + state.processImport(ctx) + } + // Read config group device associations if value := res.Get("devices"); value.Exists() && len(value.Array()) > 0 { path := fmt.Sprintf("/v1/config-group/%v/device/associate/", state.Id.ValueString()) @@ -386,6 +397,8 @@ func (r *ConfigurationGroupResource) Read(ctx context.Context, req resource.Read diags = resp.State.Set(ctx, &state) resp.Diagnostics.Append(diags...) + + helpers.SetFlagImporting(ctx, false, resp.Private, &resp.Diagnostics) } func (r *ConfigurationGroupResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { diff --git a/internal/provider/resource_sdwan_policy_group.go b/internal/provider/resource_sdwan_policy_group.go index 645346006..2268548de 100644 --- a/internal/provider/resource_sdwan_policy_group.go +++ b/internal/provider/resource_sdwan_policy_group.go @@ -217,6 +217,8 @@ func (r *PolicyGroupResource) Create(ctx context.Context, req resource.CreateReq diags = resp.State.Set(ctx, &plan) resp.Diagnostics.Append(diags...) + + helpers.SetFlagImporting(ctx, false, resp.Private, &resp.Diagnostics) } func (r *PolicyGroupResource) Deploy(ctx context.Context, plan PolicyGroup, state *PolicyGroup, diag *diag.Diagnostics, deleteOnError bool) { @@ -303,6 +305,15 @@ func (r *PolicyGroupResource) Read(ctx context.Context, req resource.ReadRequest state.fromBodyPolicyGroup(ctx, res) + imp, diags := helpers.IsFlagImporting(ctx, req) + if resp.Diagnostics.Append(diags...); resp.Diagnostics.HasError() { + return + } + + if imp { + state.processImport(ctx) + } + // Read policy group device associations path := fmt.Sprintf("/v1/policy-group/%v/device/associate/", state.Id.ValueString()) res, err = r.client.Get(path) @@ -335,6 +346,8 @@ func (r *PolicyGroupResource) Read(ctx context.Context, req resource.ReadRequest diags = resp.State.Set(ctx, &state) resp.Diagnostics.Append(diags...) + + helpers.SetFlagImporting(ctx, false, resp.Private, &resp.Diagnostics) } func (r *PolicyGroupResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {