diff --git a/generator/golang/option.go b/generator/golang/option.go index 0e42aa6b..2e32e801 100644 --- a/generator/golang/option.go +++ b/generator/golang/option.go @@ -24,7 +24,9 @@ import ( // Features controls the behavior of CodeUtils. type Features struct { - MarshalEnumToText bool `json_enum_as_text:"Generate MarshalText for enum values"` + MarshalEnumToText bool `json_enum_as_text:"Generate MarshalText and UnmarshalText for enum values"` + MarshalEnum bool `enum_marshal:"Generate MarshalText for enum values"` + UnmarshalEnum bool `enum_unmarshal:"Generate UnmarshalText for enum values"` GenerateSetter bool `gen_setter:"Generate Set* methods for fields"` GenDatabaseTag bool `gen_db_tag:"Generate 'db:$field' tag"` GenOmitEmptyTag bool `omitempty_for_optional:"Generate 'omitempty' tags for optional fields."` @@ -56,6 +58,8 @@ type Features struct { var defaultFeatures = Features{ MarshalEnumToText: false, + MarshalEnum: false, + UnmarshalEnum: false, GenerateSetter: false, GenDatabaseTag: false, GenOmitEmptyTag: true, diff --git a/generator/golang/templates/enum.go b/generator/golang/templates/enum.go index 895f4a5d..37b7047e 100644 --- a/generator/golang/templates/enum.go +++ b/generator/golang/templates/enum.go @@ -53,12 +53,16 @@ func {{$EnumType}}FromString(s string) ({{$EnumType}}, error) { func {{$EnumType}}Ptr(v {{$EnumType}} ) *{{$EnumType}} { return &v } -{{- if Features.MarshalEnumToText}} +{{- if or Features.MarshalEnumToText Features.MarshalEnum}} func (p {{$EnumType}}) MarshalText() ([]byte, error) { return []byte(p.String()), nil } +{{end}}{{/* if or Features.MarshalEnumToText Features.MarshalEnum */}} + +{{- if or Features.MarshalEnumToText Features.UnmarshalEnum}} + func (p *{{$EnumType}}) UnmarshalText(text []byte) error { q, err := {{$EnumType}}FromString(string(text)) if err != nil { @@ -67,7 +71,7 @@ func (p *{{$EnumType}}) UnmarshalText(text []byte) error { *p = q return nil } -{{- end}}{{/* if Features.MarshalEnumToText */}} +{{end}}{{/* if or Features.MarshalEnumToText Features.UnmarshalEnum */}} {{- if Features.ScanValueForEnum}} {{- UseStdLibrary "sql" "driver"}}