Skip to content

Commit

Permalink
Add support for empty shapes, used as booleans in SDK (#536)
Browse files Browse the repository at this point in the history
Issue #, if available: prerequisite for WAFv2 [RuleGroup](aws-controllers-k8s/wafv2-controller#6) and [WebACL](aws-controllers-k8s/wafv2-controller#7) CRDs

Description of changes:

Some WAFv2 API fields have empty json specs `{}`, e.g. https://docs.aws.amazon.com/waf/latest/APIReference/API_AllQueryArguments.html

For these type of fields, codegen currently errors out because it infers their gotypes as e.g. 
`AllQueryArguments *AllQueryArguments`

but does not generate a `type AllQueryArguments struct` since the struct itself is empty, and not picked up by `newFieldRecurse` function.

The solution proposed in this PR is to allow users to define `marker-shapes`, which instruct codegen to overwrite the type of these empty structs as `[]byte`, both when generating the APIs and when setting up the SDK. 

e.g. [generator.yaml](https://github.com/aws-controllers-k8s/wafv2-controller/blob/bb682409da8f96c5035783602b9d948c8cc8e21f/apis/v1alpha1/generator.yaml#L15-L24) for WAFv2, and inline:

```
empty_shapes:
- All
- Method
- UriPath
- QueryString
- AllQueryArguments
- RateLimitIP
- RateLimitForwardedIP
- RateLimitHTTPMethod
- NoneAction
```

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
  • Loading branch information
TiberiuGC authored Aug 14, 2024
1 parent 8b54e42 commit bdf3f26
Show file tree
Hide file tree
Showing 3 changed files with 26 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"`
// EmptyShapes contains fields associated with empty struct types
EmptyShapes []string `json:"empty_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
}

// HasEmptyShape returns true if the given shape is setup as empty_shape in config,
// otherwise returns false
func (c *Config) HasEmptyShape(shapeName string) bool {
for _, emptyShape := range c.EmptyShapes {
if emptyShape == 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 @@ -1387,8 +1387,13 @@ func varEmptyConstructorK8sType(

switch shape.Type {
case "structure":
// f0 := &svcapitypes.BookData{}
out += fmt.Sprintf("%s%s := &%s{}\n", indent, varName, goType)
if r.Config().HasEmptyShape(shape.ShapeName) {
// f0 := map[string]*string{}
out += fmt.Sprintf("%s%s := map[string]*string{}\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
6 changes: 6 additions & 0 deletions pkg/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,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.HasEmptyShape(shape.ShapeName) {
return "map[string]*string"
}
panic(fmt.Sprintf("structure %s has no fields, either configure it as a `empty_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 bdf3f26

Please sign in to comment.