Skip to content

Commit

Permalink
Merge pull request #209 from pwittrock/const
Browse files Browse the repository at this point in the history
Fix issues creating fields of Pointers,Slices, and Maps of types in o…
  • Loading branch information
Phillip Wittrock committed Jan 19, 2018
2 parents aa5c760 + b7ce760 commit f37880b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 26 deletions.
71 changes: 47 additions & 24 deletions cmd/apiregister-gen/generators/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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)
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 <pkgalias>.<TypeName>
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)
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions example/pkg/apis/innsmouth/v1/deepone_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f37880b

Please sign in to comment.