-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
coercion.go
56 lines (54 loc) · 1.02 KB
/
coercion.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package graphql
import (
"encoding/json"
)
// CoerceList applies coercion from a single value to a list.
func CoerceList(v interface{}) []interface{} {
var vSlice []interface{}
if v != nil {
switch v := v.(type) {
case []interface{}:
// already a slice no coercion required
vSlice = v
case []string:
if len(v) > 0 {
vSlice = []interface{}{v[0]}
}
case []json.Number:
if len(v) > 0 {
vSlice = []interface{}{v[0]}
}
case []bool:
if len(v) > 0 {
vSlice = []interface{}{v[0]}
}
case []map[string]interface{}:
if len(v) > 0 {
vSlice = []interface{}{v[0]}
}
case []float64:
if len(v) > 0 {
vSlice = []interface{}{v[0]}
}
case []float32:
if len(v) > 0 {
vSlice = []interface{}{v[0]}
}
case []int:
if len(v) > 0 {
vSlice = []interface{}{v[0]}
}
case []int32:
if len(v) > 0 {
vSlice = []interface{}{v[0]}
}
case []int64:
if len(v) > 0 {
vSlice = []interface{}{v[0]}
}
default:
vSlice = []interface{}{v}
}
}
return vSlice
}