forked from mirror/enumer
Add withprefix flag
This commit is contained in:
parent
42bd8ecbf7
commit
ea11b04142
|
@ -158,6 +158,9 @@ The default value for `transform` flag is `noop` which means no transformation w
|
||||||
If a prefix is provided via the `trimprefix` flag, it will be trimmed from the start of each name (before
|
If a prefix is provided via the `trimprefix` flag, it will be trimmed from the start of each name (before
|
||||||
it is transformed). If a name doesn't have the prefix it will be passed unchanged.
|
it is transformed). If a name doesn't have the prefix it will be passed unchanged.
|
||||||
|
|
||||||
|
If a prefix is provided via the `addprefix` flag, it will be added to the start of each name (before
|
||||||
|
it is transformed and after trimming).
|
||||||
|
|
||||||
## Inspiring projects
|
## Inspiring projects
|
||||||
|
|
||||||
- [Stringer](https://godoc.org/golang.org/x/tools/cmd/stringer)
|
- [Stringer](https://godoc.org/golang.org/x/tools/cmd/stringer)
|
||||||
|
|
|
@ -49,8 +49,12 @@ var goldenJSONAndSQL = []Golden{
|
||||||
{"prime", primeJsonAndSqlIn, primeJsonAndSqlOut},
|
{"prime", primeJsonAndSqlIn, primeJsonAndSqlOut},
|
||||||
}
|
}
|
||||||
|
|
||||||
var goldenPrefix = []Golden{
|
var goldenTrimPrefix = []Golden{
|
||||||
{"prefix", prefixIn, dayOut},
|
{"trimprefix", trimPrefixIn, dayOut},
|
||||||
|
}
|
||||||
|
|
||||||
|
var goldenWithPrefix = []Golden{
|
||||||
|
{"withprefix", dayIn, prefixedDayOut},
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
||||||
|
@ -117,6 +121,55 @@ func (i Day) IsADay() bool {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
const prefixedDayOut = `
|
||||||
|
const _DayName = "DayMondayDayTuesdayDayWednesdayDayThursdayDayFridayDaySaturdayDaySunday"
|
||||||
|
|
||||||
|
var _DayIndex = [...]uint8{0, 9, 19, 31, 42, 51, 62, 71}
|
||||||
|
|
||||||
|
func (i Day) String() string {
|
||||||
|
if i < 0 || i >= Day(len(_DayIndex)-1) {
|
||||||
|
return fmt.Sprintf("Day(%d)", i)
|
||||||
|
}
|
||||||
|
return _DayName[_DayIndex[i]:_DayIndex[i+1]]
|
||||||
|
}
|
||||||
|
|
||||||
|
var _DayValues = []Day{0, 1, 2, 3, 4, 5, 6}
|
||||||
|
|
||||||
|
var _DayNameToValueMap = map[string]Day{
|
||||||
|
_DayName[0:9]: 0,
|
||||||
|
_DayName[9:19]: 1,
|
||||||
|
_DayName[19:31]: 2,
|
||||||
|
_DayName[31:42]: 3,
|
||||||
|
_DayName[42:51]: 4,
|
||||||
|
_DayName[51:62]: 5,
|
||||||
|
_DayName[62:71]: 6,
|
||||||
|
}
|
||||||
|
|
||||||
|
// DayString retrieves an enum value from the enum constants string name.
|
||||||
|
// Throws an error if the param is not part of the enum.
|
||||||
|
func DayString(s string) (Day, error) {
|
||||||
|
if val, ok := _DayNameToValueMap[s]; ok {
|
||||||
|
return val, nil
|
||||||
|
}
|
||||||
|
return 0, fmt.Errorf("%s does not belong to Day values", s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DayValues returns all values of the enum
|
||||||
|
func DayValues() []Day {
|
||||||
|
return _DayValues
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsADay returns "true" if the value is listed in the enum definition. "false" otherwise
|
||||||
|
func (i Day) IsADay() bool {
|
||||||
|
for _, v := range _DayValues {
|
||||||
|
if i == v {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
`
|
||||||
|
|
||||||
// Enumeration with an offset.
|
// Enumeration with an offset.
|
||||||
// Also includes a duplicate.
|
// Also includes a duplicate.
|
||||||
const offsetIn = `type Number int
|
const offsetIn = `type Number int
|
||||||
|
@ -1010,7 +1063,7 @@ func (i *Prime) Scan(value interface{}) error {
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
const prefixIn = `type Day int
|
const trimPrefixIn = `type Day int
|
||||||
const (
|
const (
|
||||||
DayMonday Day = iota
|
DayMonday Day = iota
|
||||||
DayTuesday
|
DayTuesday
|
||||||
|
@ -1024,29 +1077,32 @@ const (
|
||||||
|
|
||||||
func TestGolden(t *testing.T) {
|
func TestGolden(t *testing.T) {
|
||||||
for _, test := range golden {
|
for _, test := range golden {
|
||||||
runGoldenTest(t, test, false, false, false, false, "")
|
runGoldenTest(t, test, false, false, false, false, "", "")
|
||||||
}
|
}
|
||||||
for _, test := range goldenJSON {
|
for _, test := range goldenJSON {
|
||||||
runGoldenTest(t, test, true, false, false, false, "")
|
runGoldenTest(t, test, true, false, false, false, "", "")
|
||||||
}
|
}
|
||||||
for _, test := range goldenText {
|
for _, test := range goldenText {
|
||||||
runGoldenTest(t, test, false, false, false, true, "")
|
runGoldenTest(t, test, false, false, false, true, "", "")
|
||||||
}
|
}
|
||||||
for _, test := range goldenYAML {
|
for _, test := range goldenYAML {
|
||||||
runGoldenTest(t, test, false, true, false, false, "")
|
runGoldenTest(t, test, false, true, false, false, "", "")
|
||||||
}
|
}
|
||||||
for _, test := range goldenSQL {
|
for _, test := range goldenSQL {
|
||||||
runGoldenTest(t, test, false, false, true, false, "")
|
runGoldenTest(t, test, false, false, true, false, "", "")
|
||||||
}
|
}
|
||||||
for _, test := range goldenJSONAndSQL {
|
for _, test := range goldenJSONAndSQL {
|
||||||
runGoldenTest(t, test, true, false, true, false, "")
|
runGoldenTest(t, test, true, false, true, false, "", "")
|
||||||
}
|
}
|
||||||
for _, test := range goldenPrefix {
|
for _, test := range goldenTrimPrefix {
|
||||||
runGoldenTest(t, test, false, false, false, false, "Day")
|
runGoldenTest(t, test, false, false, false, false, "Day", "")
|
||||||
|
}
|
||||||
|
for _, test := range goldenWithPrefix {
|
||||||
|
runGoldenTest(t, test, false, false, false, false, "", "Day")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func runGoldenTest(t *testing.T, test Golden, generateJSON, generateYAML, generateSQL, generateText bool, prefix string) {
|
func runGoldenTest(t *testing.T, test Golden, generateJSON, generateYAML, generateSQL, generateText bool, trimPrefix string, prefix string) {
|
||||||
var g Generator
|
var g Generator
|
||||||
input := "package test\n" + test.input
|
input := "package test\n" + test.input
|
||||||
file := test.name + ".go"
|
file := test.name + ".go"
|
||||||
|
@ -1056,7 +1112,7 @@ func runGoldenTest(t *testing.T, test Golden, generateJSON, generateYAML, genera
|
||||||
if len(tokens) != 3 {
|
if len(tokens) != 3 {
|
||||||
t.Fatalf("%s: need type declaration on first line", test.name)
|
t.Fatalf("%s: need type declaration on first line", test.name)
|
||||||
}
|
}
|
||||||
g.generate(tokens[1], generateJSON, generateYAML, generateSQL, generateText, "noop", prefix)
|
g.generate(tokens[1], generateJSON, generateYAML, generateSQL, generateText, "noop", trimPrefix, prefix)
|
||||||
got := string(g.format())
|
got := string(g.format())
|
||||||
if got != test.output {
|
if got != test.output {
|
||||||
t.Errorf("%s: got\n====\n%s====\nexpected\n====%s", test.name, got, test.output)
|
t.Errorf("%s: got\n====\n%s====\nexpected\n====%s", test.name, got, test.output)
|
||||||
|
|
15
stringer.go
15
stringer.go
|
@ -41,6 +41,7 @@ var (
|
||||||
output = flag.String("output", "", "output file name; default srcdir/<type>_string.go")
|
output = flag.String("output", "", "output file name; default srcdir/<type>_string.go")
|
||||||
transformMethod = flag.String("transform", "noop", "enum item name transformation method. Default: noop")
|
transformMethod = flag.String("transform", "noop", "enum item name transformation method. Default: noop")
|
||||||
trimPrefix = flag.String("trimprefix", "", "transform each item name by removing a prefix. Default: \"\"")
|
trimPrefix = flag.String("trimprefix", "", "transform each item name by removing a prefix. Default: \"\"")
|
||||||
|
addPrefix = flag.String("addprefix", "", "transform each item name by adding a prefix. Default: \"\"")
|
||||||
)
|
)
|
||||||
|
|
||||||
// Usage is a replacement usage function for the flags package.
|
// Usage is a replacement usage function for the flags package.
|
||||||
|
@ -103,7 +104,7 @@ func main() {
|
||||||
|
|
||||||
// Run generate for each type.
|
// Run generate for each type.
|
||||||
for _, typeName := range types {
|
for _, typeName := range types {
|
||||||
g.generate(typeName, *json, *yaml, *sql, *text, *transformMethod, *trimPrefix)
|
g.generate(typeName, *json, *yaml, *sql, *text, *transformMethod, *trimPrefix, *addPrefix)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format the output.
|
// Format the output.
|
||||||
|
@ -302,8 +303,16 @@ func (g *Generator) trimValueNames(values []Value, prefix string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// prefixValueNames adds a prefix to each name
|
||||||
|
func (g *Generator) prefixValueNames(values []Value, prefix string) {
|
||||||
|
for i := range values {
|
||||||
|
values[i].name = prefix + values[i].name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// generate produces the String method for the named type.
|
// generate produces the String method for the named type.
|
||||||
func (g *Generator) generate(typeName string, includeJSON, includeYAML, includeSQL, includeText bool, transformMethod string, trimPrefix string) {
|
func (g *Generator) generate(typeName string, includeJSON, includeYAML, includeSQL, includeText bool,
|
||||||
|
transformMethod string, trimPrefix string, addPrefix string) {
|
||||||
values := make([]Value, 0, 100)
|
values := make([]Value, 0, 100)
|
||||||
for _, file := range g.pkg.files {
|
for _, file := range g.pkg.files {
|
||||||
// Set the state for this run of the walker.
|
// Set the state for this run of the walker.
|
||||||
|
@ -321,6 +330,8 @@ func (g *Generator) generate(typeName string, includeJSON, includeYAML, includeS
|
||||||
|
|
||||||
g.trimValueNames(values, trimPrefix)
|
g.trimValueNames(values, trimPrefix)
|
||||||
|
|
||||||
|
g.prefixValueNames(values, addPrefix)
|
||||||
|
|
||||||
g.transformValueNames(values, transformMethod)
|
g.transformValueNames(values, transformMethod)
|
||||||
|
|
||||||
runs := splitIntoRuns(values)
|
runs := splitIntoRuns(values)
|
||||||
|
|
Loading…
Reference in New Issue