Skip to content

Commit eeeb721

Browse files
authored
fix: accept empty values and buffers in comp-3 codec (#43)
* fix: accept empty values and buffers in the COMP-3 codec * fix: accept empty values and buffers in the COMP-3 codec * fix: accept invalid COMP-3 buffers * fix: accept invalid COMP-3 buffers * fix: init buffers with 0 instead of spaces * fix: revert comp-3 alpha nibbles
1 parent c8612ea commit eeeb721

File tree

10 files changed

+24
-37
lines changed

10 files changed

+24
-37
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Types of changes
1616

1717
## [0.3.1]
1818

19-
- `Fixed` accept null values and buffers in the COMP-3 codec.
19+
- `Fixed` accept empty values and buffers in the COMP-3 codec.
2020

2121
## [0.3.0]
2222

internal/appli/command/fold.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,13 @@ func (f *Fold) execUntilEOF(cfg config.Config, buffer api.Buffer, writer documen
119119
}
120120
}()
121121

122-
space, err := charsets.GetByteInCharset(f.charset, ' ')
123-
if err != nil {
124-
return fmt.Errorf("failed to get space in charset : %w", err)
125-
}
126-
127122
sep, err := charsets.GetBytesInCharset(f.charset, cfg.Separator)
128123
if err != nil {
129124
return fmt.Errorf("failed to get separator in charset : %w", err)
130125
}
131126

132127
for {
133-
if err := buffer.Reset(space, cfg.Length, sep...); errors.Is(err, io.EOF) {
128+
if err := buffer.Reset(0, cfg.Length, sep...); errors.Is(err, io.EOF) {
134129
return nil
135130
} else if err != nil {
136131
return fmt.Errorf("failed to read next buffer : %w", err)
4 Bytes
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{"compressed":"12345,67"}
22
{"compressed":"+12345,67"}
33
{"compressed":"-12345,67"}
4+
{"compressed":""}

internal/appli/command/testdata/unfold/13-comp3/stdin.jsonl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
{"compressed":"+12345,67"}
33
{"compressed":"-12345.67"}
44
{"compressed":"123,45"}
5+
{"compressed":""}
Binary file not shown.
Binary file not shown.

internal/appli/command/unfold.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,13 @@ func (u *Unfold) execute(cmd *cobra.Command, _ []string) error {
110110
}
111111

112112
func (u *Unfold) execUntilEOF(cfg config.Config, buffer api.Buffer, reader document.Reader, record api.Record) error {
113-
space, err := charsets.GetByteInCharset(u.charset, ' ')
114-
if err != nil {
115-
return fmt.Errorf("failed to get space in charset : %w", err)
116-
}
117-
118113
sep, err := charsets.GetBytesInCharset(u.charset, cfg.Separator)
119114
if err != nil {
120115
return fmt.Errorf("failed to get separator in charset : %w", err)
121116
}
122117

123118
for {
124-
if err := buffer.Reset(space, cfg.Length, sep...); err != nil {
119+
if err := buffer.Reset(0, cfg.Length, sep...); err != nil {
125120
return fmt.Errorf("failed to dump buffer : %w", err)
126121
}
127122

pkg/posimap/core/codec/comp3.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (c *Comp3) Decode(buffer api.Buffer, offset int) (any, error) {
7979
}
8080

8181
if isNull(bytes) {
82-
return nil, nil //nolint:nilnil
82+
return "", nil
8383
}
8484

8585
for byteIndex, byteVal := range bytes {
@@ -94,10 +94,7 @@ func (c *Comp3) Decode(buffer api.Buffer, offset int) (any, error) {
9494
result.WriteRune(convertNibbleToRune(high))
9595
}
9696

97-
sign, err := handleSign(byteVal)
98-
if err != nil {
99-
return result.String(), err
100-
}
97+
sign := handleSign(byteVal)
10198

10299
return sign + result.String(), nil
103100
}
@@ -115,7 +112,7 @@ func (c *Comp3) Decode(buffer api.Buffer, offset int) (any, error) {
115112
}
116113

117114
func (c *Comp3) Encode(buffer api.Buffer, offset int, value any) error {
118-
if value == nil {
115+
if value == "" {
119116
// write null value (all bytes set to zero)
120117
if err := buffer.Write(offset, make([]byte, c.size)); err != nil {
121118
return fmt.Errorf("%w", err)
@@ -263,21 +260,21 @@ func convertRuneToNibble(runeVal rune) (byte, error) {
263260
return 0, fmt.Errorf("%w: invalid rune %q", ErrInvalidComp3Nibble, runeVal)
264261
}
265262

266-
func handleSign(byteVal byte) (string, error) {
263+
func handleSign(byteVal byte) string {
267264
signNibble := byteVal & lowNibbleMask
268265

269266
if signNibble != signNibblePositive && signNibble != signNibbleNegative && signNibble != signNibbleZero {
270-
return "", fmt.Errorf("%w: 0x%X", ErrInvalidComp3Sign, signNibble)
267+
return "#"
271268
}
272269

273270
switch signNibble {
274271
case signNibbleNegative:
275-
return "-", nil
272+
return "-"
276273
case signNibblePositive:
277-
return "+", nil
274+
return "+"
278275
}
279276

280-
return "", nil
277+
return ""
281278
}
282279

283280
func isNull(bytes []byte) bool {

pkg/posimap/core/codec/comp3_test.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,15 @@ func TestComp3_Decode(t *testing.T) {
7171
intDigits: 5,
7272
decDigits: 2,
7373
sep: '.',
74-
expected: "12345.67",
75-
wantErr: true,
74+
expected: "#12345.67",
75+
},
76+
{
77+
name: "invalid digit nibble",
78+
data: []byte{0x12, 0x3A, 0x5C},
79+
intDigits: 2,
80+
decDigits: 3,
81+
sep: '.',
82+
expected: "+12.3A5",
7683
},
7784
{
7885
name: "short buffer",
@@ -128,7 +135,7 @@ func TestComp3_Decode(t *testing.T) {
128135
intDigits: 5,
129136
decDigits: 2,
130137
sep: ',',
131-
expected: nil,
138+
expected: "", // null value is decoded as empty string
132139
},
133140
}
134141

@@ -255,8 +262,7 @@ func TestComp3_Encode(t *testing.T) {
255262
intDigits: 5,
256263
decDigits: 2,
257264
sep: '.',
258-
expected: []byte{},
259-
wantErr: true,
265+
expected: []byte{0x00, 0x00, 0x00, 0x00},
260266
},
261267
{
262268
name: "misplaced decimal",
@@ -293,14 +299,6 @@ func TestComp3_Encode(t *testing.T) {
293299
sep: '.', // even if the expected separator is '.', accept ',' in input
294300
expected: []byte{0x12, 0x34, 0x56, 0x7C},
295301
},
296-
{
297-
name: "null value",
298-
value: nil,
299-
intDigits: 5,
300-
decDigits: 2,
301-
sep: '.',
302-
expected: []byte{0x00, 0x00, 0x00, 0x00},
303-
},
304302
}
305303

306304
for _, testcase := range tests {

0 commit comments

Comments
 (0)