cmd/stringer: add -trimprefix option
To trim a string prefix from the names when generating their final strings. Add a simple test too. There is no automatic detection of prefixes for now. That can be added later, building on top of this first simple implementation. Fixes #16539. Change-Id: Ica37273ac74bb0a6cbd43e61823786963d86a492 Reviewed-on: https://go-review.googlesource.com/76650 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
parent
05e91d0638
commit
bce7a99f8a
|
@ -16,18 +16,20 @@ import (
|
||||||
|
|
||||||
// Golden represents a test case.
|
// Golden represents a test case.
|
||||||
type Golden struct {
|
type Golden struct {
|
||||||
name string
|
name string
|
||||||
input string // input; the package clause is provided when running the test.
|
trimPrefix string
|
||||||
output string // exected output.
|
input string // input; the package clause is provided when running the test.
|
||||||
|
output string // exected output.
|
||||||
}
|
}
|
||||||
|
|
||||||
var golden = []Golden{
|
var golden = []Golden{
|
||||||
{"day", day_in, day_out},
|
{"day", "", day_in, day_out},
|
||||||
{"offset", offset_in, offset_out},
|
{"offset", "", offset_in, offset_out},
|
||||||
{"gap", gap_in, gap_out},
|
{"gap", "", gap_in, gap_out},
|
||||||
{"num", num_in, num_out},
|
{"num", "", num_in, num_out},
|
||||||
{"unum", unum_in, unum_out},
|
{"unum", "", unum_in, unum_out},
|
||||||
{"prime", prime_in, prime_out},
|
{"prime", "", prime_in, prime_out},
|
||||||
|
{"prefix", "Type", prefix_in, prefix_out},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Each example starts with "type XXX [u]int", with a single space separating them.
|
// Each example starts with "type XXX [u]int", with a single space separating them.
|
||||||
|
@ -238,9 +240,34 @@ func (i Prime) String() string {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
const prefix_in = `type Type int
|
||||||
|
const (
|
||||||
|
TypeInt Type = iota
|
||||||
|
TypeString
|
||||||
|
TypeFloat
|
||||||
|
TypeRune
|
||||||
|
TypeByte
|
||||||
|
TypeStruct
|
||||||
|
TypeSlice
|
||||||
|
)
|
||||||
|
`
|
||||||
|
|
||||||
|
const prefix_out = `
|
||||||
|
const _Type_name = "IntStringFloatRuneByteStructSlice"
|
||||||
|
|
||||||
|
var _Type_index = [...]uint8{0, 3, 9, 14, 18, 22, 28, 33}
|
||||||
|
|
||||||
|
func (i Type) String() string {
|
||||||
|
if i < 0 || i >= Type(len(_Type_index)-1) {
|
||||||
|
return fmt.Sprintf("Type(%d)", i)
|
||||||
|
}
|
||||||
|
return _Type_name[_Type_index[i]:_Type_index[i+1]]
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
func TestGolden(t *testing.T) {
|
func TestGolden(t *testing.T) {
|
||||||
for _, test := range golden {
|
for _, test := range golden {
|
||||||
var g Generator
|
g := Generator{trimPrefix: test.trimPrefix}
|
||||||
input := "package test\n" + test.input
|
input := "package test\n" + test.input
|
||||||
file := test.name + ".go"
|
file := test.name + ".go"
|
||||||
g.parsePackage(".", []string{file}, input)
|
g.parsePackage(".", []string{file}, input)
|
||||||
|
|
|
@ -78,8 +78,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
typeNames = flag.String("type", "", "comma-separated list of type names; must be set")
|
typeNames = flag.String("type", "", "comma-separated list of type names; must be set")
|
||||||
output = flag.String("output", "", "output file name; default srcdir/<type>_string.go")
|
output = flag.String("output", "", "output file name; default srcdir/<type>_string.go")
|
||||||
|
trimprefix = flag.String("trimprefix", "", "trim the `prefix` from the generated constant names")
|
||||||
)
|
)
|
||||||
|
|
||||||
// Usage is a replacement usage function for the flags package.
|
// Usage is a replacement usage function for the flags package.
|
||||||
|
@ -112,10 +113,8 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the package once.
|
// Parse the package once.
|
||||||
var (
|
var dir string
|
||||||
dir string
|
g := Generator{trimPrefix: *trimprefix}
|
||||||
g Generator
|
|
||||||
)
|
|
||||||
if len(args) == 1 && isDirectory(args[0]) {
|
if len(args) == 1 && isDirectory(args[0]) {
|
||||||
dir = args[0]
|
dir = args[0]
|
||||||
g.parsePackageDir(args[0])
|
g.parsePackageDir(args[0])
|
||||||
|
@ -165,6 +164,8 @@ func isDirectory(name string) bool {
|
||||||
type Generator struct {
|
type Generator struct {
|
||||||
buf bytes.Buffer // Accumulated output.
|
buf bytes.Buffer // Accumulated output.
|
||||||
pkg *Package // Package we are scanning.
|
pkg *Package // Package we are scanning.
|
||||||
|
|
||||||
|
trimPrefix string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Generator) Printf(format string, args ...interface{}) {
|
func (g *Generator) Printf(format string, args ...interface{}) {
|
||||||
|
@ -178,6 +179,8 @@ type File struct {
|
||||||
// These fields are reset for each type being generated.
|
// These fields are reset for each type being generated.
|
||||||
typeName string // Name of the constant type.
|
typeName string // Name of the constant type.
|
||||||
values []Value // Accumulator for constant values of that type.
|
values []Value // Accumulator for constant values of that type.
|
||||||
|
|
||||||
|
trimPrefix string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Package struct {
|
type Package struct {
|
||||||
|
@ -240,8 +243,9 @@ func (g *Generator) parsePackage(directory string, names []string, text interfac
|
||||||
}
|
}
|
||||||
astFiles = append(astFiles, parsedFile)
|
astFiles = append(astFiles, parsedFile)
|
||||||
files = append(files, &File{
|
files = append(files, &File{
|
||||||
file: parsedFile,
|
file: parsedFile,
|
||||||
pkg: g.pkg,
|
pkg: g.pkg,
|
||||||
|
trimPrefix: g.trimPrefix,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if len(astFiles) == 0 {
|
if len(astFiles) == 0 {
|
||||||
|
@ -453,6 +457,7 @@ func (f *File) genDecl(node ast.Node) bool {
|
||||||
signed: info&types.IsUnsigned == 0,
|
signed: info&types.IsUnsigned == 0,
|
||||||
str: value.String(),
|
str: value.String(),
|
||||||
}
|
}
|
||||||
|
v.name = strings.TrimPrefix(v.name, f.trimPrefix)
|
||||||
f.values = append(f.values, v)
|
f.values = append(f.values, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue