Skip to content

Commit

Permalink
refacotred to be on Operatin level
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-fbudzynski committed Dec 16, 2024
1 parent 1310f63 commit d610978
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 56 deletions.
2 changes: 1 addition & 1 deletion pkg/sdk/api_integrations_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,4 @@ var ApiIntegrationsDef = g.NewInterface(
SQL("API INTEGRATION").
Name().
WithValidation(g.ValidIdentifier, "name"),
).HelperMethodID()
)
2 changes: 1 addition & 1 deletion pkg/sdk/notification_integrations_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,4 @@ var NotificationIntegrationsDef = g.NewInterface(
SQL("NOTIFICATION INTEGRATION").
Name().
WithValidation(g.ValidIdentifier, "name"),
).HelperMethodID()
)
93 changes: 51 additions & 42 deletions pkg/sdk/poc/generator/helper_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package generator
import (
"fmt"
"log"
"slices"
)

type objectIdentifier string
Expand All @@ -29,6 +30,13 @@ func identifierStringToObjectIdentifier(s string) objectIdentifier {
}
}

type ObjectHelperMethodKind uint

const (
ObjectHelperMethodID ObjectHelperMethodKind = iota
ObjectHelperMethodObjectType
)

type HelperMethod struct {
Name string
StructName string
Expand All @@ -45,71 +53,72 @@ func newHelperMethod(name, structName, returnValue string, returnType string) *H
}
}

var requiredFieldsForIDMethodMapping map[objectIdentifier][]string = map[objectIdentifier][]string{
AccountObjectIdentifier: {"Name"},
DatabaseObjectIdentifier: {"Name", "DatabaseName"},
SchemaObjectIdentifier: {"Name", "DatabaseName", "SchemaName"},
}
func newObjectHelperMethodID(structName string, helperStructs []*Field, identifierString string) *HelperMethod {
objectIdentifier := identifierStringToObjectIdentifier(identifierString)
requiredFields, ok := requiredFieldsForIDMethodMapping[objectIdentifier]
if !ok {
log.Printf("WARNING: No required fields mapping defined for identifier %s", objectIdentifier)
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
}

func newIDHelperMethod(structName string, objectIdentifier objectIdentifier) *HelperMethod {
var args string
fields := requiredFieldsForIDMethodMapping[objectIdentifier]
for _, field := range fields {
for _, field := range requiredFields {
args += fmt.Sprintf("v.%v, ", field)
}

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

func newObjectTypeHelperMethod(structName string) *HelperMethod {
func newObjectHelperMethodObjectType(structName string) *HelperMethod {
returnValue := fmt.Sprintf("ObjectType%v", structName)
return newHelperMethod("ObjectType", structName, returnValue, "ObjectType")
}

var requiredFieldsForIDMethodMapping map[objectIdentifier][]string = map[objectIdentifier][]string{
AccountObjectIdentifier: {"Name"},
DatabaseObjectIdentifier: {"Name", "DatabaseName"},
SchemaObjectIdentifier: {"Name", "DatabaseName", "SchemaName"},
}

func hasRequiredFieldsForIDMethod(structName string, helperStructs []*Field, requiredFields ...string) bool {
for _, field := range helperStructs {
if field.Name == structName {
return containsFieldNames(field.Fields, requiredFields...)
}
}
return false
}

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

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

return true
}

func hasRequiredFieldsForIDMethod(operations []*Operation, structName string, requiredFields ...string) bool {
for _, op := range operations {
if op.Name != string(OperationKindShow) {
continue
}
for _, field := range op.HelperStructs {
if field.Name == structName {
return containsFieldNames(field.Fields, requiredFields...)
}
func (s *Operation) withObjectHelperMethods(structName string, helperMethods ...ObjectHelperMethodKind) *Operation {
for _, helperMethod := range helperMethods {
switch helperMethod {
case ObjectHelperMethodID:
s.HelperMethods = append(s.HelperMethods, newObjectHelperMethodID(structName, s.HelperStructs, s.ObjectInterface.IdentifierKind))
case ObjectHelperMethodObjectType:
s.HelperMethods = append(s.HelperMethods, newObjectHelperMethodObjectType(structName))
default:
log.Println("No object helper method found for kind:", helperMethod)
}
log.Printf("WARNING: Struct: '%s' not found in '%s' operation. Couldn't generate ID() helper method.", structName, OperationKindShow)
}
log.Printf("WARNING: Operation: '%s' not found. Couldn't generate ID() helper method.", OperationKindShow)
return false
}

// HelperMethodID adds a helper method "ID()" to the interface file that returns the ObjectIdentifier of the object
func (i *Interface) HelperMethodID() *Interface {
identifierKind := identifierStringToObjectIdentifier(i.IdentifierKind)
requiredFields := requiredFieldsForIDMethodMapping[identifierKind]
if !hasRequiredFieldsForIDMethod(i.Operations, i.NameSingular, 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", i.NameSingular, requiredFields)
return i
}
i.HelperMethods = append(i.HelperMethods, newIDHelperMethod(i.NameSingular, identifierKind))
return i
}

// HelperMethodObjectType adds a helper method "ObjectType()" to the interface file that returns the ObjectType for the struct
func (i *Interface) HelperMethodObjectType() *Interface {
i.HelperMethods = append(i.HelperMethods, newObjectTypeHelperMethod(i.NameSingular))
return i
return s
}
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 @@ type Interface struct {
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 @@ func (i *Interface) ObjectIdentifierPrefix() idPrefix {
// return strings.Replace(i.IdentifierKind, "ObjectIdentifier", "", 1)
return identifierStringToPrefix(i.IdentifierKind)
}

17 changes: 14 additions & 3 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
// HelperMethods contains helper methods for the Interface file (i.e. ID(), ObjectType())
HelperMethods []*HelperMethod
}

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

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

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),
objectHelperMethods ...ObjectHelperMethodKind,
) *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).
withObjectHelperMethods(res.Name, objectHelperMethods...)

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 ...ObjectHelperMethodKind) *Interface {
i.newOperationWithDBMapping(string(OperationKindShow), doc, dbRepresentation, resourceRepresentation, queryStruct, addShowMapping, helperMethods...)
return i
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/sdk/poc/generator/template_executors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ func GenerateInterface(writer io.Writer, def *Interface) {
if o.OptsField != nil {
generateOptionsStruct(writer, o)
}
}
for _, m := range def.HelperMethods {
if m != nil {
generateHelperMethods(writer, m)
if o.HelperMethods != nil {
for _, m := range o.HelperMethods {
generateHelperMethods(writer, m)
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/sdk/secrets_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ var SecretsDef = g.NewInterface(
SQL("SECRETS").
OptionalLike().
OptionalExtendedIn(),
g.ObjectHelperMethodID,
g.ObjectHelperMethodObjectType,
).ShowByIdOperationWithFiltering(
g.ShowByIDLikeFiltering,
g.ShowByIDExtendedInFiltering,
Expand All @@ -254,5 +256,4 @@ var SecretsDef = g.NewInterface(
SQL("SECRET").
Name().
WithValidation(g.ValidIdentifier, "name"),
).HelperMethodID().
HelperMethodObjectType()
)
2 changes: 1 addition & 1 deletion pkg/sdk/streamlits_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,4 @@ var StreamlitsDef = g.NewInterface(
SQL("STREAMLIT").
Name().
WithValidation(g.ValidIdentifier, "name"),
).HelperMethodID()
)

0 comments on commit d610978

Please sign in to comment.