Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate enum marshal and unmarshal separately #137

Merged
merged 1 commit into from
Oct 24, 2023
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
6 changes: 5 additions & 1 deletion generator/golang/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."`
Expand Down Expand Up @@ -56,6 +58,8 @@ type Features struct {

var defaultFeatures = Features{
MarshalEnumToText: false,
MarshalEnum: false,
UnmarshalEnum: false,
GenerateSetter: false,
GenDatabaseTag: false,
GenOmitEmptyTag: true,
Expand Down
8 changes: 6 additions & 2 deletions generator/golang/templates/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"}}
Expand Down
Loading