Skip to content

Commit

Permalink
improve number parsing. update json scalar description
Browse files Browse the repository at this point in the history
  • Loading branch information
nasdf committed Oct 3, 2024
1 parent dd0d847 commit dc9c9eb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
4 changes: 4 additions & 0 deletions client/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,10 @@ func getJSON(v any) (any, error) {
return true, nil

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

case fastjson.TypeString:
Expand Down
21 changes: 18 additions & 3 deletions client/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,16 @@ func TestNewFromJSON_WithValidJSONFieldValue_NoError(t *testing.T) {
objWithJSONField := []byte(`{
"Name": "John",
"Age": 26,
"Custom": {"tree": "maple", "age": 260}
"Custom": {
"string": "maple",
"int": 260,
"float": 3.14,
"false": false,
"true": true,
"null": null,
"array": ["one", 1],
"object": {"one": 1}
}
}`)
doc, err := NewDocFromJSON(objWithJSONField, def)
if err != nil {
Expand All @@ -184,8 +193,14 @@ func TestNewFromJSON_WithValidJSONFieldValue_NoError(t *testing.T) {
assert.Equal(t, doc.values[doc.fields["Age"]].Value(), int64(26))
assert.Equal(t, doc.values[doc.fields["Age"]].IsDocument(), false)
assert.Equal(t, doc.values[doc.fields["Custom"]].Value(), map[string]any{
"tree": "maple",
"age": float64(260),
"string": "maple",
"int": int64(260),
"float": float64(3.14),
"false": false,
"true": true,
"null": nil,
"array": []any{"one", int64(1)},
"object": map[string]any{"one": int64(1)},
})
assert.Equal(t, doc.values[doc.fields["Custom"]].IsDocument(), false)
}
Expand Down
12 changes: 5 additions & 7 deletions internal/request/graphql/schema/types/scalars.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,16 @@ func parseJSON(valueAST ast.Value) any {
func JSONScalarType() *graphql.Scalar {
return graphql.NewScalar(graphql.ScalarConfig{
Name: "JSON",
Description: "The `JSON` scalar type represents a JSON string.",
// Serialize converts the value to a json string
Description: "The `JSON` scalar type represents a JSON value.",
// Serialize converts the value to json value
Serialize: func(value any) any {
return value
},
// ParseValue converts the value to a json string
// ParseValue converts the value to json value
ParseValue: func(value any) any {
return value
},
// ParseLiteral converts the ast value to a json string
ParseLiteral: func(valueAST ast.Value) any {
return any(parseJSON(valueAST))
},
// ParseLiteral converts the ast value to a json value
ParseLiteral: parseJSON,
})
}

0 comments on commit dc9c9eb

Please sign in to comment.