Skip to content

Commit

Permalink
Merge pull request #1340 from bickyeric/master
Browse files Browse the repository at this point in the history
serialize ID just like String
  • Loading branch information
lwc authored Sep 8, 2021
2 parents 522cab5 + fa371b9 commit a557c90
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
4 changes: 1 addition & 3 deletions graphql/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (
)

func MarshalID(s string) Marshaler {
return WriterFunc(func(w io.Writer) {
io.WriteString(w, strconv.Quote(s))
})
return MarshalString(s)
}
func UnmarshalID(v interface{}) (string, error) {
switch v := v.(type) {
Expand Down
20 changes: 20 additions & 0 deletions graphql/id_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
package graphql

import (
"bytes"
"math"
"testing"

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

func TestMarshalID(t *testing.T) {
marshalID := func(s string) string {
var buf bytes.Buffer
MarshalID(s).MarshalGQL(&buf)
return buf.String()
}

assert.Equal(t, `"hello"`, marshalID("hello"))
assert.Equal(t, `"he\tllo"`, marshalID("he\tllo"))
assert.Equal(t, `"he\tllo"`, marshalID("he llo"))
assert.Equal(t, `"he\nllo"`, marshalID("he\nllo"))
assert.Equal(t, `"he\r\nllo"`, marshalID("he\r\nllo"))
assert.Equal(t, `"he\\llo"`, marshalID(`he\llo`))
assert.Equal(t, `"quotes\"nested\"in\"quotes\""`, marshalID(`quotes"nested"in"quotes"`))
assert.Equal(t, `"\u0000"`, marshalID("\u0000"))
assert.Equal(t, "\"\U000fe4ed\"", marshalID("\U000fe4ed"))
assert.Equal(t, "\"\\u001B\"", marshalID("\u001B"))
}

func TestUnmarshalID(t *testing.T) {
tests := []struct {
Name string
Input interface{}
Expand Down

0 comments on commit a557c90

Please sign in to comment.