forked from mirror/enumer
enum value change detector
This commit is contained in:
parent
9793e421ce
commit
8d7221cc8a
854
golden_test.go
854
golden_test.go
File diff suppressed because it is too large
Load Diff
19
stringer.go
19
stringer.go
|
@ -445,6 +445,7 @@ func (g *Generator) generate(typeName string, includeJSON, includeYAML, includeS
|
||||||
default:
|
default:
|
||||||
g.buildMap(runs, typeName)
|
g.buildMap(runs, typeName)
|
||||||
}
|
}
|
||||||
|
g.buildNoOpOrderChangeDetect(runs, typeName)
|
||||||
|
|
||||||
g.buildBasicExtras(runs, typeName, runsThreshold)
|
g.buildBasicExtras(runs, typeName, runsThreshold)
|
||||||
if includeJSON {
|
if includeJSON {
|
||||||
|
@ -819,6 +820,24 @@ func (g *Generator) buildMap(runs [][]Value, typeName string) {
|
||||||
g.Printf(stringMap, typeName)
|
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.
|
// Argument to format is the type name.
|
||||||
const stringMap = `func (i %[1]s) String() string {
|
const stringMap = `func (i %[1]s) String() string {
|
||||||
if str, ok := _%[1]sMap[i]; ok {
|
if str, ok := _%[1]sMap[i]; ok {
|
||||||
|
|
|
@ -54,7 +54,7 @@ Outer:
|
||||||
for n, test := range splitTests {
|
for n, test := range splitTests {
|
||||||
values := make([]Value, len(test.input))
|
values := make([]Value, len(test.input))
|
||||||
for i, v := range 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)
|
runs := splitIntoRuns(values)
|
||||||
if len(runs) != len(test.output) {
|
if len(runs) != len(test.output) {
|
||||||
|
|
Loading…
Reference in New Issue