From fa371b9bb76e478dabc649564e1f465c45057f72 Mon Sep 17 00:00:00 2001 From: Bicky Eric Kantona Date: Sat, 26 Sep 2020 01:38:48 +0700 Subject: [PATCH] serialize ID just like String --- graphql/id.go | 4 +--- graphql/id_test.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/graphql/id.go b/graphql/id.go index 2e78a5ec4b2..f1c5886a9ef 100644 --- a/graphql/id.go +++ b/graphql/id.go @@ -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) { diff --git a/graphql/id_test.go b/graphql/id_test.go index ad600102edb..89523f52b45 100644 --- a/graphql/id_test.go +++ b/graphql/id_test.go @@ -1,6 +1,7 @@ package graphql import ( + "bytes" "math" "testing" @@ -8,6 +9,25 @@ import ( ) 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{}