enum value change detector

This commit is contained in:
Dan Markham 2020-02-23 21:21:36 -08:00
parent 9793e421ce
commit 8d7221cc8a
No known key found for this signature in database
GPG Key ID: 80673ED3335C219F
3 changed files with 596 additions and 279 deletions

File diff suppressed because it is too large Load Diff

View File

@ -445,6 +445,7 @@ func (g *Generator) generate(typeName string, includeJSON, includeYAML, includeS
default:
g.buildMap(runs, typeName)
}
g.buildNoOpOrderChangeDetect(runs, typeName)
g.buildBasicExtras(runs, typeName, runsThreshold)
if includeJSON {
@ -819,6 +820,24 @@ func (g *Generator) buildMap(runs [][]Value, typeName string) {
g.Printf(stringMap, typeName)
}
// buildNoOpOrderChangeDetect try to let the compiler and the user know if the order/value of the ENUMS have changed.
func (g *Generator) buildNoOpOrderChangeDetect(runs [][]Value, typeName string) {
g.Printf("\n")
g.Printf(`
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
`)
g.Printf("func _%sNoOp (){ ", typeName)
g.Printf("\n var x [1]struct{}\n")
for _, values := range runs {
for _, value := range values {
g.Printf("\t_ = x[%s-(%s)]\n", value.originalName, value.str)
}
}
g.Printf("}\n\n")
}
// Argument to format is the type name.
const stringMap = `func (i %[1]s) String() string {
if str, ok := _%[1]sMap[i]; ok {

View File

@ -54,7 +54,7 @@ Outer:
for n, test := range splitTests {
values := make([]Value, len(test.input))
for i, v := range test.input {
values[i] = Value{"", v, test.signed, fmt.Sprint(v)}
values[i] = Value{"", "", v, test.signed, fmt.Sprint(v)}
}
runs := splitIntoRuns(values)
if len(runs) != len(test.output) {