Skip to content

Commit

Permalink
chore: accept non-pointer values in the generated builder methods (#2816
Browse files Browse the repository at this point in the history
)

Accept non-pointer values in the generated builder methods. Now the
nested requests have to be explicitly unreferenced, but two additional
cases could be added to resolve this "issue". There's only a special
case for strings which shouldn't be set whenever they're optional and
the input value is equal to an empty string, because:
1. that would be a rare case to have an empty string in Snowflake.
2. empty strings are treated as non-set values in Terraform.

The improved generator was tested and used as an example on the external
table.
  • Loading branch information
sfc-gh-jcieslak authored May 23, 2024
1 parent 2e2ca6c commit c29fbf1
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 269 deletions.
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
79 changes: 36 additions & 43 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, hasPattern := d.GetOk("pattern")
awsSnsTopic, hasAwsSnsTopic := d.GetOk("aws_sns_topic")
comment, hasComment := d.GetOk("comment")

var tagAssociationRequests []*sdk.TagAssociationRequest
if _, ok := d.GetOk("tag"); ok {
Expand All @@ -210,36 +199,40 @@ func CreateExternalTable(d *schema.ResourceData, meta any) error {

switch {
case d.Get("table_format").(string) == "delta":
err := client.ExternalTables.CreateDeltaLake(
ctx,
sdk.NewCreateDeltaLakeExternalTableRequest(id, location).
WithColumns(columnRequests).
WithPartitionBy(partitionBy).
WithRefreshOnCreate(refreshOnCreate).
WithAutoRefresh(autoRefresh).
WithRawFileFormat(&fileFormat).
WithCopyGrants(copyGrants).
WithComment(comment).
WithTag(tagAssociationRequests),
)
req := sdk.NewCreateDeltaLakeExternalTableRequest(id, location).
WithColumns(columnRequests).
WithPartitionBy(partitionBy).
WithRefreshOnCreate(refreshOnCreate).
WithAutoRefresh(autoRefresh).
WithRawFileFormat(fileFormat).
WithCopyGrants(copyGrants).
WithTag(tagAssociationRequests)
if hasComment {
req = req.WithComment(comment.(string))
}
err := client.ExternalTables.CreateDeltaLake(ctx, req)
if err != nil {
return err
}
default:
err := client.ExternalTables.Create(
ctx,
sdk.NewCreateExternalTableRequest(id, location).
WithColumns(columnRequests).
WithPartitionBy(partitionBy).
WithRefreshOnCreate(refreshOnCreate).
WithAutoRefresh(autoRefresh).
WithPattern(pattern).
WithRawFileFormat(&fileFormat).
WithAwsSnsTopic(awsSnsTopic).
WithCopyGrants(copyGrants).
WithComment(comment).
WithTag(tagAssociationRequests),
)
req := sdk.NewCreateExternalTableRequest(id, location).
WithColumns(columnRequests).
WithPartitionBy(partitionBy).
WithRefreshOnCreate(refreshOnCreate).
WithAutoRefresh(autoRefresh).
WithRawFileFormat(fileFormat).
WithCopyGrants(copyGrants).
WithTag(tagAssociationRequests)
if hasPattern {
req = req.WithPattern(pattern.(string))
}
if hasAwsSnsTopic {
req = req.WithAwsSnsTopic(awsSnsTopic.(string))
}
if hasComment {
req = req.WithComment(comment.(string))
}
err := client.ExternalTables.Create(ctx, req)
if err != nil {
return err
}
Expand Down
11 changes: 9 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,15 @@ 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 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

0 comments on commit c29fbf1

Please sign in to comment.