Skip to content

Commit ce5dc75

Browse files
authored
Field Config Fixes for Code Generator (#295)
- Support for nested list of secrets - Code was getting generated for single SecretRef Generated code with the fix ``` authenticationMode: description: Denotes the user's authentication properties, such as whether it requires a password to authenticate. properties: passwords: items: description: SecretKeyReference combines a k8s corev1.SecretReference with a specific key within the referred-to Secret properties: key: description: Key is the key within the secret type: string name: description: Name is unique within a namespace to reference a secret resource. type: string namespace: description: Namespace defines the space within which the secret name must be unique. type: string required: - key type: object type: array type_: type: string type: object ``` - Fix the struct compare logic - Spec was appended to field path due to this compare config was ignored. https://github.com/aws-controllers-k8s/code-generator/blob/main/pkg/generate/code/compare.go#L128 Issue #, if available: Description of changes: By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 386dc0d commit ce5dc75

File tree

7 files changed

+3511
-3
lines changed

7 files changed

+3511
-3
lines changed

pkg/generate/code/compare.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,9 @@ func CompareStruct(
562562
secondResAdaptedVarName := secondResVarName + "." + memberNameClean
563563

564564
var compareConfig *ackgenconfig.CompareFieldConfig
565-
fieldConfig := fieldConfigs[memberFieldPath]
565+
// memberFieldPath contains the field path along with the prefix cfg.PrefixConfig.SpecField + "." hence we
566+
// would need to substring to exclude cfg.PrefixConfig.SpecField + "." to get correct field config.
567+
fieldConfig := fieldConfigs[memberFieldPath[len(cfg.PrefixConfig.SpecField) + 1 :len(memberFieldPath)]]
566568
if fieldConfig != nil {
567569
compareConfig = fieldConfig.Compare
568570
}

pkg/model/field.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,9 @@ func NewField(
260260
cleanMemberNames := names.New(memberName)
261261
memberPath := path + "." + cleanMemberNames.Camel
262262
memberShape := containerShape.MemberRefs[memberName]
263+
fConfigs := crd.cfg.ResourceFields(crd.Names.Original)
263264
memberField := NewField(
264-
crd, memberPath, cleanMemberNames, memberShape, cfg,
265+
crd, memberPath, cleanMemberNames, memberShape, fConfigs[memberPath],
265266
)
266267
memberFields[cleanMemberNames.Camel] = memberField
267268
}

pkg/model/model.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ func replaceSecretAttrGoType(
541541
)
542542
panic(msg)
543543
}
544-
attr.GoType = "*ackv1alpha1.SecretKeyReference"
544+
attr.GoType = field.GoType
545545
}
546546

547547
// processFields is responsible for walking all of the CRDs' Spec and

pkg/model/model_memorydb_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package model_test
2+
3+
import (
4+
"github.com/aws-controllers-k8s/code-generator/pkg/testutil"
5+
"github.com/stretchr/testify/assert"
6+
"github.com/stretchr/testify/require"
7+
"testing"
8+
)
9+
10+
func TestUserPasswords_IsSecret(t *testing.T) {
11+
require := require.New(t)
12+
13+
g := testutil.NewModelForService(t, "memorydb")
14+
crds, err := g.GetCRDs()
15+
16+
require.Nil(err)
17+
18+
crd := getCRDByName("User", crds)
19+
require.NotNil(crd)
20+
assert := assert.New(t)
21+
assert.Equal("[]*ackv1alpha1.SecretKeyReference", crd.SpecFields["AuthenticationMode"].MemberFields["Passwords"].GoType)
22+
assert.Equal("SecretKeyReference", crd.SpecFields["AuthenticationMode"].MemberFields["Passwords"].GoTypeElem)
23+
assert.Equal("[]*ackv1alpha1.SecretKeyReference", crd.SpecFields["AuthenticationMode"].MemberFields["Passwords"].GoTypeWithPkgName)
24+
}

0 commit comments

Comments
 (0)