diff --git a/cmd/apiregister-gen/generators/parser.go b/cmd/apiregister-gen/generators/parser.go index 022a8f80b6..cc0b356615 100644 --- a/cmd/apiregister-gen/generators/parser.go +++ b/cmd/apiregister-gen/generators/parser.go @@ -85,7 +85,7 @@ type Field struct { Name string // For versioned Kubernetes types, this is the versioned package VersionedPackage string - // For versioned Kubernetes types, this is theun versioned package + // For versioned Kubernetes types, this is the unversioned package UnversionedImport string UnversionedType string } @@ -691,14 +691,27 @@ func (apigroup *APIGroup) DoType(t *types.Type) (*Struct, []*types.Type) { t = t.Elem hasElem = true } - uImportName := path.Base(path.Dir(t.Name.Package)) - uImport = path.Dir(t.Name.Package) + // 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) } - fmt.Printf("\nDifferent Package Parent Package: %s Child Package: %s Name: %s : %s : %s\n%s : %s\n\n", t.Name.Package, member.Type.Name.Package, member.Type.Kind, member.Type.String(), path.Dir(uImport), 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/common/constants.go b/example/pkg/apis/innsmouth/common/constants.go new file mode 100644 index 0000000000..f58f5eee62 --- /dev/null +++ b/example/pkg/apis/innsmouth/common/constants.go @@ -0,0 +1,26 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License 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 common + +// Constants aren't automatically generated for unversioned packages. +// Instead share the same constant for both packages +type CustomType string + +const ( + CUSTOM1 CustomType = "test1" + CUSTOM2 CustomType = "test2" +) diff --git a/example/pkg/apis/innsmouth/v1/deepone_types.go b/example/pkg/apis/innsmouth/v1/deepone_types.go index 06b9ca0694..0918d3768a 100644 --- a/example/pkg/apis/innsmouth/v1/deepone_types.go +++ b/example/pkg/apis/innsmouth/v1/deepone_types.go @@ -18,6 +18,7 @@ package v1 import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "github.com/kubernetes-incubator/apiserver-builder/example/pkg/apis/innsmouth/common" ) // +genclient @@ -45,6 +46,9 @@ type DeepOneSpec struct { SamplePointerList []*SampleListPointerElem `json:"sample_pointer_list,omitempty"` SampleMap map[string]SampleMapElem `json:"sample_map,omitempty"` SamplePointerMap map[string]*SampleMapPointerElem `json:"sample_pointer_map,omitempty"` + + // Example of using a constant + Const common.CustomType `json:"const,omitempty"` } type SampleListElem struct {