Skip to content

Commit

Permalink
Refine the codec function
Browse files Browse the repository at this point in the history
Signed-off-by: JmPotato <[email protected]>
  • Loading branch information
JmPotato committed Nov 28, 2023
1 parent eeaf477 commit 2205946
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions client/http/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ func encodeBytes(data []byte) []byte {
return result
}

func decodeBytes(b []byte) ([]byte, []byte, error) {
func decodeBytes(b []byte) ([]byte, error) {
buf := make([]byte, 0, len(b))
for {
if len(b) < encGroupSize+1 {
return nil, nil, errors.New("insufficient bytes to decode value")
return nil, errors.New("insufficient bytes to decode value")
}

groupBytes := b[:encGroupSize+1]
Expand All @@ -78,7 +78,7 @@ func decodeBytes(b []byte) ([]byte, []byte, error) {

padCount := encMarker - marker
if padCount > encGroupSize {
return nil, nil, errors.Errorf("invalid marker byte, group bytes %q", groupBytes)
return nil, errors.Errorf("invalid marker byte, group bytes %q", groupBytes)
}

realGroupSize := encGroupSize - padCount
Expand All @@ -89,11 +89,11 @@ func decodeBytes(b []byte) ([]byte, []byte, error) {
// Check validity of padding bytes.
for _, v := range group[realGroupSize:] {
if v != encPad {
return nil, nil, errors.Errorf("invalid padding byte, group bytes %q", groupBytes)
return nil, errors.Errorf("invalid padding byte, group bytes %q", groupBytes)
}
}
break
}
}
return b, buf, nil
return buf, nil
}
4 changes: 2 additions & 2 deletions client/http/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestBytesCodec(t *testing.T) {
b := encodeBytes(input.enc)
require.Equal(t, input.dec, b)

_, d, err := decodeBytes(b)
d, err := decodeBytes(b)
require.NoError(t, err)
require.Equal(t, input.enc, d)
}
Expand All @@ -58,7 +58,7 @@ func TestBytesCodec(t *testing.T) {
}

for _, input := range errInputs {
_, _, err := decodeBytes(input)
_, err := decodeBytes(input)
require.Error(t, err)
}
}
8 changes: 4 additions & 4 deletions client/http/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,12 @@ func (r *Rule) UnmarshalJSON(bytes []byte) error {
return err
}

_, r.StartKey, err = decodeBytes(startKey)
r.StartKey, err = decodeBytes(startKey)
if err != nil {
return err
}

_, r.EndKey, err = decodeBytes(endKey)
r.EndKey, err = decodeBytes(endKey)

return err
}
Expand Down Expand Up @@ -388,12 +388,12 @@ func (r *RuleOp) UnmarshalJSON(bytes []byte) error {
return err
}

_, r.StartKey, err = decodeBytes(startKey)
r.StartKey, err = decodeBytes(startKey)
if err != nil {
return err
}

_, r.EndKey, err = decodeBytes(endKey)
r.EndKey, err = decodeBytes(endKey)

return err
}
Expand Down

0 comments on commit 2205946

Please sign in to comment.