Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support current package in RawNamer. #17009

Merged
merged 1 commit into from
Nov 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 16 additions & 4 deletions cmd/libs/go2idl/namer/namer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,18 @@ func NewPrivateNamer(prependPackageNames int, ignoreWords ...string) *NameStrate
// 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
// 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
// return "map[string]int".
//
// 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
// not nil, will record that package foo needs to be imported.
func NewRawNamer(tracker ImportTracker) *rawNamer {
return &rawNamer{tracker: tracker}
func NewRawNamer(pkg string, tracker ImportTracker) *rawNamer {
return &rawNamer{pkg: pkg, tracker: tracker}
}

// Names is a map from Type to name, as defined by some Namer.
Expand Down Expand Up @@ -273,6 +276,7 @@ type ImportTracker interface {
}

type rawNamer struct {
pkg string
tracker ImportTracker
Names
}
Expand All @@ -291,9 +295,17 @@ func (r *rawNamer) Name(t *types.Type) string {
var name string
if r.tracker != nil {
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 {
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
return name
Expand Down
14 changes: 13 additions & 1 deletion cmd/libs/go2idl/namer/namer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestNameStrategy(t *testing.T) {
t.Errorf("Wanted %#v, got %#v", e, a)
}

o = Orderer{NewRawNamer(nil)}
o = Orderer{NewRawNamer("my/package", nil)}
order = o.Order(u)
orderedNames = make([]string, len(order))
for i, t := range order {
Expand All @@ -71,6 +71,18 @@ func TestNameStrategy(t *testing.T) {
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)}
order = o.Order(u)
orderedNames = make([]string, len(order))
Expand Down
2 changes: 1 addition & 1 deletion cmd/libs/go2idl/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ type Interface interface{Method(a, b string) (c, d string)}
namer.NewPublicNamer(1),
namer.NewPrivateNamer(0),
namer.NewPrivateNamer(1),
namer.NewRawNamer(nil),
namer.NewRawNamer("", nil),
}

for nameIndex, namer := range namers {
Expand Down
14 changes: 8 additions & 6 deletions cmd/libs/go2idl/set-gen/generators/sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NameSystems() namer.NameSystems {
return namer.NameSystems{
"public": namer.NewPublicNamer(0),
"private": namer.NewPrivateNamer(0),
"raw": namer.NewRawNamer(nil),
"raw": namer.NewRawNamer("", nil),
}
}

Expand Down Expand Up @@ -90,8 +90,9 @@ func Packages(_ *generator.Context, arguments *args.GeneratorArgs) generator.Pac
// names?
OptionalName: c.Namers["private"].Name(t),
},
typeToMatch: t,
imports: generator.NewImportTracker(),
outputPackage: arguments.OutputPackagePath,
typeToMatch: t,
imports: generator.NewImportTracker(),
})
}
return generators
Expand Down Expand Up @@ -121,16 +122,17 @@ func Packages(_ *generator.Context, arguments *args.GeneratorArgs) generator.Pac
// genSet produces a file with a set for a single type.
type genSet struct {
generator.DefaultGen
typeToMatch *types.Type
imports *generator.ImportTracker
outputPackage string
typeToMatch *types.Type
imports *generator.ImportTracker
}

// Filter ignores all but one type because we're making a single file per type.
func (g *genSet) Filter(c *generator.Context, t *types.Type) bool { return t == g.typeToMatch }

func (g *genSet) Namers(c *generator.Context) namer.NameSystems {
return namer.NameSystems{
"raw": namer.NewRawNamer(g.imports),
"raw": namer.NewRawNamer(g.outputPackage, g.imports),
}
}

Expand Down