Skip to content

Commit 491504a

Browse files
authored
Merge pull request #133 from sogko/sogko/float64
Allow GraphQLFloat to be represented by `float64`
2 parents 4a8d417 + 3582a63 commit 491504a

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

scalars.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,27 +96,27 @@ var Int = NewScalar(ScalarConfig{
9696
},
9797
})
9898

99-
func coerceFloat32(value interface{}) interface{} {
99+
func coerceFloat(value interface{}) interface{} {
100100
switch value := value.(type) {
101101
case bool:
102102
if value == true {
103-
return float32(1)
103+
return 1.0
104104
}
105-
return float32(0)
105+
return 0.0
106106
case int:
107-
return float32(value)
107+
return float64(value)
108108
case float32:
109109
return value
110110
case float64:
111-
return float32(value)
111+
return value
112112
case string:
113113
val, err := strconv.ParseFloat(value, 0)
114114
if err != nil {
115115
return nil
116116
}
117-
return coerceFloat32(val)
117+
return val
118118
}
119-
return float32(0)
119+
return 0.0
120120
}
121121

122122
// Float is the GraphQL float type definition.
@@ -125,8 +125,8 @@ var Float = NewScalar(ScalarConfig{
125125
Description: "The `Float` scalar type represents signed double-precision fractional " +
126126
"values as specified by " +
127127
"[IEEE 754](http://en.wikipedia.org/wiki/IEEE_floating_point). ",
128-
Serialize: coerceFloat32,
129-
ParseValue: coerceFloat32,
128+
Serialize: coerceFloat,
129+
ParseValue: coerceFloat,
130130
ParseLiteral: func(valueAST ast.Value) interface{} {
131131
switch valueAST := valueAST.(type) {
132132
case *ast.FloatValue:

scalars_serialization_test.go

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type intSerializationTest struct {
1212
Value interface{}
1313
Expected interface{}
1414
}
15-
type float32SerializationTest struct {
15+
type float64SerializationTest struct {
1616
Value interface{}
1717
Expected interface{}
1818
}
@@ -37,6 +37,12 @@ func TestTypeSystem_Scalar_SerializesOutputInt(t *testing.T) {
3737
{float32(-1.1), -1},
3838
{float32(1e5), 100000},
3939
{float32(math.MaxFloat32), nil},
40+
{float64(0.1), 0},
41+
{float64(1.1), 1},
42+
{float64(-1.1), -1},
43+
{float64(1e5), 100000},
44+
{float64(math.MaxFloat32), nil},
45+
{float64(math.MaxFloat64), nil},
4046
// Maybe a safe Go/Javascript `int`, but bigger than 2^32, so not
4147
// representable as a GraphQL Int
4248
{9876504321, nil},
@@ -71,34 +77,49 @@ func TestTypeSystem_Scalar_SerializesOutputInt(t *testing.T) {
7177
{[]int{}, nil},
7278
}
7379

74-
for _, test := range tests {
80+
for i, test := range tests {
7581
val := graphql.Int.Serialize(test.Value)
7682
if val != test.Expected {
77-
reflectedValue := reflect.ValueOf(test.Value)
78-
t.Fatalf("Failed Int.Serialize(%v(%v)), expected: %v, got %v", reflectedValue.Type(), test.Value, test.Expected, val)
83+
reflectedTestValue := reflect.ValueOf(test.Value)
84+
reflectedExpectedValue := reflect.ValueOf(test.Expected)
85+
reflectedValue := reflect.ValueOf(val)
86+
t.Fatalf("Failed test #%d - Int.Serialize(%v(%v)), expected: %v(%v), got %v(%v)",
87+
i, reflectedTestValue.Type(), test.Value,
88+
reflectedExpectedValue.Type(), test.Expected,
89+
reflectedValue.Type(), val,
90+
)
7991
}
8092
}
8193
}
8294

8395
func TestTypeSystem_Scalar_SerializesOutputFloat(t *testing.T) {
84-
tests := []float32SerializationTest{
85-
{int(1), float32(1.0)},
86-
{int(0), float32(0.0)},
87-
{int(-1), float32(-1.0)},
96+
tests := []float64SerializationTest{
97+
{int(1), 1.0},
98+
{int(0), 0.0},
99+
{int(-1), -1.0},
88100
{float32(0.1), float32(0.1)},
89101
{float32(1.1), float32(1.1)},
90102
{float32(-1.1), float32(-1.1)},
91-
{"-1.1", float32(-1.1)},
103+
{float64(0.1), float64(0.1)},
104+
{float64(1.1), float64(1.1)},
105+
{float64(-1.1), float64(-1.1)},
106+
{"-1.1", -1.1},
92107
{"one", nil},
93-
{false, float32(0.0)},
94-
{true, float32(1.0)},
108+
{false, 0.0},
109+
{true, 1.0},
95110
}
96111

97112
for i, test := range tests {
98113
val := graphql.Float.Serialize(test.Value)
99114
if val != test.Expected {
100-
reflectedValue := reflect.ValueOf(test.Value)
101-
t.Fatalf("Failed test #%d - Float.Serialize(%v(%v)), expected: %v, got %v", i, reflectedValue.Type(), test.Value, test.Expected, val)
115+
reflectedTestValue := reflect.ValueOf(test.Value)
116+
reflectedExpectedValue := reflect.ValueOf(test.Expected)
117+
reflectedValue := reflect.ValueOf(val)
118+
t.Fatalf("Failed test #%d - Float.Serialize(%v(%v)), expected: %v(%v), got %v(%v)",
119+
i, reflectedTestValue.Type(), test.Value,
120+
reflectedExpectedValue.Type(), test.Expected,
121+
reflectedValue.Type(), val,
122+
)
102123
}
103124
}
104125
}
@@ -108,6 +129,7 @@ func TestTypeSystem_Scalar_SerializesOutputStrings(t *testing.T) {
108129
{"string", "string"},
109130
{int(1), "1"},
110131
{float32(-1.1), "-1.1"},
132+
{float64(-1.1), "-1.1"},
111133
{true, "true"},
112134
{false, "false"},
113135
}

0 commit comments

Comments
 (0)