Skip to content

Commit

Permalink
split Package.Types to Types, Variables and Functions;
Browse files Browse the repository at this point in the history
add DeclarationOf Kind.
  • Loading branch information
Chao Xu committed Dec 3, 2015
1 parent 6117707 commit 0d7d4c0
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 53 deletions.
12 changes: 6 additions & 6 deletions cmd/libs/go2idl/client-gen/generators/generator-for-group.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ func (g *genGroup) GenerateType(c *generator.Context, t *types.Type, w io.Writer
"group": g.group,
"Group": namer.IC(g.group),
"types": g.types,
"Config": c.Universe.Get(types.Name{Package: pkgUnversioned, Name: "Config"}),
"DefaultKubernetesUserAgent": c.Universe.Get(types.Name{Package: pkgUnversioned, Name: "DefaultKubernetesUserAgent"}),
"RESTClient": c.Universe.Get(types.Name{Package: pkgUnversioned, Name: "RESTClient"}),
"RESTClientFor": c.Universe.Get(types.Name{Package: pkgUnversioned, Name: "RESTClientFor"}),
"latestGroup": c.Universe.Get(types.Name{Package: pkgLatest, Name: "Group"}),
"GroupOrDie": c.Universe.Get(types.Name{Package: pkgLatest, Name: "GroupOrDie"}),
"Config": c.Universe.Type(types.Name{Package: pkgUnversioned, Name: "Config"}),
"DefaultKubernetesUserAgent": c.Universe.Function(types.Name{Package: pkgUnversioned, Name: "DefaultKubernetesUserAgent"}),
"RESTClient": c.Universe.Type(types.Name{Package: pkgUnversioned, Name: "RESTClient"}),
"RESTClientFor": c.Universe.Function(types.Name{Package: pkgUnversioned, Name: "RESTClientFor"}),
"latestGroup": c.Universe.Variable(types.Name{Package: pkgLatest, Name: "Group"}),
"GroupOrDie": c.Universe.Variable(types.Name{Package: pkgLatest, Name: "GroupOrDie"}),
}
sw.Do(groupInterfaceTemplate, m)
sw.Do(groupClientTemplate, m)
Expand Down
6 changes: 3 additions & 3 deletions cmd/libs/go2idl/client-gen/generators/generator-for-type.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ func (g *genClientForType) GenerateType(c *generator.Context, t *types.Type, w i
"type": t,
"package": pkg,
"Package": namer.IC(pkg),
"watchInterface": c.Universe.Get(types.Name{Package: "k8s.io/kubernetes/pkg/watch", Name: "Interface"}),
"apiDeleteOptions": c.Universe.Get(types.Name{Package: "k8s.io/kubernetes/pkg/api", Name: "DeleteOptions"}),
"unvListOptions": c.Universe.Get(types.Name{Package: "k8s.io/kubernetes/pkg/api/unversioned", Name: "ListOptions"}),
"watchInterface": c.Universe.Type(types.Name{Package: "k8s.io/kubernetes/pkg/watch", Name: "Interface"}),
"apiDeleteOptions": c.Universe.Type(types.Name{Package: "k8s.io/kubernetes/pkg/api", Name: "DeleteOptions"}),
"unvListOptions": c.Universe.Type(types.Name{Package: "k8s.io/kubernetes/pkg/api/unversioned", Name: "ListOptions"}),
}
sw.Do(namespacerTemplate, m)
sw.Do(interfaceTemplate, m)
Expand Down
10 changes: 5 additions & 5 deletions cmd/libs/go2idl/namer/namer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,26 @@ func TestNameStrategy(t *testing.T) {
u := types.Universe{}

// Add some types.
base := u.Get(types.Name{Package: "foo/bar", Name: "Baz"})
base := u.Type(types.Name{Package: "foo/bar", Name: "Baz"})
base.Kind = types.Struct

tmp := u.Get(types.Name{Package: "", Name: "[]bar.Baz"})
tmp := u.Type(types.Name{Package: "", Name: "[]bar.Baz"})
tmp.Kind = types.Slice
tmp.Elem = base

tmp = u.Get(types.Name{Package: "", Name: "map[string]bar.Baz"})
tmp = u.Type(types.Name{Package: "", Name: "map[string]bar.Baz"})
tmp.Kind = types.Map
tmp.Key = types.String
tmp.Elem = base

tmp = u.Get(types.Name{Package: "foo/other", Name: "Baz"})
tmp = u.Type(types.Name{Package: "foo/other", Name: "Baz"})
tmp.Kind = types.Struct
tmp.Members = []types.Member{{
Embedded: true,
Type: base,
}}

u.Get(types.Name{Package: "", Name: "string"})
u.Type(types.Name{Package: "", Name: "string"})

o := Orderer{NewPublicNamer(0)}
order := o.Order(u)
Expand Down
6 changes: 6 additions & 0 deletions cmd/libs/go2idl/namer/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ func (o *Orderer) Order(u types.Universe) []*types.Type {
for _, t := range p.Types {
list.types = append(list.types, t)
}
for _, f := range p.Functions {
list.types = append(list.types, f)
}
for _, v := range p.Variables {
list.types = append(list.types, v)
}
}
sort.Sort(list)
return list.types
Expand Down
59 changes: 41 additions & 18 deletions cmd/libs/go2idl/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,13 @@ func (b *Builder) FindTypes() (types.Universe, error) {
t.CommentLines = b.priorCommentLines(obj.Pos())
}
tf, ok := obj.(*tc.Func)
if ok {
b.addFunc(u, nil, tf)
// We only care about functions, not concrete/abstract methods.
if ok && tf.Type() != nil && tf.Type().(*tc.Signature).Recv() == nil {
b.addFunction(u, nil, tf)
}
tv, ok := obj.(*tc.Var)
if ok && !tv.IsField() {
b.addVariable(u, nil, tv)
}
}
for p := range b.importGraph[pkgName] {
Expand All @@ -330,6 +335,13 @@ func tcFuncNameToName(in string) types.Name {
return tcNameToName(nameParts[0])
}

func tcVarNameToName(in string) types.Name {
nameParts := strings.Split(in, " ")
// nameParts[0] is "var".
// nameParts[2:] is the type of the variable, we ignore it for now.
return tcNameToName(nameParts[1])
}

func tcNameToName(in string) types.Name {
// Detect anonymous type names. (These may have '.' characters because
// embedded types may have packages, so we detect them specially.)
Expand Down Expand Up @@ -377,7 +389,7 @@ func (b *Builder) walkType(u types.Universe, useName *types.Name, in tc.Type) *t

switch t := in.(type) {
case *tc.Struct:
out := u.Get(name)
out := u.Type(name)
if out.Kind != types.Unknown {
return out
}
Expand All @@ -395,7 +407,7 @@ func (b *Builder) walkType(u types.Universe, useName *types.Name, in tc.Type) *t
}
return out
case *tc.Map:
out := u.Get(name)
out := u.Type(name)
if out.Kind != types.Unknown {
return out
}
Expand All @@ -404,23 +416,23 @@ func (b *Builder) walkType(u types.Universe, useName *types.Name, in tc.Type) *t
out.Key = b.walkType(u, nil, t.Key())
return out
case *tc.Pointer:
out := u.Get(name)
out := u.Type(name)
if out.Kind != types.Unknown {
return out
}
out.Kind = types.Pointer
out.Elem = b.walkType(u, nil, t.Elem())
return out
case *tc.Slice:
out := u.Get(name)
out := u.Type(name)
if out.Kind != types.Unknown {
return out
}
out.Kind = types.Slice
out.Elem = b.walkType(u, nil, t.Elem())
return out
case *tc.Array:
out := u.Get(name)
out := u.Type(name)
if out.Kind != types.Unknown {
return out
}
Expand All @@ -430,7 +442,7 @@ func (b *Builder) walkType(u types.Universe, useName *types.Name, in tc.Type) *t
// cannot be properly written.
return out
case *tc.Chan:
out := u.Get(name)
out := u.Type(name)
if out.Kind != types.Unknown {
return out
}
Expand All @@ -440,7 +452,7 @@ func (b *Builder) walkType(u types.Universe, useName *types.Name, in tc.Type) *t
// cannot be properly written.
return out
case *tc.Basic:
out := u.Get(types.Name{
out := u.Type(types.Name{
Package: "",
Name: t.Name(),
})
Expand All @@ -450,15 +462,15 @@ func (b *Builder) walkType(u types.Universe, useName *types.Name, in tc.Type) *t
out.Kind = types.Unsupported
return out
case *tc.Signature:
out := u.Get(name)
out := u.Type(name)
if out.Kind != types.Unknown {
return out
}
out.Kind = types.Func
out.Signature = b.convertSignature(u, t)
return out
case *tc.Interface:
out := u.Get(name)
out := u.Type(name)
if out.Kind != types.Unknown {
return out
}
Expand All @@ -472,7 +484,7 @@ func (b *Builder) walkType(u types.Universe, useName *types.Name, in tc.Type) *t
switch t.Underlying().(type) {
case *tc.Named, *tc.Basic:
name := tcNameToName(t.String())
out := u.Get(name)
out := u.Type(name)
if out.Kind != types.Unknown {
return out
}
Expand All @@ -485,7 +497,7 @@ func (b *Builder) walkType(u types.Universe, useName *types.Name, in tc.Type) *t
// "feature" for users. This flattens those types
// together.
name := tcNameToName(t.String())
if out := u.Get(name); out.Kind != types.Unknown {
if out := u.Type(name); out.Kind != types.Unknown {
return out // short circuit if we've already made this.
}
out := b.walkType(u, &name, t.Underlying())
Expand All @@ -500,7 +512,7 @@ func (b *Builder) walkType(u types.Universe, useName *types.Name, in tc.Type) *t
return out
}
default:
out := u.Get(name)
out := u.Type(name)
if out.Kind != types.Unknown {
return out
}
Expand All @@ -510,13 +522,24 @@ func (b *Builder) walkType(u types.Universe, useName *types.Name, in tc.Type) *t
}
}

func (b *Builder) addFunc(u types.Universe, useName *types.Name, in *tc.Func) *types.Type {
func (b *Builder) addFunction(u types.Universe, useName *types.Name, in *tc.Func) *types.Type {
name := tcFuncNameToName(in.String())
if useName != nil {
name = *useName
}
out := u.Get(name)
out.Kind = types.Func
out.Signature = b.convertSignature(u, in.Type().(*tc.Signature))
out := u.Function(name)
out.Kind = types.DeclarationOf
out.Underlying = b.walkType(u, nil, in.Type())
return out
}

func (b *Builder) addVariable(u types.Universe, useName *types.Name, in *tc.Var) *types.Type {
name := tcVarNameToName(in.String())
if useName != nil {
name = *useName
}
out := u.Variable(name)
out.Kind = types.DeclarationOf
out.Underlying = b.walkType(u, nil, in.Type())
return out
}
30 changes: 22 additions & 8 deletions cmd/libs/go2idl/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ type Object struct {
func AFunc(obj1 common.Object, obj2 Object) Frobber {
}
var AVar Frobber
var (
AnotherVar = Frobber{}
)
`,
"base/common/proto/common.go": `
package common
Expand All @@ -91,16 +96,21 @@ package o
}
{{end}}
{{define "Func"}}{{$s := .Signature}}func {{Raw .}}( {{range $s.Parameters}}{{Raw .}} {{end}}) {{range $s.Results}}{{Raw .}} {{end}}{}
{{define "Func"}}{{$s := .Underlying.Signature}}var {{Name .}} func({{range $index,$elem := $s.Parameters}}{{if $index}}, {{end}}{{Raw $elem}}{{end}}) {{if $s.Results|len |gt 1}}({{end}}{{range $index,$elem := $s.Results}}{{if $index}}, {{end}}{{Raw .}}{{end}}{{if $s.Results|len |gt 1}}){{end}} = {{Raw .}}
{{end}}
{{define "Var"}}{{$t := .Underlying}}var {{Name .}} {{Raw $t}} = {{Raw .}}
{{end}}
{{range $t := .}}{{if eq $t.Kind "Struct"}}{{template "Struct" $t}}{{end}}{{end}}
{{range $t := .}}{{if eq $t.Kind "Func"}}{{template "Func" $t}}{{end}}{{end}}`
{{range $t := .}}{{if eq $t.Kind "DeclarationOf"}}{{if eq $t.Underlying.Kind "Func"}}{{template "Func" $t}}{{end}}{{end}}{{end}}
{{range $t := .}}{{if eq $t.Kind "DeclarationOf"}}{{if ne $t.Underlying.Kind "Func"}}{{template "Var" $t}}{{end}}{{end}}{{end}}`

var expect = `
package o
type CommonObject interface {
ID() Int64
SetID(Int64)
Expand Down Expand Up @@ -128,7 +138,12 @@ type FooObject interface {
}
func proto.AFunc( proto.Object proto.Object ) proto.Frobber {}
var FooAFunc func(proto.Object, proto.Object) proto.Frobber = proto.AFunc
var FooAVar proto.Frobber = proto.AVar
var FooAnotherVar proto.Frobber = proto.AnotherVar
`
testNamer := namer.NewPublicNamer(1, "proto")
Expand All @@ -149,7 +164,6 @@ func proto.AFunc( proto.Object proto.Object ) proto.Frobber {}
if e, a := expect, buf.String(); e != a {
t.Errorf("Wanted, got:\n%v\n-----\n%v\n", e, a)
}

if p := u.Package("base/foo/proto"); !p.HasImport("base/common/proto") {
t.Errorf("Unexpected lack of import line: %#s", p.Imports)
}
Expand All @@ -175,7 +189,7 @@ type Blah struct {

_, u, o := construct(t, structTest, namer.NewPublicNamer(0))
t.Logf("%#v", o)
blahT := u.Get(types.Name{Package: "base/foo/proto", Name: "Blah"})
blahT := u.Type(types.Name{Package: "base/foo/proto", Name: "Blah"})
if blahT == nil {
t.Fatal("type not found")
}
Expand Down Expand Up @@ -330,7 +344,7 @@ type Interface interface{Method(a, b string) (c, d string)}

for _, item := range assertions {
n := types.Name{Package: item.Package, Name: item.Name}
thisType := u.Get(n)
thisType := u.Type(n)
if thisType == nil {
t.Errorf("type %s not found", n)
continue
Expand All @@ -344,11 +358,11 @@ type Interface interface{Method(a, b string) (c, d string)}
}

// Also do some one-off checks
gtest := u.Get(types.Name{Package: "g", Name: "Test"})
gtest := u.Type(types.Name{Package: "g", Name: "Test"})
if e, a := 1, len(gtest.Methods); e != a {
t.Errorf("expected %v but found %v methods: %#v", e, a, gtest)
}
iface := u.Get(types.Name{Package: "g", Name: "Interface"})
iface := u.Type(types.Name{Package: "g", Name: "Interface"})
if e, a := 1, len(iface.Methods); e != a {
t.Errorf("expected %v but found %v methods: %#v", e, a, iface)
}
Expand Down
Loading

0 comments on commit 0d7d4c0

Please sign in to comment.