Skip to content

Commit fe52096

Browse files
authored
Merge pull request #199 from jhaynie/master
fixed issue with pointer to scalar values
2 parents 44a52a8 + 1fc7c7c commit fe52096

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

scalars.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,52 +25,78 @@ func coerceInt(value interface{}) interface{} {
2525
return nil
2626
}
2727
return value
28+
case *int:
29+
return coerceInt(*value)
2830
case int8:
2931
return int(value)
32+
case *int8:
33+
return int(*value)
3034
case int16:
3135
return int(value)
36+
case *int16:
37+
return int(*value)
3238
case int32:
3339
return int(value)
40+
case *int32:
41+
return int(*value)
3442
case int64:
3543
if value < int64(math.MinInt32) || value > int64(math.MaxInt32) {
3644
return nil
3745
}
3846
return int(value)
47+
case *int64:
48+
return coerceInt(*value)
3949
case uint:
4050
if value > math.MaxInt32 {
4151
return nil
4252
}
4353
return int(value)
54+
case *uint:
55+
return coerceInt(*value)
4456
case uint8:
4557
return int(value)
58+
case *uint8:
59+
return int(*value)
4660
case uint16:
4761
return int(value)
62+
case *uint16:
63+
return int(*value)
4864
case uint32:
4965
if value > uint32(math.MaxInt32) {
5066
return nil
5167
}
5268
return int(value)
69+
case *uint32:
70+
return coerceInt(*value)
5371
case uint64:
5472
if value > uint64(math.MaxInt32) {
5573
return nil
5674
}
5775
return int(value)
76+
case *uint64:
77+
return coerceInt(*value)
5878
case float32:
5979
if value < float32(math.MinInt32) || value > float32(math.MaxInt32) {
6080
return nil
6181
}
6282
return int(value)
83+
case *float32:
84+
return coerceInt(*value)
6385
case float64:
6486
if value < float64(math.MinInt32) || value > float64(math.MaxInt32) {
6587
return nil
6688
}
6789
return int(value)
90+
case *float64:
91+
return coerceInt(*value)
6892
case string:
6993
val, err := strconv.ParseFloat(value, 0)
7094
if err != nil {
7195
return nil
7296
}
7397
return coerceInt(val)
98+
case *string:
99+
return coerceInt(*value)
74100
}
75101

76102
// If the value cannot be transformed into an int, return nil instead of '0'
@@ -103,18 +129,28 @@ func coerceFloat(value interface{}) interface{} {
103129
return 1.0
104130
}
105131
return 0.0
132+
case *bool:
133+
return coerceFloat(*value)
106134
case int:
107135
return float64(value)
136+
case *int32:
137+
return coerceFloat(*value)
108138
case float32:
109139
return value
140+
case *float32:
141+
return coerceFloat(*value)
110142
case float64:
111143
return value
144+
case *float64:
145+
return coerceFloat(*value)
112146
case string:
113147
val, err := strconv.ParseFloat(value, 0)
114148
if err != nil {
115149
return nil
116150
}
117151
return val
152+
case *string:
153+
return coerceFloat(*value)
118154
}
119155
return 0.0
120156
}
@@ -143,6 +179,9 @@ var Float = NewScalar(ScalarConfig{
143179
})
144180

145181
func coerceString(value interface{}) interface{} {
182+
if v, ok := value.(*string); ok {
183+
return *v
184+
}
146185
return fmt.Sprintf("%v", value)
147186
}
148187

@@ -167,27 +206,37 @@ func coerceBool(value interface{}) interface{} {
167206
switch value := value.(type) {
168207
case bool:
169208
return value
209+
case *bool:
210+
return *value
170211
case string:
171212
switch value {
172213
case "", "false":
173214
return false
174215
}
175216
return true
217+
case *string:
218+
return coerceBool(*value)
176219
case float64:
177220
if value != 0 {
178221
return true
179222
}
180223
return false
224+
case *float64:
225+
return coerceBool(*value)
181226
case float32:
182227
if value != 0 {
183228
return true
184229
}
185230
return false
231+
case *float32:
232+
return coerceBool(*value)
186233
case int:
187234
if value != 0 {
188235
return true
189236
}
190237
return false
238+
case *int:
239+
return coerceBool(*value)
191240
}
192241
return false
193242
}

values.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,15 +320,39 @@ func isNullish(value interface{}) bool {
320320
if value, ok := value.(string); ok {
321321
return value == ""
322322
}
323+
if value, ok := value.(*string); ok {
324+
if value == nil {
325+
return true
326+
}
327+
return *value == ""
328+
}
323329
if value, ok := value.(int); ok {
324330
return math.IsNaN(float64(value))
325331
}
332+
if value, ok := value.(*int); ok {
333+
if value == nil {
334+
return true
335+
}
336+
return math.IsNaN(float64(*value))
337+
}
326338
if value, ok := value.(float32); ok {
327339
return math.IsNaN(float64(value))
328340
}
341+
if value, ok := value.(*float32); ok {
342+
if value == nil {
343+
return true
344+
}
345+
return math.IsNaN(float64(*value))
346+
}
329347
if value, ok := value.(float64); ok {
330348
return math.IsNaN(value)
331349
}
350+
if value, ok := value.(*float64); ok {
351+
if value == nil {
352+
return true
353+
}
354+
return math.IsNaN(*value)
355+
}
332356
return value == nil
333357
}
334358

0 commit comments

Comments
 (0)