Skip to content

Commit 4bbc4f1

Browse files
authored
Fix Spec field length (#310)
Updated to trim the prefix "." to calculate the length of the `Spec`. `cfg.PrefixConfig.SpecField` value is `.Spec` so old code `len(cfg.PrefixConfig.SpecField) + 1` constructed incorrect field path and ignored the config set in `generator.yaml` Added unit test to validate that below block is not generated. ``` if !ackcompare.SliceStringPEqual(a.ko.Spec.AuthenticationMode.Passwords, b.ko.Spec.AuthenticationMode.Passwords) { delta.Add("Spec.AuthenticationMode.Passwords", a.ko.Spec.AuthenticationMode.Passwords, b.ko.Spec.AuthenticationMode.Passwords) } ``` By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent c4516f4 commit 4bbc4f1

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

pkg/generate/code/compare.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,8 @@ func CompareStruct(
564564
var compareConfig *ackgenconfig.CompareFieldConfig
565565
// memberFieldPath contains the field path along with the prefix cfg.PrefixConfig.SpecField + "." hence we
566566
// 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)]]
567+
specFieldLen := len(strings.TrimPrefix(cfg.PrefixConfig.SpecField, "."))
568+
fieldConfig := fieldConfigs[memberFieldPath[specFieldLen + 1: len(memberFieldPath)]]
568569
if fieldConfig != nil {
569570
compareConfig = fieldConfig.Compare
570571
}

pkg/generate/code/compare_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,3 +475,49 @@ func TestCompareResource_APIGatewayv2_Route(t *testing.T) {
475475
),
476476
)
477477
}
478+
479+
func TestCompareResource_MemoryDB_User(t *testing.T) {
480+
assert := assert.New(t)
481+
require := require.New(t)
482+
483+
g := testutil.NewModelForService(t, "memorydb")
484+
485+
crd := testutil.GetCRDByName(t, g, "User")
486+
require.NotNil(crd)
487+
expected := `
488+
if ackcompare.HasNilDifference(a.ko.Spec.AccessString, b.ko.Spec.AccessString) {
489+
delta.Add("Spec.AccessString", a.ko.Spec.AccessString, b.ko.Spec.AccessString)
490+
} else if a.ko.Spec.AccessString != nil && b.ko.Spec.AccessString != nil {
491+
if *a.ko.Spec.AccessString != *b.ko.Spec.AccessString {
492+
delta.Add("Spec.AccessString", a.ko.Spec.AccessString, b.ko.Spec.AccessString)
493+
}
494+
}
495+
if ackcompare.HasNilDifference(a.ko.Spec.AuthenticationMode, b.ko.Spec.AuthenticationMode) {
496+
delta.Add("Spec.AuthenticationMode", a.ko.Spec.AuthenticationMode, b.ko.Spec.AuthenticationMode)
497+
} else if a.ko.Spec.AuthenticationMode != nil && b.ko.Spec.AuthenticationMode != nil {
498+
if ackcompare.HasNilDifference(a.ko.Spec.AuthenticationMode.Type, b.ko.Spec.AuthenticationMode.Type) {
499+
delta.Add("Spec.AuthenticationMode.Type", a.ko.Spec.AuthenticationMode.Type, b.ko.Spec.AuthenticationMode.Type)
500+
} else if a.ko.Spec.AuthenticationMode.Type != nil && b.ko.Spec.AuthenticationMode.Type != nil {
501+
if *a.ko.Spec.AuthenticationMode.Type != *b.ko.Spec.AuthenticationMode.Type {
502+
delta.Add("Spec.AuthenticationMode.Type", a.ko.Spec.AuthenticationMode.Type, b.ko.Spec.AuthenticationMode.Type)
503+
}
504+
}
505+
}
506+
if ackcompare.HasNilDifference(a.ko.Spec.Name, b.ko.Spec.Name) {
507+
delta.Add("Spec.Name", a.ko.Spec.Name, b.ko.Spec.Name)
508+
} else if a.ko.Spec.Name != nil && b.ko.Spec.Name != nil {
509+
if *a.ko.Spec.Name != *b.ko.Spec.Name {
510+
delta.Add("Spec.Name", a.ko.Spec.Name, b.ko.Spec.Name)
511+
}
512+
}
513+
if !reflect.DeepEqual(a.ko.Spec.Tags, b.ko.Spec.Tags) {
514+
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
515+
}
516+
`
517+
assert.Equal(
518+
expected,
519+
code.CompareResource(
520+
crd.Config(), crd, "delta", "a.ko", "b.ko", 1,
521+
),
522+
)
523+
}

pkg/testdata/models/apis/memorydb/0000-00-00/generator.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ resources:
1111
terminal_codes:
1212
- InvalidParameterValueException
1313
- UserAlreadyExistsFault
14+
renames:
15+
operations:
16+
CreateUser:
17+
input_fields:
18+
UserName: Name
19+
UpdateUser:
20+
input_fields:
21+
UserName: Name
22+
DeleteUser:
23+
input_fields:
24+
UserName: Name
25+
DescribeUsers:
26+
input_fields:
27+
UserName: Name
1428
fields:
1529
AuthenticationMode.Passwords:
1630
is_secret: true

0 commit comments

Comments
 (0)