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 all 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
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
Loading