tinylib / msgp

A Go code generator for MessagePack / msgpack.org[Go]
MIT License
1.77k stars 189 forks source link

Fix ignored tag directives on unnamed fields #352

Closed klauspost closed 4 days ago

klauspost commented 4 days ago

Example:

type S struct {
    ABool bool `msg:",omitempty"`
}

... would ignore the omitempty, since all tags were replaced, resulting in output like:

// MarshalMsg implements msgp.Marshaler
func (z S) MarshalMsg(b []byte) (o []byte, err error) {
    o = msgp.Require(b, z.Msgsize())
    // map header, size 1
    // string "ABool"
    o = append(o, 0x81, 0xa5, 0x41, 0x42, 0x6f, 0x6f, 0x6c)
    o = msgp.AppendBool(o, z.ABool)
    return
}

Keep remaining tags when filling out the field name.

Code after:

// MarshalMsg implements msgp.Marshaler
func (z S) MarshalMsg(b []byte) (o []byte, err error) {
    o = msgp.Require(b, z.Msgsize())
    // check for omitted fields
    zb0001Len := uint32(1)
    var zb0001Mask uint8 /* 1 bits */
    _ = zb0001Mask
    if z.ABool == false {
        zb0001Len--
        zb0001Mask |= 0x1
    }
    // variable map header, size zb0001Len
    o = append(o, 0x80|uint8(zb0001Len))
    if zb0001Len == 0 {
        return
    }
    if (zb0001Mask & 0x1) == 0 { // if not omitted
        // string "ABool"
        o = append(o, 0xa5, 0x41, 0x42, 0x6f, 0x6f, 0x6c)
        o = msgp.AppendBool(o, z.ABool)
    }
    return
}

Regression test added.