Skip to content

Commit e8be3ca

Browse files
committed
remove import tracking from TypeDef collection
This commit replaces the tracking of unique imported packages from the processing of TypeDef structs by the code generator. Instead, we now use the same technique to avoid import errors that we do in the `sdk.go` code: ```go import ( ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1" "github.com/aws/aws-sdk-go/aws" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) // Hack to avoid import errors during build... var ( _ = &metav1.Time{} _ = &aws.JSONValue{} _ = ackv1alpha1.AWSAccountID("") ) ``` The above is all the packages that are referenced by any type definition's attribute fields that is collected by the code generator during API inference.
1 parent 6b6f1c2 commit e8be3ca

File tree

7 files changed

+33
-75
lines changed

7 files changed

+33
-75
lines changed

pkg/generate/ack/apis.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func APIs(
5252
if err != nil {
5353
return nil, err
5454
}
55-
typeDefs, typeImports, err := g.GetTypeDefs()
55+
typeDefs, err := g.GetTypeDefs()
5656
if err != nil {
5757
return nil, err
5858
}
@@ -73,7 +73,6 @@ func APIs(
7373
metaVars,
7474
enumDefs,
7575
typeDefs,
76-
typeImports,
7776
}
7877
for _, path := range apisTemplatePaths {
7978
outPath := strings.TrimSuffix(filepath.Base(path), ".tpl")
@@ -101,7 +100,6 @@ type templateAPIVars struct {
101100
templateset.MetaVars
102101
EnumDefs []*ackmodel.EnumDef
103102
TypeDefs []*ackmodel.TypeDef
104-
Imports map[string]string
105103
}
106104

107105
// templateCRDVars contains template variables for the template that outputs Go

pkg/generate/apigwv2_test.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,12 @@ func TestAPIGatewayV2_GetTypeDefs(t *testing.T) {
2828

2929
g := testutil.NewGeneratorForService(t, "apigatewayv2")
3030

31-
tdefs, timports, err := g.GetTypeDefs()
32-
require.Nil(err)
33-
34-
// APIGatewayV2 shapes have time.Time ("timestamp") types and so
35-
// GetTypeDefs() should return the special-cased with apimachinery/metav1
36-
// import, aliased as "metav1"
37-
expImports := map[string]string{"k8s.io/apimachinery/pkg/apis/meta/v1": "metav1"}
38-
assert.Equal(expImports, timports)
39-
4031
// There is an "Api" Shape that is a struct that is an element of the
4132
// GetApis Operation. Its name conflicts with the CRD called API and thus
4233
// we need to check that its cleaned name is set to API_SDK (the _SDK
4334
// suffix is appended to the type name to avoid the conflict with
4435
// CRD-specific structs.
45-
tdef := getTypeDefByName("Api", tdefs)
36+
tdef := testutil.GetTypeDefByName(t, g, "Api")
4637
require.NotNil(tdef)
4738

4839
assert.Equal("API_SDK", tdef.Names.Camel)

pkg/generate/crossplane/crossplane.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ var (
3232
"crossplane/apis/groupversion_info.go.tpl",
3333
"crossplane/apis/types.go.tpl",
3434
}
35-
crdTemplatePath = "crossplane/apis/crd.go.tpl"
36-
controllerTmplPath = "crossplane/pkg/controller.go.tpl"
35+
crdTemplatePath = "crossplane/apis/crd.go.tpl"
36+
controllerTmplPath = "crossplane/pkg/controller.go.tpl"
3737
conversionsTmplPath = "crossplane/pkg/conversions.go.tpl"
38-
includePaths = []string{
38+
includePaths = []string{
3939
"crossplane/boilerplate.go.tpl",
4040
"crossplane/apis/enum_def.go.tpl",
4141
"crossplane/apis/type_def.go.tpl",
@@ -100,7 +100,6 @@ type templateAPIVars struct {
100100
templateset.MetaVars
101101
EnumDefs []*ackmodel.EnumDef
102102
TypeDefs []*ackmodel.TypeDef
103-
Imports map[string]string
104103
}
105104

106105
// templateCRDVars contains template variables for the template that outputs Go
@@ -120,7 +119,7 @@ func Crossplane(
120119
if err != nil {
121120
return nil, err
122121
}
123-
typeDefs, typeImports, err := g.GetTypeDefs()
122+
typeDefs, err := g.GetTypeDefs()
124123
if err != nil {
125124
return nil, err
126125
}
@@ -143,7 +142,6 @@ func Crossplane(
143142
metaVars,
144143
enumDefs,
145144
typeDefs,
146-
typeImports,
147145
}
148146
for _, path := range apisGenericTemplatesPaths {
149147
outPath := filepath.Join(

pkg/generate/generator.go

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -273,17 +273,13 @@ func (g *Generator) IsShapeUsedInCRDs(shapeName string) bool {
273273
return false
274274
}
275275

276-
// GetTypeDefs returns a slice of `ackmodel.TypeDef` pointers and a map of
277-
// package import information
278-
func (g *Generator) GetTypeDefs() ([]*ackmodel.TypeDef, map[string]string, error) {
276+
// GetTypeDefs returns a slice of `ackmodel.TypeDef` pointers
277+
func (g *Generator) GetTypeDefs() ([]*ackmodel.TypeDef, error) {
279278
if g.typeDefs != nil {
280-
return g.typeDefs, g.typeImports, nil
279+
return g.typeDefs, nil
281280
}
282281

283282
tdefs := []*ackmodel.TypeDef{}
284-
// Map, keyed by package import path, with the values being an alias to use
285-
// for the package
286-
timports := map[string]string{}
287283
// Map, keyed by original Shape GoTypeElem(), with the values being a
288284
// renamed type name (due to conflicting names)
289285
trenames := map[string]string{}
@@ -315,38 +311,6 @@ func (g *Generator) GetTypeDefs() ([]*ackmodel.TypeDef, map[string]string, error
315311
if !g.IsShapeUsedInCRDs(memberShape.ShapeName) {
316312
continue
317313
}
318-
goPkgType := memberRef.Shape.GoTypeWithPkgNameElem()
319-
if strings.Contains(goPkgType, ".") {
320-
if strings.HasPrefix(goPkgType, "[]") {
321-
// For slice types, we just want the element type...
322-
goPkgType = strings.TrimLeft(goPkgType, "[]")
323-
}
324-
if strings.HasPrefix(goPkgType, "map[") {
325-
// Assuming the map keys are always of type string.
326-
goPkgType = strings.TrimLeft(goPkgType, "map[string]")
327-
}
328-
if strings.HasPrefix(goPkgType, "*") {
329-
// For slice and map types, the element type might be a
330-
// pointer to a struct...
331-
goPkgType = goPkgType[1:]
332-
}
333-
pkg := strings.Split(goPkgType, ".")[0]
334-
if pkg != g.SDKAPI.API.PackageName() {
335-
// time.Time needs to be converted to apimachinery/metav1.Time otherwise there is no DeepCopy support
336-
if pkg == "time" {
337-
timports["k8s.io/apimachinery/pkg/apis/meta/v1"] = "metav1"
338-
} else if pkg == "aws" {
339-
// The "aws.JSONValue" type needs to be handled
340-
// specially.
341-
timports["github.com/aws/aws-sdk-go/aws"] = ""
342-
} else {
343-
// Shape.GoPTypeWithPkgNameElem() always returns the type
344-
// as a full package dot-notation name. We only want to add
345-
// imports for "normal" packages
346-
timports[pkg] = ""
347-
}
348-
}
349-
}
350314
// There are shapes that are called things like DBProxyStatus that are
351315
// fields in a DBProxy CRD... we need to ensure the type names don't
352316
// conflict. Also, the name of the Go type in the generated code is
@@ -401,9 +365,8 @@ func (g *Generator) GetTypeDefs() ([]*ackmodel.TypeDef, map[string]string, error
401365
})
402366
g.processNestedFieldTypeDefs(tdefs)
403367
g.typeDefs = tdefs
404-
g.typeImports = timports
405368
g.typeRenames = trenames
406-
return tdefs, timports, nil
369+
return tdefs, nil
407370
}
408371

409372
// processNestedFieldTypeDefs updates the supplied TypeDef structs' if a nested

pkg/testutil/get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func GetTypeDefByName(
4949
) *model.TypeDef {
5050
require := require.New(t)
5151

52-
tdefs, _, err := g.GetTypeDefs()
52+
tdefs, err := g.GetTypeDefs()
5353
require.Nil(err)
5454

5555
for _, tdef := range tdefs {

templates/apis/types.go.tpl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
{{- template "boilerplate" }}
22

33
package {{ .APIVersion }}
4-
{{- if .Imports }}
4+
55
import (
6-
{{- end -}}
7-
{{- range $packagePath, $alias := .Imports }}
8-
{{ if $alias -}}{{ $alias }} {{ end -}}"{{ $packagePath }}"
9-
{{ end -}}
10-
{{- if .Imports }}
6+
ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
7+
"github.com/aws/aws-sdk-go/aws"
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
)
10+
11+
// Hack to avoid import errors during build...
12+
var (
13+
_ = &metav1.Time{}
14+
_ = &aws.JSONValue{}
15+
_ = ackv1alpha1.AWSAccountID("")
1116
)
12-
{{- end -}}
1317
{{- range $typeDef := .TypeDefs }}
1418

1519
{{ template "type_def" $typeDef }}

templates/crossplane/apis/types.go.tpl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@
33
// Code generated by ack-generate. DO NOT EDIT.
44

55
package {{ .APIVersion }}
6-
{{- if .Imports }}
6+
77
import (
8-
{{- end -}}
9-
{{- range $packagePath, $alias := .Imports }}
10-
{{ if $alias -}}{{ $alias }} {{ end -}}"{{ $packagePath }}"
11-
{{ end -}}
12-
{{- if .Imports }}
8+
ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
9+
"github.com/aws/aws-sdk-go/aws"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
)
12+
13+
// Hack to avoid import errors during build...
14+
var (
15+
_ = &metav1.Time{}
16+
_ = &aws.JSONValue{}
17+
_ = ackv1alpha1.AWSAccountID("")
1318
)
14-
{{- end -}}
1519
{{- range $typeDef := .TypeDefs }}
1620

1721
{{ template "type_def" $typeDef }}

0 commit comments

Comments
 (0)