Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Types of changes
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## [0.3.1]

- `Fixed` accept null values and buffers in the COMP-3 codec.

## [0.3.0]

- `Added` COMP-3 codec now accepts both `.` and `,` as decimal separators from JSON input.
Expand Down
23 changes: 23 additions & 0 deletions pkg/posimap/core/codec/comp3.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func (c *Comp3) Decode(buffer api.Buffer, offset int) (any, error) {
return nil, fmt.Errorf("%w", err) // return error if buffer is too short
}

if isNull(bytes) {
return nil, nil //nolint:nilnil
}

for byteIndex, byteVal := range bytes {
if byteIndex*2 == c.intDigits {
result.WriteRune(c.sep)
Expand Down Expand Up @@ -111,6 +115,15 @@ func (c *Comp3) Decode(buffer api.Buffer, offset int) (any, error) {
}

func (c *Comp3) Encode(buffer api.Buffer, offset int, value any) error {
if value == nil {
// write null value (all bytes set to zero)
if err := buffer.Write(offset, make([]byte, c.size)); err != nil {
return fmt.Errorf("%w", err)
}

return nil
}

nibbleSign, str, err := c.detectSignAndAddLeadingZeroes(value)
if err != nil {
return err
Expand Down Expand Up @@ -266,3 +279,13 @@ func handleSign(byteVal byte) (string, error) {

return "", nil
}

func isNull(bytes []byte) bool {
for _, b := range bytes {
if b != 0 {
return false
}
}

return true
}
16 changes: 16 additions & 0 deletions pkg/posimap/core/codec/comp3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ func TestComp3_Decode(t *testing.T) {
sep: ',',
expected: "+12345,67",
},
{
name: "null buffer",
data: []byte{0x00, 0x00, 0x00, 0x00},
intDigits: 5,
decDigits: 2,
sep: ',',
expected: nil,
},
}

for _, testcase := range tests {
Expand Down Expand Up @@ -285,6 +293,14 @@ func TestComp3_Encode(t *testing.T) {
sep: '.', // even if the expected separator is '.', accept ',' in input
expected: []byte{0x12, 0x34, 0x56, 0x7C},
},
{
name: "null value",
value: nil,
intDigits: 5,
decDigits: 2,
sep: '.',
expected: []byte{0x00, 0x00, 0x00, 0x00},
},
}

for _, testcase := range tests {
Expand Down