Skip to content

Commit

Permalink
resolver: remove unnecessary valueToJson
Browse files Browse the repository at this point in the history
  • Loading branch information
mjarkk committed Jun 27, 2021
1 parent 91e269e commit 6d3edb7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 146 deletions.
151 changes: 8 additions & 143 deletions resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,17 +745,13 @@ func (ctx *Ctx) resolveFieldDataValue(query *field, codeStructure *obj, dept uin
return
}

if codeStructure.isID {
if codeStructure.isID && codeStructure.dataValueType != reflect.String {
// Graphql ID fields are always strings
if codeStructure.dataValueType == reflect.String {
ctx.reflectValueToJson(value, codeStructure.dataValueType)
} else {
ctx.writeByte('"')
ctx.reflectValueToJson(value, codeStructure.dataValueType)
ctx.writeByte('"')
}
ctx.writeByte('"')
ctx.valueToJson(value, codeStructure.dataValueType)
ctx.writeByte('"')
} else {
ctx.reflectValueToJson(value, codeStructure.dataValueType)
ctx.valueToJson(value, codeStructure.dataValueType)
}

return
Expand Down Expand Up @@ -788,7 +784,7 @@ func (ctx *Ctx) resolveFieldDataValue(query *field, codeStructure *obj, dept uin
ctx.write([]byte("null"))
return
}
ctx.valueToJson(timeToString(timeValue))
stringToJson([]byte(timeToString(timeValue)), &ctx.result)
return
default:
ctx.addErr("has invalid data type")
Expand All @@ -797,138 +793,7 @@ func (ctx *Ctx) resolveFieldDataValue(query *field, codeStructure *obj, dept uin
}
}

func (ctx *Ctx) valueToJson(in interface{}) {
switch v := in.(type) {
case string:
stringToJson([]byte(v), &ctx.result)
case bool:
if v {
ctx.write([]byte("true"))
} else {
ctx.write([]byte("false"))
}
case int:
ctx.writeString(strconv.Itoa(v))
case int8:
ctx.writeString(strconv.Itoa(int(v)))
case int16:
ctx.writeString(strconv.Itoa(int(v)))
case int32: // == rune
ctx.writeString(strconv.Itoa(int(v)))
case int64:
ctx.writeString(strconv.FormatInt(v, 10))
case uint:
ctx.writeString(strconv.FormatUint(uint64(v), 10))
case uint8: // == byte
ctx.writeString(strconv.FormatUint(uint64(v), 10))
case uint16:
ctx.writeString(strconv.FormatUint(uint64(v), 10))
case uint32:
ctx.writeString(strconv.FormatUint(uint64(v), 10))
case uint64:
ctx.writeString(strconv.FormatUint(v, 10))
case uintptr:
ctx.writeString(strconv.FormatUint(uint64(v), 10))
case float32:
floatToJson(32, float64(v), &ctx.result)
case float64:
floatToJson(64, v, &ctx.result)
case *string:
if v == nil {
ctx.write([]byte("null"))
} else {
ctx.valueToJson(*v)
}
case *bool:
if v == nil {
ctx.write([]byte("null"))
} else {
ctx.valueToJson(*v)
}
case *int:
if v == nil {
ctx.write([]byte("null"))
} else {
ctx.valueToJson(*v)
}
case *int8:
if v == nil {
ctx.write([]byte("null"))
} else {
ctx.valueToJson(*v)
}
case *int16:
if v == nil {
ctx.write([]byte("null"))
} else {
ctx.valueToJson(*v)
}
case *int32: // = *rune
if v == nil {
ctx.write([]byte("null"))
} else {
ctx.valueToJson(*v)
}
case *int64:
if v == nil {
ctx.write([]byte("null"))
} else {
ctx.valueToJson(*v)
}
case *uint:
if v == nil {
ctx.write([]byte("null"))
} else {
ctx.valueToJson(*v)
}
case *uint8: // = *byte
if v == nil {
ctx.write([]byte("null"))
} else {
ctx.valueToJson(*v)
}
case *uint16:
if v == nil {
ctx.write([]byte("null"))
} else {
ctx.valueToJson(*v)
}
case *uint32:
if v == nil {
ctx.write([]byte("null"))
} else {
ctx.valueToJson(*v)
}
case *uint64:
if v == nil {
ctx.write([]byte("null"))
} else {
ctx.valueToJson(*v)
}
case *uintptr:
if v == nil {
ctx.write([]byte("null"))
} else {
ctx.valueToJson(*v)
}
case *float32:
if v == nil {
ctx.write([]byte("null"))
} else {
ctx.valueToJson(*v)
}
case *float64:
if v == nil {
ctx.write([]byte("null"))
} else {
ctx.valueToJson(*v)
}
default:
ctx.write([]byte("null"))
}
}

func (ctx *Ctx) reflectValueToJson(in reflect.Value, kind reflect.Kind) {
func (ctx *Ctx) valueToJson(in reflect.Value, kind reflect.Kind) {
switch kind {
case reflect.String:
stringToJson([]byte(in.String()), &ctx.result)
Expand All @@ -951,7 +816,7 @@ func (ctx *Ctx) reflectValueToJson(in reflect.Value, kind reflect.Kind) {
ctx.write([]byte("null"))
} else {
element := in.Elem()
ctx.reflectValueToJson(element, element.Kind())
ctx.valueToJson(element, element.Kind())
}
default:
ctx.write([]byte("null"))
Expand Down
1 change: 1 addition & 0 deletions resolve_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func BenchmarkResolve(b *testing.B) {
// BenchmarkResolve-12 3452 320228 ns/op 57235 B/op 3686 allocs/op
// BenchmarkResolve-12 3250 311136 ns/op 57281 B/op 3686 allocs/op
// BenchmarkResolve-12 4326 257411 ns/op 50270 B/op 2843 allocs/op
// BenchmarkResolve-12 4885 226071 ns/op 46005 B/op 2544 allocs/op

// On desktop
// BenchmarkResolve-16 2259 503592 ns/op 62823 B/op 4340 allocs/op
Expand Down
8 changes: 5 additions & 3 deletions resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io/ioutil"
"mime/multipart"
"reflect"
"testing"
"time"

Expand Down Expand Up @@ -66,7 +67,7 @@ func TestValueToJson(t *testing.T) {
{uint16_, "8"},
{uint32_, "9"},
{uint64_, "10"},
{uintptr_, "11"},
{uintptr_, "null"}, // We do not support this datavalue
{float32_, "12"},
{float64_, "13"},
{float64WExponent, "1e-98"},
Expand All @@ -84,7 +85,7 @@ func TestValueToJson(t *testing.T) {
{&uint16_, "8"},
{&uint32_, "9"},
{&uint64_, "10"},
{&uintptr_, "11"},
{&uintptr_, "null"}, // We do not support this datavalue
{&float32_, "12"},
{&float64_, "13"},

Expand All @@ -110,7 +111,8 @@ func TestValueToJson(t *testing.T) {
c := &Ctx{
result: []byte{},
}
c.valueToJson(option.value)
v := reflect.ValueOf(option.value)
c.valueToJson(v, v.Kind())
Equal(t, option.expect, string(c.result))
}
}
Expand Down

0 comments on commit 6d3edb7

Please sign in to comment.