2015-12-29 16:14:54 +03:00
|
|
|
package main
|
2016-01-19 22:39:33 +03:00
|
|
|
|
2015-12-29 16:14:54 +03:00
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
// Arguments to format are:
|
|
|
|
// [1]: type name
|
2018-01-03 17:35:09 +03:00
|
|
|
const stringValueToNameMap = `// %[1]sString retrieves an enum value from the enum constants string name.
|
|
|
|
// Throws an error if the param is not part of the enum.
|
|
|
|
func %[1]sString(s string) (%[1]s, error) {
|
|
|
|
if val, ok := _%[1]sNameToValueMap[s]; ok {
|
2015-12-29 16:14:54 +03:00
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
return 0, fmt.Errorf("%%s does not belong to %[1]s values", s)
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
func (g *Generator) buildValueToNameMap(runs [][]Value, typeName string, runsThreshold int) {
|
|
|
|
// At this moment, either "g.declareIndexAndNameVars()" or "g.declareNameVars()" has been called
|
2018-01-03 17:35:09 +03:00
|
|
|
g.Printf("\nvar _%sNameToValueMap = map[string]%s{\n", typeName, typeName)
|
2015-12-29 16:14:54 +03:00
|
|
|
thereAreRuns := len(runs) > 1 && len(runs) <= runsThreshold
|
2015-12-31 17:03:36 +03:00
|
|
|
var n int
|
2015-12-29 16:14:54 +03:00
|
|
|
var runID string
|
|
|
|
for i, values := range runs {
|
2015-12-31 17:03:36 +03:00
|
|
|
if thereAreRuns {
|
2016-01-19 22:39:33 +03:00
|
|
|
runID = "_" + fmt.Sprintf("%d", i)
|
2015-12-31 17:03:36 +03:00
|
|
|
n = 0
|
|
|
|
} else {
|
|
|
|
runID = ""
|
|
|
|
}
|
2015-12-29 16:14:54 +03:00
|
|
|
|
2015-12-31 17:03:36 +03:00
|
|
|
for _, value := range values {
|
2018-01-03 17:35:09 +03:00
|
|
|
g.Printf("\t_%sName%s[%d:%d]: %s,\n", typeName, runID, n, n+len(value.name), &value)
|
2015-12-29 16:14:54 +03:00
|
|
|
n += len(value.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g.Printf("}\n\n")
|
|
|
|
g.Printf(stringValueToNameMap, typeName)
|
|
|
|
}
|
2016-01-19 22:39:33 +03:00
|
|
|
|
|
|
|
// Arguments to format are:
|
|
|
|
// [1]: type name
|
|
|
|
const jsonMethods = `
|
|
|
|
func (i %[1]s) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(i.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *%[1]s) UnmarshalJSON(data []byte) error {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(data, &s); err != nil {
|
|
|
|
return fmt.Errorf("%[1]s should be a string, got %%s", data)
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
*i, err = %[1]sString(s)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
func (g *Generator) buildJSONMethods(runs [][]Value, typeName string, runsThreshold int) {
|
|
|
|
g.Printf(jsonMethods, typeName)
|
|
|
|
}
|
2016-12-20 15:57:15 +03:00
|
|
|
|
|
|
|
// Arguments to format are:
|
|
|
|
// [1]: type name
|
|
|
|
const yamlMethods = `
|
|
|
|
func (i %[1]s) MarshalYAML() (interface{}, error) {
|
|
|
|
return i.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *%[1]s) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
var s string
|
|
|
|
if err := unmarshal(&s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
*i, err = %[1]sString(s)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
func (g *Generator) buildYAMLMethods(runs [][]Value, typeName string, runsThreshold int) {
|
|
|
|
g.Printf(yamlMethods, typeName)
|
|
|
|
}
|