Switch to new generator

This commit is contained in:
Wojciech Tyczynski
2016-04-01 19:18:54 +02:00
parent cd2de7d4cd
commit 7448cc04e1
20 changed files with 197 additions and 44 deletions

View File

@@ -57,9 +57,20 @@ func DefaultNameSystem() string {
return "public"
}
var fallbackPackages = []string{
"k8s.io/kubernetes/pkg/api/unversioned",
"k8s.io/kubernetes/pkg/apis/extensions",
}
func getInternalTypeFor(context *generator.Context, t *types.Type) (*types.Type, bool) {
internalPackage := filepath.Dir(t.Name.Package)
if !context.Universe.Package(internalPackage).Has(t.Name.Name) {
for _, fallbackPackage := range fallbackPackages {
if fallbackPackage == t.Name.Package || !context.Universe.Package(fallbackPackage).Has(t.Name.Name) {
continue
}
return context.Universe.Package(fallbackPackage).Type(t.Name.Name), true
}
return nil, false
}
return context.Universe.Package(internalPackage).Type(t.Name.Name), true
@@ -149,13 +160,21 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
// (in the directory one above) and can be automatically converted to.
for _, p := range context.Universe {
path := p.Path
// TODO: Only a subset of InputDirs is actually where we would like
// to generate conversions, the rest of files are added, because either
// conversion methods are generated there or they contain types
// necessary for conversions.
if !inputs.Has(path) {
continue
}
// Only generate conversions for package which explicitly requested it
// byt setting "+genversion=true" in their doc.go file.
filtered := false
for _, comment := range p.DocComments {
comment := strings.Trim(comment, "//")
if types.ExtractCommentTags("+", comment)["genconversion"] == "true" {
filtered = true
}
}
if !filtered {
continue
}
convertibleType := false
for _, t := range p.Types {
@@ -166,6 +185,10 @@ func Packages(context *generator.Context, arguments *args.GeneratorArgs) generat
// There is no corresponding type in the internal package.
continue
}
// We won't be able to convert to private type.
if namer.IsPrivateGoName(internalType.Name.Name) {
continue
}
// If we can generate conversion in any direction, we should
// generate this package.
if isConvertible(t, internalType, preexisting) || isConvertible(internalType, t, preexisting) {
@@ -308,10 +331,11 @@ func (g *genConversion) Namers(c *generator.Context) namer.NameSystems {
func (g *genConversion) convertibleOnlyWithinPackage(inType, outType *types.Type) bool {
var t *types.Type
var other *types.Type
if inType.Name.Package == g.targetPackage {
t = inType
t, other = inType, outType
} else {
t = outType
t, other = outType, inType
}
if t.Name.Package != g.targetPackage {
@@ -325,7 +349,7 @@ func (g *genConversion) convertibleOnlyWithinPackage(inType, outType *types.Type
return false
}
// Also, filter out private types.
if namer.IsPrivateGoName(t.Name.Name) {
if namer.IsPrivateGoName(other.Name.Name) {
return false
}
return true

View File

@@ -35,6 +35,18 @@ func main() {
arguments.InputDirs = []string{
"k8s.io/kubernetes/pkg/api/v1",
"k8s.io/kubernetes/pkg/api",
"k8s.io/kubernetes/pkg/apis/authorization",
"k8s.io/kubernetes/pkg/apis/authorization/v1beta1",
"k8s.io/kubernetes/pkg/apis/autoscaling",
"k8s.io/kubernetes/pkg/apis/autoscaling/v1",
"k8s.io/kubernetes/pkg/apis/batch",
"k8s.io/kubernetes/pkg/apis/batch/v1",
"k8s.io/kubernetes/pkg/apis/componentconfig",
"k8s.io/kubernetes/pkg/apis/componentconfig/v1alpha1",
"k8s.io/kubernetes/pkg/apis/extensions",
"k8s.io/kubernetes/pkg/apis/extensions/v1beta1",
"k8s.io/kubernetes/pkg/apis/metrics",
"k8s.io/kubernetes/pkg/apis/metrics/v1alpha1",
"k8s.io/kubernetes/pkg/conversion",
"k8s.io/kubernetes/pkg/runtime",
}

View File

@@ -42,7 +42,7 @@ type Builder struct {
fset *token.FileSet
// map of package id to list of parsed files
parsed map[string][]*ast.File
parsed map[string][]parsedFile
// Set by makePackages, used by importer() and friends.
pkgs map[string]*tc.Package
@@ -58,6 +58,12 @@ type Builder struct {
importGraph map[string]map[string]struct{}
}
// parsedFile is for tracking files with name
type parsedFile struct {
name string
file *ast.File
}
// key type for finding comments.
type fileLine struct {
file string
@@ -79,7 +85,7 @@ func New() *Builder {
context: &c,
buildInfo: map[string]*build.Package{},
fset: token.NewFileSet(),
parsed: map[string][]*ast.File{},
parsed: map[string][]parsedFile{},
userRequested: map[string]bool{},
endLineToCommentGroup: map[fileLine]*ast.CommentGroup{},
importGraph: map[string]map[string]struct{}{},
@@ -135,7 +141,7 @@ func (b *Builder) addFile(name string, src []byte, userRequested bool) error {
return err
}
pkg := filepath.Dir(name)
b.parsed[pkg] = append(b.parsed[pkg], p)
b.parsed[pkg] = append(b.parsed[pkg], parsedFile{name, p})
b.userRequested[pkg] = userRequested
for _, c := range p.Comments {
position := b.fset.Position(c.End())
@@ -272,10 +278,14 @@ func (b *Builder) typeCheckPackage(id string) (*tc.Package, error) {
// already processing this package.
return nil, fmt.Errorf("circular dependency for %q", id)
}
files, ok := b.parsed[id]
parsedFiles, ok := b.parsed[id]
if !ok {
return nil, fmt.Errorf("No files for pkg %q: %#v", id, b.parsed)
}
files := make([]*ast.File, len(parsedFiles))
for i := range parsedFiles {
files[i] = parsedFiles[i].file
}
b.pkgs[id] = nil
c := tc.Config{
IgnoreFuncBodies: true,
@@ -325,6 +335,18 @@ func (b *Builder) FindTypes() (types.Universe, error) {
// *packages* they depend on.
continue
}
for _, f := range b.parsed[pkgPath] {
if strings.HasSuffix(f.name, "/doc.go") {
if f.file.Doc != nil {
tp := u.Package(pkgPath)
for _, c := range f.file.Doc.List {
tp.DocComments = append(tp.DocComments, c.Text)
}
}
}
}
s := pkg.Scope()
for _, n := range s.Names() {
obj := s.Lookup(n)

View File

@@ -89,6 +89,9 @@ type Package struct {
// 'package x' line.
Name string
// Comments from doc.go file.
DocComments []string
// Types within this package, indexed by their name (*not* including
// package name).
Types map[string]*Type