Skip to content

Slice struct with all "omitempty" fields set to zero value ​​will generate wrong data #376

@ljfuyuan

Description

@ljfuyuan

look at this sample below:

type Sample struct {
	K        uint32 `msg:"k,omitempty"`
        V        uint32 `msg:"v,omitempty"`
}

type Samples []Sample

and now we construct a set of test cases:

s := Samples{
		{1, 1},
		{0, 0},
		{3, 3},
	}

and the generated code is:

	for za0001 := range z {
		// check for omitted fields
		zb0001Len := uint32(2)
		var zb0001Mask uint8 /* 2 bits */
		_ = zb0001Mask
		if z[za0001].K == 0 {
			zb0001Len--
			zb0001Mask |= 0x1
		}
		if z[za0001].V == 0 {
			zb0001Len--
			zb0001Mask |= 0x2
		}
		// variable map header, size zb0001Len
		o = append(o, 0x80|uint8(zb0001Len))
		if zb0001Len == 0 {
			return     //  <---- !!! When looping to the second element, wrong data will be generated due to return. Continue may be used here. 

		}
...... ignore

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions