forked from mirror/enumer
Merge pull request #51 from mvrahden/feature/values
Feature/values add support for ent EnumValues interface
This commit is contained in:
commit
585d6b237e
|
@ -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).
|
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
|
## Inspiring projects
|
||||||
|
|
||||||
- [Álvaro López Espinosa](https://github.com/alvaroloes/enumer)
|
- [Álvaro López Espinosa](https://github.com/alvaroloes/enumer)
|
||||||
|
|
12
enumer.go
12
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) {
|
func (g *Generator) buildBasicExtras(runs [][]Value, typeName string, runsThreshold int) {
|
||||||
// At this moment, either "g.declareIndexAndNameVars()" or "g.declareNameVars()" has been called
|
// At this moment, either "g.declareIndexAndNameVars()" or "g.declareNameVars()" has been called
|
||||||
|
|
||||||
|
|
|
@ -315,45 +315,45 @@ 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, false, false, "", "")
|
runGoldenTest(t, test, false, false, false, false, false, false, true, "", "")
|
||||||
}
|
}
|
||||||
for _, test := range goldenJSON {
|
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 {
|
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 {
|
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 {
|
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 {
|
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 {
|
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 {
|
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 {
|
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 {
|
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 {
|
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 {
|
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,
|
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) {
|
trimPrefix string, prefix string) {
|
||||||
|
|
||||||
var g Generator
|
var g Generator
|
||||||
|
@ -382,7 +382,7 @@ func runGoldenTest(t *testing.T, test Golden,
|
||||||
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, generateGQLGen, "noop", trimPrefix, prefix, linecomment)
|
g.generate(tokens[1], generateJSON, generateYAML, generateSQL, generateText, generateGQLGen, "noop", trimPrefix, prefix, linecomment, generateValuesMethod)
|
||||||
got := string(g.format())
|
got := string(g.format())
|
||||||
if got != loadGolden(test.name) {
|
if got != loadGolden(test.name) {
|
||||||
// Use this to help build a golden text when changes are needed
|
// Use this to help build a golden text when changes are needed
|
||||||
|
|
11
stringer.go
11
stringer.go
|
@ -50,6 +50,7 @@ var (
|
||||||
yaml = flag.Bool("yaml", false, "if true, yaml marshaling methods will be generated. Default: false")
|
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")
|
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")
|
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/<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: \"\"")
|
||||||
|
@ -134,7 +135,7 @@ func main() {
|
||||||
|
|
||||||
// Run generate for each type.
|
// Run generate for each type.
|
||||||
for _, typeName := range typs {
|
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.
|
// Format the output.
|
||||||
|
@ -414,7 +415,7 @@ func (g *Generator) prefixValueNames(values []Value, prefix string) {
|
||||||
// generate produces the String method for the named type.
|
// generate produces the String method for the named type.
|
||||||
func (g *Generator) generate(typeName string,
|
func (g *Generator) generate(typeName string,
|
||||||
includeJSON, includeYAML, includeSQL, includeText, includeGQLGen bool,
|
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)
|
values := make([]Value, 0, 100)
|
||||||
for _, file := range g.pkg.files {
|
for _, file := range g.pkg.files {
|
||||||
file.lineComment = lineComment
|
file.lineComment = lineComment
|
||||||
|
@ -461,6 +462,10 @@ func (g *Generator) generate(typeName string,
|
||||||
default:
|
default:
|
||||||
g.buildMap(runs, typeName)
|
g.buildMap(runs, typeName)
|
||||||
}
|
}
|
||||||
|
if includeValuesMethod {
|
||||||
|
g.buildAltStringValuesMethod(typeName)
|
||||||
|
}
|
||||||
|
|
||||||
g.buildNoOpOrderChangeDetect(runs, typeName)
|
g.buildNoOpOrderChangeDetect(runs, typeName)
|
||||||
|
|
||||||
g.buildBasicExtras(runs, typeName, runsThreshold)
|
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
|
// [2]: lowest defined value for type, as a string
|
||||||
// [3]: size of index element (8 for uint8 etc.)
|
// [3]: size of index element (8 for uint8 etc.)
|
||||||
// [4]: less than zero check (for signed types)
|
// [4]: less than zero check (for signed types)
|
||||||
/*
|
|
||||||
*/
|
|
||||||
const stringOneRunWithOffset = `func (i %[1]s) String() string {
|
const stringOneRunWithOffset = `func (i %[1]s) String() string {
|
||||||
i -= %[2]s
|
i -= %[2]s
|
||||||
if %[4]si >= %[1]s(len(_%[1]sIndex)-1) {
|
if %[4]si >= %[1]s(len(_%[1]sIndex)-1) {
|
||||||
|
|
|
@ -12,6 +12,10 @@ func (i Day) String() string {
|
||||||
return _DayName[_DayIndex[i]:_DayIndex[i+1]]
|
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.
|
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||||
// Re-run the stringer command to generate them again.
|
// Re-run the stringer command to generate them again.
|
||||||
func _DayNoOp() {
|
func _DayNoOp() {
|
||||||
|
|
|
@ -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.
|
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||||
// Re-run the stringer command to generate them again.
|
// Re-run the stringer command to generate them again.
|
||||||
func _GapNoOp() {
|
func _GapNoOp() {
|
||||||
|
|
|
@ -13,6 +13,10 @@ func (i Num) String() string {
|
||||||
return _NumName[_NumIndex[i]:_NumIndex[i+1]]
|
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.
|
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||||
// Re-run the stringer command to generate them again.
|
// Re-run the stringer command to generate them again.
|
||||||
func _NumNoOp() {
|
func _NumNoOp() {
|
||||||
|
|
|
@ -13,6 +13,10 @@ func (i Number) String() string {
|
||||||
return _NumberName[_NumberIndex[i]:_NumberIndex[i+1]]
|
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.
|
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||||
// Re-run the stringer command to generate them again.
|
// Re-run the stringer command to generate them again.
|
||||||
func _NumberNoOp() {
|
func _NumberNoOp() {
|
||||||
|
|
|
@ -25,6 +25,10 @@ func (i Prime) String() string {
|
||||||
return fmt.Sprintf("Prime(%d)", i)
|
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.
|
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||||
// Re-run the stringer command to generate them again.
|
// Re-run the stringer command to generate them again.
|
||||||
func _PrimeNoOp() {
|
func _PrimeNoOp() {
|
||||||
|
|
|
@ -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.
|
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||||
// Re-run the stringer command to generate them again.
|
// Re-run the stringer command to generate them again.
|
||||||
func _UnumNoOp() {
|
func _UnumNoOp() {
|
||||||
|
|
Loading…
Reference in New Issue