Skip to content

Commit 34d99ce

Browse files
authored
Simplify Enums from JSONMarshaler to TextMarshaler interface (#29)
* Simplify Enums from JSONMarshaler to TextMarshaler interface Compared to JSONMarshaler interface, this removes: 1. an extra bytes.NewBufferString() buffer during the marshaling 2. an extra json.Unmarshal() call during the unmarshaling 3. need for "bytes" import in the server-only code * Regenerate examples
1 parent dc4839d commit 34d99ce

File tree

4 files changed

+16
-42
lines changed

4 files changed

+16
-42
lines changed

_examples/golang-basics/example.gen.go

+6-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_examples/golang-imports/api.gen.go

+5-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

enum.go.tmpl

+4-12
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,12 @@ func (x {{$name}}) String() string {
3131
return {{$name}}_name[{{$type}}(x)]
3232
}
3333

34-
func (x {{$name}}) MarshalJSON() ([]byte, error) {
35-
buf := bytes.NewBufferString(`"`)
36-
buf.WriteString({{$name}}_name[{{$type}}(x)])
37-
buf.WriteString(`"`)
38-
return buf.Bytes(), nil
34+
func (x Status) MarshalText() ([]byte, error) {
35+
return []byte({{$name}}_name[{{$type}}(x)]), nil
3936
}
4037

41-
func (x *{{$name}}) UnmarshalJSON(b []byte) error {
42-
var j string
43-
err := json.Unmarshal(b, &j)
44-
if err != nil {
45-
return err
46-
}
47-
*x = {{$name}}({{$name}}_value[j])
38+
func (x *{{$name}}) UnmarshalText(b []byte) error {
39+
*x = {{$name}}({{$name}}_value[string(b)])
4840
return nil
4941
}
5042

imports.go.tmpl

+1-4
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@
1414
{{- set $stdlibImports "encoding/json" "" -}}
1515
{{- end -}}
1616

17-
{{- if or $opts.client (not $opts.types) }}
18-
{{- set $stdlibImports "bytes" "" -}}
19-
{{- end -}}
20-
2117
{{- if $opts.client }}
18+
{{- set $stdlibImports "bytes" "" -}}
2219
{{- set $stdlibImports "io" "" -}}
2320
{{- set $stdlibImports "net/url" "" -}}
2421
{{- end -}}

0 commit comments

Comments
 (0)