-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Generate marshal json for each model (#3307)
- generate marshal and `depends_on` for each resource and data source model - replace deprecated `FromModel` with the new one utilizing the aforementioned marshaling - fix test with explicit empty list (and add it to special variables) - fix tests with explicit null value (and add it to special variables) - generalize multiline handling and test it - use multiline strings for definitions in functions and procedures config builders - simplify function and procedure definitions - format whitespace in function and procedure definitions For the next PRs (added to the issue): - current config building for functions/procedures (especially definitions) is complex: - definition is build using multiline golang strong and sprintf - whitespace is formatted - this is added in the config builder wrapped in special multiline marker - hcl-compatible json is generated - json converted to hcl - hcl formatted using custom formatters This should be refined and made simpler (e.g. newline replacement, encoding problems, etc.) - marking in config builders generators which fields should be handled as multiline by default (because now e.g. SQL function requires the definition, so it's generated in basic builder as a string input, and not the multiline one; because of that it has to be added again with `WithFunctionDefinitionValue(...)` which is confusing)
- Loading branch information
1 parent
0d5b45a
commit 6fe31ba
Showing
108 changed files
with
1,738 additions
and
858 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,46 @@ | ||
package config | ||
|
||
import "encoding/json" | ||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
type nullVariable struct{} | ||
type emptyListVariable struct{} | ||
|
||
// MarshalJSON returns the JSON encoding of nullVariable. | ||
func (v nullVariable) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(nil) | ||
// MarshalJSON returns the JSON encoding of emptyListVariable. | ||
func (v emptyListVariable) MarshalJSON() ([]byte, error) { | ||
return json.Marshal([]any{}) | ||
} | ||
|
||
// NullVariable returns nullVariable which implements Variable. | ||
func NullVariable() nullVariable { | ||
return nullVariable{} | ||
// EmptyListVariable returns Variable representing an empty list. This is because the current hcl parser handles empty SetVariable incorrectly. | ||
func EmptyListVariable() emptyListVariable { | ||
return emptyListVariable{} | ||
} | ||
|
||
type replacementPlaceholderVariable struct { | ||
placeholder ReplacementPlaceholder | ||
} | ||
|
||
// MarshalJSON returns the JSON encoding of replacementPlaceholderVariable. | ||
func (v replacementPlaceholderVariable) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(v.placeholder) | ||
} | ||
|
||
// ReplacementPlaceholderVariable returns Variable containing one of the ReplacementPlaceholder which is later replaced by HclFormatter. | ||
func ReplacementPlaceholderVariable(placeholder ReplacementPlaceholder) replacementPlaceholderVariable { | ||
return replacementPlaceholderVariable{placeholder} | ||
} | ||
|
||
type multilineWrapperVariable struct { | ||
content string | ||
} | ||
|
||
// MarshalJSON returns the JSON encoding of multilineWrapperVariable. | ||
func (v multilineWrapperVariable) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(fmt.Sprintf(`%[1]s%[2]s%[1]s`, SnowflakeProviderConfigMultilineMarker, v.content)) | ||
} | ||
|
||
// MultilineWrapperVariable returns Variable containing multiline content wrapped with SnowflakeProviderConfigMultilineMarker later replaced by HclFormatter. | ||
func MultilineWrapperVariable(content string) multilineWrapperVariable { | ||
return multilineWrapperVariable{content} | ||
} |
45 changes: 39 additions & 6 deletions
45
pkg/acceptance/bettertestspoc/config/datasourcemodel/accounts_model_gen.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
pkg/acceptance/bettertestspoc/config/datasourcemodel/database_model_ext.go
This file was deleted.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
pkg/acceptance/bettertestspoc/config/datasourcemodel/database_model_gen.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
pkg/acceptance/bettertestspoc/config/datasourcemodel/databases_model_ext.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
pkg/acceptance/bettertestspoc/config/datasourcemodel/databases_model_gen.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
pkg/acceptance/bettertestspoc/config/datasourcemodel/gen/templates/marshal_json.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} | ||
} |
Oops, something went wrong.