Skip to content

Commit

Permalink
add support for empty shapes, used as markers
Browse files Browse the repository at this point in the history
  • Loading branch information
TiberiuGC committed Aug 8, 2024
1 parent 587b90d commit ec4cbc2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
13 changes: 13 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type Config struct {
// Resources contains generator instructions for individual CRDs within an
// API
Resources map[string]ResourceConfig `json:"resources"`
// MarkerShapes contains fields associated with empty structs used as markers
MarkerShapes []string `json:"marker_shapes,omitempty"`
// CRDs to ignore. ACK generator would skip these resources.
Ignore IgnoreSpec `json:"ignore"`
// Contains generator instructions for individual API operations.
Expand Down Expand Up @@ -151,6 +153,17 @@ func (c *Config) GetCustomMapFieldMembers() []string {
return members
}

// IsMarkerShape returns true if a given shape name is a marker shape,
// otherwise returns false
func (c *Config) IsMarkerShape(shapeName string) bool {
for _, markerShape := range c.MarkerShapes {
if markerShape == shapeName {
return true
}
}
return false
}

// New returns a new Config object given a supplied
// path to a config file
func New(
Expand Down
9 changes: 7 additions & 2 deletions pkg/generate/code/set_sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -1388,8 +1388,13 @@ func varEmptyConstructorK8sType(

switch shape.Type {
case "structure":
// f0 := &svcapitypes.BookData{}
out += fmt.Sprintf("%s%s := &%s{}\n", indent, varName, goType)
if r.Config().IsMarkerShape(shape.ShapeName) {
// f0 := []byte{}
out += fmt.Sprintf("%s%s := []byte{}\n", indent, varName)
} else {
// f0 := &svcapitypes.BookData{}
out += fmt.Sprintf("%s%s := &%s{}\n", indent, varName, goType)
}
case "list", "map":
// f0 := []*string{}
out += fmt.Sprintf("%s%s := %s{}\n", indent, varName, goType)
Expand Down
12 changes: 12 additions & 0 deletions pkg/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package model
import (
"errors"
"fmt"
"slices"
"sort"
"strings"

Expand Down Expand Up @@ -395,6 +396,11 @@ func (m *Model) IsShapeUsedInCRDs(shapeName string) bool {
return false
}

// IsMarkerShape return true if the supplied shape name is a marker shape
func (m *Model) IsMarkerShape(shapeName string) bool {
return slices.Contains(m.cfg.MarkerShapes, shapeName)
}

// GetTypeDefs returns a slice of `TypeDef` pointers
func (m *Model) GetTypeDefs() ([]*TypeDef, error) {
if m.typeDefs != nil {
Expand Down Expand Up @@ -471,6 +477,12 @@ func (m *Model) getShapeCleanGoType(shape *awssdkmodel.Shape) string {
// otherwise there is no DeepCopy support
return "*metav1.Time"
case "structure":
if len(shape.MemberRefs) == 0 {
if m.cfg.IsMarkerShape(shape.ShapeName) {
return "[]byte"
}
panic(fmt.Sprintf("structure %s has no fields, either configure it as a `marker_shape` or manually set the field type", shape.ShapeName))
}
// There are shapes that are called things like DBProxyStatus that are
// fields in a DBProxy CRD... we need to ensure the type names don't
// conflict. Also, the name of the Go type in the generated code is
Expand Down

0 comments on commit ec4cbc2

Please sign in to comment.