Skip to content

Commit

Permalink
factor out getStringLength, use putArrayLength
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin committed Dec 5, 2017
1 parent b3f149d commit 7957e72
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
4 changes: 3 additions & 1 deletion create_partitions_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ func (t *TopicPartition) encode(pe packetEncoder) error {
return nil
}

pe.putInt32(int32(len(t.Assignment)))
if err := pe.putArrayLength(len(t.Assignment)); err != nil {
return err
}

for _, assign := range t.Assignment {
if err := pe.putInt32Array(assign); err != nil {
Expand Down
35 changes: 21 additions & 14 deletions real_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,25 +128,29 @@ func (rd *realDecoder) getVarintBytes() ([]byte, error) {
return rd.getRawBytes(int(tmp))
}

func (rd *realDecoder) getString() (string, error) {
tmp, err := rd.getInt16()

func (rd *realDecoder) getStringLength() (int, error) {
length, err := rd.getInt16()
if err != nil {
return "", err
return 0, err
}

n := int(tmp)
n := int(length)

switch {
case n < -1:
return "", errInvalidStringLength
case n == -1:
return "", nil
case n == 0:
return "", nil
return 0, errInvalidStringLength
case n > rd.remaining():
rd.off = len(rd.raw)
return "", ErrInsufficientData
return 0, ErrInsufficientData
}

return n, nil
}

func (rd *realDecoder) getString() (string, error) {
n, err := rd.getStringLength()
if err != nil || n == -1 {
return "", err
}

tmpStr := string(rd.raw[rd.off : rd.off+n])
Expand All @@ -155,11 +159,14 @@ func (rd *realDecoder) getString() (string, error) {
}

func (rd *realDecoder) getNullableString() (*string, error) {
str, err := rd.getString()
if err != nil || str == "" {
n, err := rd.getStringLength()
if err != nil || n == -1 {
return nil, err
}
return &str, err

tmpStr := string(rd.raw[rd.off : rd.off+n])
rd.off += n
return &tmpStr, err
}

func (rd *realDecoder) getInt32Array() ([]int32, error) {
Expand Down

0 comments on commit 7957e72

Please sign in to comment.