Exit early if stringy values have 0 length when doing gob encoding

This commit is contained in:
mariusor 2021-12-30 15:53:51 +01:00
parent 3fe3ad49be
commit a0add4eee6
No known key found for this signature in database
GPG key ID: DBF5E47F5DBC4D21
2 changed files with 13 additions and 4 deletions

View file

@ -204,6 +204,9 @@ func encodeGobStringLikeType(g *gob.Encoder, s []byte) error {
}
func (l LangRef) GobEncode() ([]byte, error) {
if len(l) == 0 {
return []byte{}, nil
}
b := new(bytes.Buffer)
gg := gob.NewEncoder(b)
if err := encodeGobStringLikeType(gg, []byte(l)); err != nil {
@ -262,6 +265,9 @@ type kv struct {
}
func (l LangRefValue) GobEncode() ([]byte, error) {
if len(l.Value) == 0 && len(l.Ref) == 0 {
return []byte{}, nil
}
b := new(bytes.Buffer)
gg := gob.NewEncoder(b)
mm := kv{
@ -337,9 +343,12 @@ func (c *Content) UnmarshalText(data []byte) error {
}
func (c Content) GobEncode() ([]byte, error) {
if len(c) == 0 {
return []byte{}, nil
}
b := new(bytes.Buffer)
gg := gob.NewEncoder(b)
if err := encodeGobStringLikeType(gg, []byte(c)); err != nil {
if err := encodeGobStringLikeType(gg, c); err != nil {
return nil, err
}
return b.Bytes(), nil

View file

@ -561,7 +561,7 @@ func TestContent_GobEncode(t *testing.T) {
{
name: "empty",
c: Content{},
want: gobValue([]byte{}),
want: []byte{},
wantErr: false,
},
{
@ -665,7 +665,7 @@ func TestLangRef_GobEncode(t *testing.T) {
{
name: "empty",
l: "",
want: gobValue([]byte{}),
want: []byte{},
wantErr: false,
},
{
@ -703,7 +703,7 @@ func TestLangRefValue_GobEncode(t *testing.T) {
{
name: "empty",
fields: fields{},
want: gobValue(kv{}),
want: []byte{},
wantErr: false,
},
{