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: Generate ID and ObjectType Show Object Methods #3292

Open
wants to merge 45 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
cea5dd3
init for interface helper methods
sfc-gh-fbudzynski Dec 9, 2024
d47db72
Merge branch 'main' of github.com:Snowflake-Labs/terraform-provider-s…
sfc-gh-fbudzynski Dec 10, 2024
4750632
enum
sfc-gh-fbudzynski Dec 10, 2024
e051890
refactor
sfc-gh-fbudzynski Dec 10, 2024
86d55bd
doc to helper methods
sfc-gh-fbudzynski Dec 10, 2024
cf927ef
generated for streamlits
sfc-gh-fbudzynski Dec 10, 2024
495e236
generated for notification integrations
sfc-gh-fbudzynski Dec 10, 2024
c9cb8d7
checks for missing fields to create ID() method
sfc-gh-fbudzynski Dec 10, 2024
8fdc1dd
small fix
sfc-gh-fbudzynski Dec 12, 2024
a3784c0
slight refactor and generated some examples
sfc-gh-fbudzynski Dec 12, 2024
a1d1067
Merge branch 'main' of github.com:Snowflake-Labs/terraform-provider-s…
sfc-gh-fbudzynski Dec 12, 2024
1c5c00d
Merge branch 'main' of github.com:Snowflake-Labs/terraform-provider-s…
sfc-gh-fbudzynski Dec 12, 2024
590cb48
ff
sfc-gh-fbudzynski Dec 12, 2024
b0725ae
Merge branch 'main' of github.com:Snowflake-Labs/terraform-provider-s…
sfc-gh-fbudzynski Dec 12, 2024
78a01e3
log change
sfc-gh-fbudzynski Dec 12, 2024
d5d1aa6
ref
sfc-gh-fbudzynski Dec 12, 2024
6b4be76
Merge branch 'main' of github.com:Snowflake-Labs/terraform-provider-s…
sfc-gh-fbudzynski Dec 12, 2024
e1a27df
ref
sfc-gh-fbudzynski Dec 12, 2024
38b8a3e
Merge branch 'main' of github.com:Snowflake-Labs/terraform-provider-s…
sfc-gh-fbudzynski Dec 16, 2024
9f29c42
ref
sfc-gh-fbudzynski Dec 16, 2024
1310f63
Merge branch 'dev' of github.com:Snowflake-Labs/terraform-provider-sn…
sfc-gh-fbudzynski Dec 16, 2024
d610978
refacotred to be on Operatin level
sfc-gh-fbudzynski Dec 16, 2024
72fc399
rename
sfc-gh-fbudzynski Dec 16, 2024
475a570
small ref
sfc-gh-fbudzynski Dec 16, 2024
388abfc
rename
sfc-gh-fbudzynski Dec 16, 2024
546e125
self rev
sfc-gh-fbudzynski Dec 16, 2024
64fea51
self rev
sfc-gh-fbudzynski Dec 16, 2024
3f44299
self rev
sfc-gh-fbudzynski Dec 16, 2024
8987603
self rev
sfc-gh-fbudzynski Dec 16, 2024
05914c3
self rev
sfc-gh-fbudzynski Dec 16, 2024
3954232
rev
sfc-gh-fbudzynski Dec 18, 2024
05c44e5
def fixes
sfc-gh-fbudzynski Dec 18, 2024
9e757be
order fixed
sfc-gh-fbudzynski Dec 18, 2024
8183ff7
rename...
sfc-gh-fbudzynski Dec 18, 2024
268e2d8
lint
sfc-gh-fbudzynski Dec 18, 2024
c60dec5
ref
sfc-gh-fbudzynski Dec 18, 2024
df0563b
ref
sfc-gh-fbudzynski Dec 18, 2024
a8ff21d
ref
sfc-gh-fbudzynski Dec 18, 2024
a9d50cb
ref
sfc-gh-fbudzynski Dec 18, 2024
57ebbc3
ref
sfc-gh-fbudzynski Dec 18, 2024
560cc8b
small ref
sfc-gh-fbudzynski Dec 19, 2024
891ce2b
removed methods from operation
sfc-gh-fbudzynski Dec 19, 2024
2d9930e
Merge branch 'dev' of github.com:Snowflake-Labs/terraform-provider-sn…
sfc-gh-fbudzynski Dec 19, 2024
5f5e263
merge
sfc-gh-fbudzynski Dec 19, 2024
cae24f6
Merge branch 'dev' into sdk-helper-methods
sfc-gh-fbudzynski Dec 20, 2024
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
3 changes: 2 additions & 1 deletion pkg/sdk/api_integrations_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ var ApiIntegrationsDef = g.NewInterface(
Show().
SQL("API INTEGRATIONS").
OptionalLike(),
g.ResourceIDHelperMethod,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resource is bad, because it's coupled with TF resource, and SDK should not be coupled with TF. My proposals are g.ShowObjectIdMethod and g.ObjectIdMethod. I like the former more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

g.ShowObjectIdMethod and g.ShowObjectTypeMethod seems fine to me as well

).
ShowByIdOperationWithFiltering(
g.ShowByIDLikeFiltering,
Expand All @@ -172,4 +173,4 @@ var ApiIntegrationsDef = g.NewInterface(
SQL("API INTEGRATION").
Name().
WithValidation(g.ValidIdentifier, "name"),
).HelperMethodID()
)
8 changes: 4 additions & 4 deletions pkg/sdk/api_integrations_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ type ApiIntegration struct {
CreatedOn time.Time
}

