From d7d0ebd4afd59bf883c2b579f4f10403ffa0ca18 Mon Sep 17 00:00:00 2001 From: tsh96 Date: Sun, 9 Jan 2022 17:05:16 +0800 Subject: [PATCH] Improve performance of MarshalBoolean --- graphql/bool.go | 11 ++++------- graphql/bool_test.go | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 graphql/bool_test.go diff --git a/graphql/bool.go b/graphql/bool.go index b175ca98628..f435e0c0988 100644 --- a/graphql/bool.go +++ b/graphql/bool.go @@ -7,13 +7,10 @@ import ( ) func MarshalBoolean(b bool) Marshaler { - return WriterFunc(func(w io.Writer) { - if b { - w.Write(trueLit) - } else { - w.Write(falseLit) - } - }) + if b { + return WriterFunc(func(w io.Writer) { w.Write(trueLit) }) + } + return WriterFunc(func(w io.Writer) { w.Write(falseLit) }) } func UnmarshalBoolean(v interface{}) (bool, error) { diff --git a/graphql/bool_test.go b/graphql/bool_test.go new file mode 100644 index 00000000000..147237a1d63 --- /dev/null +++ b/graphql/bool_test.go @@ -0,0 +1,19 @@ +package graphql + +import ( + "bytes" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestBoolean(t *testing.T) { + assert.Equal(t, "true", doBooleanMarshal(true)) + assert.Equal(t, "false", doBooleanMarshal(false)) +} + +func doBooleanMarshal(b bool) string { + var buf bytes.Buffer + MarshalBoolean(b).MarshalGQL(&buf) + return buf.String() +}