Merge pull request #17009 from wojtek-t/raw_namer_with_package

Auto commit by PR queue bot
This commit is contained in:
k8s-merge-robot 2015-11-11 03:26:14 -08:00
commit 4b8f232ec4
4 changed files with 38 additions and 12 deletions

View File

@ -56,6 +56,9 @@ func NewPrivateNamer(prependPackageNames int, ignoreWords ...string) *NameStrate
// NewRawNamer will return a Namer that makes a name by which you would // NewRawNamer will return a Namer that makes a name by which you would
// directly refer to a type, optionally keeping track of the import paths // directly refer to a type, optionally keeping track of the import paths
// necessary to reference the names it provides. Tracker may be nil. // necessary to reference the names it provides. Tracker may be nil.
// The 'pkg' is the full package name, in which the Namer is used - all
// types from that package will be referenced by just type name without
// referencing the package.
// //
// For example, if the type is map[string]int, a raw namer will literally // For example, if the type is map[string]int, a raw namer will literally
// return "map[string]int". // return "map[string]int".
@ -63,8 +66,8 @@ func NewPrivateNamer(prependPackageNames int, ignoreWords ...string) *NameStrate
// Or if the type, in package foo, is "type Bar struct { ... }", then the raw // Or if the type, in package foo, is "type Bar struct { ... }", then the raw
// namer will return "foo.Bar" as the name of the type, and if 'tracker' was // namer will return "foo.Bar" as the name of the type, and if 'tracker' was
// not nil, will record that package foo needs to be imported. // not nil, will record that package foo needs to be imported.
func NewRawNamer(tracker ImportTracker) *rawNamer { func NewRawNamer(pkg string, tracker ImportTracker) *rawNamer {
return &rawNamer{tracker: tracker} return &rawNamer{pkg: pkg, tracker: tracker}
} }
// Names is a map from Type to name, as defined by some Namer. // Names is a map from Type to name, as defined by some Namer.
@ -273,6 +276,7 @@ type ImportTracker interface {
} }
type rawNamer struct { type rawNamer struct {
pkg string
tracker ImportTracker tracker ImportTracker
Names Names
} }
@ -291,9 +295,17 @@ func (r *rawNamer) Name(t *types.Type) string {
var name string var name string
if r.tracker != nil { if r.tracker != nil {
r.tracker.AddType(t) r.tracker.AddType(t)
name = r.tracker.LocalNameOf(t.Name.Package) + "." + t.Name.Name if t.Name.Package == r.pkg {
name = t.Name.Name
} else {
name = r.tracker.LocalNameOf(t.Name.Package) + "." + t.Name.Name
}
} else { } else {
name = filepath.Base(t.Name.Package) + "." + t.Name.Name if t.Name.Package == r.pkg {
name = t.Name.Name
} else {
name = filepath.Base(t.Name.Package) + "." + t.Name.Name
}
} }
r.Names[t] = name r.Names[t] = name
return name return name

View File

@ -59,7 +59,7 @@ func TestNameStrategy(t *testing.T) {
t.Errorf("Wanted %#v, got %#v", e, a) t.Errorf("Wanted %#v, got %#v", e, a)
} }
o = Orderer{NewRawNamer(nil)} o = Orderer{NewRawNamer("my/package", nil)}
order = o.Order(u) order = o.Order(u)
orderedNames = make([]string, len(order)) orderedNames = make([]string, len(order))
for i, t := range order { for i, t := range order {
@ -71,6 +71,18 @@ func TestNameStrategy(t *testing.T) {
t.Errorf("Wanted %#v, got %#v", e, a) t.Errorf("Wanted %#v, got %#v", e, a)
} }
o = Orderer{NewRawNamer("foo/bar", nil)}
order = o.Order(u)
orderedNames = make([]string, len(order))
for i, t := range order {
orderedNames[i] = o.Name(t)
}
expect = []string{"Baz", "[]Baz", "map[string]Baz", "other.Baz", "string"}
if e, a := expect, orderedNames; !reflect.DeepEqual(e, a) {
t.Errorf("Wanted %#v, got %#v", e, a)
}
o = Orderer{NewPublicNamer(1)} o = Orderer{NewPublicNamer(1)}
order = o.Order(u) order = o.Order(u)
orderedNames = make([]string, len(order)) orderedNames = make([]string, len(order))

View File

@ -302,7 +302,7 @@ type Interface interface{Method(a, b string) (c, d string)}
namer.NewPublicNamer(1), namer.NewPublicNamer(1),
namer.NewPrivateNamer(0), namer.NewPrivateNamer(0),
namer.NewPrivateNamer(1), namer.NewPrivateNamer(1),
namer.NewRawNamer(nil), namer.NewRawNamer("", nil),
} }
for nameIndex, namer := range namers { for nameIndex, namer := range namers {

View File

@ -35,7 +35,7 @@ func NameSystems() namer.NameSystems {
return namer.NameSystems{ return namer.NameSystems{
"public": namer.NewPublicNamer(0), "public": namer.NewPublicNamer(0),
"private": namer.NewPrivateNamer(0), "private": namer.NewPrivateNamer(0),
"raw": namer.NewRawNamer(nil), "raw": namer.NewRawNamer("", nil),
} }
} }
@ -90,8 +90,9 @@ func Packages(_ *generator.Context, arguments *args.GeneratorArgs) generator.Pac
// names? // names?
OptionalName: c.Namers["private"].Name(t), OptionalName: c.Namers["private"].Name(t),
}, },
typeToMatch: t, outputPackage: arguments.OutputPackagePath,
imports: generator.NewImportTracker(), typeToMatch: t,
imports: generator.NewImportTracker(),
}) })
} }
return generators return generators
@ -121,8 +122,9 @@ func Packages(_ *generator.Context, arguments *args.GeneratorArgs) generator.Pac
// genSet produces a file with a set for a single type. // genSet produces a file with a set for a single type.
type genSet struct { type genSet struct {
generator.DefaultGen generator.DefaultGen
typeToMatch *types.Type outputPackage string
imports *generator.ImportTracker typeToMatch *types.Type
imports *generator.ImportTracker
} }
// Filter ignores all but one type because we're making a single file per type. // Filter ignores all but one type because we're making a single file per type.
@ -130,7 +132,7 @@ func (g *genSet) Filter(c *generator.Context, t *types.Type) bool { return t ==
func (g *genSet) Namers(c *generator.Context) namer.NameSystems { func (g *genSet) Namers(c *generator.Context) namer.NameSystems {
return namer.NameSystems{ return namer.NameSystems{
"raw": namer.NewRawNamer(g.imports), "raw": namer.NewRawNamer(g.outputPackage, g.imports),
} }
} }