mirror of https://github.com/dmarkham/enumer.git
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).
|
||||
|
||||
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)
|
||||
|
|
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) {
|
||||
// At this moment, either "g.declareIndexAndNameVars()" or "g.declareNameVars()" has been called
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
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")
|
||||
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/<type>_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) {
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in New Issue