-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c45222
commit 0ce1369
Showing
6 changed files
with
308 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package _generated | ||
|
||
import "time" | ||
|
||
//go:generate msgp | ||
|
||
// check some specific cases for omitzero | ||
|
||
type OmitZero0 struct { | ||
AStruct OmitZeroA `msg:"astruct,omitempty"` // leave this one omitempty | ||
BStruct OmitZeroA `msg:"bstruct,omitzero"` // and compare to this | ||
AStructPtr *OmitZeroA `msg:"astructptr,omitempty"` // a pointer case omitempty | ||
BStructPtr *OmitZeroA `msg:"bstructptr,omitzero"` // a pointer case omitzero | ||
AExt OmitZeroExt `msg:"aext,omitzero"` // external type case | ||
AExtPtr *OmitZeroExtPtr `msg:"aextptr,omitzero"` // external type pointer case | ||
|
||
// more | ||
APtrNamedStr *NamedStringOZ `msg:"aptrnamedstr,omitzero"` | ||
ANamedStruct NamedStructOZ `msg:"anamedstruct,omitzero"` | ||
APtrNamedStruct *NamedStructOZ `msg:"aptrnamedstruct,omitzero"` | ||
EmbeddableStruct `msg:",flatten,omitzero"` // embed flat | ||
EmbeddableStructOZ `msg:"embeddablestruct2,omitzero"` // embed non-flat | ||
ATime time.Time `msg:"atime,omitzero"` | ||
|
||
OmitZeroTuple OmitZeroTuple `msg:"ozt"` // the inside of a tuple should ignore both omitempty and omitzero | ||
} | ||
|
||
type OmitZeroA struct { | ||
A string `msg:"a,omitempty"` | ||
B NamedStringOZ `msg:"b,omitzero"` | ||
C NamedStringOZ `msg:"c,omitzero"` | ||
} | ||
|
||
func (o *OmitZeroA) IsZero() bool { | ||
if o == nil { | ||
return true | ||
} | ||
return *o == (OmitZeroA{}) | ||
} | ||
|
||
type NamedStructOZ struct { | ||
A string `msg:"a,omitempty"` | ||
B string `msg:"b,omitempty"` | ||
} | ||
|
||
func (ns *NamedStructOZ) IsZero() bool { | ||
if ns == nil { | ||
return true | ||
} | ||
return *ns == (NamedStructOZ{}) | ||
} | ||
|
||
type NamedStringOZ string | ||
|
||
func (ns *NamedStringOZ) IsZero() bool { | ||
if ns == nil { | ||
return true | ||
} | ||
return *ns == "" | ||
} | ||
|
||
type EmbeddableStructOZ struct { | ||
SomeEmbed string `msg:"someembed2,omitempty"` | ||
} | ||
|
||
func (es EmbeddableStructOZ) IsZero() bool { return es == (EmbeddableStructOZ{}) } | ||
|
||
type EmbeddableStructOZ2 struct { | ||
SomeEmbed2 string `msg:"someembed2,omitempty"` | ||
} | ||
|
||
func (es EmbeddableStructOZ2) IsZero() bool { return es == (EmbeddableStructOZ2{}) } | ||
|
||
//msgp:tuple OmitZeroTuple | ||
|
||
// OmitZeroTuple is flagged for tuple output, it should ignore all omitempty and omitzero functionality | ||
// since it's fundamentally incompatible. | ||
type OmitZeroTuple struct { | ||
FieldA string `msg:"fielda,omitempty"` | ||
FieldB NamedStringOZ `msg:"fieldb,omitzero"` | ||
FieldC NamedStringOZ `msg:"fieldc,omitzero"` | ||
} | ||
|
||
type OmitZero1 struct { | ||
T1 OmitZeroTuple `msg:"t1"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package _generated | ||
|
||
import ( | ||
"github.com/tinylib/msgp/msgp" | ||
) | ||
|
||
// this has "external" types that will show up | ||
// as generic IDENT during code generation | ||
|
||
type OmitZeroExt struct { | ||
a int // custom type | ||
} | ||
|
||
// IsZero will return true if a is not positive | ||
func (o OmitZeroExt) IsZero() bool { return o.a <= 0 } | ||
|
||
// EncodeMsg implements msgp.Encodable | ||
func (o OmitZeroExt) EncodeMsg(en *msgp.Writer) (err error) { | ||
if o.a > 0 { | ||
return en.WriteInt(o.a) | ||
} | ||
return en.WriteNil() | ||
} | ||
|
||
// DecodeMsg implements msgp.Decodable | ||
func (o *OmitZeroExt) DecodeMsg(dc *msgp.Reader) (err error) { | ||
if dc.IsNil() { | ||
err = dc.ReadNil() | ||
if err != nil { | ||
return | ||
} | ||
o.a = 0 | ||
return | ||
} | ||
o.a, err = dc.ReadInt() | ||
return err | ||
} | ||
|
||
// MarshalMsg implements msgp.Marshaler | ||
func (o OmitZeroExt) MarshalMsg(b []byte) (ret []byte, err error) { | ||
ret = msgp.Require(b, o.Msgsize()) | ||
if o.a > 0 { | ||
return msgp.AppendInt(ret, o.a), nil | ||
} | ||
return msgp.AppendNil(ret), nil | ||
} | ||
|
||
// UnmarshalMsg implements msgp.Unmarshaler | ||
func (o *OmitZeroExt) UnmarshalMsg(bts []byte) (ret []byte, err error) { | ||
if msgp.IsNil(bts) { | ||
bts, err = msgp.ReadNilBytes(bts) | ||
return bts, err | ||
} | ||
o.a, bts, err = msgp.ReadIntBytes(bts) | ||
return bts, err | ||
} | ||
|
||
// Msgsize implements msgp.Msgsizer | ||
func (o OmitZeroExt) Msgsize() (s int) { | ||
return msgp.IntSize | ||
} | ||
|
||
type OmitZeroExtPtr struct { | ||
a int // custom type | ||
} | ||
|
||
// IsZero will return true if a is nil or not positive | ||
func (o *OmitZeroExtPtr) IsZero() bool { return o == nil || o.a <= 0 } | ||
|
||
// EncodeMsg implements msgp.Encodable | ||
func (o *OmitZeroExtPtr) EncodeMsg(en *msgp.Writer) (err error) { | ||
if o.a > 0 { | ||
return en.WriteInt(o.a) | ||
} | ||
return en.WriteNil() | ||
} | ||
|
||
// DecodeMsg implements msgp.Decodable | ||
func (o *OmitZeroExtPtr) DecodeMsg(dc *msgp.Reader) (err error) { | ||
if dc.IsNil() { | ||
err = dc.ReadNil() | ||
if err != nil { | ||
return | ||
} | ||
o.a = 0 | ||
return | ||
} | ||
o.a, err = dc.ReadInt() | ||
return err | ||
} | ||
|
||
// MarshalMsg implements msgp.Marshaler | ||
func (o *OmitZeroExtPtr) MarshalMsg(b []byte) (ret []byte, err error) { | ||
ret = msgp.Require(b, o.Msgsize()) | ||
if o.a > 0 { | ||
return msgp.AppendInt(ret, o.a), nil | ||
} | ||
return msgp.AppendNil(ret), nil | ||
} | ||
|
||
// UnmarshalMsg implements msgp.Unmarshaler | ||
func (o *OmitZeroExtPtr) UnmarshalMsg(bts []byte) (ret []byte, err error) { | ||
if msgp.IsNil(bts) { | ||
bts, err = msgp.ReadNilBytes(bts) | ||
return bts, err | ||
} | ||
o.a, bts, err = msgp.ReadIntBytes(bts) | ||
return bts, err | ||
} | ||
|
||
// Msgsize implements msgp.Msgsizer | ||
func (o *OmitZeroExtPtr) Msgsize() (s int) { | ||
return msgp.IntSize | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package _generated | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestOmitZero(t *testing.T) { | ||
|
||
t.Run("OmitZeroExt_not_empty", func(t *testing.T) { | ||
|
||
z := OmitZero0{AExt: OmitZeroExt{a: 1}} | ||
b, err := z.MarshalMsg(nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !bytes.Contains(b, []byte("aext")) { | ||
t.Errorf("expected to find aext in bytes %X", b) | ||
} | ||
z = OmitZero0{} | ||
_, err = z.UnmarshalMsg(b) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if z.AExt.a != 1 { | ||
t.Errorf("z.AExt.a expected 1 but got %d", z.AExt.a) | ||
} | ||
|
||
}) | ||
|
||
t.Run("OmitZeroExt_negative", func(t *testing.T) { | ||
|
||
z := OmitZero0{AExt: OmitZeroExt{a: -1}} // negative value should act as empty, via IsEmpty() call | ||
b, err := z.MarshalMsg(nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if bytes.Contains(b, []byte("aext")) { | ||
t.Errorf("expected to not find aext in bytes %X", b) | ||
} | ||
z = OmitZero0{} | ||
_, err = z.UnmarshalMsg(b) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if z.AExt.a != 0 { | ||
t.Errorf("z.AExt.a expected 0 but got %d", z.AExt.a) | ||
} | ||
|
||
}) | ||
|
||
t.Run("OmitZeroTuple", func(t *testing.T) { | ||
|
||
// make sure tuple encoding isn't affected by omitempty or omitzero | ||
|
||
z := OmitZero0{OmitZeroTuple: OmitZeroTuple{FieldA: "", FieldB: "", FieldC: "fcval"}} | ||
b, err := z.MarshalMsg(nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
// verify the exact binary encoding, that the values follow each other without field names | ||
if !bytes.Contains(b, []byte{0xA0, 0xA0, 0xA5, 'f', 'c', 'v', 'a', 'l'}) { | ||
t.Errorf("failed to find expected bytes in %X", b) | ||
} | ||
z = OmitZero0{} | ||
_, err = z.UnmarshalMsg(b) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if z.OmitZeroTuple.FieldA != "" || | ||
z.OmitZeroTuple.FieldB != "" || | ||
z.OmitZeroTuple.FieldC != "fcval" { | ||
t.Errorf("z.OmitZeroTuple unexpected value: %#v", z.OmitZeroTuple) | ||
} | ||
|
||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters