Skip to content

Commit

Permalink
Add json.Number support to UnmarshalString (#2396)
Browse files Browse the repository at this point in the history
* Add json.Number support to UnmarshalString

* Add UnmarshalString tests

* Remove trailing zeros when calling UnmarshalString with float64
  • Loading branch information
Desuuuu committed Sep 28, 2022
1 parent daa4407 commit a9d0603
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
5 changes: 4 additions & 1 deletion graphql/string.go
@@ -1,6 +1,7 @@
package graphql

import (
"encoding/json"
"fmt"
"io"
"strconv"
Expand Down Expand Up @@ -55,7 +56,9 @@ func UnmarshalString(v interface{}) (string, error) {
case int64:
return strconv.FormatInt(v, 10), nil
case float64:
return fmt.Sprintf("%f", v), nil
return strconv.FormatFloat(v, 'f', -1, 64), nil
case json.Number:
return string(v), nil
case bool:
if v {
return "true", nil
Expand Down
45 changes: 30 additions & 15 deletions graphql/string_test.go
@@ -1,27 +1,42 @@
package graphql

import (
"bytes"
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)

func TestString(t *testing.T) {
assert.Equal(t, `"hello"`, doStrMarshal("hello"))
assert.Equal(t, `"he\tllo"`, doStrMarshal("he\tllo"))
assert.Equal(t, `"he\tllo"`, doStrMarshal("he llo"))
assert.Equal(t, `"he\nllo"`, doStrMarshal("he\nllo"))
assert.Equal(t, `"he\r\nllo"`, doStrMarshal("he\r\nllo"))
assert.Equal(t, `"he\\llo"`, doStrMarshal(`he\llo`))
assert.Equal(t, `"quotes\"nested\"in\"quotes\""`, doStrMarshal(`quotes"nested"in"quotes"`))
assert.Equal(t, `"\u0000"`, doStrMarshal("\u0000"))
assert.Equal(t, `"\u0000"`, doStrMarshal("\u0000"))
assert.Equal(t, "\"\U000fe4ed\"", doStrMarshal("\U000fe4ed"))
t.Run("marshal", func(t *testing.T) {
assert.Equal(t, `"hello"`, m2s(MarshalString("hello")))
assert.Equal(t, `"he\tllo"`, m2s(MarshalString("he\tllo")))
assert.Equal(t, `"he\tllo"`, m2s(MarshalString("he llo")))
assert.Equal(t, `"he\nllo"`, m2s(MarshalString("he\nllo")))
assert.Equal(t, `"he\r\nllo"`, m2s(MarshalString("he\r\nllo")))
assert.Equal(t, `"he\\llo"`, m2s(MarshalString(`he\llo`)))
assert.Equal(t, `"quotes\"nested\"in\"quotes\""`, m2s(MarshalString(`quotes"nested"in"quotes"`)))
assert.Equal(t, `"\u0000"`, m2s(MarshalString("\u0000")))
assert.Equal(t, `"\u0000"`, m2s(MarshalString("\u0000")))
assert.Equal(t, "\"\U000fe4ed\"", m2s(MarshalString("\U000fe4ed")))
})

t.Run("unmarshal", func(t *testing.T) {
assert.Equal(t, "123", mustUnmarshalString("123"))
assert.Equal(t, "123", mustUnmarshalString(123))
assert.Equal(t, "123", mustUnmarshalString(int64(123)))
assert.Equal(t, "123", mustUnmarshalString(float64(123)))
assert.Equal(t, "123", mustUnmarshalString(json.Number("123")))
assert.Equal(t, "true", mustUnmarshalString(true))
assert.Equal(t, "false", mustUnmarshalString(false))
assert.Equal(t, "null", mustUnmarshalString(nil))
})
}

func doStrMarshal(s string) string {
var buf bytes.Buffer
MarshalString(s).MarshalGQL(&buf)
return buf.String()
func mustUnmarshalString(v interface{}) string {
res, err := UnmarshalString(v)
if err != nil {
panic(err)
}
return res
}

0 comments on commit a9d0603

Please sign in to comment.