Skip to content

Commit

Permalink
protocol: add nullable string support
Browse files Browse the repository at this point in the history
  • Loading branch information
travisjeffery committed Mar 24, 2018
1 parent f50f5b4 commit c98abb7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions protocol/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type PacketDecoder interface {
ArrayLength() (int, error)
Bytes() ([]byte, error)
String() (string, error)
NullableString() (*string, error)
Int32Array() ([]int32, error)
Int64Array() ([]int64, error)
StringArray() ([]string, error)
Expand Down Expand Up @@ -168,6 +169,12 @@ func (d *ByteDecoder) String() (string, error) {
return tmpStr, nil
}

func (d *ByteDecoder) NullableString() (*string, error) {
tmpStr, err := d.String()
return &tmpStr, err

}

func (d *ByteDecoder) Int32Array() ([]int32, error) {
if d.remaining() < 4 {
d.off = len(d.b)
Expand Down
17 changes: 17 additions & 0 deletions protocol/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type PacketEncoder interface {
PutRawBytes(in []byte) error
PutBytes(in []byte) error
PutString(in string) error
PutNullableString(in *string) error
PutStringArray(in []string) error
PutInt32Array(in []int32) error
PutInt64Array(in []int64) error
Expand Down Expand Up @@ -110,6 +111,14 @@ func (e *LenEncoder) PutString(in string) error {
return nil
}

func (e *LenEncoder) PutNullableString(in *string) error {
if in == nil {
e.Length += 2
return nil
}
return e.PutString(*in)
}

func (e *LenEncoder) PutStringArray(in []string) error {
err := e.PutArrayLength(len(in))
if err != nil {
Expand Down Expand Up @@ -219,6 +228,14 @@ func (e *ByteEncoder) PutString(in string) error {
return nil
}

func (e *ByteEncoder) PutNullableString(in *string) error {
if in == nil {
e.PutInt16(-1)
return nil
}
return e.PutString(*in)
}

func (e *ByteEncoder) PutStringArray(in []string) error {
err := e.PutArrayLength(len(in))
if err != nil {
Expand Down

0 comments on commit c98abb7

Please sign in to comment.