Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions enumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import "fmt"

// Arguments to format are:
//
// [1]: type name
const stringNameToValueMethod = `// %[1]sString retrieves an enum value from the enum constants string name.
// Throws an error if the param is not part of the enum.
Expand All @@ -19,6 +20,7 @@ func %[1]sString(s string) (%[1]s, error) {
`

// Arguments to format are:
//
// [1]: type name
const stringValuesMethod = `// %[1]sValues returns all values of the enum
func %[1]sValues() []%[1]s {
Expand All @@ -27,6 +29,7 @@ func %[1]sValues() []%[1]s {
`

// Arguments to format are:
//
// [1]: type name
const stringsMethod = `// %[1]sStrings returns a slice of all String values of the enum
func %[1]sStrings() []string {
Expand All @@ -37,6 +40,7 @@ func %[1]sStrings() []string {
`

// Arguments to format are:
//
// [1]: type name
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 {
Expand All @@ -50,6 +54,7 @@ func (i %[1]s) IsA%[1]s() bool {
`

// Arguments to format are:
//
// [1]: type name
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 {
Expand All @@ -59,6 +64,7 @@ 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()
Expand Down Expand Up @@ -144,6 +150,7 @@ func (g *Generator) printNamesSlice(runs [][]Value, typeName string, runsThresho
}

// Arguments to format are:
//
// [1]: type name
const jsonMethods = `
// MarshalJSON implements the json.Marshaler interface for %[1]s
Expand All @@ -169,6 +176,31 @@ func (g *Generator) buildJSONMethods(runs [][]Value, typeName string, runsThresh
}

// Arguments to format are:
//
// [1]: type name
const intJsonMethods = `
// MarshalJSON implements the json.Marshaler interface for %[1]s
func (i %[1]s) MarshalJSON() ([]byte, error) {
return json.Marshal(int(i))
}

// UnmarshalJSON implements the json.Unmarshaler interface for %[1]s
func (i *%[1]s) UnmarshalJSON(data []byte) error {
var v int
if err := json.Unmarshal(data, &v); err != nil {
return fmt.Errorf("%[1]s should be a int, got %%s", data)
}
*i = %[1]s(v)
return nil
}
`

func (g *Generator) buildIntJSONMethods(runs [][]Value, typeName string, runsThreshold int) {
g.Printf(intJsonMethods, typeName)
}

// Arguments to format are:
//
// [1]: type name
const textMethods = `
// MarshalText implements the encoding.TextMarshaler interface for %[1]s
Expand All @@ -189,6 +221,31 @@ func (g *Generator) buildTextMethods(runs [][]Value, typeName string, runsThresh
}

// Arguments to format are:
//
// [1]: type name
const intTextMethods = `
// MarshalText implements the encoding.TextMarshaler interface for %[1]s
func (i %[1]s) MarshalText() ([]byte, error) {
return []byte(strconv.Itoa(int(i))), nil
}

// UnmarshalText implements the encoding.TextUnmarshaler interface for %[1]s
func (i *%[1]s) UnmarshalText(text []byte) error {
v, err := strconv.Atoi(string(text))
if err != nil {
return err
}
*i = %[1]s(v)
return nil
}
`

func (g *Generator) buildIntTextMethods(runs [][]Value, typeName string, runsThreshold int) {
g.Printf(intTextMethods, typeName)
}

// Arguments to format are:
//
// [1]: type name
const yamlMethods = `
// MarshalYAML implements a YAML Marshaler for %[1]s
Expand All @@ -212,3 +269,27 @@ func (i *%[1]s) UnmarshalYAML(unmarshal func(interface{}) error) error {
func (g *Generator) buildYAMLMethods(runs [][]Value, typeName string, runsThreshold int) {
g.Printf(yamlMethods, typeName)
}

// Arguments to format are:
//
// [1]: type name
const intYamlMethods = `
// MarshalYAML implements a YAML Marshaler for %[1]s
func (i %[1]s) MarshalYAML() (interface{}, error) {
return strconv.Itoa(int(i)), nil
}

// UnmarshalYAML implements a YAML Unmarshaler for %[1]s
func (i *%[1]s) UnmarshalYAML(unmarshal func(interface{}) error) error {
var v int
if err := unmarshal(&v); err != nil {
return err
}
*i = %[1]s(v)
return nil
}
`

func (g *Generator) buildIntYAMLMethods(runs [][]Value, typeName string, runsThreshold int) {
g.Printf(intYamlMethods, typeName)
}