This makes the ,omitempty tag work correctly.master
parent
50a5fcc22f
commit
8c7f4ae1e5
@ -0,0 +1,17 @@ |
||||
// Copyright 2017 Felix Lange <fjl@twurst.com>.
|
||||
// Use of this source code is governed by the MIT license,
|
||||
// which can be found in the LICENSE file.
|
||||
|
||||
//go:generate gencodec -type X -field-override Xo -formats json,yaml -out output.go
|
||||
|
||||
package omitempty |
||||
|
||||
type replacedInt int |
||||
|
||||
type X struct { |
||||
Int int `json:",omitempty"` |
||||
} |
||||
|
||||
type Xo struct { |
||||
Int replacedInt |
||||
} |
@ -0,0 +1,21 @@ |
||||
// Copyright 2017 Felix Lange <fjl@twurst.com>.
|
||||
// Use of this source code is governed by the MIT license,
|
||||
// which can be found in the LICENSE file.
|
||||
|
||||
package omitempty |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"testing" |
||||
) |
||||
|
||||
func TestOmitemptyJSON(t *testing.T) { |
||||
want := `{}` |
||||
out, err := json.Marshal(new(X)) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if string(out) != want { |
||||
t.Fatalf("got %#q, want %#q", string(out), want) |
||||
} |
||||
} |
@ -0,0 +1,60 @@ |
||||
// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
|
||||
|
||||
package omitempty |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"errors" |
||||
) |
||||
|
||||
func (x X) MarshalJSON() ([]byte, error) { |
||||
type XJSON struct { |
||||
Int replacedInt `json:",omitempty"` |
||||
} |
||||
var enc XJSON |
||||
enc.Int = replacedInt(x.Int) |
||||
return json.Marshal(&enc) |
||||
} |
||||
|
||||
func (x *X) UnmarshalJSON(input []byte) error { |
||||
type XJSON struct { |
||||
Int *replacedInt `json:",omitempty"` |
||||
} |
||||
var dec XJSON |
||||
if err := json.Unmarshal(input, &dec); err != nil { |
||||
return err |
||||
} |
||||
var x0 X |
||||
if dec.Int == nil { |
||||
return errors.New("missing required field 'int' for X") |
||||
} |
||||
x0.Int = int(*dec.Int) |
||||
*x = x0 |
||||
return nil |
||||
} |
||||
|
||||
func (x X) MarshalYAML() (interface{}, error) { |
||||
type XYAML struct { |
||||
Int replacedInt `json:",omitempty"` |
||||
} |
||||
var enc XYAML |
||||
enc.Int = replacedInt(x.Int) |
||||
return &enc, nil |
||||
} |
||||
|
||||
func (x *X) UnmarshalYAML(unmarshal func(interface{}) error) error { |
||||
type XYAML struct { |
||||
Int *replacedInt `json:",omitempty"` |
||||
} |
||||
var dec XYAML |
||||
if err := unmarshal(&dec); err != nil { |
||||
return err |
||||
} |
||||
var x0 X |
||||
if dec.Int == nil { |
||||
return errors.New("missing required field 'int' for X") |
||||
} |
||||
x0.Int = int(*dec.Int) |
||||
*x = x0 |
||||
return nil |
||||
} |
Loading…
Reference in new issue