Skip to content
Merged
Show file tree
Hide file tree
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
31 changes: 16 additions & 15 deletions pkg/enumr/enum.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,29 @@ func (t {{.TypeName}}) String() string {
return ""
}

// Parse{{.TypeName}} converts a string to a {{.TypeName}}.
func Parse{{.TypeName}}(text string) ({{.TypeName}}, error) {
switch text { {{- range .Instances -}}
{{printf "\n\t"}}case {{ if $marshalField }}{{ index .Fields $marshalField }}{{ else }}"{{transformName .Name $format}}"{{ end }}:
return {{.Name}}, nil
{{- end}}
default:
return {{.TypeName}}{}, fmt.Errorf("unknown enum value: %q", text)
}
}

// MarshalText converts the enum value to a string.
func (t {{.TypeName}}) MarshalText() (text []byte, err error) {
return []byte(t.String()), nil
}

// UnmarshalText converts a string to the appropriate enum value.
func (t *{{.TypeName}}) UnmarshalText(text []byte) error {
value := string(text)
switch value { {{- range .Instances -}}
{{printf "\n\t"}}case {{ if $marshalField }}{{ index .Fields $marshalField }}{{ else }}"{{transformName .Name $format}}"{{ end }}:
*t = {{.Name}}
{{- end}}
default:
return fmt.Errorf("unknown enum value: %q", value)
val, err := Parse{{.TypeName}}(string(text))
if err != nil {
return err
}
*t = val
return nil
}

Expand All @@ -55,11 +63,4 @@ func {{.TypeName}}Values() []{{.TypeName}} {
{{- end}}
}
}

// Parse{{.TypeName}} converts a string to a {{.TypeName}}.
func Parse{{.TypeName}}(text string) ({{.TypeName}}, error) {
var t {{.TypeName}}
err := t.UnmarshalText([]byte(text))
return t, err
}
{{end}}
{{end}}
30 changes: 16 additions & 14 deletions pkg/enumr/testdata/myenum_enum.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,30 @@ func (t MyEnum) String() string {
return ""
}

// ParseMyEnum converts a string to a MyEnum.
func ParseMyEnum(text string) (MyEnum, error) {
switch text {
case "value_one":
return ValueOne, nil
case "value_two":
return ValueTwo, nil
default:
return MyEnum{}, fmt.Errorf("unknown enum value: %q", text)
}
}

// MarshalText converts the enum value to a string.
func (t MyEnum) MarshalText() (text []byte, err error) {
return []byte(t.String()), nil
}

// UnmarshalText converts a string to the appropriate enum value.
func (t *MyEnum) UnmarshalText(text []byte) error {
value := string(text)
switch value {
case "value_one":
*t = ValueOne
case "value_two":
*t = ValueTwo
default:
return fmt.Errorf("unknown enum value: %q", value)
val, err := ParseMyEnum(string(text))
if err != nil {
return err
}
*t = val
return nil
}

Expand All @@ -44,9 +52,3 @@ func MyEnumValues() []MyEnum {
}
}

// ParseMyEnum converts a string to a MyEnum.
func ParseMyEnum(text string) (MyEnum, error) {
var t MyEnum
err := t.UnmarshalText([]byte(text))
return t, err
}