-
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: fix missing references in toOpts and changes with newlines (#3240
) <!-- Feel free to delete comments as you fill this in --> <!-- summary of changes --> ## Changes - fixed issue with newlines in _impl file - fixed issue with missing reference operators in toOpts functions - fixed issue with no newline between helper structs for implementation file ## Notes - generated an example with optional and non-optional queryStructs to showcase the generation now handles optional and non-optional structs respectively
- Loading branch information
1 parent
3df59dd
commit 246547f
Showing
11 changed files
with
245 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package example | ||
|
||
import ( | ||
g "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator" | ||
) | ||
|
||
//go:generate go run ../main.go | ||
|
||
var ToOptsOptionalExample = g.NewInterface( | ||
"ToOptsOptionalExamples", | ||
"ToOptsOptionalExample", | ||
g.KindOfT[DatabaseObjectIdentifier](), | ||
).CreateOperation("https://example.com", | ||
g.NewQueryStruct("Alter"). | ||
Alter(). | ||
IfExists(). | ||
Name(), | ||
).AlterOperation("https://example.com", | ||
g.NewQueryStruct("Alter"). | ||
Alter(). | ||
IfExists(). | ||
Name(). | ||
OptionalQueryStructField( | ||
"OptionalField", | ||
g.NewQueryStruct("OptionalField"). | ||
List("SomeList", "DatabaseObjectIdentifier", g.ListOptions()), | ||
g.KeywordOptions(), | ||
). | ||
QueryStructField( | ||
"RequiredField", | ||
g.NewQueryStruct("RequiredField"). | ||
List("SomeRequiredList", "DatabaseObjectIdentifier", g.ListOptions().Required()), | ||
g.KeywordOptions().Required(), | ||
), | ||
) |
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,28 @@ | ||
package example | ||
|
||
//go:generate go run ./dto-builder-generator/main.go | ||
|
||
var ( | ||
_ optionsProvider[CreateToOptsOptionalExampleOptions] = new(CreateToOptsOptionalExampleRequest) | ||
_ optionsProvider[AlterToOptsOptionalExampleOptions] = new(AlterToOptsOptionalExampleRequest) | ||
) | ||
|
||
type CreateToOptsOptionalExampleRequest struct { | ||
IfExists *bool | ||
name DatabaseObjectIdentifier // required | ||
} | ||
|
||
type AlterToOptsOptionalExampleRequest struct { | ||
IfExists *bool | ||
name DatabaseObjectIdentifier // required | ||
OptionalField *OptionalFieldRequest | ||
RequiredField RequiredFieldRequest // required | ||
} | ||
|
||
type OptionalFieldRequest struct { | ||
SomeList []DatabaseObjectIdentifier | ||
} | ||
|
||
type RequiredFieldRequest struct { | ||
SomeRequiredList []DatabaseObjectIdentifier // required | ||
} |
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,32 @@ | ||
package example | ||
|
||
import "context" | ||
|
||
type ToOptsOptionalExamples interface { | ||
Create(ctx context.Context, request *CreateToOptsOptionalExampleRequest) error | ||
Alter(ctx context.Context, request *AlterToOptsOptionalExampleRequest) error | ||
} | ||
|
||
// CreateToOptsOptionalExampleOptions is based on https://example.com. | ||
type CreateToOptsOptionalExampleOptions struct { | ||
alter bool `ddl:"static" sql:"ALTER"` | ||
IfExists *bool `ddl:"keyword" sql:"IF EXISTS"` | ||
name DatabaseObjectIdentifier `ddl:"identifier"` | ||
} | ||
|
||
// AlterToOptsOptionalExampleOptions is based on https://example.com. | ||
type AlterToOptsOptionalExampleOptions struct { | ||
alter bool `ddl:"static" sql:"ALTER"` | ||
IfExists *bool `ddl:"keyword" sql:"IF EXISTS"` | ||
name DatabaseObjectIdentifier `ddl:"identifier"` | ||
OptionalField *OptionalField `ddl:"keyword"` | ||
RequiredField RequiredField `ddl:"keyword"` | ||
} | ||
|
||
type OptionalField struct { | ||
SomeList []DatabaseObjectIdentifier `ddl:"list"` | ||
} | ||
|
||
type RequiredField struct { | ||
SomeRequiredList []DatabaseObjectIdentifier `ddl:"list"` | ||
} |
15 changes: 15 additions & 0 deletions
15
pkg/sdk/poc/example/to_opts_optional_example_gen_integration_test.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package example | ||
|
||
import "testing" | ||
|
||
func TestInt_ToOptsOptionalExamples(t *testing.T) { | ||
// TODO: prepare common resources | ||
|
||
t.Run("Create", func(t *testing.T) { | ||
// TODO: fill me | ||
}) | ||
|
||
t.Run("Alter", func(t *testing.T) { | ||
// TODO: fill me | ||
}) | ||
} |
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,57 @@ | ||
package example | ||
|
||
import "testing" | ||
|
||
func TestToOptsOptionalExamples_Create(t *testing.T) { | ||
id := RandomDatabaseObjectIdentifier(t) | ||
// Minimal valid CreateToOptsOptionalExampleOptions | ||
defaultOpts := func() *CreateToOptsOptionalExampleOptions { | ||
return &CreateToOptsOptionalExampleOptions{ | ||
name: id, | ||
} | ||
} | ||
|
||
t.Run("validation: nil options", func(t *testing.T) { | ||
var opts *CreateToOptsOptionalExampleOptions = nil | ||
assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) | ||
}) | ||
|
||
t.Run("basic", func(t *testing.T) { | ||
opts := defaultOpts() | ||
// TODO: fill me | ||
assertOptsValidAndSQLEquals(t, opts, "TODO: fill me") | ||
}) | ||
|
||
t.Run("all options", func(t *testing.T) { | ||
opts := defaultOpts() | ||
// TODO: fill me | ||
assertOptsValidAndSQLEquals(t, opts, "TODO: fill me") | ||
}) | ||
} | ||
|
||
func TestToOptsOptionalExamples_Alter(t *testing.T) { | ||
id := RandomDatabaseObjectIdentifier(t) | ||
// Minimal valid AlterToOptsOptionalExampleOptions | ||
defaultOpts := func() *AlterToOptsOptionalExampleOptions { | ||
return &AlterToOptsOptionalExampleOptions{ | ||
name: id, | ||
} | ||
} | ||
|
||
t.Run("validation: nil options", func(t *testing.T) { | ||
var opts *AlterToOptsOptionalExampleOptions = nil | ||
assertOptsInvalidJoinedErrors(t, opts, ErrNilOptions) | ||
}) | ||
|
||
t.Run("basic", func(t *testing.T) { | ||
opts := defaultOpts() | ||
// TODO: fill me | ||
assertOptsValidAndSQLEquals(t, opts, "TODO: fill me") | ||
}) | ||
|
||
t.Run("all options", func(t *testing.T) { | ||
opts := defaultOpts() | ||
// TODO: fill me | ||
assertOptsValidAndSQLEquals(t, opts, "TODO: fill me") | ||
}) | ||
} |
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,47 @@ | ||
package example | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
var _ ToOptsOptionalExamples = (*toOptsOptionalExamples)(nil) | ||
|
||
type toOptsOptionalExamples struct { | ||
client *Client | ||
} | ||
|
||
func (v *toOptsOptionalExamples) Create(ctx context.Context, request *CreateToOptsOptionalExampleRequest) error { | ||
opts := request.toOpts() | ||
return validateAndExec(v.client, ctx, opts) | ||
} | ||
|
||
func (v *toOptsOptionalExamples) Alter(ctx context.Context, request *AlterToOptsOptionalExampleRequest) error { | ||
opts := request.toOpts() | ||
return validateAndExec(v.client, ctx, opts) | ||
} | ||
|
||
func (r *CreateToOptsOptionalExampleRequest) toOpts() *CreateToOptsOptionalExampleOptions { | ||
opts := &CreateToOptsOptionalExampleOptions{ | ||
IfExists: r.IfExists, | ||
name: r.name, | ||
} | ||
return opts | ||
} | ||
|
||
func (r *AlterToOptsOptionalExampleRequest) toOpts() *AlterToOptsOptionalExampleOptions { | ||
opts := &AlterToOptsOptionalExampleOptions{ | ||
IfExists: r.IfExists, | ||
name: r.name, | ||
} | ||
|
||
if r.OptionalField != nil { | ||
opts.OptionalField = &OptionalField{ | ||
SomeList: r.OptionalField.SomeList, | ||
} | ||
} | ||
opts.RequiredField = RequiredField{ | ||
SomeRequiredList: r.RequiredField.SomeRequiredList, | ||
} | ||
|
||
return opts | ||
} |
24 changes: 24 additions & 0 deletions
24
pkg/sdk/poc/example/to_opts_optional_example_validations_gen.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package example | ||
|
||
import "errors" | ||
|
||
var ( | ||
_ validatable = new(CreateToOptsOptionalExampleOptions) | ||
_ validatable = new(AlterToOptsOptionalExampleOptions) | ||
) | ||
|
||
func (opts *CreateToOptsOptionalExampleOptions) validate() error { | ||
if opts == nil { | ||
return ErrNilOptions | ||
} | ||
var errs []error | ||
return errors.Join(errs...) | ||
} | ||
|
||
func (opts *AlterToOptsOptionalExampleOptions) validate() error { | ||
if opts == nil { | ||
return ErrNilOptions | ||
} | ||
var errs []error | ||
return errors.Join(errs...) | ||
} |
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 |
---|---|---|
|
@@ -6,3 +6,4 @@ type {{ .OptsField.KindNoPtr }} struct { | |
{{ .Name }} {{ .Kind }} {{ .TagsPrintable }} | ||
{{- end }} | ||
} | ||
|
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 |
---|---|---|
|
@@ -5,3 +5,4 @@ type {{ .KindNoPtr }} struct { | |
{{ .Name }} {{ .Kind }} {{ .TagsPrintable }} | ||
{{- end }} | ||
} | ||
|
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