Skip to content

Commit

Permalink
Add unit test and use defer for using the override mechanism
Browse files Browse the repository at this point in the history
Signed-off-by: Sergen Yalçın <[email protected]>
  • Loading branch information
sergenyalcin committed Jan 30, 2024
1 parent 6a2b6de commit 5b8a0c8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
19 changes: 12 additions & 7 deletions pkg/types/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,26 +447,31 @@ func (r *resource) addReferenceFields(g *Builder, paramName *types.TypeName, fie
// generateTypeName generates a unique name for the type if its original name
// is used by another one. It adds the former field names recursively until it
// finds a unique name.
func generateTypeName(suffix string, pkg *types.Package, overrideFieldNames map[string]string, names ...string) (string, error) {
func generateTypeName(suffix string, pkg *types.Package, overrideFieldNames map[string]string, names ...string) (calculated string, _ error) {
defer func() {
if v, ok := overrideFieldNames[calculated]; ok {
calculated = v
}
}()
n := names[len(names)-1] + suffix
if v, ok := overrideFieldNames[n]; ok {
return v, nil
}
for i := len(names) - 2; i >= 0; i-- {
if pkg.Scope().Lookup(n) == nil {
return n, nil
calculated = n
return
}
n = names[i] + n
}
if pkg.Scope().Lookup(n) == nil {
return n, nil
calculated = n
return
}
// start from 2 considering the 1st of this type is the one without an
// index.
for i := 2; i < 10; i++ {
nn := fmt.Sprintf("%s_%d", n, i)
if pkg.Scope().Lookup(nn) == nil {
return nn, nil
calculated = nn
return
}
}
return "", errors.Errorf("could not generate a unique name for %s", n)
Expand Down
20 changes: 19 additions & 1 deletion pkg/types/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func TestBuilder_generateTypeName(t *testing.T) {
existing []string
suffix string
names []string

overrideFieldNames map[string]string
}
type want struct {
out string
Expand Down Expand Up @@ -169,6 +171,22 @@ func TestBuilder_generateTypeName(t *testing.T) {
err: nil,
},
},
"OverrideFieldNames": {
args: args{
suffix: "Parameters",
names: []string{
"Cluster",
"Tag",
},
overrideFieldNames: map[string]string{
"TagParameters": "ClusterTagParameters",
},
},
want: want{
out: "ClusterTagParameters",
err: nil,
},
},
}
for n, tc := range cases {
t.Run(n, func(t *testing.T) {
Expand All @@ -180,7 +198,7 @@ func TestBuilder_generateTypeName(t *testing.T) {
g := &Builder{
Package: p,
}
got, gotErr := generateTypeName(tc.args.suffix, g.Package, map[string]string{}, tc.args.names...)
got, gotErr := generateTypeName(tc.args.suffix, g.Package, tc.args.overrideFieldNames, tc.args.names...)
if diff := cmp.Diff(tc.want.err, gotErr, test.EquateErrors()); diff != "" {
t.Fatalf("generateTypeName(...): -want error, +got error: %s", diff)
}
Expand Down

0 comments on commit 5b8a0c8

Please sign in to comment.