Skip to content

Commit

Permalink
Merge pull request #236 from goccy/feature/fix-encoder
Browse files Browse the repository at this point in the history
Fix value of totalLength for encoding
  • Loading branch information
goccy authored Jun 2, 2021
2 parents 6562473 + 544e731 commit 07920c8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
19 changes: 19 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1893,3 +1893,22 @@ func TestIssue180(t *testing.T) {
t.Fatalf("failed to equal encoded result: expected %s but got %s", string(b1), string(b2))
}
}

func TestIssue235(t *testing.T) {
type TaskMessage struct {
Type string
Payload map[string]interface{}
UniqueKey string
}
msg := TaskMessage{
Payload: map[string]interface{}{
"sent_at": map[string]interface{}{
"Time": "0001-01-01T00:00:00Z",
"Valid": false,
},
},
}
if _, err := json.Marshal(msg); err != nil {
t.Fatal(err)
}
}
24 changes: 22 additions & 2 deletions internal/encoder/opcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ type Opcode struct {
DisplayKey string // key text to display
}

func (c *Opcode) MaxIdx() uint32 {
max := uint32(0)
for _, value := range []uint32{
c.Idx,
c.ElemIdx,
c.Length,
c.MapIter,
c.MapPos,
c.Size,
} {
if max < value {
max = value
}
}
return max
}

func (c *Opcode) ToHeaderType(isString bool) OpType {
switch c.Op {
case OpInt:
Expand Down Expand Up @@ -338,7 +355,10 @@ func (c *Opcode) BeforeLastCode() *Opcode {
func (c *Opcode) TotalLength() int {
var idx int
for code := c; code.Op != OpEnd; {
idx = int(code.Idx / uintptrSize)
maxIdx := int(code.MaxIdx() / uintptrSize)
if idx < maxIdx {
idx = maxIdx
}
if code.Op == OpRecursiveEnd {
break
}
Expand All @@ -349,7 +369,7 @@ func (c *Opcode) TotalLength() int {
code = code.Next
}
}
return idx + 2 // opEnd + 1
return idx + 1
}

func (c *Opcode) decOpcodeIndex() {
Expand Down

0 comments on commit 07920c8

Please sign in to comment.