Skip to content

Commit bdf3f26

Browse files
authored
Add support for empty shapes, used as booleans in SDK (#536)
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.
1 parent 8b54e42 commit bdf3f26

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

pkg/config/config.go

+13
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ type Config struct {
2525
// Resources contains generator instructions for individual CRDs within an
2626
// API
2727
Resources map[string]ResourceConfig `json:"resources"`
28+
// EmptyShapes contains fields associated with empty struct types
29+
EmptyShapes []string `json:"empty_shapes,omitempty"`
2830
// CRDs to ignore. ACK generator would skip these resources.
2931
Ignore IgnoreSpec `json:"ignore"`
3032
// Contains generator instructions for individual API operations.
@@ -151,6 +153,17 @@ func (c *Config) GetCustomMapFieldMembers() []string {
151153
return members
152154
}
153155

156+
// HasEmptyShape returns true if the given shape is setup as empty_shape in config,
157+
// otherwise returns false
158+
func (c *Config) HasEmptyShape(shapeName string) bool {
159+
for _, emptyShape := range c.EmptyShapes {
160+
if emptyShape == shapeName {
161+
return true
162+
}
163+
}
164+
return false
165+
}
166+
154167
// New returns a new Config object given a supplied
155168
// path to a config file
156169
func New(

pkg/generate/code/set_sdk.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -1387,8 +1387,13 @@ func varEmptyConstructorK8sType(
13871387

13881388
switch shape.Type {
13891389
case "structure":
1390-
// f0 := &svcapitypes.BookData{}
1391-
out += fmt.Sprintf("%s%s := &%s{}\n", indent, varName, goType)
1390+
if r.Config().HasEmptyShape(shape.ShapeName) {
1391+
// f0 := map[string]*string{}
1392+
out += fmt.Sprintf("%s%s := map[string]*string{}\n", indent, varName)
1393+
} else {
1394+
// f0 := &svcapitypes.BookData{}
1395+
out += fmt.Sprintf("%s%s := &%s{}\n", indent, varName, goType)
1396+
}
13921397
case "list", "map":
13931398
// f0 := []*string{}
13941399
out += fmt.Sprintf("%s%s := %s{}\n", indent, varName, goType)

pkg/model/model.go

+6
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,12 @@ func (m *Model) getShapeCleanGoType(shape *awssdkmodel.Shape) string {
471471
// otherwise there is no DeepCopy support
472472
return "*metav1.Time"
473473
case "structure":
474+
if len(shape.MemberRefs) == 0 {
475+
if m.cfg.HasEmptyShape(shape.ShapeName) {
476+
return "map[string]*string"
477+
}
478+
panic(fmt.Sprintf("structure %s has no fields, either configure it as a `empty_shape` or manually set the field type", shape.ShapeName))
479+
}
474480
// There are shapes that are called things like DBProxyStatus that are
475481
// fields in a DBProxy CRD... we need to ensure the type names don't
476482
// conflict. Also, the name of the Go type in the generated code is

0 commit comments

Comments
 (0)