From 49fa643993b411f3abcc9880b8f43e1b0c284ea9 Mon Sep 17 00:00:00 2001 From: Dan Markham Date: Sun, 23 Feb 2020 13:18:32 -0800 Subject: [PATCH 1/3] Allow multiple trim prefixes based on changes from @sgtsquiggs --- .gitignore | 1 + golden_test.go | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++ stringer.go | 4 ++- 3 files changed, 98 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 543b324..44f0924 100644 --- a/.gitignore +++ b/.gitignore @@ -26,5 +26,6 @@ coverage.txt *.prof .idea +.vscode enumer diff --git a/golden_test.go b/golden_test.go index 35aec3f..cdb6ed9 100644 --- a/golden_test.go +++ b/golden_test.go @@ -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") } diff --git a/stringer.go b/stringer.go index 1c23057..60e19c2 100644 --- a/stringer.go +++ b/stringer.go @@ -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) From f6864bbebcfebe60581f30f3433b5d56e5548d38 Mon Sep 17 00:00:00 2001 From: Dan Markham Date: Sun, 23 Feb 2020 13:30:02 -0800 Subject: [PATCH 2/3] Move to go 1.13.x for default build --- .travis.yml | 4 ++-- go.mod | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6ee0484..2a6381a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/go.mod b/go.mod index 1c1f81c..0d4f072 100644 --- a/go.mod +++ b/go.mod @@ -4,3 +4,5 @@ require ( github.com/pascaldekloe/name v0.0.0-20180628100202-0fd16699aae1 golang.org/x/tools v0.0.0-20190404132500-923d25813098 ) + +go 1.13 From df8df69062d558ec3ced80d949f17932d493ae58 Mon Sep 17 00:00:00 2001 From: Dan Markham Date: Sun, 23 Feb 2020 13:40:57 -0800 Subject: [PATCH 3/3] Update Deps --- go.mod | 4 ++-- go.sum | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 0d4f072..fbc95ae 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +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 diff --git a/go.sum b/go.sum index 4b4e5f7..b90dc53 100644 --- a/go.sum +++ b/go.sum @@ -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=