Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: accept non-pointer values in the generated builder methods #2816

Merged
merged 5 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/datasources/external_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func ReadExternalTables(d *schema.ResourceData, meta interface{}) error {

schemaId := sdk.NewDatabaseObjectIdentifier(databaseName, schemaName)
showIn := sdk.NewShowExternalTableInRequest().WithSchema(schemaId)
externalTables, err := client.ExternalTables.Show(ctx, sdk.NewShowExternalTableRequest().WithIn(showIn))
externalTables, err := client.ExternalTables.Show(ctx, sdk.NewShowExternalTableRequest().WithIn(*showIn))
if err != nil {
log.Printf("[DEBUG] failed when searching external tables in schema (%s), err = %s", schemaId.FullyQualifiedName(), err.Error())
d.SetId("")
Expand Down
27 changes: 8 additions & 19 deletions pkg/resources/external_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,29 +175,18 @@ func CreateExternalTable(d *schema.ResourceData, meta any) error {
columnDef["as"],
)
}
autoRefresh := sdk.Bool(d.Get("auto_refresh").(bool))
refreshOnCreate := sdk.Bool(d.Get("refresh_on_create").(bool))
copyGrants := sdk.Bool(d.Get("copy_grants").(bool))
autoRefresh := d.Get("auto_refresh").(bool)
refreshOnCreate := d.Get("refresh_on_create").(bool)
copyGrants := d.Get("copy_grants").(bool)

var partitionBy []string
if v, ok := d.GetOk("partition_by"); ok {
partitionBy = expandStringList(v.([]any))
}

var pattern *string
if v, ok := d.GetOk("pattern"); ok {
pattern = sdk.String(v.(string))
}

var awsSnsTopic *string
if v, ok := d.GetOk("aws_sns_topic"); ok {
awsSnsTopic = sdk.String(v.(string))
}

var comment *string
if v, ok := d.GetOk("comment"); ok {
comment = sdk.String(v.(string))
}
pattern := d.Get("pattern").(string)
awsSnsTopic := d.Get("aws_sns_topic").(string)
comment := d.Get("comment").(string)

var tagAssociationRequests []*sdk.TagAssociationRequest
if _, ok := d.GetOk("tag"); ok {
Expand All @@ -217,7 +206,7 @@ func CreateExternalTable(d *schema.ResourceData, meta any) error {
WithPartitionBy(partitionBy).
WithRefreshOnCreate(refreshOnCreate).
WithAutoRefresh(autoRefresh).
WithRawFileFormat(&fileFormat).
WithRawFileFormat(fileFormat).
WithCopyGrants(copyGrants).
WithComment(comment).
WithTag(tagAssociationRequests),
Expand All @@ -234,7 +223,7 @@ func CreateExternalTable(d *schema.ResourceData, meta any) error {
WithRefreshOnCreate(refreshOnCreate).
WithAutoRefresh(autoRefresh).
WithPattern(pattern).
WithRawFileFormat(&fileFormat).
WithRawFileFormat(fileFormat).
WithAwsSnsTopic(awsSnsTopic).
WithCopyGrants(copyGrants).
WithComment(comment).
Expand Down
16 changes: 14 additions & 2 deletions pkg/sdk/dto-builder-generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,20 @@ func (gen *Generator) generateBuilderMethods(d *structDef) {
}

for _, field := range optionalFields {
gen.printf("func (s *%s) With%s(%s %s) *%s {\n", d.name, toTitle(field.name), field.name, field.typeString, d.name)
gen.printf("s.%s = %s\n", field.name, field.name)
gen.printf("func (s *%s) With%s(%s %s) *%s {\n", d.name, toTitle(field.name), field.name, strings.TrimLeft(field.typeString, "*"), d.name)

switch {
case field.typeString == "*string":
sfc-gh-asawicki marked this conversation as resolved.
Show resolved Hide resolved
// When the expected type is optional string (*string), we want to avoid assigning empty strings, because:
// 1. It's a rare (or non-existent) case to set something to an empty string in Snowflake (most of the cases are resolved by explicit UNSET commands).
// 2. Empty strings are treated as non-set values in Terraform, so no value set and empty string would have the same effect.
gen.printf("if len(%[1]s) > 0 {\ns.%[1]s = &%[1]s\n}\n", field.name)
case strings.HasPrefix(field.typeString, "*"):
// If the target field is a pointer, assign the address of input field because right now we always pass them by value
gen.printf("s.%s = &%s\n", field.name, field.name)
default:
gen.printf("s.%s = %s\n", field.name, field.name)
}
gen.printf("return s\n")
gen.printf("}\n\n")
}
Expand Down
Loading
Loading