Skip to content

Commit a676537

Browse files
authored
fix required fields that are renamed (#393)
A previous commit that used the `github.com/aws-controllers-k8s/pkg/names` package instead of the local `pkg/names` package changed the implementation of the `Field.IsRequired` method slightly by no longer referring to the `names.ModelOriginal` value. This ModelOriginal value was really just a hack from a while ago because we didn't have support for reverse-lookups of renamed fields to original Input/Output struct member names. This patch adds that reverse lookup and fixes the IsRequired method to properly look up the original struct member name in the Input shape's required list. Signed-off-by: Jay Pipes <[email protected]> By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 6713768 commit a676537

File tree

2 files changed

+79
-29
lines changed

2 files changed

+79
-29
lines changed

pkg/config/resource.go

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,11 @@ type SyncedCondition struct {
145145
// Example usage from the AmazonMQ generator config:
146146
//
147147
// resources:
148-
// Broker:
149-
// hooks:
150-
// sdk_update_pre_build_request:
151-
// code: if err := rm.requeueIfNotRunning(latest); err != nil { return nil, err }
148+
//
149+
// Broker:
150+
// hooks:
151+
// sdk_update_pre_build_request:
152+
// code: if err := rm.requeueIfNotRunning(latest); err != nil { return nil, err }
152153
//
153154
// Note that the implementor of the AmazonMQ service controller for ACK should
154155
// ensure that there is a `requeueIfNotRunning()` method implementation in
@@ -159,10 +160,11 @@ type SyncedCondition struct {
159160
// `template_path` field:
160161
//
161162
// resources:
162-
// Broker:
163-
// hooks:
164-
// sdk_update_pre_build_update_request:
165-
// template_path: templates/sdk_update_pre_build_request.go.tpl
163+
//
164+
// Broker:
165+
// hooks:
166+
// sdk_update_pre_build_update_request:
167+
// template_path: templates/sdk_update_pre_build_request.go.tpl
166168
type HooksConfig struct {
167169
// Code is the Go code to be injected at the hook point
168170
Code *string `json:"code,omitempty"`
@@ -187,27 +189,27 @@ type CompareConfig struct {
187189
// API accepts a parameter called "Attributes" that can contain one of four
188190
// keys:
189191
//
190-
// * DeliveryPolicy – The policy that defines how Amazon SNS retries failed
191-
// deliveries to HTTP/S endpoints.
192-
// * DisplayName – The display name to use for a topic with SMS subscriptions
193-
// * Policy – The policy that defines who can access your topic.
194-
// * KmsMasterKeyId - The ID of an AWS-managed customer master key (CMK) for
195-
// Amazon SNS or a custom CMK.
192+
// - DeliveryPolicy – The policy that defines how Amazon SNS retries failed
193+
// deliveries to HTTP/S endpoints.
194+
// - DisplayName – The display name to use for a topic with SMS subscriptions
195+
// - Policy – The policy that defines who can access your topic.
196+
// - KmsMasterKeyId - The ID of an AWS-managed customer master key (CMK) for
197+
// Amazon SNS or a custom CMK.
196198
//
197199
// The `CreateTopic` API call **returns** only a single field: the TopicARN.
198200
// But there is a separate `GetTopicAttributes` call that needs to be made that
199201
// returns the above attributes (that are ReadWrite) along with a set of
200202
// key/values that are ReadOnly:
201203
//
202-
// * Owner – The AWS account ID of the topic's owner.
203-
// * SubscriptionsConfirmed – The number of confirmed subscriptions for the
204-
// topic.
205-
// * SubscriptionsDeleted – The number of deleted subscriptions for the topic.
206-
// * SubscriptionsPending – The number of subscriptions pending confirmation
207-
// for the topic.
208-
// * TopicArn – The topic's ARN.
209-
// * EffectiveDeliveryPolicy – The JSON serialization of the effective delivery
210-
// policy, taking system defaults into account.
204+
// - Owner – The AWS account ID of the topic's owner.
205+
// - SubscriptionsConfirmed – The number of confirmed subscriptions for the
206+
// topic.
207+
// - SubscriptionsDeleted – The number of deleted subscriptions for the topic.
208+
// - SubscriptionsPending – The number of subscriptions pending confirmation
209+
// for the topic.
210+
// - TopicArn – The topic's ARN.
211+
// - EffectiveDeliveryPolicy – The JSON serialization of the effective delivery
212+
// policy, taking system defaults into account.
211213
//
212214
// This structure instructs the code generator about the above real, schema'd
213215
// fields that are masquerading as raw key/value pairs.
@@ -534,6 +536,42 @@ func (c *Config) GetResourceFieldName(
534536
return renamed
535537
}
536538

539+
// GetOriginalMemberName returns the original struct member name within an
540+
// Input or Output shape given a resource and field name. This accounts for any
541+
// renames that may have occurred. It is the reverse of the
542+
// Config.GetResourceFieldName method.
543+
func (c *Config) GetOriginalMemberName(
544+
resourceName string,
545+
opID string,
546+
fieldName string,
547+
) string {
548+
if c == nil {
549+
return fieldName
550+
}
551+
rConfig, ok := c.Resources[resourceName]
552+
if !ok {
553+
return fieldName
554+
}
555+
if rConfig.Renames == nil {
556+
return fieldName
557+
}
558+
oRenames, ok := rConfig.Renames.Operations[opID]
559+
if !ok {
560+
return fieldName
561+
}
562+
for from, to := range oRenames.InputFields {
563+
if to == fieldName {
564+
return from
565+
}
566+
}
567+
for from, to := range oRenames.OutputFields {
568+
if to == fieldName {
569+
return from
570+
}
571+
}
572+
return fieldName
573+
}
574+
537575
// GetResourceShortNames returns the CRD list of aliases
538576
func (c *Config) GetResourceShortNames(resourceName string) []string {
539577
if c == nil {

pkg/model/field.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,17 @@ func (f *Field) IsRequired() bool {
7575
if f.FieldConfig != nil && f.FieldConfig.IsRequired != nil {
7676
return *f.FieldConfig.IsRequired
7777
}
78-
return util.InStrings(f.Names.Original, f.CRD.Ops.Create.InputRef.Shape.Required)
78+
// We need to look up the original member name in the input struct
79+
// otherwise renamed fields will not be discovered as required.
80+
originalMember := f.CRD.Config().GetOriginalMemberName(
81+
f.CRD.Names.Original,
82+
f.CRD.Ops.Create.Name,
83+
f.Names.Original,
84+
)
85+
return util.InStrings(
86+
originalMember,
87+
f.CRD.Ops.Create.InputRef.Shape.Required,
88+
)
7989
}
8090

8191
// GetSetterConfig returns the SetFieldConfig object associated with this field
@@ -100,11 +110,13 @@ func (f *Field) GetSetterConfig(opType OpType) *ackgenconfig.SetFieldConfig {
100110
// Ex:
101111
// ```
102112
// Integration:
103-
// fields:
104-
// ApiId:
105-
// references:
106-
// resource: API
107-
// path: Status.APIID
113+
//
114+
// fields:
115+
// ApiId:
116+
// references:
117+
// resource: API
118+
// path: Status.APIID
119+
//
108120
// ```
109121
// For the above configuration, 'HasReference' for 'ApiId'(Original name) field
110122
// will return true because a corresponding 'APIRef' field will be generated

0 commit comments

Comments
 (0)