Skip to content

Commit

Permalink
Generate marshalling for datasource config builders
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-asawicki committed Dec 19, 2024
1 parent aebab06 commit 9c333df
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 37 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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}
)
Original file line number Diff line number Diff line change
@@ -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 }}
}

0 comments on commit 9c333df

Please sign in to comment.