From b7ce760cf80c9917270f830b9589e948e085144d Mon Sep 17 00:00:00 2001 From: Phillip Wittrock Date: Thu, 18 Jan 2018 17:11:45 -0800 Subject: [PATCH] Fix issues creating fields of Pointers,Slices, and Maps of types in other packages --- cmd/apiregister-gen/generators/parser.go | 71 ++++++++++++------- .../pkg/apis/innsmouth/v1/deepone_types.go | 11 ++- 2 files changed, 56 insertions(+), 26 deletions(-) diff --git a/cmd/apiregister-gen/generators/parser.go b/cmd/apiregister-gen/generators/parser.go index cc0b356615..91712ec63a 100644 --- a/cmd/apiregister-gen/generators/parser.go +++ b/cmd/apiregister-gen/generators/parser.go @@ -685,33 +685,56 @@ func (apigroup *APIGroup) DoType(t *types.Type) (*Struct, []*types.Type) { default: // Use unversioned types for everything else t := member.Type - hasElem := false + if t.Elem != nil { - // Handle Pointers, Maps, Slices correctly + // Handle Pointers, Maps, Slices + + // We need to parse the package from the Type String t = t.Elem - hasElem = true - } - // Come up with the alias the package is imported under - // Concatenate with directory package to reduce naming collisions - uImportName := path.Base(path.Dir(t.Name.Package)) + path.Base(t.Name.Package) - - // Create the import statement - uImport = fmt.Sprintf("%s \"%s\"", uImportName, t.Name.Package) - - // Create the field type name - should be . - uType = uImportName + "." + t.Name.Name - if hasElem { - uType = strings.Replace(member.Type.String(), path.Dir(uImport)+"/", "", 1) - uType = strings.Replace(uType, "/"+path.Base(t.Name.Package), "", 1) + str := member.Type.String() + startPkg := strings.LastIndexAny(str, "*]") + endPkg := strings.LastIndexAny(str, ".") + pkg := str[startPkg+1 : endPkg] + name := str[endPkg+1:] + prefix := str[:startPkg+1] + + uImportBase := path.Base(pkg) + uImportName := path.Base(path.Dir(pkg)) + uImportBase + uImport = fmt.Sprintf("%s \"%s\"", uImportName, pkg) + + uType = prefix + uImportName + "." + name + + fmt.Printf("\nDifferent Parent Package: %s\nChild Package: %s\nKind: %s (Kind.String() %s)\nImport stmt: %s\nType: %s\n\n", + pkg, + member.Type.Name.Package, + member.Type.Kind, + member.Type.String(), + uImport, + uType) + } else { + // Handle non- Pointer, Maps, Slices + pkg := t.Name.Package + name := t.Name.Name + + // Come up with the alias the package is imported under + // Concatenate with directory package to reduce naming collisions + uImportBase := path.Base(pkg) + uImportName := path.Base(path.Dir(pkg)) + uImportBase + + // Create the import statement + uImport = fmt.Sprintf("%s \"%s\"", uImportName, pkg) + + // Create the field type name - should be . + uType = uImportName + "." + name + + fmt.Printf("\nDifferent Parent Package: %s\nChild Package: %s\nKind: %s (Kind.String() %s)\nImport stmt: %s\nType: %s\n\n", + pkg, + member.Type.Name.Package, + member.Type.Kind, + member.Type.String(), + uImport, + uType) } - fmt.Printf("\nDifferent Package Parent Package: %s\nChild Package: %s\nName: %s : %s : %s\n%s %s : %s\n\n", - t.Name.Package, - member.Type.Name.Package, - member.Type.Kind, - member.Type.String(), - path.Dir(uImport), - uImportName, uImport, - uType) } } } diff --git a/example/pkg/apis/innsmouth/v1/deepone_types.go b/example/pkg/apis/innsmouth/v1/deepone_types.go index 0918d3768a..14213ef133 100644 --- a/example/pkg/apis/innsmouth/v1/deepone_types.go +++ b/example/pkg/apis/innsmouth/v1/deepone_types.go @@ -17,8 +17,8 @@ limitations under the License. package v1 import ( + "github.com/kubernetes-incubator/apiserver-builder/example/pkg/apis/innsmouth/common" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "github.com/kubernetes-incubator/apiserver-builder/example/pkg/apis/innsmouth/common" ) // +genclient @@ -48,7 +48,14 @@ type DeepOneSpec struct { SamplePointerMap map[string]*SampleMapPointerElem `json:"sample_pointer_map,omitempty"` // Example of using a constant - Const common.CustomType `json:"const,omitempty"` + Const common.CustomType `json:"const,omitempty"` + ConstPtr *common.CustomType `json:"constPtr,omitempty"` + ConstSlice []common.CustomType `json:"constSlice,omitempty"` + ConstMap map[string]common.CustomType `json:"constMap,omitempty"` + + // TODO: Fix issues with deep copy to make these work + //ConstSlicePtr []*common.CustomType `json:"constSlicePtr,omitempty"` + //ConstMapPtr map[string]*common.CustomType `json:"constMapPtr,omitempty"` } type SampleListElem struct {