Skip to content

Commit

Permalink
Merge pull request #208 from pwittrock/const
Browse files Browse the repository at this point in the history
Fix issue with generating fields for types external to the versioned …
  • Loading branch information
Phillip Wittrock authored Jan 18, 2018
2 parents 369902e + 3fd0360 commit aa5c760
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
21 changes: 17 additions & 4 deletions cmd/apiregister-gen/generators/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 <pkgalias>.<TypeName>
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)
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions example/pkg/apis/innsmouth/common/constants.go
Original file line number Diff line number Diff line change
@@ -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"
)
4 changes: 4 additions & 0 deletions example/pkg/apis/innsmouth/v1/deepone_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit aa5c760

Please sign in to comment.