diff --git a/pkg/acceptance/bettertestspoc/config/datasourcemodel/accounts_model_gen.go b/pkg/acceptance/bettertestspoc/config/datasourcemodel/accounts_model_gen.go index 382a0d2e28..cfc7559032 100644 --- a/pkg/acceptance/bettertestspoc/config/datasourcemodel/accounts_model_gen.go +++ b/pkg/acceptance/bettertestspoc/config/datasourcemodel/accounts_model_gen.go @@ -3,6 +3,8 @@ package datasourcemodel import ( + "encoding/json" + tfconfig "github.com/hashicorp/terraform-plugin-testing/config" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/config" @@ -32,6 +34,26 @@ func AccountsWithDefaultMeta() *AccountsModel { return a } +/////////////////////////////////////////////////////// +// set proper json marshalling and handle depends on // +/////////////////////////////////////////////////////// + +func (a *AccountsModel) MarshalJSON() ([]byte, error) { + type Alias AccountsModel + return json.Marshal(&struct { + *Alias + DependsOn []string `json:"depends_on,omitempty"` + }{ + Alias: (*Alias)(a), + DependsOn: a.DependsOn(), + }) +} + +func (a *AccountsModel) WithDependsOn(values ...string) *AccountsModel { + a.SetDependsOn(values...) + return a +} + ///////////////////////////////// // below all the proper values // ///////////////////////////////// diff --git a/pkg/acceptance/bettertestspoc/config/datasourcemodel/database_model_ext.go b/pkg/acceptance/bettertestspoc/config/datasourcemodel/database_model_ext.go deleted file mode 100644 index 96e4ec2d28..0000000000 --- a/pkg/acceptance/bettertestspoc/config/datasourcemodel/database_model_ext.go +++ /dev/null @@ -1,17 +0,0 @@ -package datasourcemodel - -import ( - "encoding/json" -) - -// Based on https://medium.com/picus-security-engineering/custom-json-marshaller-in-go-and-common-pitfalls-c43fa774db05. -func (d *DatabaseModel) MarshalJSON() ([]byte, error) { - type Alias DatabaseModel - return json.Marshal(&struct { - *Alias - DependsOn []string `json:"depends_on,omitempty"` - }{ - Alias: (*Alias)(d), - DependsOn: d.DependsOn(), - }) -} diff --git a/pkg/acceptance/bettertestspoc/config/datasourcemodel/database_model_gen.go b/pkg/acceptance/bettertestspoc/config/datasourcemodel/database_model_gen.go index dd99002397..7ecb98bf7d 100644 --- a/pkg/acceptance/bettertestspoc/config/datasourcemodel/database_model_gen.go +++ b/pkg/acceptance/bettertestspoc/config/datasourcemodel/database_model_gen.go @@ -3,6 +3,8 @@ package datasourcemodel import ( + "encoding/json" + tfconfig "github.com/hashicorp/terraform-plugin-testing/config" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/config" @@ -44,6 +46,26 @@ func DatabaseWithDefaultMeta( return d } +/////////////////////////////////////////////////////// +// set proper json marshalling and handle depends on // +/////////////////////////////////////////////////////// + +func (d *DatabaseModel) MarshalJSON() ([]byte, error) { + type Alias DatabaseModel + return json.Marshal(&struct { + *Alias + DependsOn []string `json:"depends_on,omitempty"` + }{ + Alias: (*Alias)(d), + DependsOn: d.DependsOn(), + }) +} + +func (d *DatabaseModel) WithDependsOn(values ...string) *DatabaseModel { + d.SetDependsOn(values...) + return d +} + ///////////////////////////////// // below all the proper values // ///////////////////////////////// diff --git a/pkg/acceptance/bettertestspoc/config/datasourcemodel/databases_model_ext.go b/pkg/acceptance/bettertestspoc/config/datasourcemodel/databases_model_ext.go index 1e19163087..53b23120a4 100644 --- a/pkg/acceptance/bettertestspoc/config/datasourcemodel/databases_model_ext.go +++ b/pkg/acceptance/bettertestspoc/config/datasourcemodel/databases_model_ext.go @@ -1,28 +1,9 @@ package datasourcemodel import ( - "encoding/json" - tfconfig "github.com/hashicorp/terraform-plugin-testing/config" ) -// Based on https://medium.com/picus-security-engineering/custom-json-marshaller-in-go-and-common-pitfalls-c43fa774db05. -func (d *DatabasesModel) MarshalJSON() ([]byte, error) { - type Alias DatabasesModel - return json.Marshal(&struct { - *Alias - DependsOn []string `json:"depends_on,omitempty"` - }{ - Alias: (*Alias)(d), - DependsOn: d.DependsOn(), - }) -} - -func (d *DatabasesModel) WithDependsOn(values ...string) *DatabasesModel { - d.SetDependsOn(values...) - return d -} - func (d *DatabasesModel) WithLimit(rows int) *DatabasesModel { return d.WithLimitValue( tfconfig.ObjectVariable(map[string]tfconfig.Variable{ diff --git a/pkg/acceptance/bettertestspoc/config/datasourcemodel/databases_model_gen.go b/pkg/acceptance/bettertestspoc/config/datasourcemodel/databases_model_gen.go index f2adf41b91..bab46b9750 100644 --- a/pkg/acceptance/bettertestspoc/config/datasourcemodel/databases_model_gen.go +++ b/pkg/acceptance/bettertestspoc/config/datasourcemodel/databases_model_gen.go @@ -3,6 +3,8 @@ package datasourcemodel import ( + "encoding/json" + tfconfig "github.com/hashicorp/terraform-plugin-testing/config" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/config" @@ -36,6 +38,26 @@ func DatabasesWithDefaultMeta() *DatabasesModel { return d } +/////////////////////////////////////////////////////// +// set proper json marshalling and handle depends on // +/////////////////////////////////////////////////////// + +func (d *DatabasesModel) MarshalJSON() ([]byte, error) { + type Alias DatabasesModel + return json.Marshal(&struct { + *Alias + DependsOn []string `json:"depends_on,omitempty"` + }{ + Alias: (*Alias)(d), + DependsOn: d.DependsOn(), + }) +} + +func (d *DatabasesModel) WithDependsOn(values ...string) *DatabasesModel { + d.SetDependsOn(values...) + return d +} + ///////////////////////////////// // below all the proper values // ///////////////////////////////// diff --git a/pkg/acceptance/bettertestspoc/config/datasourcemodel/gen/templates.go b/pkg/acceptance/bettertestspoc/config/datasourcemodel/gen/templates.go index 0c0da8794d..1b4931c3cf 100644 --- a/pkg/acceptance/bettertestspoc/config/datasourcemodel/gen/templates.go +++ b/pkg/acceptance/bettertestspoc/config/datasourcemodel/gen/templates.go @@ -23,6 +23,14 @@ var ( genhelpers.SnakeCaseToCamel, )).Parse(definitionTemplateContent) + //go:embed templates/marshal_json.tmpl + marshalJsonTemplateContent string + MarshalJsonTemplate, _ = template.New("marshalJsonTemplate").Funcs(genhelpers.BuildTemplateFuncMap( + genhelpers.FirstLetterLowercase, + genhelpers.FirstLetter, + genhelpers.SnakeCaseToCamel, + )).Parse(marshalJsonTemplateContent) + // TODO [SNOW-1501905]: consider duplicating the builders template from resource (currently same template used for datasources and provider which limits the customization possibilities for just one block type) - AllTemplates = []*template.Template{PreambleTemplate, DefinitionTemplate, resourcemodel.BuildersTemplate} + AllTemplates = []*template.Template{PreambleTemplate, DefinitionTemplate, MarshalJsonTemplate, resourcemodel.BuildersTemplate} ) diff --git a/pkg/acceptance/bettertestspoc/config/datasourcemodel/gen/templates/marshal_json.tmpl b/pkg/acceptance/bettertestspoc/config/datasourcemodel/gen/templates/marshal_json.tmpl new file mode 100644 index 0000000000..96c00d6e1b --- /dev/null +++ b/pkg/acceptance/bettertestspoc/config/datasourcemodel/gen/templates/marshal_json.tmpl @@ -0,0 +1,24 @@ +{{- /*gotype: github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/config/model/gen.ResourceConfigBuilderModel*/ -}} + +{{- $modelName := .Name | printf "%sModel" -}} +{{- $nameLowerCase := FirstLetterLowercase .Name -}} +{{- $modelVar := FirstLetter $nameLowerCase }} +/////////////////////////////////////////////////////// +// set proper json marshalling and handle depends on // +/////////////////////////////////////////////////////// + +func ({{ $modelVar }} *{{ $modelName }}) MarshalJSON() ([]byte, error) { + type Alias {{ $modelName }} + return json.Marshal(&struct { + *Alias + DependsOn []string `json:"depends_on,omitempty"` + }{ + Alias: (*Alias)({{ $modelVar }}), + DependsOn: {{ $modelVar }}.DependsOn(), + }) +} + +func ({{ $modelVar }} *{{ $modelName }}) WithDependsOn(values ...string) *{{ $modelName }} { + {{ $modelVar }}.SetDependsOn(values...) + return {{ $modelVar }} +}