Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENG-6203: Encode/decode stream token values #139

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
typeTagDoc typeTag = "@doc"
typeTagRef typeTag = "@ref"
typeTagSet typeTag = "@set"
typeTagStream typeTag = "@stream"
typeTagMod typeTag = "@mod"
typeTagObject typeTag = "@object"
)
Expand Down Expand Up @@ -96,6 +97,8 @@ func (p Page) Unmarshal(into any) error {
return decodeInto(p.Data, into)
}

type Stream string

func mapDecoder(into any) (*mapstructure.Decoder, error) {
return mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "fauna",
Expand Down Expand Up @@ -232,6 +235,8 @@ func unboxType(body map[string]any) (any, error) {
return unboxRef(v.(map[string]any))
case typeTagSet:
return unboxSet(v)
case typeTagStream:
return unboxStream(v)
case typeTagDoc:
return unboxDoc(v.(map[string]any))
case typeTagObject:
Expand Down Expand Up @@ -400,6 +405,14 @@ func unboxSet(v any) (any, error) {
return nil, fmt.Errorf("invalid set %v", v)
}

func unboxStream(v any) (any, error) {
if token, ok := v.(string); ok {
return Stream(token), nil
} else {
return nil, fmt.Errorf("invalid stream %v", v)
}
}

func unboxTime(v string) (*time.Time, error) {
if t, err := time.Parse(timeFormat, v); err != nil {
return nil, err
Expand Down Expand Up @@ -466,6 +479,9 @@ func encode(v any, hint string) (any, error) {
case Page:
return encodeFaunaStruct(typeTagSet, vt)

case Stream:
return map[typeTag]any{typeTagStream: vt}, nil

case time.Time:
return encodeTime(vt, hint)

Expand Down
6 changes: 6 additions & 0 deletions serializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type BusinessObj struct {
RefField Ref `fauna:"ref_field"`
NamedRefField NamedRef `fauna:"named_ref_field"`
SetField Page `fauna:"set_field"`
StreamField Stream `fauna:"stream_field"`
ObjField SubBusinessObj `fauna:"obj_field"`
DocField DocBusinessObj `fauna:"doc_field"`
NamedDocField NamedDocBusinessObj `fauna:"named_doc_field"`
Expand Down Expand Up @@ -266,6 +267,11 @@ func TestEncodingFaunaStructs(t *testing.T) {
roundTripCheck(t, obj, `{"@set":{"data":["0","1","2"],"after":"foobarbaz"}}`)
})

t.Run("encodes Stream", func(t *testing.T) {
stream := Stream("abcd==")
roundTripCheck(t, stream, `{"@stream":"abcd=="}`)
})

t.Run("encode NullDoc", func(t *testing.T) {
obj := NullDocument{Cause: "Foo", Ref: &Ref{ID: "1234", Coll: &Module{"Foo"}}}
roundTripCheck(t, obj, `{"cause": "Foo", "ref": {"@ref":{"id":"1234","coll":{"@mod":"Foo"}}}}`)
Expand Down
Loading