Skip to content

Commit

Permalink
add support for @bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
cynicaljoy committed May 19, 2024
1 parent 1344e06 commit 2deb8f6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
21 changes: 21 additions & 0 deletions serializer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fauna

import (
"encoding/base64"
"encoding/json"
"fmt"
"reflect"
Expand Down Expand Up @@ -37,6 +38,7 @@ const (
typeTagStream typeTag = "@stream"
typeTagMod typeTag = "@mod"
typeTagObject typeTag = "@object"
typeTagBytes typeTag = "@bytes"
)

func keyConflicts(key string) bool {
Expand Down Expand Up @@ -241,6 +243,8 @@ func unboxType(body map[string]any) (any, error) {
return unboxDoc(v.(map[string]any))
case typeTagObject:
return convertMap(v.(map[string]any))
case typeTagBytes:
return unboxBytes(v.(string))
}
}
}
Expand Down Expand Up @@ -445,6 +449,14 @@ func unboxDouble(v string) (any, error) {
}
}

func unboxBytes(v string) (any, error) {
if b, err := base64.StdEncoding.DecodeString(v); err != nil {
return nil, err
} else {
return b, nil
}
}

func marshal(v any) ([]byte, error) {
if enc, err := encode(v, ""); err != nil {
return nil, err
Expand Down Expand Up @@ -507,6 +519,9 @@ func encode(v any, hint string) (any, error) {
out["start_ts"] = vt.StartTS
}
return out, nil

case []byte:
return encodeBytes(vt)
}

switch value := reflect.ValueOf(v); value.Kind() {
Expand Down Expand Up @@ -546,6 +561,12 @@ func encode(v any, hint string) (any, error) {
return v, nil
}

func encodeBytes(b []byte) (any, error) {
return map[typeTag]any{
typeTagBytes: base64.StdEncoding.EncodeToString(b),
}, nil
}

func encodeInt(i int64) (any, error) {
tag := typeTagLong
if i <= maxInt && i >= minInt {
Expand Down
17 changes: 16 additions & 1 deletion serializer_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fauna

import (
"encoding/base64"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -55,6 +56,7 @@ type BusinessObj struct {
NamedDocField NamedDocBusinessObj `fauna:"named_doc_field"`
NullDocField NullDocBusinessObj `fauna:"nulldoc_field"`
NullNamedDocField NullNamedDocBusinessObj `fauna:"nulldoc_named_field"`
BytesField []byte `fauna:"bytes_field"`

IgnoredField string `fauna:"-"`
}
Expand Down Expand Up @@ -108,7 +110,8 @@ func TestRoundtrip(t *testing.T) {
"tuple_field": ["one",2,3.0],
"int_field": 1234,
"double_field": 1234.567
}}
}},
"bytes_field": {"@bytes":"SGVsbG8sIGZyb20gRmF1bmEh"}
}`)

obj := &BusinessObj{}
Expand Down Expand Up @@ -185,6 +188,18 @@ func TestEncodingPrimitives(t *testing.T) {
assert.Nil(t, decoded)
}
})

t.Run("encode bytes", func(t *testing.T) {
inputBytes := []byte("This is a test string 🚀 with various characters: !@#$%^&*()_+=-`~[]{}|;:'\\\",./<>?")
encoded := base64.StdEncoding.EncodeToString(inputBytes)

bs := marshalAndCheck(t, inputBytes)
if assert.JSONEq(t, `{"@bytes": "`+encoded+`"}`, string(bs)) {
var decoded []byte
unmarshalAndCheck(t, bs, &decoded)
assert.Equal(t, inputBytes, decoded)
}
})
}

func TestEncodingTime(t *testing.T) {
Expand Down

0 comments on commit 2deb8f6

Please sign in to comment.