mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-04 04:08:16 +00:00 
			
		
		
		
	Support current package in RawNamer.
This commit is contained in:
		@@ -56,6 +56,9 @@ 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".
 | 
			
		||||
@@ -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
 | 
			
		||||
// 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.
 | 
			
		||||
@@ -273,6 +276,7 @@ type ImportTracker interface {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type rawNamer struct {
 | 
			
		||||
	pkg     string
 | 
			
		||||
	tracker ImportTracker
 | 
			
		||||
	Names
 | 
			
		||||
}
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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 {
 | 
			
		||||
@@ -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))
 | 
			
		||||
 
 | 
			
		||||
@@ -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 {
 | 
			
		||||
 
 | 
			
		||||
@@ -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),
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -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
 | 
			
		||||
@@ -121,8 +122,9 @@ 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.
 | 
			
		||||
@@ -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 {
 | 
			
		||||
	return namer.NameSystems{
 | 
			
		||||
		"raw": namer.NewRawNamer(g.imports),
 | 
			
		||||
		"raw": namer.NewRawNamer(g.outputPackage, g.imports),
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user