Merge pull request #29 from dmarkham/multi-prefix

Allow multiple trim prefixes
This commit is contained in:
Dan Markham 2020-02-23 13:55:50 -08:00 committed by GitHub
commit 56342fda5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 117 additions and 10 deletions

1
.gitignore vendored
View File

@ -26,5 +26,6 @@ coverage.txt
*.prof
.idea
.vscode
enumer

View File

@ -4,8 +4,8 @@ matrix:
allow_failures:
- go: master
include:
- go: 1.11.x
- go: 1.12.x
- go: 1.13.x
- go: master
env:
global:
@ -24,6 +24,6 @@ deploy:
- cli/build/enumer.windows-amd64.exe.tar.gz
- cli/build/sha256sum.txt
on:
go: 1.12.x
go: 1.13.x
repo: dmarkham/enumer
tags: true

6
go.mod
View File

@ -1,6 +1,8 @@
module github.com/dmarkham/enumer
require (
github.com/pascaldekloe/name v0.0.0-20180628100202-0fd16699aae1
golang.org/x/tools v0.0.0-20190404132500-923d25813098
github.com/pascaldekloe/name v1.0.0
golang.org/x/tools v0.0.0-20200221224223-e1da425f72fd
)
go 1.13

18
go.sum
View File

@ -1,8 +1,16 @@
github.com/pascaldekloe/name v0.0.0-20180628100202-0fd16699aae1 h1:/I3lTljEEDNYLho3/FUB7iD/oc2cEFgVmbHzV+O0PtU=
github.com/pascaldekloe/name v0.0.0-20180628100202-0fd16699aae1/go.mod h1:eD5JxqMiuNYyFNmyY9rkJ/slN8y59oEu4Ei7F8OoKWQ=
github.com/pascaldekloe/name v1.0.0 h1:n7LKFgHixETzxpRv2R77YgPUFo85QHGZKrdaYm7eY5U=
github.com/pascaldekloe/name v1.0.0/go.mod h1:Z//MfYJnH4jVpQ9wkclwu2I2MkHmXTlT9wR5UZScttM=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee h1:WG0RUwxtNT4qqaXX3DPA8zHFNm/D9xaBpxzHt1WcA/E=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190404132500-923d25813098 h1:MtqjsZmyGRgMmLUgxnmMJ6RYdvd2ib8ipiayHhqSxs4=
golang.org/x/tools v0.0.0-20190404132500-923d25813098/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20200221224223-e1da425f72fd h1:hHkvGJK23seRCflePJnVa9IMv8fsuavSCWKd11kDQFs=
golang.org/x/tools v0.0.0-20200221224223-e1da425f72fd/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

View File

@ -56,6 +56,10 @@ var goldenTrimPrefix = []Golden{
{"trim prefix", trimPrefixIn, dayOut},
}
var goldenTrimPrefixMultiple = []Golden{
{"trim multiple prefixes", trimPrefixMultipleIn, dayNightOut},
}
var goldenWithPrefix = []Golden{
{"with prefix", dayIn, prefixedDayOut},
}
@ -154,6 +158,81 @@ func (i Day) IsADay() bool {
}
`
const dayNightOut = `
const _DayName = "MondayTuesdayWednesdayThursdayFridaySaturdaySunday"
var _DayIndex = [...]uint8{0, 6, 13, 22, 30, 36, 44, 50}
const _DayLowerName = "mondaytuesdaywednesdaythursdayfridaysaturdaysunday"
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:6]: 0,
_DayLowerName[0:6]: 0,
_DayName[6:13]: 1,
_DayLowerName[6:13]: 1,
_DayName[13:22]: 2,
_DayLowerName[13:22]: 2,
_DayName[22:30]: 3,
_DayLowerName[22:30]: 3,
_DayName[30:36]: 4,
_DayLowerName[30:36]: 4,
_DayName[36:44]: 5,
_DayLowerName[36:44]: 5,
_DayName[44:50]: 6,
_DayLowerName[44:50]: 6,
}
var _DayNames = []string{
_DayName[0:6],
_DayName[6:13],
_DayName[13:22],
_DayName[22:30],
_DayName[30:36],
_DayName[36:44],
_DayName[44:50],
}
// 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
}
// DayStrings returns a slice of all String values of the enum
func DayStrings() []string {
strs := make([]string, len(_DayNames))
copy(strs, _DayNames)
return strs
}
// 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
}
`
const prefixedDayOut = `
const _DayName = "DayMondayDayTuesdayDayWednesdayDayThursdayDayFridayDaySaturdayDaySunday"
@ -1522,6 +1601,18 @@ const (
)
`
const trimPrefixMultipleIn = `type Day int
const (
DayMonday Day = iota
NightTuesday
DayWednesday
NightThursday
DayFriday
NightSaturday
DaySunday
)
`
func TestGolden(t *testing.T) {
for _, test := range golden {
runGoldenTest(t, test, false, false, false, false, "", "")
@ -1544,6 +1635,9 @@ func TestGolden(t *testing.T) {
for _, test := range goldenTrimPrefix {
runGoldenTest(t, test, false, false, false, false, "Day", "")
}
for _, test := range goldenTrimPrefixMultiple {
runGoldenTest(t, test, false, false, false, false, "Day,Night", "")
}
for _, test := range goldenWithPrefix {
runGoldenTest(t, test, false, false, false, false, "", "Day")
}

View File

@ -415,7 +415,9 @@ func (g *Generator) generate(typeName string, includeJSON, includeYAML, includeS
log.Fatalf("no values defined for type %s", typeName)
}
g.trimValueNames(values, trimPrefix)
for _, prefix := range strings.Split(trimPrefix, ",") {
g.trimValueNames(values, prefix)
}
g.transformValueNames(values, transformMethod)