func (v *ApiIntegration) ID() AccountObjectIdentifier {
return NewAccountObjectIdentifier(v.Name)
}

// DescribeApiIntegrationOptions is based on https://docs.snowflake.com/en/sql-reference/sql/desc-integration.
type DescribeApiIntegrationOptions struct {
describe bool `ddl:"static" sql:"DESCRIBE"`
Expand All @@ -150,7 +154,3 @@ type ApiIntegrationProperty struct {
Value string
Default string
}

func (v *ApiIntegration) ID() AccountObjectIdentifier {
return NewAccountObjectIdentifier(v.Name)
}
3 changes: 2 additions & 1 deletion pkg/sdk/notification_integrations_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ var NotificationIntegrationsDef = g.NewInterface(
Show().
SQL("NOTIFICATION INTEGRATIONS").
OptionalLike(),
g.ResourceIDHelperMethod,
).
ShowByIdOperationWithFiltering(
g.ShowByIDLikeFiltering,
Expand All @@ -202,4 +203,4 @@ var NotificationIntegrationsDef = g.NewInterface(
SQL("NOTIFICATION INTEGRATION").
Name().
WithValidation(g.ValidIdentifier, "name"),
).HelperMethodID()
)
8 changes: 4 additions & 4 deletions pkg/sdk/notification_integrations_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ type NotificationIntegration struct {
CreatedOn time.Time
}

func (v *NotificationIntegration) ID() AccountObjectIdentifier {
return NewAccountObjectIdentifier(v.Name)
}

// DescribeNotificationIntegrationOptions is based on https://docs.snowflake.com/en/sql-reference/sql/desc-integration.
type DescribeNotificationIntegrationOptions struct {
describe bool `ddl:"static" sql:"DESCRIBE"`
Expand All @@ -181,7 +185,3 @@ type NotificationIntegrationProperty struct {
Value string
Default string
}

func (v *NotificationIntegration) ID() AccountObjectIdentifier {
return NewAccountObjectIdentifier(v.Name)
}
115 changes: 0 additions & 115 deletions pkg/sdk/poc/generator/helper_methods.go

This file was deleted.

3 changes: 1 addition & 2 deletions pkg/sdk/poc/generator/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
Operations []*Operation
// IdentifierKind keeps identifier of the underlying object (e.g. DatabaseObjectIdentifier)
IdentifierKind string
// HelperMethods contains helper methods for the Interface file (i.e. ID(), ObjectType())
HelperMethods []*HelperMethod
}

func NewInterface(name string, nameSingular string, identifierKind string, operations ...*Operation) *Interface {
Expand All @@ -33,3 +31,4 @@
// return strings.Replace(i.IdentifierKind, "ObjectIdentifier", "", 1)
return identifierStringToPrefix(i.IdentifierKind)
}

Check failure on line 34 in pkg/sdk/poc/generator/interface.go

View workflow job for this annotation

GitHub Actions / reviewdog

[golangci] reported by reviewdog 🐶 File is not `gofmt`-ed with `-s` (gofmt) Raw Output: pkg/sdk/poc/generator/interface.go:34: File is not `gofmt`-ed with `-s` (gofmt)
23 changes: 17 additions & 6 deletions pkg/sdk/poc/generator/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type Operation struct {
DescribeMapping *Mapping
// ShowByIDFiltering defines a kind of filterings performed in ShowByID operation
ShowByIDFiltering []ShowByIDFiltering
// ResourceHelperMethods contains helper methods for the Interface file (i.e. ID(), ObjectType())
ResourceHelperMethods []*ResourceHelperMethod
}

type Mapping struct {
Expand Down Expand Up @@ -79,6 +81,11 @@ func (s *Operation) withHelperStructs(helperStructs ...*Field) *Operation {
return s
}

func (s *Operation) withObjectInterface(objectInterface *Interface) *Operation {
s.ObjectInterface = objectInterface
return s
}

func addShowMapping(op *Operation, from, to *Field) {
op.ShowMapping = newMapping("convert", from, to)
}
Expand Down Expand Up @@ -117,6 +124,7 @@ func (i *Interface) newOperationWithDBMapping(
resourceRepresentation *plainStruct,
queryStruct *QueryStruct,
addMappingFunc func(op *Operation, from, to *Field),
resourceHelperMethods ...ResourceHelperMethodKind,
) *Operation {
db := dbRepresentation.IntoField()
res := resourceRepresentation.IntoField()
Expand All @@ -126,7 +134,10 @@ func (i *Interface) newOperationWithDBMapping(
op := newOperation(kind, doc).
withHelperStruct(db).
withHelperStruct(res).
withOptionsStruct(queryStruct.IntoField())
withOptionsStruct(queryStruct.IntoField()).
withObjectInterface(i).
withResourceHelperMethods(res.Name, resourceHelperMethods...)

addMappingFunc(op, db, res)
i.Operations = append(i.Operations, op)
return op
Expand Down Expand Up @@ -156,8 +167,8 @@ func (i *Interface) RevokeOperation(doc string, queryStruct *QueryStruct) *Inter
return i.newSimpleOperation(string(OperationKindRevoke), doc, queryStruct)
}

func (i *Interface) ShowOperation(doc string, dbRepresentation *dbStruct, resourceRepresentation *plainStruct, queryStruct *QueryStruct) *Interface {
i.newOperationWithDBMapping(string(OperationKindShow), doc, dbRepresentation, resourceRepresentation, queryStruct, addShowMapping)
func (i *Interface) ShowOperation(doc string, dbRepresentation *dbStruct, resourceRepresentation *plainStruct, queryStruct *QueryStruct, helperMethods ...ResourceHelperMethodKind) *Interface {
i.newOperationWithDBMapping(string(OperationKindShow), doc, dbRepresentation, resourceRepresentation, queryStruct, addShowMapping, helperMethods...)
return i
}

Expand All @@ -170,9 +181,9 @@ func (i *Interface) ShowByIdOperationWithNoFiltering() *Interface {

// ShowByIdOperationWithFiltering adds a ShowByID operation to the interface with filtering. Should be used for objects that implement filtering options e.g. Like or In.
func (i *Interface) ShowByIdOperationWithFiltering(filter ShowByIDFilteringKind, filtering ...ShowByIDFilteringKind) *Interface {
op := newNoSqlOperation(string(OperationKindShowByID))
op.ObjectInterface = i
op.withFiltering(append([]ShowByIDFilteringKind{filter}, filtering...)...)
op := newNoSqlOperation(string(OperationKindShowByID)).
withObjectInterface(i).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

withFiltering(append(filtering, filter)...)
i.Operations = append(i.Operations, op)
return i
}
Expand Down
122 changes: 122 additions & 0 deletions pkg/sdk/poc/generator/resource_helper_methods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package generator

import (
"fmt"
"log"
"slices"
)

type objectIdentifier string

const (
AccountObjectIdentifier objectIdentifier = "AccountObjectIdentifier"
DatabaseObjectIdentifier objectIdentifier = "DatabaseObjectIdentifier"
SchemaObjectIdentifier objectIdentifier = "SchemaObjectIdentifier"
SchemaObjectIdentifierWithArguments objectIdentifier = "SchemaObjectIdentifierWithArguments"
)

func identifierStringToObjectIdentifier(s string) objectIdentifier {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: pls add basic unit tests.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

switch s {
case "AccountObjectIdentifier":
return AccountObjectIdentifier
case "DatabaseObjectIdentifier":
return DatabaseObjectIdentifier
case "SchemaObjectIdentifier":
return SchemaObjectIdentifier
case "SchemaObjectIdentifierWithArguments":
return SchemaObjectIdentifierWithArguments
default:
return ""
}
}

type ResourceHelperMethodKind uint

const (
ResourceIDHelperMethod ResourceHelperMethodKind = iota
ResourceObjectTypeHelperMethod
)

type ResourceHelperMethod struct {
Name string
StructName string
ReturnValue string
ReturnType string
}

func newResourceHelperMethod(name, structName, returnValue string, returnType string) *ResourceHelperMethod {
return &ResourceHelperMethod{
Name: name,
StructName: structName,
ReturnValue: returnValue,
ReturnType: returnType,
}
}

var requiredFieldsForIDMethodMapping map[objectIdentifier][]string = map[objectIdentifier][]string{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var idTypeParts = map[objectIdentifier][]string{ , wdyt?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems all right

AccountObjectIdentifier: {"Name"},
DatabaseObjectIdentifier: {"Name", "DatabaseName"},
SchemaObjectIdentifier: {"Name", "DatabaseName", "SchemaName"},
}

func hasRequiredFieldsForIDMethod(structName string, helperStructs []*Field, requiredFields ...string) bool {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: we should receive requiredFields ...objectIdentifier and convert to string inside.

for _, field := range helperStructs {
if field.Name == structName {
return containsFieldNames(field.Fields, requiredFields...)
}
}
return false
}

func containsFieldNames(fields []*Field, names ...string) bool {
fieldNames := []string{}
for _, field := range fields {
fieldNames = append(fieldNames, field.Name)
}

for _, name := range names {
if !slices.Contains(fieldNames, name) {
return false
}
}
return true
}

func newResourceIDHelperMethod(structName string, helperStructs []*Field, identifierString string) *ResourceHelperMethod {
objectIdentifier := identifierStringToObjectIdentifier(identifierString)
requiredFields, ok := requiredFieldsForIDMethodMapping[objectIdentifier]
if !ok {
log.Printf("WARNING: No required fields mapping defined for identifier %s", objectIdentifier)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: we us [WARN] prefix for such logs.

return nil
}
if !hasRequiredFieldsForIDMethod(structName, helperStructs, requiredFields...) {
log.Printf("WARNING: Struct '%s' does not contain needed fields to build ID() helper method. Create the method manually in _ext file or add missing one of required fields: %v.\n", structName, requiredFields)
return nil
}

var args string
for _, field := range requiredFields {
args += fmt.Sprintf("v.%v, ", field)
}

returnValue := fmt.Sprintf("New%v(%v)", objectIdentifier, args)
return newResourceHelperMethod("ID", structName, returnValue, string(objectIdentifier))
}

func newResourceObjectTypeHelperMethod(structName string) *ResourceHelperMethod {
return newResourceHelperMethod("ObjectType", structName, "ObjectType"+structName, "ObjectType")
}

func (s *Operation) withResourceHelperMethods(structName string, helperMethods ...ResourceHelperMethodKind) *Operation {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: in general, it's easier to read a file when upstream functions are below functions that call them. So, this function should be moved higher than newResourceIDHelperMethod and newResourceObjectTypeHelperMethod.

for _, helperMethod := range helperMethods {
switch helperMethod {
case ResourceIDHelperMethod:
s.ResourceHelperMethods = append(s.ResourceHelperMethods, newResourceIDHelperMethod(structName, s.HelperStructs, s.ObjectInterface.IdentifierKind))
case ResourceObjectTypeHelperMethod:
s.ResourceHelperMethods = append(s.ResourceHelperMethods, newResourceObjectTypeHelperMethod(structName))
default:
log.Println("No resourceHelperMethod found for kind:", helperMethod)
}
}
return s
}
Loading
Loading