From a3f4a8bd528390d03f17ecc5a8f7930f5fe552ab Mon Sep 17 00:00:00 2001 From: Chao Xu Date: Wed, 31 May 2017 17:43:19 -0700 Subject: [PATCH] update gengo godep --- Godeps/Godeps.json | 20 +++--- .../deepcopy-gen/generators/deepcopy.go | 1 - .../defaulter-gen/generators/defaulter.go | 65 +++++++++++++++---- vendor/k8s.io/gengo/generator/generator.go | 8 +++ vendor/k8s.io/gengo/parser/parse.go | 22 +++++++ vendor/k8s.io/gengo/types/types.go | 5 +- 6 files changed, 97 insertions(+), 24 deletions(-) diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index cc4c30ff8ea..0ef86b78e78 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -2860,43 +2860,43 @@ }, { "ImportPath": "k8s.io/gengo/args", - "Rev": "4b7dfb7a2f42f66d9f5dfdf2c1557f409eac0869" + "Rev": "c79c13d131b0a8f42d05faa6491c12e94ccc6f30" }, { "ImportPath": "k8s.io/gengo/examples/deepcopy-gen/generators", - "Rev": "4b7dfb7a2f42f66d9f5dfdf2c1557f409eac0869" + "Rev": "c79c13d131b0a8f42d05faa6491c12e94ccc6f30" }, { "ImportPath": "k8s.io/gengo/examples/defaulter-gen/generators", - "Rev": "4b7dfb7a2f42f66d9f5dfdf2c1557f409eac0869" + "Rev": "c79c13d131b0a8f42d05faa6491c12e94ccc6f30" }, { "ImportPath": "k8s.io/gengo/examples/import-boss/generators", - "Rev": "4b7dfb7a2f42f66d9f5dfdf2c1557f409eac0869" + "Rev": "c79c13d131b0a8f42d05faa6491c12e94ccc6f30" }, { "ImportPath": "k8s.io/gengo/examples/set-gen/generators", - "Rev": "4b7dfb7a2f42f66d9f5dfdf2c1557f409eac0869" + "Rev": "c79c13d131b0a8f42d05faa6491c12e94ccc6f30" }, { "ImportPath": "k8s.io/gengo/examples/set-gen/sets", - "Rev": "4b7dfb7a2f42f66d9f5dfdf2c1557f409eac0869" + "Rev": "c79c13d131b0a8f42d05faa6491c12e94ccc6f30" }, { "ImportPath": "k8s.io/gengo/generator", - "Rev": "4b7dfb7a2f42f66d9f5dfdf2c1557f409eac0869" + "Rev": "c79c13d131b0a8f42d05faa6491c12e94ccc6f30" }, { "ImportPath": "k8s.io/gengo/namer", - "Rev": "4b7dfb7a2f42f66d9f5dfdf2c1557f409eac0869" + "Rev": "c79c13d131b0a8f42d05faa6491c12e94ccc6f30" }, { "ImportPath": "k8s.io/gengo/parser", - "Rev": "4b7dfb7a2f42f66d9f5dfdf2c1557f409eac0869" + "Rev": "c79c13d131b0a8f42d05faa6491c12e94ccc6f30" }, { "ImportPath": "k8s.io/gengo/types", - "Rev": "4b7dfb7a2f42f66d9f5dfdf2c1557f409eac0869" + "Rev": "c79c13d131b0a8f42d05faa6491c12e94ccc6f30" }, { "ImportPath": "k8s.io/heapster/metrics/api/v1/types", diff --git a/vendor/k8s.io/gengo/examples/deepcopy-gen/generators/deepcopy.go b/vendor/k8s.io/gengo/examples/deepcopy-gen/generators/deepcopy.go index c126b925624..68be1fc45bf 100644 --- a/vendor/k8s.io/gengo/examples/deepcopy-gen/generators/deepcopy.go +++ b/vendor/k8s.io/gengo/examples/deepcopy-gen/generators/deepcopy.go @@ -196,7 +196,6 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat expandedPath := strings.TrimPrefix(pkg.SourcePath, arguments.OutputBase) if strings.Contains(expandedPath, "/vendor/") { path = expandedPath - glog.V(3).Infof(" %s", path) } } packages = append(packages, diff --git a/vendor/k8s.io/gengo/examples/defaulter-gen/generators/defaulter.go b/vendor/k8s.io/gengo/examples/defaulter-gen/generators/defaulter.go index f72a5e3b164..02241828457 100644 --- a/vendor/k8s.io/gengo/examples/defaulter-gen/generators/defaulter.go +++ b/vendor/k8s.io/gengo/examples/defaulter-gen/generators/defaulter.go @@ -38,13 +38,18 @@ type CustomArgs struct { ExtraPeerDirs []string // Always consider these as last-ditch possibilities for conversions. } -// This is the comment tag that carries parameters for defaulter generation. +// These are the comment tags that carry parameters for defaulter generation. const tagName = "k8s:defaulter-gen" +const intputTagName = "k8s:defaulter-gen-input" func extractTag(comments []string) []string { return types.ExtractCommentTags("+", comments)[tagName] } +func extractInputTag(comments []string) []string { + return types.ExtractCommentTags("+", comments)[intputTagName] +} + func checkTag(comments []string, require ...string) bool { values := types.ExtractCommentTags("+", comments)[tagName] if len(require) == 0 { @@ -220,6 +225,11 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat // If the input had no Go files, for example. continue } + // typesPkg is where the types that needs defaulter are defined. + // Sometimes it is different from pkg. For example, kubernetes core/v1 + // types are defined in vendor/k8s.io/api/core/v1, while pkg is at + // pkg/api/v1. + typesPkg := pkg // Add defaulting functions. getManualDefaultingFunctions(context, pkg, existingDefaulters) @@ -271,8 +281,24 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat return false } + // if the types are not in the same package where the defaulter functions to be generated + inputTags := extractInputTag(pkg.Comments) + if len(inputTags) > 1 { + panic(fmt.Sprintf("there could only be one input tag, got %#v", inputTags)) + } + if len(inputTags) == 1 { + var err error + typesPkg, err = context.AddDirectory(filepath.Join(pkg.Path, inputTags[0])) + if err != nil { + glog.Fatalf("cannot import package %s", inputTags[0]) + } + // update context.Order to the latest context.Universe + orderer := namer.Orderer{Namer: namer.NewPublicNamer(1)} + context.Order = orderer.OrderUniverse(context.Universe) + } + newDefaulters := defaulterFuncMap{} - for _, t := range pkg.Types { + for _, t := range typesPkg.Types { if !shouldCreateObjectDefaulterFn(t) { continue } @@ -328,18 +354,33 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat glog.V(5).Infof("no defaulters in package %s", pkg.Name) } + path := pkg.Path + // if the source path is within a /vendor/ directory (for example, + // k8s.io/kubernetes/vendor/k8s.io/apimachinery/pkg/apis/meta/v1), allow + // generation to output to the proper relative path (under vendor). + // Otherwise, the generator will create the file in the wrong location + // in the output directory. + // TODO: build a more fundamental concept in gengo for dealing with modifications + // to vendored packages. + if strings.HasPrefix(pkg.SourcePath, arguments.OutputBase) { + expandedPath := strings.TrimPrefix(pkg.SourcePath, arguments.OutputBase) + if strings.Contains(expandedPath, "/vendor/") { + path = expandedPath + } + } + packages = append(packages, &generator.DefaultPackage{ PackageName: filepath.Base(pkg.Path), - PackagePath: pkg.Path, + PackagePath: path, HeaderText: header, GeneratorFunc: func(c *generator.Context) (generators []generator.Generator) { return []generator.Generator{ - NewGenDefaulter(arguments.OutputFileBaseName, pkg.Path, existingDefaulters, newDefaulters, peerPkgs), + NewGenDefaulter(arguments.OutputFileBaseName, typesPkg.Path, pkg.Path, existingDefaulters, newDefaulters, peerPkgs), } }, FilterFunc: func(c *generator.Context, t *types.Type) bool { - return t.Name.Package == pkg.Path + return t.Name.Package == typesPkg.Path }, }) } @@ -442,7 +483,8 @@ const ( // genDefaulter produces a file with a autogenerated conversions. type genDefaulter struct { generator.DefaultGen - targetPackage string + typesPackage string + outputPackage string peerPackages []string newDefaulters defaulterFuncMap existingDefaulters defaulterFuncMap @@ -450,12 +492,13 @@ type genDefaulter struct { typesForInit []*types.Type } -func NewGenDefaulter(sanitizedName, targetPackage string, existingDefaulters, newDefaulters defaulterFuncMap, peerPkgs []string) generator.Generator { +func NewGenDefaulter(sanitizedName, typesPackage, outputPackage string, existingDefaulters, newDefaulters defaulterFuncMap, peerPkgs []string) generator.Generator { return &genDefaulter{ DefaultGen: generator.DefaultGen{ OptionalName: sanitizedName, }, - targetPackage: targetPackage, + typesPackage: typesPackage, + outputPackage: outputPackage, peerPackages: peerPkgs, newDefaulters: newDefaulters, existingDefaulters: existingDefaulters, @@ -467,15 +510,15 @@ func NewGenDefaulter(sanitizedName, targetPackage string, existingDefaulters, ne func (g *genDefaulter) Namers(c *generator.Context) namer.NameSystems { // Have the raw namer for this file track what it imports. return namer.NameSystems{ - "raw": namer.NewRawNamer(g.targetPackage, g.imports), + "raw": namer.NewRawNamer(g.outputPackage, g.imports), } } func (g *genDefaulter) isOtherPackage(pkg string) bool { - if pkg == g.targetPackage { + if pkg == g.outputPackage { return false } - if strings.HasSuffix(pkg, `"`+g.targetPackage+`"`) { + if strings.HasSuffix(pkg, `"`+g.outputPackage+`"`) { return false } return true diff --git a/vendor/k8s.io/gengo/generator/generator.go b/vendor/k8s.io/gengo/generator/generator.go index 0770dbe6079..05a3f65fe87 100644 --- a/vendor/k8s.io/gengo/generator/generator.go +++ b/vendor/k8s.io/gengo/generator/generator.go @@ -206,6 +206,14 @@ func NewContext(b *parser.Builder, nameSystems namer.NameSystems, canonicalOrder // 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. func (ctxt *Context) AddDir(path string) error { return ctxt.builder.AddDirTo(path, &ctxt.Universe) } + +// AddDirectory 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. +func (ctxt *Context) AddDirectory(path string) (*types.Package, error) { + return ctxt.builder.AddDirectoryTo(path, &ctxt.Universe) +} diff --git a/vendor/k8s.io/gengo/parser/parse.go b/vendor/k8s.io/gengo/parser/parse.go index 30650aff6bc..2288a0e473d 100644 --- a/vendor/k8s.io/gengo/parser/parse.go +++ b/vendor/k8s.io/gengo/parser/parse.go @@ -251,6 +251,7 @@ func (b *Builder) AddDirRecursive(dir string) error { // 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. func (b *Builder) AddDirTo(dir string, u *types.Universe) error { // We want all types from this package, as if they were directly added // by the user. They WERE added by the user, in effect. @@ -260,6 +261,24 @@ func (b *Builder) AddDirTo(dir string, u *types.Universe) error { return b.findTypesIn(canonicalizeImportPath(b.buildPackages[dir].ImportPath), u) } +// AddDirectoryTo adds an entire directory to a given Universe. Unlike AddDir, +// this processes the package immediately, which makes it safe to use from +// within a 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. +func (b *Builder) AddDirectoryTo(dir string, u *types.Universe) (*types.Package, error) { + // We want all types from this package, as if they were directly added + // by the user. They WERE added by the user, in effect. + if _, err := b.importPackage(dir, true); err != nil { + return nil, err + } + path := canonicalizeImportPath(b.buildPackages[dir].ImportPath) + if err := b.findTypesIn(path, u); err != nil { + return nil, err + } + return u.Package(string(path)), nil +} + // The implementation of AddDir. A flag indicates whether this directory was // user-requested or just from following the import graph. func (b *Builder) addDir(dir string, userRequested bool) error { @@ -462,6 +481,9 @@ func (b *Builder) findTypesIn(pkgPath importPathString, u *types.Universe) error for _, f := range b.parsed[pkgPath] { if strings.HasSuffix(f.name, "/doc.go") { tp := u.Package(string(pkgPath)) + // findTypesIn might be called multiple times. Clean up tp.Comments + // to avoid repeatedly fill same comments to it. + tp.Comments = []string{} for i := range f.file.Comments { tp.Comments = append(tp.Comments, splitLines(f.file.Comments[i].Text())...) } diff --git a/vendor/k8s.io/gengo/types/types.go b/vendor/k8s.io/gengo/types/types.go index 363aaa4f18c..12cfdd132d5 100644 --- a/vendor/k8s.io/gengo/types/types.go +++ b/vendor/k8s.io/gengo/types/types.go @@ -101,10 +101,11 @@ type Package struct { // 'package x' line. Name string - // DocComments from doc.go, if any. + // The comment right above the package declaration in doc.go, if any. DocComments []string - // Comments from doc.go, if any. + // All comments from doc.go, if any. + // TODO: remove Comments and use DocComments everywhere. Comments []string // Types within this package, indexed by their name (*not* including