From 98ffc3321095937a7e53b9e3b124ec028c56832a Mon Sep 17 00:00:00 2001 From: Carl Henrik Lunde Date: Tue, 3 Aug 2021 08:37:56 +0200 Subject: [PATCH] Support nested Ignore.FieldPaths The current implementation of IgnoreFields did not support nested shapes. For Ignore.FieldPaths: Foo.Bar.Baz, it would ignore the whole of Bar. With this change, it supports multiple levels of nested fields. A practical example for this is ses CreateTemplateInput.Template.TemplateName. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. --- pkg/model/model.go | 41 ++++++++++++++-- pkg/model/model_eks_test.go | 48 +++++++++++++++++++ .../models/apis/eks/0000-00-00/generator.yaml | 7 +++ 3 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 pkg/model/model_eks_test.go diff --git a/pkg/model/model.go b/pkg/model/model.go index 5f956d20..2bd533a9 100644 --- a/pkg/model/model.go +++ b/pkg/model/model.go @@ -23,6 +23,7 @@ import ( "github.com/aws-controllers-k8s/code-generator/pkg/generate/templateset" "github.com/aws-controllers-k8s/code-generator/pkg/names" "github.com/aws-controllers-k8s/code-generator/pkg/util" + awssdkmodel "github.com/aws/aws-sdk-go/private/model/api" ) var ( @@ -657,6 +658,28 @@ func (m *Model) GetEnumDefs() ([]*EnumDef, error) { return edefs, nil } +// getShapeMember recursively traverses the requested fieldpath. It +// does not support the .. syntax for lists. If the field cannot be +// resolved, nil is returned. +func getShapeMember(shape *awssdkmodel.Shape, fieldPath string) *awssdkmodel.Shape { + if fieldPath == "" { + return shape + } + + fields := strings.Split(fieldPath, ".") + + sh := shape + for _, fn := range fields { + shapeRef, _ := sh.MemberRefs[fn] + if shapeRef == nil { + return nil + } + sh = shapeRef.Shape + } + + return sh +} + // ApplyShapeIgnoreRules removes the ignored shapes and fields from the API object // so that they are not considered in any of the calculations of code generator. func (m *Model) ApplyShapeIgnoreRules() { @@ -665,12 +688,22 @@ func (m *Model) ApplyShapeIgnoreRules() { } for sdkShapeID, shape := range m.SDKAPI.API.Shapes { for _, fieldpath := range m.cfg.Ignore.FieldPaths { - sn := strings.Split(fieldpath, ".")[0] - fn := strings.Split(fieldpath, ".")[1] - if shape.ShapeName != sn { + fields := strings.Split(fieldpath, ".") + + shapeName := fields[0] + nestedFieldPath := strings.Join(fields[1:len(fields)-1], ".") + fieldName := fields[len(fields)-1] + + if shape.ShapeName != shapeName { continue } - delete(shape.MemberRefs, fn) + + sh := getShapeMember(shape, nestedFieldPath) + if sh == nil { + continue + } + + delete(sh.MemberRefs, fieldName) } for _, sn := range m.cfg.Ignore.ShapeNames { if shape.ShapeName == sn { diff --git a/pkg/model/model_eks_test.go b/pkg/model/model_eks_test.go new file mode 100644 index 00000000..2d1a38ca --- /dev/null +++ b/pkg/model/model_eks_test.go @@ -0,0 +1,48 @@ +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may +// not use this file except in compliance with the License. A copy of the +// License is located at +// +// http://aws.amazon.com/apache2.0/ +// +// or in the "license" file accompanying this file. This file is distributed +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either +// express or implied. See the License for the specific language governing +// permissions and limitations under the License. + +package model_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/aws-controllers-k8s/code-generator/pkg/testutil" +) + +func TestEKS_IgnoreFieldPaths(t *testing.T) { + require := require.New(t) + + g := testutil.NewModelForService(t, "eks") + + crds, err := g.GetCRDs() + require.Nil(err) + + crd := getCRDByName("Nodegroup", crds) + require.NotNil(crd) + + // fields we have ignored via generator.yaml + ignoredFieldPaths := []string{ + "Version", + "ScalingConfig.DesiredSize", + } + for _, ignoredFieldPath := range ignoredFieldPaths { + _, found := crd.Fields[ignoredFieldPath] + require.False(found, "field should be ignored: %v", ignoredFieldPath) + } + + // Ensure we do not accidentally remove sibling fields + _, found := crd.Fields["ScalingConfig.MaxSize"] + require.True(found, "Sibling should be kept when ScalingConfig.DesiredSize is ignored") +} diff --git a/pkg/testdata/models/apis/eks/0000-00-00/generator.yaml b/pkg/testdata/models/apis/eks/0000-00-00/generator.yaml index 771b144c..0781cc8e 100644 --- a/pkg/testdata/models/apis/eks/0000-00-00/generator.yaml +++ b/pkg/testdata/models/apis/eks/0000-00-00/generator.yaml @@ -1,3 +1,10 @@ +ignore: + field_paths: + # Test nested field path of multiple levels + - Nodegroup.Version + - CreateNodegroupInput.Version + - CreateNodegroupInput.ScalingConfig.DesiredSize + resources: FargateProfile: renames: