Skip to content

Commit

Permalink
Introduce JSON interface with normalized values
Browse files Browse the repository at this point in the history
  • Loading branch information
islamaliev committed Nov 21, 2024
1 parent f098bf8 commit 5e4cffc
Show file tree
Hide file tree
Showing 16 changed files with 585 additions and 321 deletions.
70 changes: 2 additions & 68 deletions client/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,11 @@ func validateFieldSchema(val any, field FieldDefinition) (NormalValue, error) {
return NewNormalNillableIntArray(v), nil

case FieldKind_NILLABLE_JSON:
v, err := getJSON(val)
v, err := NewJSON(val)
if err != nil {
return nil, err
}
return NewNormalJSON(&JSON{v}), nil
return NewNormalJSON(v), nil
}

return nil, NewErrUnhandledType("FieldKind", field.Kind)
Expand Down Expand Up @@ -438,72 +438,6 @@ func getDateTime(v any) (time.Time, error) {
return time.Parse(time.RFC3339, s)
}

// getJSON converts the given value to a valid JSON value.
//
// If the value is of type *fastjson.Value it needs to be
// manually parsed. All other values are valid JSON.
func getJSON(v any) (any, error) {
val, ok := v.(*fastjson.Value)
if !ok {
return v, nil
}
switch val.Type() {
case fastjson.TypeArray:
arr, err := val.Array()
if err != nil {
return nil, err
}
out := make([]any, len(arr))
for i, v := range arr {
c, err := getJSON(v)
if err != nil {
return nil, err
}
out[i] = c
}
return out, nil

case fastjson.TypeObject:
obj, err := val.Object()
if err != nil {
return nil, err
}
out := make(map[string]any)
obj.Visit(func(key []byte, v *fastjson.Value) {
c, e := getJSON(v)
out[string(key)] = c
err = errors.Join(err, e)
})
return out, err

case fastjson.TypeFalse:
return false, nil

case fastjson.TypeTrue:
return true, nil

case fastjson.TypeNumber:
out, err := val.Int64()
if err == nil {
return out, nil
}
return val.Float64()

case fastjson.TypeString:
out, err := val.StringBytes()
if err != nil {
return nil, err
}
return string(out), nil

case fastjson.TypeNull:
return nil, nil

default:
return nil, NewErrInvalidJSONPayload(v)
}
}

func getArray[T any](
v any,
typeGetter func(any) (T, error),
Expand Down
6 changes: 3 additions & 3 deletions client/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,13 @@ func TestNewFromJSON_WithValidJSONFieldValue_NoError(t *testing.T) {
assert.Equal(t, doc.values[doc.fields["Age"]].IsDocument(), false)
assert.Equal(t, doc.values[doc.fields["Custom"]].Value(), map[string]any{
"string": "maple",
"int": int64(260),
"int": float64(260),
"float": float64(3.14),
"false": false,
"true": true,
"null": nil,
"array": []any{"one", int64(1)},
"object": map[string]any{"one": int64(1)},
"array": []any{"one", float64(1)},
"object": map[string]any{"one": float64(1)},
})
assert.Equal(t, doc.values[doc.fields["Custom"]].IsDocument(), false)
}
Expand Down
Loading

0 comments on commit 5e4cffc

Please sign in to comment.