diff --git a/README.md b/README.md index 7f5e78e..b9fafb8 100644 --- a/README.md +++ b/README.md @@ -205,6 +205,8 @@ it is transformed). If a name doesn't have the prefix it will be passed unchange If a prefix is provided via the `addprefix` flag, it will be added to the start of each name (after trimming and after transforming). +The boolean flag `values` will additionally create an alternative string values method `Values() []string` to fullfill the `EnumValues` interface of [ent](https://entgo.io/docs/schema-fields/#enum-fields). + ## Inspiring projects - [Álvaro López Espinosa](https://github.com/alvaroloes/enumer) diff --git a/enumer.go b/enumer.go index cfdc3a2..c234209 100644 --- a/enumer.go +++ b/enumer.go @@ -58,6 +58,18 @@ func (i %[1]s) IsA%[1]s() bool { } ` +// Arguments to format are: +// [1]: type name +const altStringValuesMethod = `func (%[1]s) Values() []string { + return %[1]sStrings() +} +` + +func (g *Generator) buildAltStringValuesMethod(typeName string) { + g.Printf("\n") + g.Printf(altStringValuesMethod, typeName) +} + func (g *Generator) buildBasicExtras(runs [][]Value, typeName string, runsThreshold int) { // At this moment, either "g.declareIndexAndNameVars()" or "g.declareNameVars()" has been called diff --git a/golden_test.go b/golden_test.go index 3db8602..03479d2 100644 --- a/golden_test.go +++ b/golden_test.go @@ -315,45 +315,45 @@ const ( func TestGolden(t *testing.T) { for _, test := range golden { - runGoldenTest(t, test, false, false, false, false, false, false, "", "") + runGoldenTest(t, test, false, false, false, false, false, false, true, "", "") } for _, test := range goldenJSON { - runGoldenTest(t, test, true, false, false, false, false, false, "", "") + runGoldenTest(t, test, true, false, false, false, false, false, false, "", "") } for _, test := range goldenText { - runGoldenTest(t, test, false, false, false, true, false, false, "", "") + runGoldenTest(t, test, false, false, false, true, false, false, false, "", "") } for _, test := range goldenYAML { - runGoldenTest(t, test, false, true, false, false, false, false, "", "") + runGoldenTest(t, test, false, true, false, false, false, false, false, "", "") } for _, test := range goldenSQL { - runGoldenTest(t, test, false, false, true, false, false, false, "", "") + runGoldenTest(t, test, false, false, true, false, false, false, false, "", "") } for _, test := range goldenJSONAndSQL { - runGoldenTest(t, test, true, false, true, false, false, false, "", "") + runGoldenTest(t, test, true, false, true, false, false, false, false, "", "") } for _, test := range goldenGQLGen { - runGoldenTest(t, test, false, false, false, false, false, true, "", "") + runGoldenTest(t, test, false, false, false, false, false, true, false, "", "") } for _, test := range goldenTrimPrefix { - runGoldenTest(t, test, false, false, false, false, false, false, "Day", "") + runGoldenTest(t, test, false, false, false, false, false, false, false, "Day", "") } for _, test := range goldenTrimPrefixMultiple { - runGoldenTest(t, test, false, false, false, false, false, false, "Day,Night", "") + runGoldenTest(t, test, false, false, false, false, false, false, false, "Day,Night", "") } for _, test := range goldenWithPrefix { - runGoldenTest(t, test, false, false, false, false, false, false, "", "Day") + runGoldenTest(t, test, false, false, false, false, false, false, false, "", "Day") } for _, test := range goldenTrimAndAddPrefix { - runGoldenTest(t, test, false, false, false, false, false, false, "Day", "Night") + runGoldenTest(t, test, false, false, false, false, false, false, false, "Day", "Night") } for _, test := range goldenLinecomment { - runGoldenTest(t, test, false, false, false, false, true, false, "", "") + runGoldenTest(t, test, false, false, false, false, true, false, false, "", "") } } func runGoldenTest(t *testing.T, test Golden, - generateJSON, generateYAML, generateSQL, generateText, linecomment, generateGQLGen bool, + generateJSON, generateYAML, generateSQL, generateText, linecomment, generateGQLGen, generateValuesMethod bool, trimPrefix string, prefix string) { var g Generator @@ -382,7 +382,7 @@ func runGoldenTest(t *testing.T, test Golden, if len(tokens) != 3 { t.Fatalf("%s: need type declaration on first line", test.name) } - g.generate(tokens[1], generateJSON, generateYAML, generateSQL, generateText, generateGQLGen, "noop", trimPrefix, prefix, linecomment) + g.generate(tokens[1], generateJSON, generateYAML, generateSQL, generateText, generateGQLGen, "noop", trimPrefix, prefix, linecomment, generateValuesMethod) got := string(g.format()) if got != loadGolden(test.name) { // Use this to help build a golden text when changes are needed diff --git a/stringer.go b/stringer.go index 61e8b25..49c87fd 100644 --- a/stringer.go +++ b/stringer.go @@ -50,6 +50,7 @@ var ( yaml = flag.Bool("yaml", false, "if true, yaml marshaling methods will be generated. Default: false") text = flag.Bool("text", false, "if true, text marshaling methods will be generated. Default: false") gqlgen = flag.Bool("gqlgen", false, "if true, GraphQL marshaling methods for gqlgen will be generated. Default: false") + altValuesFunc = flag.Bool("values", false, "if true, alternative string values method will be generated. Default: false") 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: \"\"") @@ -134,7 +135,7 @@ func main() { // Run generate for each type. for _, typeName := range typs { - g.generate(typeName, *json, *yaml, *sql, *text, *gqlgen, *transformMethod, *trimPrefix, *addPrefix, *linecomment) + g.generate(typeName, *json, *yaml, *sql, *text, *gqlgen, *transformMethod, *trimPrefix, *addPrefix, *linecomment, *altValuesFunc) } // Format the output. @@ -414,7 +415,7 @@ func (g *Generator) prefixValueNames(values []Value, prefix string) { // generate produces the String method for the named type. func (g *Generator) generate(typeName string, includeJSON, includeYAML, includeSQL, includeText, includeGQLGen bool, - transformMethod string, trimPrefix string, addPrefix string, lineComment bool) { + transformMethod string, trimPrefix string, addPrefix string, lineComment bool, includeValuesMethod bool) { values := make([]Value, 0, 100) for _, file := range g.pkg.files { file.lineComment = lineComment @@ -461,6 +462,10 @@ func (g *Generator) generate(typeName string, default: g.buildMap(runs, typeName) } + if includeValuesMethod { + g.buildAltStringValuesMethod(typeName) + } + g.buildNoOpOrderChangeDetect(runs, typeName) g.buildBasicExtras(runs, typeName, runsThreshold) @@ -785,8 +790,6 @@ const stringOneRun = `func (i %[1]s) String() string { // [2]: lowest defined value for type, as a string // [3]: size of index element (8 for uint8 etc.) // [4]: less than zero check (for signed types) -/* - */ const stringOneRunWithOffset = `func (i %[1]s) String() string { i -= %[2]s if %[4]si >= %[1]s(len(_%[1]sIndex)-1) { diff --git a/testdata/day.golden b/testdata/day.golden index 537b04b..f23e69b 100644 --- a/testdata/day.golden +++ b/testdata/day.golden @@ -12,6 +12,10 @@ func (i Day) String() string { return _DayName[_DayIndex[i]:_DayIndex[i+1]] } +func (Day) Values() []string { + return DayStrings() +} + // An "invalid array index" compiler error signifies that the constant values have changed. // Re-run the stringer command to generate them again. func _DayNoOp() { diff --git a/testdata/gap.golden b/testdata/gap.golden index 129891e..cc3e48d 100644 --- a/testdata/gap.golden +++ b/testdata/gap.golden @@ -29,6 +29,10 @@ func (i Gap) String() string { } } +func (Gap) Values() []string { + return GapStrings() +} + // An "invalid array index" compiler error signifies that the constant values have changed. // Re-run the stringer command to generate them again. func _GapNoOp() { diff --git a/testdata/num.golden b/testdata/num.golden index 1a5407f..97cc937 100644 --- a/testdata/num.golden +++ b/testdata/num.golden @@ -13,6 +13,10 @@ func (i Num) String() string { return _NumName[_NumIndex[i]:_NumIndex[i+1]] } +func (Num) Values() []string { + return NumStrings() +} + // An "invalid array index" compiler error signifies that the constant values have changed. // Re-run the stringer command to generate them again. func _NumNoOp() { diff --git a/testdata/offset.golden b/testdata/offset.golden index 44cc102..0e52817 100644 --- a/testdata/offset.golden +++ b/testdata/offset.golden @@ -13,6 +13,10 @@ func (i Number) String() string { return _NumberName[_NumberIndex[i]:_NumberIndex[i+1]] } +func (Number) Values() []string { + return NumberStrings() +} + // An "invalid array index" compiler error signifies that the constant values have changed. // Re-run the stringer command to generate them again. func _NumberNoOp() { diff --git a/testdata/prime.golden b/testdata/prime.golden index 283dbd1..8783338 100644 --- a/testdata/prime.golden +++ b/testdata/prime.golden @@ -25,6 +25,10 @@ func (i Prime) String() string { return fmt.Sprintf("Prime(%d)", i) } +func (Prime) Values() []string { + return PrimeStrings() +} + // An "invalid array index" compiler error signifies that the constant values have changed. // Re-run the stringer command to generate them again. func _PrimeNoOp() { diff --git a/testdata/unum.golden b/testdata/unum.golden index ed149b5..9e413dc 100644 --- a/testdata/unum.golden +++ b/testdata/unum.golden @@ -23,6 +23,10 @@ func (i Unum) String() string { } } +func (Unum) Values() []string { + return UnumStrings() +} + // An "invalid array index" compiler error signifies that the constant values have changed. // Re-run the stringer command to generate them again. func _UnumNoOp() {