Skip to content

Commit

Permalink
Nulls are now unmarshalled as zero values for primitive types (#3162)
Browse files Browse the repository at this point in the history
* Nulls are now unmarshalled as zero values for primitive types

* Address uint and run gofumpt

Signed-off-by: Steve Coffman <steve@khanacademy.org>

---------

Signed-off-by: Steve Coffman <steve@khanacademy.org>
Co-authored-by: Steve Coffman <steve@khanacademy.org>
  • Loading branch information
endSly and StevenACoffman authored Jul 1, 2024
1 parent dce2e35 commit d0e6892
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 39 deletions.
2 changes: 2 additions & 0 deletions graphql/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func UnmarshalBoolean(v any) (bool, error) {
return v != 0, nil
case bool:
return v, nil
case nil:
return false, nil
default:
return false, fmt.Errorf("%T is not a bool", v)
}
Expand Down
1 change: 0 additions & 1 deletion graphql/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ func (e *Executor) CreateOperationContext(

var err error
rc.Variables, err = validator.VariableValues(e.es.Schema(), rc.Operation, params.Variables)

if err != nil {
gqlErr, ok := err.(*gqlerror.Error)
if ok {
Expand Down
2 changes: 2 additions & 0 deletions graphql/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func UnmarshalFloat(v any) (float64, error) {
return v, nil
case json.Number:
return strconv.ParseFloat(string(v), 64)
case nil:
return 0, nil
default:
return 0, fmt.Errorf("%T is not an float", v)
}
Expand Down
1 change: 0 additions & 1 deletion graphql/handler/transport/http_graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ func cleanupBody(body string) (out string, err error) {
// is where query starts. If it is, query is url encoded.
if strings.HasPrefix(body, "%7B") {
body, err = url.QueryUnescape(body)

if err != nil {
return body, err
}
Expand Down
6 changes: 6 additions & 0 deletions graphql/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func UnmarshalInt(v any) (int, error) {
return int(v), nil
case json.Number:
return strconv.Atoi(string(v))
case nil:
return 0, nil
default:
return 0, fmt.Errorf("%T is not an int", v)
}
Expand All @@ -44,6 +46,8 @@ func UnmarshalInt64(v any) (int64, error) {
return v, nil
case json.Number:
return strconv.ParseInt(string(v), 10, 64)
case nil:
return 0, nil
default:
return 0, fmt.Errorf("%T is not an int", v)
}
Expand Down Expand Up @@ -73,6 +77,8 @@ func UnmarshalInt32(v any) (int32, error) {
return 0, err
}
return int32(iv), nil
case nil:
return 0, nil
default:
return 0, fmt.Errorf("%T is not an int", v)
}
Expand Down
46 changes: 22 additions & 24 deletions graphql/int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

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

func TestInt(t *testing.T) {
Expand All @@ -13,18 +14,17 @@ func TestInt(t *testing.T) {
})

t.Run("unmarshal", func(t *testing.T) {
assert.Equal(t, 123, mustUnmarshalInt(123))
assert.Equal(t, 123, mustUnmarshalInt(int64(123)))
assert.Equal(t, 123, mustUnmarshalInt(json.Number("123")))
assert.Equal(t, 123, mustUnmarshalInt("123"))
assert.Equal(t, 123, mustUnmarshalInt(t, 123))
assert.Equal(t, 123, mustUnmarshalInt(t, int64(123)))
assert.Equal(t, 123, mustUnmarshalInt(t, json.Number("123")))
assert.Equal(t, 123, mustUnmarshalInt(t, "123"))
assert.Equal(t, 0, mustUnmarshalInt(t, nil))
})
}

func mustUnmarshalInt(v any) int {
func mustUnmarshalInt(t *testing.T, v any) int {
res, err := UnmarshalInt(v)
if err != nil {
panic(err)
}
require.NoError(t, err)
return res
}

Expand All @@ -34,18 +34,17 @@ func TestInt32(t *testing.T) {
})

t.Run("unmarshal", func(t *testing.T) {
assert.Equal(t, int32(123), mustUnmarshalInt32(123))
assert.Equal(t, int32(123), mustUnmarshalInt32(int64(123)))
assert.Equal(t, int32(123), mustUnmarshalInt32(json.Number("123")))
assert.Equal(t, int32(123), mustUnmarshalInt32("123"))
assert.Equal(t, int32(123), mustUnmarshalInt32(t, 123))
assert.Equal(t, int32(123), mustUnmarshalInt32(t, int64(123)))
assert.Equal(t, int32(123), mustUnmarshalInt32(t, json.Number("123")))
assert.Equal(t, int32(123), mustUnmarshalInt32(t, "123"))
assert.Equal(t, int32(0), mustUnmarshalInt32(t, nil))
})
}

func mustUnmarshalInt32(v any) int32 {
func mustUnmarshalInt32(t *testing.T, v any) int32 {
res, err := UnmarshalInt32(v)
if err != nil {
panic(err)
}
require.NoError(t, err)
return res
}

Expand All @@ -55,17 +54,16 @@ func TestInt64(t *testing.T) {
})

t.Run("unmarshal", func(t *testing.T) {
assert.Equal(t, int64(123), mustUnmarshalInt64(123))
assert.Equal(t, int64(123), mustUnmarshalInt64(int64(123)))
assert.Equal(t, int64(123), mustUnmarshalInt64(json.Number("123")))
assert.Equal(t, int64(123), mustUnmarshalInt64("123"))
assert.Equal(t, int64(123), mustUnmarshalInt64(t, 123))
assert.Equal(t, int64(123), mustUnmarshalInt64(t, int64(123)))
assert.Equal(t, int64(123), mustUnmarshalInt64(t, json.Number("123")))
assert.Equal(t, int64(123), mustUnmarshalInt64(t, "123"))
assert.Equal(t, int64(0), mustUnmarshalInt64(t, nil))
})
}

func mustUnmarshalInt64(v any) int64 {
func mustUnmarshalInt64(t *testing.T, v any) int64 {
res, err := UnmarshalInt64(v)
if err != nil {
panic(err)
}
require.NoError(t, err)
return res
}
2 changes: 1 addition & 1 deletion graphql/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func UnmarshalString(v any) (string, error) {
case bool:
return strconv.FormatBool(v), nil
case nil:
return "null", nil
return "", nil
default:
return "", fmt.Errorf("%T is not a string", v)
}
Expand Down
23 changes: 11 additions & 12 deletions graphql/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

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

func TestString(t *testing.T) {
Expand All @@ -22,21 +23,19 @@ func TestString(t *testing.T) {
})

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))
assert.Equal(t, "123", mustUnmarshalString(t, "123"))
assert.Equal(t, "123", mustUnmarshalString(t, 123))
assert.Equal(t, "123", mustUnmarshalString(t, int64(123)))
assert.Equal(t, "123", mustUnmarshalString(t, float64(123)))
assert.Equal(t, "123", mustUnmarshalString(t, json.Number("123")))
assert.Equal(t, "true", mustUnmarshalString(t, true))
assert.Equal(t, "false", mustUnmarshalString(t, false))
assert.Equal(t, "", mustUnmarshalString(t, nil))
})
}

func mustUnmarshalString(v any) string {
func mustUnmarshalString(t *testing.T, v any) string {
res, err := UnmarshalString(v)
if err != nil {
panic(err)
}
require.NoError(t, err)
return res
}
6 changes: 6 additions & 0 deletions graphql/uint.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func UnmarshalUint(v any) (uint, error) {
case json.Number:
u64, err := strconv.ParseUint(string(v), 10, 64)
return uint(u64), err
case nil:
return 0, nil
default:
return 0, fmt.Errorf("%T is not an uint", v)
}
Expand Down Expand Up @@ -63,6 +65,8 @@ func UnmarshalUint64(v any) (uint64, error) {
return uint64(v), nil
case json.Number:
return strconv.ParseUint(string(v), 10, 64)
case nil:
return 0, nil
default:
return 0, fmt.Errorf("%T is not an uint", v)
}
Expand Down Expand Up @@ -100,6 +104,8 @@ func UnmarshalUint32(v any) (uint32, error) {
return 0, err
}
return uint32(iv), nil
case nil:
return 0, nil
default:
return 0, fmt.Errorf("%T is not an uint", v)
}
Expand Down

0 comments on commit d0e6892

Please sign in to comment.