add alternative string values method

This commit is contained in:
Menno 2021-10-17 14:31:43 +02:00
parent 328a338ddb
commit f80c486834
9 changed files with 57 additions and 16 deletions

View File

@ -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

View File

@ -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

View File

@ -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)

4
testdata/day.golden vendored
View File

@ -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() {

4
testdata/gap.golden vendored
View File

@ -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() {

4
testdata/num.golden vendored
View File

@ -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() {

View File

@ -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() {

View File

@ -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() {

View File

@ -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() {