go.mod: bump client-go and api machinerie

I had to run `make generate`.
Some API functions got additional parameters `Options` and `Context`.
I used empty options and `context.TODO()` for now.

Signed-off-by: leonnicolas <leonloechner@gmx.de>
This commit is contained in:
leonnicolas
2021-05-15 12:08:31 +02:00
parent f2c37b9de6
commit a3bf13711c
2386 changed files with 419055 additions and 183398 deletions

15
vendor/k8s.io/gengo/args/args.go generated vendored
View File

@@ -74,6 +74,9 @@ type GeneratorArgs struct {
// If true, only verify, don't write anything.
VerifyOnly bool
// If true, include *_test.go files
IncludeTestFiles bool
// GeneratedBuildTag is the tag used to identify code generated by execution
// of this type. Each generator should use a different tag, and different
// groups of generators (external API that depends on Kube generations) should
@@ -109,7 +112,7 @@ func (g *GeneratorArgs) LoadGoBoilerplate() ([]byte, error) {
if err != nil {
return nil, err
}
b = bytes.Replace(b, []byte("YEAR"), []byte(strconv.Itoa(time.Now().Year())), -1)
b = bytes.Replace(b, []byte("YEAR"), []byte(strconv.Itoa(time.Now().UTC().Year())), -1)
if g.GeneratedByCommentTemplate != "" {
if len(b) != 0 {
@@ -127,6 +130,10 @@ func (g *GeneratorArgs) LoadGoBoilerplate() ([]byte, error) {
// directories.
func (g *GeneratorArgs) NewBuilder() (*parser.Builder, error) {
b := parser.New()
// flag for including *_test.go
b.IncludeTestFiles = g.IncludeTestFiles
// Ignore all auto-generated files.
b.AddBuildTags(g.GeneratedBuildTag)
@@ -152,6 +159,9 @@ func (g *GeneratorArgs) InputIncludes(p *types.Package) bool {
if strings.HasSuffix(d, "...") {
d = strings.TrimSuffix(d, "...")
}
if strings.HasPrefix(d, "./vendor/") {
d = strings.TrimPrefix(d, "./vendor/")
}
if strings.HasPrefix(p.Path, d) {
return true
}
@@ -184,6 +194,9 @@ func (g *GeneratorArgs) Execute(nameSystems namer.NameSystems, defaultSystem str
return fmt.Errorf("Failed making a parser: %v", err)
}
// pass through the flag on whether to include *_test.go files
b.IncludeTestFiles = g.IncludeTestFiles
c, err := generator.NewContext(b, nameSystems, defaultSystem)
if err != nil {
return fmt.Errorf("Failed making a context: %v", err)

View File

@@ -29,7 +29,7 @@ import (
"k8s.io/gengo/namer"
"k8s.io/gengo/types"
"k8s.io/klog"
"k8s.io/klog/v2"
)
// CustomArgs is used tby the go2idl framework to pass args specific to this
@@ -40,33 +40,38 @@ type CustomArgs struct {
// This is the comment tag that carries parameters for deep-copy generation.
const (
tagName = "k8s:deepcopy-gen"
interfacesTagName = tagName + ":interfaces"
interfacesNonPointerTagName = tagName + ":nonpointer-interfaces" // attach the DeepCopy<Interface> methods to the
tagEnabledName = "k8s:deepcopy-gen"
interfacesTagName = tagEnabledName + ":interfaces"
interfacesNonPointerTagName = tagEnabledName + ":nonpointer-interfaces" // attach the DeepCopy<Interface> methods to the
)
// Known values for the comment tag.
const tagValuePackage = "package"
// tagValue holds parameters from a tagName tag.
type tagValue struct {
// enabledTagValue holds parameters from a tagName tag.
type enabledTagValue struct {
value string
register bool
}
func extractTag(comments []string) *tagValue {
tagVals := types.ExtractCommentTags("+", comments)[tagName]
func extractEnabledTypeTag(t *types.Type) *enabledTagValue {
comments := append(append([]string{}, t.SecondClosestCommentLines...), t.CommentLines...)
return extractEnabledTag(comments)
}
func extractEnabledTag(comments []string) *enabledTagValue {
tagVals := types.ExtractCommentTags("+", comments)[tagEnabledName]
if tagVals == nil {
// No match for the tag.
return nil
}
// If there are multiple values, abort.
if len(tagVals) > 1 {
klog.Fatalf("Found %d %s tags: %q", len(tagVals), tagName, tagVals)
klog.Fatalf("Found %d %s tags: %q", len(tagVals), tagEnabledName, tagVals)
}
// If we got here we are returning something.
tag := &tagValue{}
tag := &enabledTagValue{}
// Get the primary value.
parts := strings.Split(tagVals[0], ",")
@@ -89,7 +94,7 @@ func extractTag(comments []string) *tagValue {
tag.register = true
}
default:
klog.Fatalf("Unsupported %s param: %q", tagName, parts[i])
klog.Fatalf("Unsupported %s param: %q", tagEnabledName, parts[i])
}
}
return tag
@@ -150,13 +155,13 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
continue
}
ptag := extractTag(pkg.Comments)
ptag := extractEnabledTag(pkg.Comments)
ptagValue := ""
ptagRegister := false
if ptag != nil {
ptagValue = ptag.value
if ptagValue != tagValuePackage {
klog.Fatalf("Package %v: unsupported %s value: %q", i, tagName, ptagValue)
klog.Fatalf("Package %v: unsupported %s value: %q", i, tagEnabledName, ptagValue)
}
ptagRegister = ptag.register
klog.V(5).Infof(" tag.value: %q, tag.register: %t", ptagValue, ptagRegister)
@@ -171,7 +176,7 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
// explicitly wants generation.
for _, t := range pkg.Types {
klog.V(5).Infof(" considering type %q", t.Name.String())
ttag := extractTag(t.CommentLines)
ttag := extractEnabledTypeTag(t)
if ttag != nil && ttag.value == "true" {
klog.V(5).Infof(" tag=true")
if !copyableType(t) {
@@ -254,7 +259,7 @@ func (g *genDeepCopy) Filter(c *generator.Context, t *types.Type) bool {
// Filter out types not being processed or not copyable within the package.
enabled := g.allTypes
if !enabled {
ttag := extractTag(t.CommentLines)
ttag := extractEnabledTypeTag(t)
if ttag != nil && ttag.value == "true" {
enabled = true
}
@@ -391,7 +396,7 @@ func isRootedUnder(pkg string, roots []string) bool {
func copyableType(t *types.Type) bool {
// If the type opts out of copy-generation, stop.
ttag := extractTag(t.CommentLines)
ttag := extractEnabledTypeTag(t)
if ttag != nil && ttag.value == "false" {
return false
}
@@ -460,12 +465,12 @@ func (g *genDeepCopy) Init(c *generator.Context, w io.Writer) error {
}
func (g *genDeepCopy) needsGeneration(t *types.Type) bool {
tag := extractTag(t.CommentLines)
tag := extractEnabledTypeTag(t)
tv := ""
if tag != nil {
tv = tag.value
if tv != "true" && tv != "false" {
klog.Fatalf("Type %v: unsupported %s value: %q", t, tagName, tag.value)
klog.Fatalf("Type %v: unsupported %s value: %q", t, tagEnabledName, tag.value)
}
}
if g.allTypes && tv == "false" {
@@ -481,8 +486,9 @@ func (g *genDeepCopy) needsGeneration(t *types.Type) bool {
return true
}
func extractInterfacesTag(comments []string) []string {
func extractInterfacesTag(t *types.Type) []string {
var result []string
comments := append(append([]string{}, t.SecondClosestCommentLines...), t.CommentLines...)
values := types.ExtractCommentTags("+", comments)[interfacesTagName]
for _, v := range values {
if len(v) == 0 {
@@ -499,7 +505,8 @@ func extractInterfacesTag(comments []string) []string {
return result
}
func extractNonPointerInterfaces(comments []string) (bool, error) {
func extractNonPointerInterfaces(t *types.Type) (bool, error) {
comments := append(append([]string{}, t.SecondClosestCommentLines...), t.CommentLines...)
values := types.ExtractCommentTags("+", comments)[interfacesNonPointerTagName]
if len(values) == 0 {
return false, nil
@@ -518,7 +525,7 @@ func (g *genDeepCopy) deepCopyableInterfacesInner(c *generator.Context, t *types
return nil, nil
}
intfs := extractInterfacesTag(append(t.SecondClosestCommentLines, t.CommentLines...))
intfs := extractInterfacesTag(t)
var ts []*types.Type
for _, intf := range intfs {
@@ -557,7 +564,7 @@ func (g *genDeepCopy) deepCopyableInterfaces(c *generator.Context, t *types.Type
TypeSlice(result).Sort() // we need a stable sorting because it determines the order in generation
nonPointerReceiver, err := extractNonPointerInterfaces(append(t.SecondClosestCommentLines, t.CommentLines...))
nonPointerReceiver, err := extractNonPointerInterfaces(t)
if err != nil {
return nil, false, err
}
@@ -711,7 +718,7 @@ func (g *genDeepCopy) doMap(t *types.Type, sw *generator.SnippetWriter) {
}
if !ut.Key.IsAssignable() {
klog.Fatalf("Hit an unsupported type %v.", uet)
klog.Fatalf("Hit an unsupported type %v for: %v", uet, t)
}
sw.Do("*out = make($.|raw$, len(*in))\n", t)
@@ -738,6 +745,10 @@ func (g *genDeepCopy) doMap(t *types.Type, sw *generator.SnippetWriter) {
case uet.IsAssignable():
sw.Do("(*out)[key] = val\n", nil)
case uet.Kind == types.Interface:
// Note: do not generate code that won't compile as `DeepCopyinterface{}()` is not a valid function
if uet.Name.Name == "interface{}" {
klog.Fatalf("DeepCopy of %q is unsupported. Instead, use named interfaces with DeepCopy<named-interface> as one of the methods.", uet.Name.Name)
}
sw.Do("if val == nil {(*out)[key]=nil} else {\n", nil)
// Note: if t.Elem has been an alias "J" of an interface "I" in Go, we will see it
// as kind Interface of name "J" here, i.e. generate val.DeepCopyJ(). The golang
@@ -754,7 +765,7 @@ func (g *genDeepCopy) doMap(t *types.Type, sw *generator.SnippetWriter) {
case uet.Kind == types.Struct:
sw.Do("(*out)[key] = *val.DeepCopy()\n", uet)
default:
klog.Fatalf("Hit an unsupported type %v.", uet)
klog.Fatalf("Hit an unsupported type %v for %v", uet, t)
}
sw.Do("}\n", nil)
}
@@ -786,6 +797,10 @@ func (g *genDeepCopy) doSlice(t *types.Type, sw *generator.SnippetWriter) {
g.generateFor(ut.Elem, sw)
sw.Do("}\n", nil)
} else if uet.Kind == types.Interface {
// Note: do not generate code that won't compile as `DeepCopyinterface{}()` is not a valid function
if uet.Name.Name == "interface{}" {
klog.Fatalf("DeepCopy of %q is unsupported. Instead, use named interfaces with DeepCopy<named-interface> as one of the methods.", uet.Name.Name)
}
sw.Do("if (*in)[i] != nil {\n", nil)
// Note: if t.Elem has been an alias "J" of an interface "I" in Go, we will see it
// as kind Interface of name "J" here, i.e. generate val.DeepCopyJ(). The golang
@@ -795,7 +810,7 @@ func (g *genDeepCopy) doSlice(t *types.Type, sw *generator.SnippetWriter) {
} else if uet.Kind == types.Struct {
sw.Do("(*in)[i].DeepCopyInto(&(*out)[i])\n", nil)
} else {
klog.Fatalf("Hit an unsupported type %v.", uet)
klog.Fatalf("Hit an unsupported type %v for %v", uet, t)
}
sw.Do("}\n", nil)
}
@@ -856,6 +871,10 @@ func (g *genDeepCopy) doStruct(t *types.Type, sw *generator.SnippetWriter) {
sw.Do("in.$.name$.DeepCopyInto(&out.$.name$)\n", args)
}
case uft.Kind == types.Interface:
// Note: do not generate code that won't compile as `DeepCopyinterface{}()` is not a valid function
if uft.Name.Name == "interface{}" {
klog.Fatalf("DeepCopy of %q is unsupported. Instead, use named interfaces with DeepCopy<named-interface> as one of the methods.", uft.Name.Name)
}
sw.Do("if in.$.name$ != nil {\n", args)
// Note: if t.Elem has been an alias "J" of an interface "I" in Go, we will see it
// as kind Interface of name "J" here, i.e. generate val.DeepCopyJ(). The golang
@@ -863,7 +882,7 @@ func (g *genDeepCopy) doStruct(t *types.Type, sw *generator.SnippetWriter) {
sw.Do(fmt.Sprintf("out.$.name$ = in.$.name$.DeepCopy%s()\n", uft.Name.Name), args)
sw.Do("}\n", nil)
default:
klog.Fatalf("Hit an unsupported type %v.", uft)
klog.Fatalf("Hit an unsupported type %v for %v, from %v", uft, ft, t)
}
}
}
@@ -900,6 +919,6 @@ func (g *genDeepCopy) doPointer(t *types.Type, sw *generator.SnippetWriter) {
sw.Do("*out = new($.Elem|raw$)\n", ut)
sw.Do("(*in).DeepCopyInto(*out)\n", nil)
default:
klog.Fatalf("Hit an unsupported type %v.", uet)
klog.Fatalf("Hit an unsupported type %v for %v", uet, t)
}
}

View File

@@ -28,7 +28,7 @@ type Byte map[byte]Empty
// NewByte creates a Byte from a list of values.
func NewByte(items ...byte) Byte {
ss := Byte{}
ss := make(Byte, len(items))
ss.Insert(items...)
return ss
}
@@ -46,17 +46,19 @@ func ByteKeySet(theMap interface{}) Byte {
}
// Insert adds items to the set.
func (s Byte) Insert(items ...byte) {
func (s Byte) Insert(items ...byte) Byte {
for _, item := range items {
s[item] = Empty{}
}
return s
}
// Delete removes all items from the set.
func (s Byte) Delete(items ...byte) {
func (s Byte) Delete(items ...byte) Byte {
for _, item := range items {
delete(s, item)
}
return s
}
// Has returns true if and only if item is contained in the set.

View File

@@ -28,7 +28,7 @@ type Int map[int]Empty
// NewInt creates a Int from a list of values.
func NewInt(items ...int) Int {
ss := Int{}
ss := make(Int, len(items))
ss.Insert(items...)
return ss
}
@@ -46,17 +46,19 @@ func IntKeySet(theMap interface{}) Int {
}
// Insert adds items to the set.
func (s Int) Insert(items ...int) {
func (s Int) Insert(items ...int) Int {
for _, item := range items {
s[item] = Empty{}
}
return s
}
// Delete removes all items from the set.
func (s Int) Delete(items ...int) {
func (s Int) Delete(items ...int) Int {
for _, item := range items {
delete(s, item)
}
return s
}
// Has returns true if and only if item is contained in the set.

View File

@@ -28,7 +28,7 @@ type Int64 map[int64]Empty
// NewInt64 creates a Int64 from a list of values.
func NewInt64(items ...int64) Int64 {
ss := Int64{}
ss := make(Int64, len(items))
ss.Insert(items...)
return ss
}
@@ -46,17 +46,19 @@ func Int64KeySet(theMap interface{}) Int64 {
}
// Insert adds items to the set.
func (s Int64) Insert(items ...int64) {
func (s Int64) Insert(items ...int64) Int64 {
for _, item := range items {
s[item] = Empty{}
}
return s
}
// Delete removes all items from the set.
func (s Int64) Delete(items ...int64) {
func (s Int64) Delete(items ...int64) Int64 {
for _, item := range items {
delete(s, item)
}
return s
}
// Has returns true if and only if item is contained in the set.

View File

@@ -28,7 +28,7 @@ type String map[string]Empty
// NewString creates a String from a list of values.
func NewString(items ...string) String {
ss := String{}
ss := make(String, len(items))
ss.Insert(items...)
return ss
}
@@ -46,17 +46,19 @@ func StringKeySet(theMap interface{}) String {
}
// Insert adds items to the set.
func (s String) Insert(items ...string) {
func (s String) Insert(items ...string) String {
for _, item := range items {
s[item] = Empty{}
}
return s
}
// Delete removes all items from the set.
func (s String) Delete(items ...string) {
func (s String) Delete(items ...string) String {
for _, item := range items {
delete(s, item)
}
return s
}
// Has returns true if and only if item is contained in the set.

View File

@@ -26,6 +26,8 @@ type DefaultPackage struct {
PackageName string
// Import path of the package, and the location on disk of the package.
PackagePath string
// The location of the package on disk.
Source string
// Emitted at the top of every file.
HeaderText []byte
@@ -43,8 +45,9 @@ type DefaultPackage struct {
FilterFunc func(*Context, *types.Type) bool
}
func (d *DefaultPackage) Name() string { return d.PackageName }
func (d *DefaultPackage) Path() string { return d.PackagePath }
func (d *DefaultPackage) Name() string { return d.PackageName }
func (d *DefaultPackage) Path() string { return d.PackagePath }
func (d *DefaultPackage) SourcePath() string { return d.Source }
func (d *DefaultPackage) Filter(c *Context, t *types.Type) bool {
if d.FilterFunc != nil {

View File

@@ -29,7 +29,7 @@ import (
"k8s.io/gengo/namer"
"k8s.io/gengo/types"
"k8s.io/klog"
"k8s.io/klog/v2"
)
func errs2strings(errors []error) []string {
@@ -233,11 +233,13 @@ func (c *Context) ExecutePackage(outDir string, p Package) error {
if f == nil {
// This is the first generator to reference this file, so start it.
f = &File{
Name: g.Filename(),
FileType: fileType,
PackageName: p.Name(),
Header: p.Header(g.Filename()),
Imports: map[string]struct{}{},
Name: g.Filename(),
FileType: fileType,
PackageName: p.Name(),
PackagePath: p.Path(),
PackageSourcePath: p.SourcePath(),
Header: p.Header(g.Filename()),
Imports: map[string]struct{}{},
}
files[f.Name] = f
} else {

View File

@@ -31,6 +31,8 @@ type Package interface {
Name() string
// Path returns the package import path.
Path() string
// SourcePath returns the location of the package on disk.
SourcePath() string
// Filter should return true if this package cares about this type.
// Otherwise, this type will be omitted from the type ordering for
@@ -50,14 +52,16 @@ type Package interface {
}
type File struct {
Name string
FileType string
PackageName string
Header []byte
Imports map[string]struct{}
Vars bytes.Buffer
Consts bytes.Buffer
Body bytes.Buffer
Name string
FileType string
PackageName string
Header []byte
PackagePath string
PackageSourcePath string
Imports map[string]struct{}
Vars bytes.Buffer
Consts bytes.Buffer
Body bytes.Buffer
}
type FileType interface {
@@ -156,6 +160,12 @@ type Context struct {
// All the types, in case you want to look up something.
Universe types.Universe
// Incoming imports, i.e. packages importing the given package.
incomingImports map[string][]string
// Incoming transitive imports, i.e. the transitive closure of IncomingImports
incomingTransitiveImports map[string][]string
// All the user-specified packages. This is after recursive expansion.
Inputs []string
@@ -203,11 +213,36 @@ func NewContext(b *parser.Builder, nameSystems namer.NameSystems, canonicalOrder
return c, nil
}
// IncomingImports returns the incoming imports for each package. The map is lazily computed.
func (ctxt *Context) IncomingImports() map[string][]string {
if ctxt.incomingImports == nil {
incoming := map[string][]string{}
for _, pkg := range ctxt.Universe {
for imp := range pkg.Imports {
incoming[imp] = append(incoming[imp], pkg.Path)
}
}
ctxt.incomingImports = incoming
}
return ctxt.incomingImports
}
// TransitiveIncomingImports returns the transitive closure of the incoming imports for each package.
// The map is lazily computed.
func (ctxt *Context) TransitiveIncomingImports() map[string][]string {
if ctxt.incomingTransitiveImports == nil {
ctxt.incomingTransitiveImports = transitiveClosure(ctxt.IncomingImports())
}
return ctxt.incomingTransitiveImports
}
// 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 {
ctxt.incomingImports = nil
ctxt.incomingTransitiveImports = nil
return ctxt.builder.AddDirTo(path, &ctxt.Universe)
}
@@ -215,5 +250,7 @@ func (ctxt *Context) AddDir(path string) error {
// 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) {
ctxt.incomingImports = nil
ctxt.incomingTransitiveImports = nil
return ctxt.builder.AddDirectoryTo(path, &ctxt.Universe)
}

View File

@@ -17,9 +17,10 @@ limitations under the License.
package generator
import (
"go/token"
"strings"
"k8s.io/klog"
"k8s.io/klog/v2"
"k8s.io/gengo/namer"
"k8s.io/gengo/types"
@@ -58,6 +59,11 @@ func golangTrackerLocalName(tracker namer.ImportTracker, t types.Name) string {
// This name collides with some other package
continue
}
// If the import name is a Go keyword, prefix with an underscore.
if token.Lookup(name).IsKeyword() {
name = "_" + name
}
return name
}
panic("can't find import for " + path)

View File

@@ -57,7 +57,7 @@ func NewSnippetWriter(w io.Writer, c *Context, left, right string) *SnippetWrite
// Do parses format and runs args through it. You can have arbitrary logic in
// the format (see the text/template documentation), but consider running many
// short templaces, with ordinary go logic in between--this may be more
// short templates with ordinary go logic in between--this may be more
// readable. Do is chainable. Any error causes every other call to do to be
// ignored, and the error will be returned by Error(). So you can check it just
// once, at the end of your function.

65
vendor/k8s.io/gengo/generator/transitive_closure.go generated vendored Normal file
View File

@@ -0,0 +1,65 @@
/*
Copyright 2019 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 generator
import "sort"
type edge struct {
from string
to string
}
func transitiveClosure(in map[string][]string) map[string][]string {
adj := make(map[edge]bool)
imports := make(map[string]struct{})
for from, tos := range in {
for _, to := range tos {
adj[edge{from, to}] = true
imports[to] = struct{}{}
}
}
// Warshal's algorithm
for k := range in {
for i := range in {
if !adj[edge{i, k}] {
continue
}
for j := range imports {
if adj[edge{i, j}] {
continue
}
if adj[edge{k, j}] {
adj[edge{i, j}] = true
}
}
}
}
out := make(map[string][]string, len(in))
for i := range in {
for j := range imports {
if adj[edge{i, j}] {
out[i] = append(out[i], j)
}
}
sort.Strings(out[i])
}
return out
}

3
vendor/k8s.io/gengo/namer/order.go generated vendored
View File

@@ -43,6 +43,9 @@ func (o *Orderer) OrderUniverse(u types.Universe) []*types.Type {
for _, v := range p.Variables {
list.types = append(list.types, v)
}
for _, v := range p.Constants {
list.types = append(list.types, v)
}
}
sort.Sort(list)
return list.types

View File

@@ -22,7 +22,7 @@ import (
"k8s.io/gengo/types"
)
var consonants = "bcdfghjklmnpqrsttvwxyz"
var consonants = "bcdfghjklmnpqrstvwxyz"
type pluralNamer struct {
// key is the case-sensitive type name, value is the case-insensitive

127
vendor/k8s.io/gengo/parser/parse.go generated vendored
View File

@@ -20,6 +20,7 @@ import (
"fmt"
"go/ast"
"go/build"
"go/constant"
"go/parser"
"go/token"
tc "go/types"
@@ -28,11 +29,12 @@ import (
"os/exec"
"path"
"path/filepath"
"regexp"
"sort"
"strings"
"k8s.io/gengo/types"
"k8s.io/klog"
"k8s.io/klog/v2"
)
// This clarifies when a pkg path has been canonicalized.
@@ -43,6 +45,9 @@ type importPathString string
type Builder struct {
context *build.Context
// If true, include *_test.go
IncludeTestFiles bool
// Map of package names to more canonical information about the package.
// This might hold the same value for multiple names, e.g. if someone
// referenced ./pkg/name or in the case of vendoring, which canonicalizes
@@ -224,12 +229,16 @@ func (b *Builder) AddDirRecursive(dir string) error {
klog.Warningf("Ignoring directory %v: %v", dir, err)
}
// filepath.Walk includes the root dir, but we already did that, so we'll
// remove that prefix and rebuild a package import path.
prefix := b.buildPackages[dir].Dir
// filepath.Walk does not follow symlinks. We therefore evaluate symlinks and use that with
// filepath.Walk.
realPath, err := filepath.EvalSymlinks(b.buildPackages[dir].Dir)
if err != nil {
return err
}
fn := func(filePath string, info os.FileInfo, err error) error {
if info != nil && info.IsDir() {
rel := filepath.ToSlash(strings.TrimPrefix(filePath, prefix))
rel := filepath.ToSlash(strings.TrimPrefix(filePath, realPath))
if rel != "" {
// Make a pkg path.
pkg := path.Join(string(canonicalizeImportPath(b.buildPackages[dir].ImportPath)), rel)
@@ -242,7 +251,7 @@ func (b *Builder) AddDirRecursive(dir string) error {
}
return nil
}
if err := filepath.Walk(b.buildPackages[dir].Dir, fn); err != nil {
if err := filepath.Walk(realPath, fn); err != nil {
return err
}
return nil
@@ -304,11 +313,17 @@ func (b *Builder) addDir(dir string, userRequested bool) error {
b.absPaths[pkgPath] = buildPkg.Dir
}
for _, n := range buildPkg.GoFiles {
if !strings.HasSuffix(n, ".go") {
files := []string{}
files = append(files, buildPkg.GoFiles...)
if b.IncludeTestFiles {
files = append(files, buildPkg.TestGoFiles...)
}
for _, file := range files {
if !strings.HasSuffix(file, ".go") {
continue
}
absPath := filepath.Join(buildPkg.Dir, n)
absPath := filepath.Join(buildPkg.Dir, file)
data, err := ioutil.ReadFile(absPath)
if err != nil {
return fmt.Errorf("while loading %q: %v", absPath, err)
@@ -321,6 +336,13 @@ func (b *Builder) addDir(dir string, userRequested bool) error {
return nil
}
// regexErrPackageNotFound helps test the expected error for not finding a package.
var regexErrPackageNotFound = regexp.MustCompile(`^unable to import ".*?":.*`)
func isErrPackageNotFound(err error) bool {
return regexErrPackageNotFound.MatchString(err.Error())
}
// importPackage is a function that will be called by the type check package when it
// needs to import a go package. 'path' is the import path.
func (b *Builder) importPackage(dir string, userRequested bool) (*tc.Package, error) {
@@ -343,6 +365,11 @@ func (b *Builder) importPackage(dir string, userRequested bool) (*tc.Package, er
// Add it.
if err := b.addDir(dir, userRequested); err != nil {
if isErrPackageNotFound(err) {
klog.V(6).Info(err)
return nil, nil
}
return nil, err
}
@@ -403,7 +430,7 @@ func (b *Builder) typeCheckPackage(pkgPath importPathString) (*tc.Package, error
}
parsedFiles, ok := b.parsed[pkgPath]
if !ok {
return nil, fmt.Errorf("No files for pkg %q: %#v", pkgPath, b.parsed)
return nil, fmt.Errorf("No files for pkg %q", pkgPath)
}
files := make([]*ast.File, len(parsedFiles))
for i := range parsedFiles {
@@ -466,6 +493,19 @@ func (b *Builder) FindTypes() (types.Universe, error) {
return u, nil
}
// addCommentsToType takes any accumulated comment lines prior to obj and
// attaches them to the type t.
func (b *Builder) addCommentsToType(obj tc.Object, t *types.Type) {
c1 := b.priorCommentLines(obj.Pos(), 1)
// c1.Text() is safe if c1 is nil
t.CommentLines = splitLines(c1.Text())
if c1 == nil {
t.SecondClosestCommentLines = splitLines(b.priorCommentLines(obj.Pos(), 2).Text())
} else {
t.SecondClosestCommentLines = splitLines(b.priorCommentLines(c1.List[0].Slash, 2).Text())
}
}
// findTypesIn finalizes the package import and searches through the package
// for types.
func (b *Builder) findTypesIn(pkgPath importPathString, u *types.Universe) error {
@@ -509,31 +549,23 @@ func (b *Builder) findTypesIn(pkgPath importPathString, u *types.Universe) error
tn, ok := obj.(*tc.TypeName)
if ok {
t := b.walkType(*u, nil, tn.Type())
c1 := b.priorCommentLines(obj.Pos(), 1)
// c1.Text() is safe if c1 is nil
t.CommentLines = splitLines(c1.Text())
if c1 == nil {
t.SecondClosestCommentLines = splitLines(b.priorCommentLines(obj.Pos(), 2).Text())
} else {
t.SecondClosestCommentLines = splitLines(b.priorCommentLines(c1.List[0].Slash, 2).Text())
}
b.addCommentsToType(obj, t)
}
tf, ok := obj.(*tc.Func)
// We only care about functions, not concrete/abstract methods.
if ok && tf.Type() != nil && tf.Type().(*tc.Signature).Recv() == nil {
t := b.addFunction(*u, nil, tf)
c1 := b.priorCommentLines(obj.Pos(), 1)
// c1.Text() is safe if c1 is nil
t.CommentLines = splitLines(c1.Text())
if c1 == nil {
t.SecondClosestCommentLines = splitLines(b.priorCommentLines(obj.Pos(), 2).Text())
} else {
t.SecondClosestCommentLines = splitLines(b.priorCommentLines(c1.List[0].Slash, 2).Text())
}
b.addCommentsToType(obj, t)
}
tv, ok := obj.(*tc.Var)
if ok && !tv.IsField() {
b.addVariable(*u, nil, tv)
t := b.addVariable(*u, nil, tv)
b.addCommentsToType(obj, t)
}
tconst, ok := obj.(*tc.Const)
if ok {
t := b.addConstant(*u, nil, tconst)
b.addCommentsToType(obj, t)
}
}
@@ -560,7 +592,7 @@ func (b *Builder) importWithMode(dir string, mode build.ImportMode) (*build.Pack
if err != nil {
return nil, fmt.Errorf("unable to get current directory: %v", err)
}
buildPkg, err := b.context.Import(dir, cwd, mode)
buildPkg, err := b.context.Import(filepath.ToSlash(dir), cwd, mode)
if err != nil {
return nil, err
}
@@ -733,7 +765,11 @@ func (b *Builder) walkType(u types.Universe, useName *types.Name, in tc.Type) *t
if out.Methods == nil {
out.Methods = map[string]*types.Type{}
}
out.Methods[t.Method(i).Name()] = b.walkType(u, nil, t.Method(i).Type())
method := t.Method(i)
name := tcNameToName(method.String())
mt := b.walkType(u, &name, method.Type())
mt.CommentLines = splitLines(b.priorCommentLines(method.Pos(), 1).Text())
out.Methods[method.Name()] = mt
}
return out
case *tc.Named:
@@ -765,7 +801,11 @@ func (b *Builder) walkType(u types.Universe, useName *types.Name, in tc.Type) *t
if out.Methods == nil {
out.Methods = map[string]*types.Type{}
}
out.Methods[t.Method(i).Name()] = b.walkType(u, nil, t.Method(i).Type())
method := t.Method(i)
name := tcNameToName(method.String())
mt := b.walkType(u, &name, method.Type())
mt.CommentLines = splitLines(b.priorCommentLines(method.Pos(), 1).Text())
out.Methods[method.Name()] = mt
}
}
return out
@@ -802,6 +842,33 @@ func (b *Builder) addVariable(u types.Universe, useName *types.Name, in *tc.Var)
return out
}
func (b *Builder) addConstant(u types.Universe, useName *types.Name, in *tc.Const) *types.Type {
name := tcVarNameToName(in.String())
if useName != nil {
name = *useName
}
out := u.Constant(name)
out.Kind = types.DeclarationOf
out.Underlying = b.walkType(u, nil, in.Type())
var constval string
// For strings, we use `StringVal()` to get the un-truncated,
// un-quoted string. For other values, `.String()` is preferable to
// get something relatively human readable (especially since for
// floating point types, `ExactString()` will generate numeric
// expressions using `big.(*Float).Text()`.
switch in.Val().Kind() {
case constant.String:
constval = constant.StringVal(in.Val())
default:
constval = in.Val().String()
}
out.ConstValue = &constval
return out
}
// canonicalizeImportPath takes an import path and returns the actual package.
// It doesn't support nested vendoring.
func canonicalizeImportPath(importPath string) importPathString {

34
vendor/k8s.io/gengo/types/types.go generated vendored
View File

@@ -135,6 +135,10 @@ type Package struct {
// package name).
Variables map[string]*Type
// Global constants within this package, indexed by their name (*not* including
// package name).
Constants map[string]*Type
// Packages imported by this package, indexed by (canonicalized)
// package path.
Imports map[string]*Package
@@ -193,6 +197,20 @@ func (p *Package) Variable(varName string) *Type {
return t
}
// Constant gets the given constant Type in this Package. If the constant is
// not already defined, this will add it. If a constant is added, it's the caller's
// responsibility to finish construction of the constant by setting Underlying
// to the correct type.
func (p *Package) Constant(constName string) *Type {
if t, ok := p.Constants[constName]; ok {
return t
}
t := &Type{Name: Name{Package: p.Path, Name: constName}}
t.Kind = DeclarationOf
p.Constants[constName] = t
return t
}
// HasImport returns true if p imports packageName. Package names include the
// package directory.
func (p *Package) HasImport(packageName string) bool {
@@ -229,6 +247,14 @@ func (u Universe) Variable(n Name) *Type {
return u.Package(n.Package).Variable(n.Name)
}
// Constant returns the canonical constant for the given fully-qualified name.
// If a non-existing constant is requested, this will create (a marker for) it.
// If a marker is created, it's the caller's responsibility to finish
// construction of the constant by setting Underlying to the correct type.
func (u Universe) Constant(n Name) *Type {
return u.Package(n.Package).Constant(n.Name)
}
// AddImports registers import lines for packageName. May be called multiple times.
// You are responsible for canonicalizing all package paths.
func (u Universe) AddImports(packagePath string, importPaths ...string) {
@@ -251,6 +277,7 @@ func (u Universe) Package(packagePath string) *Package {
Types: map[string]*Type{},
Functions: map[string]*Type{},
Variables: map[string]*Type{},
Constants: map[string]*Type{},
Imports: map[string]*Package{},
}
u[packagePath] = p
@@ -314,6 +341,13 @@ type Type struct {
// If Kind == func, this is the signature of the function.
Signature *Signature
// ConstValue contains a stringified constant value if
// Kind == DeclarationOf and this is a constant value
// declaration. For string constants, this field contains
// the entire, un-quoted value. For other types, it contains
// a human-readable literal.
ConstValue *string
// TODO: Add:
// * channel direction
// * array length