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:
|
2023-04-10 22:37:44 +03:00
|
|
|
//
|
2015-12-29 16:14:54 +03:00
|
|
|
// [1]: type name
|
2018-03-09 14:32:26 +03:00
|
|
|
const stringNameToValueMethod = `// %[1]sString retrieves an enum value from the enum constants string name.
|
2018-02-20 06:20:31 +03:00
|
|
|
// Throws an error if the param is not part of the enum.
|
2018-01-03 17:35:09 +03:00
|
|
|
func %[1]sString(s string) (%[1]s, error) {
|
2020-08-09 07:14:17 +03:00
|
|
|
if val, ok := _%[1]sNameToValueMap[s]; ok {
|
|
|
|
return val, nil
|
|
|
|
}
|
2021-06-23 07:20:35 +03:00
|
|
|
|
|
|
|
if val, ok := _%[1]sNameToValueMap[strings.ToLower(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)
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2018-03-09 14:32:26 +03:00
|
|
|
// Arguments to format are:
|
2023-04-10 22:37:44 +03:00
|
|
|
//
|
2018-03-09 14:32:26 +03:00
|
|
|
// [1]: type name
|
|
|
|
const stringValuesMethod = `// %[1]sValues returns all values of the enum
|
|
|
|
func %[1]sValues() []%[1]s {
|
|
|
|
return _%[1]sValues
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2019-03-22 14:56:38 +03:00
|
|
|
// Arguments to format are:
|
2023-04-10 22:37:44 +03:00
|
|
|
//
|
2019-03-22 14:56:38 +03:00
|
|
|
// [1]: type name
|
|
|
|
const stringsMethod = `// %[1]sStrings returns a slice of all String values of the enum
|
|
|
|
func %[1]sStrings() []string {
|
|
|
|
strs := make([]string, len(_%[1]sNames))
|
|
|
|
copy(strs, _%[1]sNames)
|
|
|
|
return strs
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2018-03-09 14:32:26 +03:00
|
|
|
// Arguments to format are:
|
2023-04-10 22:37:44 +03:00
|
|
|
//
|
2018-03-09 14:32:26 +03:00
|
|
|
// [1]: type name
|
2018-03-09 17:44:41 +03:00
|
|
|
const stringBelongsMethodLoop = `// IsA%[1]s returns "true" if the value is listed in the enum definition. "false" otherwise
|
|
|
|
func (i %[1]s) IsA%[1]s() bool {
|
2018-03-09 14:32:26 +03:00
|
|
|
for _, v := range _%[1]sValues {
|
|
|
|
if i == v {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
`
|
2018-03-09 19:04:50 +03:00
|
|
|
|
2018-03-09 15:33:11 +03:00
|
|
|
// Arguments to format are:
|
2023-04-10 22:37:44 +03:00
|
|
|
//
|
2018-03-09 15:33:11 +03:00
|
|
|
// [1]: type name
|
2018-03-09 17:44:41 +03:00
|
|
|
const stringBelongsMethodSet = `// IsA%[1]s returns "true" if the value is listed in the enum definition. "false" otherwise
|
|
|
|
func (i %[1]s) IsA%[1]s() bool {
|
2018-03-09 15:33:11 +03:00
|
|
|
_, ok := _%[1]sMap[i]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
`
|
2018-03-09 14:32:26 +03:00
|
|
|
|
2021-10-17 15:31:43 +03:00
|
|
|
// Arguments to format are:
|
2023-04-10 22:37:44 +03:00
|
|
|
//
|
2021-10-17 15:31:43 +03:00
|
|
|
// [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)
|
|
|
|
}
|
|
|
|
|
2018-03-09 14:32:26 +03:00
|
|
|
func (g *Generator) buildBasicExtras(runs [][]Value, typeName string, runsThreshold int) {
|
2015-12-29 16:14:54 +03:00
|
|
|
// At this moment, either "g.declareIndexAndNameVars()" or "g.declareNameVars()" has been called
|
2018-03-09 14:32:26 +03:00
|
|
|
|
|
|
|
// Print the slice of values
|
|
|
|
g.Printf("\nvar _%sValues = []%s{", typeName, typeName)
|
|
|
|
for _, values := range runs {
|
|
|
|
for _, value := range values {
|
2020-02-24 02:24:20 +03:00
|
|
|
g.Printf("\t%s, ", value.originalName)
|
2018-03-09 14:32:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
g.Printf("}\n\n")
|
|
|
|
|
|
|
|
// Print the map between name and value
|
2019-03-22 14:56:38 +03:00
|
|
|
g.printValueMap(runs, typeName, runsThreshold)
|
|
|
|
|
|
|
|
// Print the slice of names
|
|
|
|
g.printNamesSlice(runs, typeName, runsThreshold)
|
|
|
|
|
|
|
|
// Print the basic extra methods
|
|
|
|
g.Printf(stringNameToValueMethod, typeName)
|
|
|
|
g.Printf(stringValuesMethod, typeName)
|
|
|
|
g.Printf(stringsMethod, typeName)
|
2019-07-18 18:06:39 +03:00
|
|
|
if len(runs) <= runsThreshold {
|
2019-03-22 14:56:38 +03:00
|
|
|
g.Printf(stringBelongsMethodLoop, typeName)
|
|
|
|
} else { // There is a map of values, the code is simpler then
|
|
|
|
g.Printf(stringBelongsMethodSet, typeName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Generator) printValueMap(runs [][]Value, typeName string, runsThreshold int) {
|
2015-12-29 16:14:54 +03:00
|
|
|
thereAreRuns := len(runs) > 1 && len(runs) <= runsThreshold
|
2019-03-22 14:56:38 +03:00
|
|
|
g.Printf("\nvar _%sNameToValueMap = map[string]%s{\n", typeName, typeName)
|
|
|
|
|
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 {
|
2020-02-24 02:24:20 +03:00
|
|
|
g.Printf("\t_%sName%s[%d:%d]: %s,\n", typeName, runID, n, n+len(value.name), value.originalName)
|
|
|
|
g.Printf("\t_%sLowerName%s[%d:%d]: %s,\n", typeName, runID, n, n+len(value.name), value.originalName)
|
2015-12-29 16:14:54 +03:00
|
|
|
n += len(value.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g.Printf("}\n\n")
|
2019-03-22 14:56:38 +03:00
|
|
|
}
|
|
|
|
func (g *Generator) printNamesSlice(runs [][]Value, typeName string, runsThreshold int) {
|
|
|
|
thereAreRuns := len(runs) > 1 && len(runs) <= runsThreshold
|
|
|
|
g.Printf("\nvar _%sNames = []string{\n", typeName)
|
2018-03-09 14:32:26 +03:00
|
|
|
|
2019-03-22 14:56:38 +03:00
|
|
|
var n int
|
|
|
|
var runID string
|
|
|
|
for i, values := range runs {
|
|
|
|
if thereAreRuns {
|
|
|
|
runID = "_" + fmt.Sprintf("%d", i)
|
|
|
|
n = 0
|
|
|
|
} else {
|
|
|
|
runID = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, value := range values {
|
|
|
|
g.Printf("\t_%sName%s[%d:%d],\n", typeName, runID, n, n+len(value.name))
|
|
|
|
n += len(value.name)
|
|
|
|
}
|
2018-03-09 15:33:11 +03:00
|
|
|
}
|
2019-03-22 14:56:38 +03:00
|
|
|
g.Printf("}\n\n")
|
2015-12-29 16:14:54 +03:00
|
|
|
}
|
2016-01-19 22:39:33 +03:00
|
|
|
|
|
|
|
// Arguments to format are:
|
2023-04-10 22:37:44 +03:00
|
|
|
//
|
2016-01-19 22:39:33 +03:00
|
|
|
// [1]: type name
|
|
|
|
const jsonMethods = `
|
2018-02-20 06:20:31 +03:00
|
|
|
// MarshalJSON implements the json.Marshaler interface for %[1]s
|
2016-01-19 22:39:33 +03:00
|
|
|
func (i %[1]s) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(i.String())
|
|
|
|
}
|
|
|
|
|
2018-02-20 06:20:31 +03:00
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface for %[1]s
|
2016-01-19 22:39:33 +03:00
|
|
|
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
|
|
|
|
2018-03-08 22:51:11 +03:00
|
|
|
// Arguments to format are:
|
2023-04-10 22:37:44 +03:00
|
|
|
//
|
2018-03-08 22:51:11 +03:00
|
|
|
// [1]: type name
|
|
|
|
const textMethods = `
|
2018-03-08 23:17:07 +03:00
|
|
|
// MarshalText implements the encoding.TextMarshaler interface for %[1]s
|
2018-03-08 22:51:11 +03:00
|
|
|
func (i %[1]s) MarshalText() ([]byte, error) {
|
|
|
|
return []byte(i.String()), nil
|
|
|
|
}
|
|
|
|
|
2018-04-10 04:09:18 +03:00
|
|
|
// UnmarshalText implements the encoding.TextUnmarshaler interface for %[1]s
|
2018-03-08 22:51:11 +03:00
|
|
|
func (i *%[1]s) UnmarshalText(text []byte) error {
|
|
|
|
var err error
|
|
|
|
*i, err = %[1]sString(string(text))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
func (g *Generator) buildTextMethods(runs [][]Value, typeName string, runsThreshold int) {
|
|
|
|
g.Printf(textMethods, typeName)
|
|
|
|
}
|
|
|
|
|
2016-12-20 15:57:15 +03:00
|
|
|
// Arguments to format are:
|
2023-04-10 22:37:44 +03:00
|
|
|
//
|
2016-12-20 15:57:15 +03:00
|
|
|
// [1]: type name
|
|
|
|
const yamlMethods = `
|
2018-03-08 23:17:07 +03:00
|
|
|
// MarshalYAML implements a YAML Marshaler for %[1]s
|
2016-12-20 15:57:15 +03:00
|
|
|
func (i %[1]s) MarshalYAML() (interface{}, error) {
|
2023-04-10 22:37:44 +03:00
|
|
|
return i.string()
|
2016-12-20 15:57:15 +03:00
|
|
|
}
|
|
|
|
|
2018-03-08 23:17:07 +03:00
|
|
|
// UnmarshalYAML implements a YAML Unmarshaler for %[1]s
|
2016-12-20 15:57:15 +03:00
|
|
|
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)
|
|
|
|
}
|