From 78452a952a9b8551e6cde249d3fb20ef3824506f Mon Sep 17 00:00:00 2001 From: Gert-Jan Timmer Date: Thu, 17 Jan 2019 13:07:52 +0100 Subject: [PATCH 1/7] Add Transformers - snake-upper - kebab-upper - lower (Lowercase) - upper (Uppercase) - title (TitleCase) - first (Use first character of string) - first-lower (same as first only lower case) - first-upper (same as first only upper case) --- README.md | 75 ++++++++++++++++++++----------- endtoend_test.go | 42 ++++++++++++++--- stringer.go | 48 ++++++++++++++++++-- testdata/transform.go | 25 ----------- testdata/transform_first.go | 25 +++++++++++ testdata/transform_first_lower.go | 25 +++++++++++ testdata/transform_first_upper.go | 25 +++++++++++ testdata/transform_kebab.go | 25 +++++++++++ testdata/transform_kebab_upper.go | 25 +++++++++++ testdata/transform_lower.go | 25 +++++++++++ testdata/transform_snake.go | 25 +++++++++++ testdata/transform_snake_upper.go | 25 +++++++++++ testdata/transform_title.go | 25 +++++++++++ testdata/transform_upper.go | 25 +++++++++++ 14 files changed, 380 insertions(+), 60 deletions(-) delete mode 100644 testdata/transform.go create mode 100644 testdata/transform_first.go create mode 100644 testdata/transform_first_lower.go create mode 100644 testdata/transform_first_upper.go create mode 100644 testdata/transform_kebab.go create mode 100644 testdata/transform_kebab_upper.go create mode 100644 testdata/transform_lower.go create mode 100644 testdata/transform_snake.go create mode 100644 testdata/transform_snake_upper.go create mode 100644 testdata/transform_title.go create mode 100644 testdata/transform_upper.go diff --git a/README.md b/README.md index f4ac14d..2cebb9f 100644 --- a/README.md +++ b/README.md @@ -1,32 +1,36 @@ # Enumer [![GoDoc](https://godoc.org/github.com/alvaroloes/enumer?status.svg)](https://godoc.org/github.com/alvaroloes/enumer) [![Go Report Card](https://goreportcard.com/badge/github.com/alvaroloes/enumer)](https://goreportcard.com/report/github.com/alvaroloes/enumer) [![cover.run go](https://cover.run/go/github.com/alvaroloes/enumer.svg?tag=golang-1.10)](https://cover.run/go/github.com/alvaroloes/enumer?tag=golang-1.10) + Enumer is a tool to generate Go code that adds useful methods to Go enums (constants with a specific type). It started as a fork of [Rob Pike’s Stringer tool](https://godoc.org/golang.org/x/tools/cmd/stringer). ## Generated functions and methods + When Enumer is applied to a type, it will generate: -* The following basic methods/functions: +- The following basic methods/functions: - * Method `String()`: returns the string representation of the enum value. This makes the enum conform -the `Stringer` interface, so whenever you print an enum value, you'll get the string name instead of a number. - * Function `String(s string)`: returns the enum value from its string representation. This is useful -when you need to read enum values from command line arguments, from a configuration file, or -from a REST API request... In short, from those places where using the real enum value (an integer) would -be almost meaningless or hard to trace or use by a human. - * Function `Values()`: returns a slice with all the values of the enum - * Method `IsA()`: returns true only if the current value is among the values of the enum. Useful for validations. -* When the flag `json` is provided, two additional methods will be generated, `MarshalJSON()` and `UnmarshalJSON()`. These make -the enum conform to the `json.Marshaler` and `json.Unmarshaler` interfaces. Very useful to use it in JSON APIs. -* When the flag `text` is provided, two additional methods will be generated, `MarshalText()` and `UnmarshalText()`. These make -the enum conform to the `encoding.TextMarshaler` and `encoding.TextUnmarshaler` interfaces. -**Note:** If you use your enum values as keys in a map and you encode the map as _JSON_, you need this flag set to true to properly -convert the map keys to json (strings). If not, the numeric values will be used instead -* When the flag `yaml` is provided, two additional methods will be generated, `MarshalYAML()` and `UnmarshalYAML()`. These make -the enum conform to the `gopkg.in/yaml.v2.Marshaler` and `gopkg.in/yaml.v2.Unmarshaler` interfaces. -* When the flag `sql` is provided, the methods for implementing the Scanner and Valuer interfaces will be also generated. -Useful when storing the enum in a database. + - Method `String()`: returns the string representation of the enum value. This makes the enum conform + the `Stringer` interface, so whenever you print an enum value, you'll get the string name instead of a number. + - Function `String(s string)`: returns the enum value from its string representation. This is useful + when you need to read enum values from command line arguments, from a configuration file, or + from a REST API request... In short, from those places where using the real enum value (an integer) would + be almost meaningless or hard to trace or use by a human. + - Function `Values()`: returns a slice with all the values of the enum + - Method `IsA()`: returns true only if the current value is among the values of the enum. Useful for validations. + +- When the flag `json` is provided, two additional methods will be generated, `MarshalJSON()` and `UnmarshalJSON()`. These make + the enum conform to the `json.Marshaler` and `json.Unmarshaler` interfaces. Very useful to use it in JSON APIs. +- When the flag `text` is provided, two additional methods will be generated, `MarshalText()` and `UnmarshalText()`. These make + the enum conform to the `encoding.TextMarshaler` and `encoding.TextUnmarshaler` interfaces. + **Note:** If you use your enum values as keys in a map and you encode the map as _JSON_, you need this flag set to true to properly + convert the map keys to json (strings). If not, the numeric values will be used instead +- When the flag `yaml` is provided, two additional methods will be generated, `MarshalYAML()` and `UnmarshalYAML()`. These make + the enum conform to the `gopkg.in/yaml.v2.Marshaler` and `gopkg.in/yaml.v2.Unmarshaler` interfaces. +- When the flag `sql` is provided, the methods for implementing the Scanner and Valuer interfaces will be also generated. + Useful when storing the enum in a database. For example, if we have an enum type called `Pill`, + ```go type Pill int @@ -38,21 +42,23 @@ const ( Acetaminophen = Paracetamol ) ``` + executing `enumer -type=Pill -json` will generate a new file with four basic methods and two extra for JSON: + ```go -func (i Pill) String() string { +func (i Pill) String() string { //... } -func PillString(s string) (Pill, error) { +func PillString(s string) (Pill, error) { //... } -func PillValues() []Pill { +func PillValues() []Pill { //... } -func (i Pill) IsAPill() bool { +func (i Pill) IsAPill() bool { //... } @@ -64,7 +70,9 @@ func (i *Pill) UnmarshalJSON(data []byte) error { //... } ``` + From now on, we can: + ```go // Convert any Pill value to string var aspirinString string = Aspirin.String() @@ -119,15 +127,29 @@ For example, the command `enumer -type=MyType -json -transform=snake` would gene ```go name := MyTypeValue.String() // name => "my_type_value" ``` + **Note**: The transformation only works form CamelCase to snake_case or kebab-case, not the other way around. +### Transformers + +- snake +- snake-upper +- kebab +- kebab-upper +- lower (Lowercase) +- upper (Uppercase) +- title (TitleCase) +- first (Use first character of string) +- first-lower (same as first only lower case) +- first-upper (same as first only upper case) + ## How to use + The usage of Enumer is the same as Stringer, so you can refer to the [Stringer docs](https://godoc.org/golang.org/x/tools/cmd/stringer) for more information. There are four boolean flags: `json`, `text`, `yaml` and `sql`. You can use any combination of them (i.e. `enumer -type=Pill -json -text`), - For enum string representation transformation the `transform` and `trimprefix` flags were added (i.e. `enumer -type=MyType -json -transform=snake`). Possible transform values are `snake` and `kebab` for transformation to snake_case and kebab-case accordingly. @@ -137,5 +159,6 @@ If a prefix is provided via the `trimprefix` flag, it will be trimmed from the s it is transformed). If a name doesn't have the prefix it will be passed unchanged. ## Inspiring projects -* [Stringer](https://godoc.org/golang.org/x/tools/cmd/stringer) -* [jsonenums](https://github.com/campoy/jsonenums) + +- [Stringer](https://godoc.org/golang.org/x/tools/cmd/stringer) +- [jsonenums](https://github.com/campoy/jsonenums) diff --git a/endtoend_test.go b/endtoend_test.go index 3397c06..10e0e39 100644 --- a/endtoend_test.go +++ b/endtoend_test.go @@ -57,13 +57,45 @@ func TestEndToEnd(t *testing.T) { t.Logf("cgo is no enabled for %s", name) continue } - // Names are known to be ASCII and long enough. - typeName := fmt.Sprintf("%c%s", name[0]+'A'-'a', name[1:len(name)-len(".go")]) - transformNameMethod := "noop" - if name == "transform.go" { - typeName = "CamelCaseValue" + // Names are known to be ASCII and long enough. + var typeName string + var transformNameMethod string + + switch name { + case "transform_snake.go": + typeName = "SnakeCaseValue" transformNameMethod = "snake" + case "transform_snake_upper.go": + typeName = "SnakeUpperCaseValue" + transformNameMethod = "snake-upper" + case "transform_kebab.go": + typeName = "KebabCaseValue" + transformNameMethod = "kebab" + case "transform_kebab_upper.go": + typeName = "KebabUpperCaseValue" + transformNameMethod = "kebab-upper" + case "transform_upper.go": + typeName = "UpperCaseValue" + transformNameMethod = "upper" + case "transform_lower.go": + typeName = "LowerCaseValue" + transformNameMethod = "lower" + case "transform_title.go": + typeName = "TitleCaseValue" + transformNameMethod = "title" + case "transform_first.go": + typeName = "FirstCaseValue" + transformNameMethod = "first" + case "transform_first_upper.go": + typeName = "FirstUpperCaseValue" + transformNameMethod = "first-upper" + case "transform_first_lower.go": + typeName = "FirstLowerCaseValue" + transformNameMethod = "first-lower" + default: + typeName = fmt.Sprintf("%c%s", name[0]+'A'-'a', name[1:len(name)-len(".go")]) + transformNameMethod = "noop" } stringerCompileAndRun(t, dir, stringer, typeName, name, transformNameMethod) diff --git a/stringer.go b/stringer.go index 6c267e5..e3ac91c 100644 --- a/stringer.go +++ b/stringer.go @@ -27,6 +27,7 @@ import ( "path/filepath" "sort" "strings" + "unicode/utf8" "github.com/pascaldekloe/name" ) @@ -240,18 +241,57 @@ func (pkg *Package) check(fs *token.FileSet, astFiles []*ast.File) { } func (g *Generator) transformValueNames(values []Value, transformMethod string) { - var sep rune + var fn func(src string) string switch transformMethod { case "snake": - sep = '_' + fn = func(s string) string { + return strings.ToLower(name.Delimit(s, '_')) + } + case "snake_upper", "snake-upper": + fn = func(s string) string { + return strings.ToUpper(name.Delimit(s, '_')) + } case "kebab": - sep = '-' + fn = func(s string) string { + return strings.ToLower(name.Delimit(s, '-')) + } + case "kebab_upper", "kebab-upper": + fn = func(s string) string { + return strings.ToUpper(name.Delimit(s, '-')) + } + case "upper": + fn = func(s string) string { + return strings.ToUpper(s) + } + case "lower": + fn = func(s string) string { + return strings.ToLower(s) + } + case "title": + fn = func(s string) string { + return strings.Title(s) + } + case "first": + fn = func(s string) string { + r, _ := utf8.DecodeRuneInString(s) + return string(r) + } + case "first_upper", "first-upper": + fn = func(s string) string { + r, _ := utf8.DecodeRuneInString(s) + return strings.ToUpper(string(r)) + } + case "first_lower", "first-lower": + fn = func(s string) string { + r, _ := utf8.DecodeRuneInString(s) + return strings.ToLower(string(r)) + } default: return } for i := range values { - values[i].name = strings.ToLower(name.Delimit(values[i].name, sep)) + values[i].name = fn(values[i].name) } } diff --git a/testdata/transform.go b/testdata/transform.go deleted file mode 100644 index 70d4b1f..0000000 --- a/testdata/transform.go +++ /dev/null @@ -1,25 +0,0 @@ -package main - -import "fmt" - -type CamelCaseValue int - -const ( - CamelCaseValueOne CamelCaseValue = iota - CamelCaseValueTwo - CamelCaseValueThree -) - -func main() { - ck(CamelCaseValueOne, "camel_case_value_one") - ck(CamelCaseValueTwo, "camel_case_value_two") - ck(CamelCaseValueThree, "camel_case_value_three") - ck(-127, "CamelCaseValue(-127)") - ck(127, "CamelCaseValue(127)") -} - -func ck(value CamelCaseValue, str string) { - if fmt.Sprint(value) != str { - panic("transform.go: " + str) - } -} diff --git a/testdata/transform_first.go b/testdata/transform_first.go new file mode 100644 index 0000000..f1f56c8 --- /dev/null +++ b/testdata/transform_first.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +type FirstCaseValue int + +const ( + Male FirstCaseValue = iota + Female + unknown +) + +func main() { + ck(Male, "M") + ck(Female, "F") + ck(unknown, "u") + ck(-127, "FirstCaseValue(-127)") + ck(127, "FirstCaseValue(127)") +} + +func ck(value FirstCaseValue, str string) { + if fmt.Sprint(value) != str { + panic("transform_first.go: " + str) + } +} diff --git a/testdata/transform_first_lower.go b/testdata/transform_first_lower.go new file mode 100644 index 0000000..865a265 --- /dev/null +++ b/testdata/transform_first_lower.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +type FirstLowerCaseValue int + +const ( + Male FirstLowerCaseValue = iota + Female + Unknown +) + +func main() { + ck(Male, "m") + ck(Female, "f") + ck(Unknown, "u") + ck(-127, "FirstLowerCaseValue(-127)") + ck(127, "FirstLowerCaseValue(127)") +} + +func ck(value FirstLowerCaseValue, str string) { + if fmt.Sprint(value) != str { + panic("transform_first_lower.go: " + str) + } +} diff --git a/testdata/transform_first_upper.go b/testdata/transform_first_upper.go new file mode 100644 index 0000000..f7282a5 --- /dev/null +++ b/testdata/transform_first_upper.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +type FirstUpperCaseValue int + +const ( + male FirstUpperCaseValue = iota + female + unknown +) + +func main() { + ck(male, "M") + ck(female, "F") + ck(unknown, "U") + ck(-127, "FirstUpperCaseValue(-127)") + ck(127, "FirstUpperCaseValue(127)") +} + +func ck(value FirstUpperCaseValue, str string) { + if fmt.Sprint(value) != str { + panic("transform_first_upper.go: " + str) + } +} diff --git a/testdata/transform_kebab.go b/testdata/transform_kebab.go new file mode 100644 index 0000000..8af3704 --- /dev/null +++ b/testdata/transform_kebab.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +type KebabCaseValue int + +const ( + KebabCaseValueOne KebabCaseValue = iota + KebabCaseValueTwo + KebabCaseValueThree +) + +func main() { + ck(KebabCaseValueOne, "kebab-case-value-one") + ck(KebabCaseValueTwo, "kebab-case-value-two") + ck(KebabCaseValueThree, "kebab-case-value-three") + ck(-127, "KebabCaseValue(-127)") + ck(127, "KebabCaseValue(127)") +} + +func ck(value KebabCaseValue, str string) { + if fmt.Sprint(value) != str { + panic("transform_kebab.go: " + str) + } +} diff --git a/testdata/transform_kebab_upper.go b/testdata/transform_kebab_upper.go new file mode 100644 index 0000000..1d15f9b --- /dev/null +++ b/testdata/transform_kebab_upper.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +type KebabUpperCaseValue int + +const ( + KebabUpperCaseValueOne KebabUpperCaseValue = iota + KebabUpperCaseValueTwo + KebabUpperCaseValueThree +) + +func main() { + ck(KebabUpperCaseValueOne, "KEBAB-UPPER-CASE-VALUE-ONE") + ck(KebabUpperCaseValueTwo, "KEBAB-UPPER-CASE-VALUE-TWO") + ck(KebabUpperCaseValueThree, "KEBAB-UPPER-CASE-VALUE-THREE") + ck(-127, "KebabUpperCaseValue(-127)") + ck(127, "KebabUpperCaseValue(127)") +} + +func ck(value KebabUpperCaseValue, str string) { + if fmt.Sprint(value) != str { + panic("transform_kebab_upper.go: " + str) + } +} diff --git a/testdata/transform_lower.go b/testdata/transform_lower.go new file mode 100644 index 0000000..e9ebe29 --- /dev/null +++ b/testdata/transform_lower.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +type LowerCaseValue int + +const ( + LowerCaseValueOne LowerCaseValue = iota + LowerCaseValueTwo + LowerCaseValueThree +) + +func main() { + ck(LowerCaseValueOne, "lowercasevalueone") + ck(LowerCaseValueTwo, "lowercasevaluetwo") + ck(LowerCaseValueThree, "lowercasevaluethree") + ck(-127, "LowerCaseValue(-127)") + ck(127, "LowerCaseValue(127)") +} + +func ck(value LowerCaseValue, str string) { + if fmt.Sprint(value) != str { + panic("transform_lower.go: " + str) + } +} diff --git a/testdata/transform_snake.go b/testdata/transform_snake.go new file mode 100644 index 0000000..c8cce88 --- /dev/null +++ b/testdata/transform_snake.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +type SnakeCaseValue int + +const ( + SnakeCaseValueOne SnakeCaseValue = iota + SnakeCaseValueTwo + SnakeCaseValueThree +) + +func main() { + ck(SnakeCaseValueOne, "snake_case_value_one") + ck(SnakeCaseValueTwo, "snake_case_value_two") + ck(SnakeCaseValueThree, "snake_case_value_three") + ck(-127, "SnakeCaseValue(-127)") + ck(127, "SnakeCaseValue(127)") +} + +func ck(value SnakeCaseValue, str string) { + if fmt.Sprint(value) != str { + panic("transform_snake.go: " + str) + } +} diff --git a/testdata/transform_snake_upper.go b/testdata/transform_snake_upper.go new file mode 100644 index 0000000..46e41e1 --- /dev/null +++ b/testdata/transform_snake_upper.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +type SnakeUpperCaseValue int + +const ( + SnakeUpperCaseValueOne SnakeUpperCaseValue = iota + SnakeUpperCaseValueTwo + SnakeUpperCaseValueThree +) + +func main() { + ck(SnakeUpperCaseValueOne, "SNAKE_UPPER_CASE_VALUE_ONE") + ck(SnakeUpperCaseValueTwo, "SNAKE_UPPER_CASE_VALUE_TWO") + ck(SnakeUpperCaseValueThree, "SNAKE_UPPER_CASE_VALUE_THREE") + ck(-127, "SnakeUpperCaseValue(-127)") + ck(127, "SnakeUpperCaseValue(127)") +} + +func ck(value SnakeUpperCaseValue, str string) { + if fmt.Sprint(value) != str { + panic("transform_snake_upper.go: " + str) + } +} diff --git a/testdata/transform_title.go b/testdata/transform_title.go new file mode 100644 index 0000000..a36a745 --- /dev/null +++ b/testdata/transform_title.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +type TitleCaseValue int + +const ( + titlecasevalueone TitleCaseValue = iota + titlecasevaluetwo + titlecasevaluethree +) + +func main() { + ck(titlecasevalueone, "Titlecasevalueone") + ck(titlecasevaluetwo, "Titlecasevaluetwo") + ck(titlecasevaluethree, "Titlecasevaluethree") + ck(-127, "TitleCaseValue(-127)") + ck(127, "TitleCaseValue(127)") +} + +func ck(value TitleCaseValue, str string) { + if fmt.Sprint(value) != str { + panic("transform_title.go: " + str) + } +} diff --git a/testdata/transform_upper.go b/testdata/transform_upper.go new file mode 100644 index 0000000..a18a4bf --- /dev/null +++ b/testdata/transform_upper.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +type UpperCaseValue int + +const ( + UpperCaseValueOne UpperCaseValue = iota + UpperCaseValueTwo + UpperCaseValueThree +) + +func main() { + ck(UpperCaseValueOne, "UPPERCASEVALUEONE") + ck(UpperCaseValueTwo, "UPPERCASEVALUETWO") + ck(UpperCaseValueThree, "UPPERCASEVALUETHREE") + ck(-127, "UpperCaseValue(-127)") + ck(127, "UpperCaseValue(127)") +} + +func ck(value UpperCaseValue, str string) { + if fmt.Sprint(value) != str { + panic("transform_upper.go: " + str) + } +} From ea11b04142d0dceeb0f1c4c02b07ad1251ced0ba Mon Sep 17 00:00:00 2001 From: Luca Osti Date: Fri, 22 Mar 2019 10:54:07 +0100 Subject: [PATCH 2/7] Add withprefix flag --- README.md | 3 ++ golden_test.go | 82 ++++++++++++++++++++++++++++++++++++++++++-------- stringer.go | 15 +++++++-- 3 files changed, 85 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 2cebb9f..5ad10be 100644 --- a/README.md +++ b/README.md @@ -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 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 - [Stringer](https://godoc.org/golang.org/x/tools/cmd/stringer) diff --git a/golden_test.go b/golden_test.go index 0a81aad..021a05d 100644 --- a/golden_test.go +++ b/golden_test.go @@ -49,8 +49,12 @@ var goldenJSONAndSQL = []Golden{ {"prime", primeJsonAndSqlIn, primeJsonAndSqlOut}, } -var goldenPrefix = []Golden{ - {"prefix", prefixIn, dayOut}, +var goldenTrimPrefix = []Golden{ + {"trimprefix", trimPrefixIn, dayOut}, +} + +var goldenWithPrefix = []Golden{ + {"withprefix", dayIn, prefixedDayOut}, } // 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. // Also includes a duplicate. 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 ( DayMonday Day = iota DayTuesday @@ -1024,29 +1077,32 @@ const ( func TestGolden(t *testing.T) { for _, test := range golden { - runGoldenTest(t, test, false, false, false, false, "") + runGoldenTest(t, test, false, false, false, false, "", "") } for _, test := range goldenJSON { - runGoldenTest(t, test, true, false, false, false, "") + runGoldenTest(t, test, true, false, false, false, "", "") } for _, test := range goldenText { - runGoldenTest(t, test, false, false, false, true, "") + runGoldenTest(t, test, false, false, false, true, "", "") } for _, test := range goldenYAML { - runGoldenTest(t, test, false, true, false, false, "") + runGoldenTest(t, test, false, true, false, false, "", "") } for _, test := range goldenSQL { - runGoldenTest(t, test, false, false, true, false, "") + runGoldenTest(t, test, false, false, true, false, "", "") } for _, test := range goldenJSONAndSQL { - runGoldenTest(t, test, true, false, true, false, "") + runGoldenTest(t, test, true, false, true, false, "", "") } - for _, test := range goldenPrefix { - runGoldenTest(t, test, false, false, false, false, "Day") + for _, test := range goldenTrimPrefix { + 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 input := "package test\n" + test.input file := test.name + ".go" @@ -1056,7 +1112,7 @@ func runGoldenTest(t *testing.T, test Golden, generateJSON, generateYAML, genera if len(tokens) != 3 { 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()) if got != test.output { t.Errorf("%s: got\n====\n%s====\nexpected\n====%s", test.name, got, test.output) diff --git a/stringer.go b/stringer.go index e3ac91c..d194a8b 100644 --- a/stringer.go +++ b/stringer.go @@ -41,6 +41,7 @@ var ( output = flag.String("output", "", "output file name; default srcdir/_string.go") 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: \"\"") + addPrefix = flag.String("addprefix", "", "transform each item name by adding a prefix. Default: \"\"") ) // Usage is a replacement usage function for the flags package. @@ -103,7 +104,7 @@ func main() { // Run generate for each type. 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. @@ -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. -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) for _, file := range g.pkg.files { // 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.prefixValueNames(values, addPrefix) + g.transformValueNames(values, transformMethod) runs := splitIntoRuns(values) From 5511caae23f705a8dd71c1eb32ffe5fe6c2cef1f Mon Sep 17 00:00:00 2001 From: Luca Osti Date: Fri, 22 Mar 2019 11:36:16 +0100 Subject: [PATCH 3/7] Add prefix after transform --- README.md | 3 +-- stringer.go | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5ad10be..ccd9920 100644 --- a/README.md +++ b/README.md @@ -158,8 +158,7 @@ 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 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). +If a prefix is provided via the `addprefix` flag, it will be added to the start of each name (after trimming and after transforming). ## Inspiring projects diff --git a/stringer.go b/stringer.go index d194a8b..62a7725 100644 --- a/stringer.go +++ b/stringer.go @@ -330,10 +330,10 @@ func (g *Generator) generate(typeName string, includeJSON, includeYAML, includeS g.trimValueNames(values, trimPrefix) - g.prefixValueNames(values, addPrefix) - g.transformValueNames(values, transformMethod) + g.prefixValueNames(values, addPrefix) + runs := splitIntoRuns(values) // The decision of which pattern to use depends on the number of // runs in the numbers. If there's only one, it's easy. For more than From d806ebd41f7d8807dd5d2d28327a71f7f07b6e5d Mon Sep 17 00:00:00 2001 From: Luca Osti Date: Fri, 22 Mar 2019 11:36:20 +0100 Subject: [PATCH 4/7] Add test --- golden_test.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/golden_test.go b/golden_test.go index 021a05d..2fb1808 100644 --- a/golden_test.go +++ b/golden_test.go @@ -50,11 +50,15 @@ var goldenJSONAndSQL = []Golden{ } var goldenTrimPrefix = []Golden{ - {"trimprefix", trimPrefixIn, dayOut}, + {"trim prefix", trimPrefixIn, dayOut}, } var goldenWithPrefix = []Golden{ - {"withprefix", dayIn, prefixedDayOut}, + {"with prefix", dayIn, prefixedDayOut}, +} + +var goldenTrimAndAddPrefix = []Golden{ + {"trim and add prefix", trimPrefixIn, trimmedPrefixedDayOut}, } // Each example starts with "type XXX [u]int", with a single space separating them. @@ -170,6 +174,55 @@ func (i Day) IsADay() bool { } ` +const trimmedPrefixedDayOut = ` +const _DayName = "NightMondayNightTuesdayNightWednesdayNightThursdayNightFridayNightSaturdayNightSunday" + +var _DayIndex = [...]uint8{0, 11, 23, 37, 50, 61, 74, 85} + +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:11]: 0, + _DayName[11:23]: 1, + _DayName[23:37]: 2, + _DayName[37:50]: 3, + _DayName[50:61]: 4, + _DayName[61:74]: 5, + _DayName[74:85]: 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. // Also includes a duplicate. const offsetIn = `type Number int @@ -1100,6 +1153,9 @@ func TestGolden(t *testing.T) { for _, test := range goldenWithPrefix { runGoldenTest(t, test, false, false, false, false, "", "Day") } + for _, test := range goldenTrimAndAddPrefix { + runGoldenTest(t, test, false, false, false, false, "Day", "Night") + } } func runGoldenTest(t *testing.T, test Golden, generateJSON, generateYAML, generateSQL, generateText bool, trimPrefix string, prefix string) { From 188f5ebedcb37feb761971bdfb6315f9b9e4f0f9 Mon Sep 17 00:00:00 2001 From: Luca Osti Date: Fri, 22 Mar 2019 11:37:02 +0100 Subject: [PATCH 5/7] Add title-lower transform --- README.md | 5 +++-- stringer.go | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ccd9920..75eb42d 100644 --- a/README.md +++ b/README.md @@ -136,9 +136,10 @@ name := MyTypeValue.String() // name => "my_type_value" - snake-upper - kebab - kebab-upper -- lower (Lowercase) -- upper (Uppercase) +- lower (lowercase) +- upper (UPPERCASE) - title (TitleCase) +- title-lower (titleCase) - first (Use first character of string) - first-lower (same as first only lower case) - first-upper (same as first only upper case) diff --git a/stringer.go b/stringer.go index 62a7725..a16670b 100644 --- a/stringer.go +++ b/stringer.go @@ -27,6 +27,7 @@ import ( "path/filepath" "sort" "strings" + "unicode" "unicode/utf8" "github.com/pascaldekloe/name" @@ -272,6 +273,12 @@ func (g *Generator) transformValueNames(values []Value, transformMethod string) fn = func(s string) string { return strings.Title(s) } + case "title-lower": + fn = func(s string) string { + title := []rune(strings.Title(s)) + title[0] = unicode.ToLower(title[0]) + return string(title) + } case "first": fn = func(s string) string { r, _ := utf8.DecodeRuneInString(s) From 63cd80f057a55829addab344e7a51d3f19b69b6f Mon Sep 17 00:00:00 2001 From: Luca Osti Date: Fri, 22 Mar 2019 12:56:38 +0100 Subject: [PATCH 6/7] Add method for getting a slice of strings --- enumer.go | 56 +++++++++-- golden_test.go | 255 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 303 insertions(+), 8 deletions(-) diff --git a/enumer.go b/enumer.go index 6c3d6fd..651ed7d 100644 --- a/enumer.go +++ b/enumer.go @@ -22,6 +22,16 @@ func %[1]sValues() []%[1]s { } ` +// Arguments to format are: +// [1]: type name +const stringsMethod = `// %[1]sStrings returns a slice of all String values of the enum +func %[1]sStrings() []string { + strs := make([]string, len(_%[1]sNames)) + copy(strs, _%[1]sNames) + return strs +} +` + // Arguments to format are: // [1]: type name const stringBelongsMethodLoop = `// IsA%[1]s returns "true" if the value is listed in the enum definition. "false" otherwise @@ -57,8 +67,26 @@ func (g *Generator) buildBasicExtras(runs [][]Value, typeName string, runsThresh g.Printf("}\n\n") // Print the map between name and value - g.Printf("\nvar _%sNameToValueMap = map[string]%s{\n", typeName, typeName) + g.printValueMap(runs, typeName, runsThreshold) + + // Print the slice of names + g.printNamesSlice(runs, typeName, runsThreshold) + + // Print the basic extra methods + g.Printf(stringNameToValueMethod, typeName) + g.Printf(stringValuesMethod, typeName) + g.Printf(stringsMethod, typeName) + if len(runs) < runsThreshold { + g.Printf(stringBelongsMethodLoop, typeName) + } else { // There is a map of values, the code is simpler then + g.Printf(stringBelongsMethodSet, typeName) + } +} + +func (g *Generator) printValueMap(runs [][]Value, typeName string, runsThreshold int) { thereAreRuns := len(runs) > 1 && len(runs) <= runsThreshold + g.Printf("\nvar _%sNameToValueMap = map[string]%s{\n", typeName, typeName) + var n int var runID string for i, values := range runs { @@ -75,15 +103,27 @@ func (g *Generator) buildBasicExtras(runs [][]Value, typeName string, runsThresh } } g.Printf("}\n\n") +} +func (g *Generator) printNamesSlice(runs [][]Value, typeName string, runsThreshold int) { + thereAreRuns := len(runs) > 1 && len(runs) <= runsThreshold + g.Printf("\nvar _%sNames = []string{\n", typeName) - // Print the basic extra methods - g.Printf(stringNameToValueMethod, typeName) - g.Printf(stringValuesMethod, typeName) - if len(runs) < runsThreshold { - g.Printf(stringBelongsMethodLoop, typeName) - } else { // There is a map of values, the code is simpler then - g.Printf(stringBelongsMethodSet, typeName) + var n int + var runID string + for i, values := range runs { + if thereAreRuns { + runID = "_" + fmt.Sprintf("%d", i) + n = 0 + } else { + runID = "" + } + + for _, value := range values { + g.Printf("\t_%sName%s[%d:%d],\n", typeName, runID, n, n+len(value.name)) + n += len(value.name) + } } + g.Printf("}\n\n") } // Arguments to format are: diff --git a/golden_test.go b/golden_test.go index 2fb1808..ef190ff 100644 --- a/golden_test.go +++ b/golden_test.go @@ -100,6 +100,16 @@ var _DayNameToValueMap = map[string]Day{ _DayName[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) { @@ -114,6 +124,13 @@ 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 { @@ -149,6 +166,16 @@ var _DayNameToValueMap = map[string]Day{ _DayName[62:71]: 6, } +var _DayNames = []string{ + _DayName[0:9], + _DayName[9:19], + _DayName[19:31], + _DayName[31:42], + _DayName[42:51], + _DayName[51:62], + _DayName[62:71], +} + // 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) { @@ -163,6 +190,13 @@ 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 { @@ -198,6 +232,16 @@ var _DayNameToValueMap = map[string]Day{ _DayName[74:85]: 6, } +var _DayNames = []string{ + _DayName[0:11], + _DayName[11:23], + _DayName[23:37], + _DayName[37:50], + _DayName[50:61], + _DayName[61:74], + _DayName[74:85], +} + // 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) { @@ -212,6 +256,13 @@ 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 { @@ -256,6 +307,12 @@ var _NumberNameToValueMap = map[string]Number{ _NumberName[6:11]: 3, } +var _NumberNames = []string{ + _NumberName[0:3], + _NumberName[3:6], + _NumberName[6:11], +} + // NumberString retrieves an enum value from the enum constants string name. // Throws an error if the param is not part of the enum. func NumberString(s string) (Number, error) { @@ -270,6 +327,13 @@ func NumberValues() []Number { return _NumberValues } +// NumberStrings returns a slice of all String values of the enum +func NumberStrings() []string { + strs := make([]string, len(_NumberNames)) + copy(strs, _NumberNames) + return strs +} + // IsANumber returns "true" if the value is listed in the enum definition. "false" otherwise func (i Number) IsANumber() bool { for _, v := range _NumberValues { @@ -336,6 +400,17 @@ var _GapNameToValueMap = map[string]Gap{ _GapName_2[0:6]: 11, } +var _GapNames = []string{ + _GapName_0[0:3], + _GapName_0[3:8], + _GapName_1[0:4], + _GapName_1[4:7], + _GapName_1[7:12], + _GapName_1[12:17], + _GapName_1[17:21], + _GapName_2[0:6], +} + // GapString retrieves an enum value from the enum constants string name. // Throws an error if the param is not part of the enum. func GapString(s string) (Gap, error) { @@ -350,6 +425,13 @@ func GapValues() []Gap { return _GapValues } +// GapStrings returns a slice of all String values of the enum +func GapStrings() []string { + strs := make([]string, len(_GapNames)) + copy(strs, _GapNames) + return strs +} + // IsAGap returns "true" if the value is listed in the enum definition. "false" otherwise func (i Gap) IsAGap() bool { for _, v := range _GapValues { @@ -395,6 +477,14 @@ var _NumNameToValueMap = map[string]Num{ _NumName[10:12]: 2, } +var _NumNames = []string{ + _NumName[0:3], + _NumName[3:6], + _NumName[6:8], + _NumName[8:10], + _NumName[10:12], +} + // NumString retrieves an enum value from the enum constants string name. // Throws an error if the param is not part of the enum. func NumString(s string) (Num, error) { @@ -409,6 +499,13 @@ func NumValues() []Num { return _NumValues } +// NumStrings returns a slice of all String values of the enum +func NumStrings() []string { + strs := make([]string, len(_NumNames)) + copy(strs, _NumNames) + return strs +} + // IsANum returns "true" if the value is listed in the enum definition. "false" otherwise func (i Num) IsANum() bool { for _, v := range _NumValues { @@ -467,6 +564,14 @@ var _UnumNameToValueMap = map[string]Unum{ _UnumName_1[3:6]: 254, } +var _UnumNames = []string{ + _UnumName_0[0:2], + _UnumName_0[2:4], + _UnumName_0[4:6], + _UnumName_1[0:3], + _UnumName_1[3:6], +} + // UnumString retrieves an enum value from the enum constants string name. // Throws an error if the param is not part of the enum. func UnumString(s string) (Unum, error) { @@ -481,6 +586,13 @@ func UnumValues() []Unum { return _UnumValues } +// UnumStrings returns a slice of all String values of the enum +func UnumStrings() []string { + strs := make([]string, len(_UnumNames)) + copy(strs, _UnumNames) + return strs +} + // IsAUnum returns "true" if the value is listed in the enum definition. "false" otherwise func (i Unum) IsAUnum() bool { for _, v := range _UnumValues { @@ -557,6 +669,22 @@ var _PrimeNameToValueMap = map[string]Prime{ _PrimeName[32:35]: 43, } +var _PrimeNames = []string{ + _PrimeName[0:2], + _PrimeName[2:4], + _PrimeName[4:6], + _PrimeName[6:8], + _PrimeName[8:11], + _PrimeName[11:14], + _PrimeName[14:17], + _PrimeName[17:20], + _PrimeName[20:23], + _PrimeName[23:26], + _PrimeName[26:29], + _PrimeName[29:32], + _PrimeName[32:35], +} + // PrimeString retrieves an enum value from the enum constants string name. // Throws an error if the param is not part of the enum. func PrimeString(s string) (Prime, error) { @@ -571,6 +699,13 @@ func PrimeValues() []Prime { return _PrimeValues } +// PrimeStrings returns a slice of all String values of the enum +func PrimeStrings() []string { + strs := make([]string, len(_PrimeNames)) + copy(strs, _PrimeNames) + return strs +} + // IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise func (i Prime) IsAPrime() bool { _, ok := _PrimeMap[i] @@ -640,6 +775,22 @@ var _PrimeNameToValueMap = map[string]Prime{ _PrimeName[32:35]: 43, } +var _PrimeNames = []string{ + _PrimeName[0:2], + _PrimeName[2:4], + _PrimeName[4:6], + _PrimeName[6:8], + _PrimeName[8:11], + _PrimeName[11:14], + _PrimeName[14:17], + _PrimeName[17:20], + _PrimeName[20:23], + _PrimeName[23:26], + _PrimeName[26:29], + _PrimeName[29:32], + _PrimeName[32:35], +} + // PrimeString retrieves an enum value from the enum constants string name. // Throws an error if the param is not part of the enum. func PrimeString(s string) (Prime, error) { @@ -654,6 +805,13 @@ func PrimeValues() []Prime { return _PrimeValues } +// PrimeStrings returns a slice of all String values of the enum +func PrimeStrings() []string { + strs := make([]string, len(_PrimeNames)) + copy(strs, _PrimeNames) + return strs +} + // IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise func (i Prime) IsAPrime() bool { _, ok := _PrimeMap[i] @@ -741,6 +899,22 @@ var _PrimeNameToValueMap = map[string]Prime{ _PrimeName[32:35]: 43, } +var _PrimeNames = []string{ + _PrimeName[0:2], + _PrimeName[2:4], + _PrimeName[4:6], + _PrimeName[6:8], + _PrimeName[8:11], + _PrimeName[11:14], + _PrimeName[14:17], + _PrimeName[17:20], + _PrimeName[20:23], + _PrimeName[23:26], + _PrimeName[26:29], + _PrimeName[29:32], + _PrimeName[32:35], +} + // PrimeString retrieves an enum value from the enum constants string name. // Throws an error if the param is not part of the enum. func PrimeString(s string) (Prime, error) { @@ -755,6 +929,13 @@ func PrimeValues() []Prime { return _PrimeValues } +// PrimeStrings returns a slice of all String values of the enum +func PrimeStrings() []string { + strs := make([]string, len(_PrimeNames)) + copy(strs, _PrimeNames) + return strs +} + // IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise func (i Prime) IsAPrime() bool { _, ok := _PrimeMap[i] @@ -837,6 +1018,22 @@ var _PrimeNameToValueMap = map[string]Prime{ _PrimeName[32:35]: 43, } +var _PrimeNames = []string{ + _PrimeName[0:2], + _PrimeName[2:4], + _PrimeName[4:6], + _PrimeName[6:8], + _PrimeName[8:11], + _PrimeName[11:14], + _PrimeName[14:17], + _PrimeName[17:20], + _PrimeName[20:23], + _PrimeName[23:26], + _PrimeName[26:29], + _PrimeName[29:32], + _PrimeName[32:35], +} + // PrimeString retrieves an enum value from the enum constants string name. // Throws an error if the param is not part of the enum. func PrimeString(s string) (Prime, error) { @@ -851,6 +1048,13 @@ func PrimeValues() []Prime { return _PrimeValues } +// PrimeStrings returns a slice of all String values of the enum +func PrimeStrings() []string { + strs := make([]string, len(_PrimeNames)) + copy(strs, _PrimeNames) + return strs +} + // IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise func (i Prime) IsAPrime() bool { _, ok := _PrimeMap[i] @@ -938,6 +1142,22 @@ var _PrimeNameToValueMap = map[string]Prime{ _PrimeName[32:35]: 43, } +var _PrimeNames = []string{ + _PrimeName[0:2], + _PrimeName[2:4], + _PrimeName[4:6], + _PrimeName[6:8], + _PrimeName[8:11], + _PrimeName[11:14], + _PrimeName[14:17], + _PrimeName[17:20], + _PrimeName[20:23], + _PrimeName[23:26], + _PrimeName[26:29], + _PrimeName[29:32], + _PrimeName[32:35], +} + // PrimeString retrieves an enum value from the enum constants string name. // Throws an error if the param is not part of the enum. func PrimeString(s string) (Prime, error) { @@ -952,6 +1172,13 @@ func PrimeValues() []Prime { return _PrimeValues } +// PrimeStrings returns a slice of all String values of the enum +func PrimeStrings() []string { + strs := make([]string, len(_PrimeNames)) + copy(strs, _PrimeNames) + return strs +} + // IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise func (i Prime) IsAPrime() bool { _, ok := _PrimeMap[i] @@ -1050,6 +1277,22 @@ var _PrimeNameToValueMap = map[string]Prime{ _PrimeName[32:35]: 43, } +var _PrimeNames = []string{ + _PrimeName[0:2], + _PrimeName[2:4], + _PrimeName[4:6], + _PrimeName[6:8], + _PrimeName[8:11], + _PrimeName[11:14], + _PrimeName[14:17], + _PrimeName[17:20], + _PrimeName[20:23], + _PrimeName[23:26], + _PrimeName[26:29], + _PrimeName[29:32], + _PrimeName[32:35], +} + // PrimeString retrieves an enum value from the enum constants string name. // Throws an error if the param is not part of the enum. func PrimeString(s string) (Prime, error) { @@ -1064,6 +1307,13 @@ func PrimeValues() []Prime { return _PrimeValues } +// PrimeStrings returns a slice of all String values of the enum +func PrimeStrings() []string { + strs := make([]string, len(_PrimeNames)) + copy(strs, _PrimeNames) + return strs +} + // IsAPrime returns "true" if the value is listed in the enum definition. "false" otherwise func (i Prime) IsAPrime() bool { _, ok := _PrimeMap[i] @@ -1171,6 +1421,11 @@ func runGoldenTest(t *testing.T, test Golden, generateJSON, generateYAML, genera g.generate(tokens[1], generateJSON, generateYAML, generateSQL, generateText, "noop", trimPrefix, prefix) got := string(g.format()) if got != test.output { + // dmp := diffmatchpatch.New() + + // diffs := dmp.DiffMain(got, test.output, false) + + // t.Errorf(dmp.DiffPrettyText(diffs)) t.Errorf("%s: got\n====\n%s====\nexpected\n====%s", test.name, got, test.output) } } From 586f4733ff61855aa69f7fcdbf805f8a2b567c55 Mon Sep 17 00:00:00 2001 From: Dan Markham Date: Thu, 4 Apr 2019 17:09:21 -0700 Subject: [PATCH 7/7] adding in sum --- go.sum | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 go.sum diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4b4e5f7 --- /dev/null +++ b/go.sum @@ -0,0 +1,8 @@ +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= +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/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +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=