-
Notifications
You must be signed in to change notification settings - Fork 199
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Default-gen changes for moving API types to k8s.io/api #54
Conversation
I'll add some tests. |
@mbohlool could you help review it? Thanks. |
LGTM, do you want to add some tests before merging this? |
Yes, after the other is merged. |
The tests PR (#55) is not going anywhere, can we get this merged before code freeze? It's a noop currently until i set up the codegen tags in kubernetes/kubernetes#44784. |
parser/parse.go
Outdated
@@ -260,6 +260,18 @@ func (b *Builder) AddDirTo(dir string, u *types.Universe) error { | |||
return b.findTypesIn(canonicalizeImportPath(b.buildPackages[dir].ImportPath), u) | |||
} | |||
|
|||
// AddDirToAndReturnAddedPath is the same as AddDirTo except that it also | |||
// returns the path of the added package. | |||
func (b *Builder) AddDirToAndReturnAddedPath(dir string, u *types.Universe) (string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of doing this, add a method that canonicalizes a path and returns a *Package object.
parser/parse.go
Outdated
@@ -260,6 +260,26 @@ func (b *Builder) AddDirTo(dir string, u *types.Universe) error { | |||
return b.findTypesIn(canonicalizeImportPath(b.buildPackages[dir].ImportPath), u) | |||
} | |||
|
|||
func (b *Builder) CanonicalPath(dir string) (string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean adding a method like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about DirectoryToPackage(dir string, u *types.Universe) (*types.Package, error)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returning the package
is not going to help me. I need the canonicalizeImportPath(package.ImportPath)
.
parser/parse.go
Outdated
|
||
// AddDirToAndReturnAddedPath is the same as AddDirTo except that it also | ||
// returns the path of the added package. | ||
func (b *Builder) AddDirToAndReturnAddedPath(dir string, u *types.Universe) (string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and remove this function
3fcd613
to
17faa00
Compare
parser/parse.go
Outdated
@@ -260,6 +260,16 @@ func (b *Builder) AddDirTo(dir string, u *types.Universe) error { | |||
return b.findTypesIn(canonicalizeImportPath(b.buildPackages[dir].ImportPath), u) | |||
} | |||
|
|||
// DirectoryToPackage returns the package path given a dir. dir must be already | |||
// parsed. | |||
func (b *Builder) DirectoryToPackagePath(dir string) (string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about this?
Returning Package is not going to help me, because what I need is canonicalizeImportPath(package.ImportPath).
I actually feel my original code is better. This version makes the generator call context.AddDir and context.DirectoryToPackagePath, while my version only calls context.AddDirAndReturnAddedPkgPath.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think my first version is better. I can give the method a better name, maybe AddDirectory (just to be different from AddDir). WDYT?
glog.Fatalf("cannot translate dir %s to package path package %s", inputDir) | ||
} | ||
var ok bool | ||
typesPkg, ok = context.Universe[inputPkgPath] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this just getting the package? Why do you need its path?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
context.Universe[inputPkgPath] returns k8s.io/gengo/types#Package.
context.AddDirTo
calls builder to parse the source code, which only knows go/build#Package and go/types#Package.
The translation from directory to canonical path is done in builder, i can add a method to it to export the functionality, and then export this method via context. But I think that is worse than just return the package path via AddDirTo
.
4e6277e
to
6bcea36
Compare
Updated. PTAL. Thanks. |
parser/parse.go
Outdated
// generator (rather than just at init time. 'dir' must be a single go package. | ||
// GOPATH, GOROOT, and the location of your go binary (`which go`) will all be | ||
// searched if dir doesn't literally resolve. | ||
// Deprecated. Please use AddDirectoryTo. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please leave the former godoc comment and just append this note :)
generator/generator.go
Outdated
// AddDir adds a Go package to the context. The specified path must be a single | ||
// go package import path. GOPATH, GOROOT, and the location of your go binary | ||
// (`which go`) will all be searched, in the normal Go fashion. | ||
// Deprecated. Please use AddDirectory. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same
6bcea36
to
d6ea4d5
Compare
d6ea4d5
to
e9953d1
Compare
LGTM |
Currently, the
k8s:defaulter-gen
tag is in thek8s.io/kubernetes/pkg/apis/<group>/<version>/doc.go
, co-located with the input (API types) and the output (default func) of default-gen.After kubernetes/kubernetes#44065 is implemented, the input/output/tag are not going to be in the same package:
This PR adds the
k8s:defaulter-gen-input
tag, so that the API types can be supplied from a different package.@lavalamp @thockin