forked from graph-gophers/graphql-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nullable_types.go
166 lines (135 loc) · 3.38 KB
/
nullable_types.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package graphql
import (
"fmt"
"math"
)
// NullString is a string that can be null. Use it in input structs to
// differentiate a value explicitly set to null from an omitted value.
// When the value is defined (either null or a value) Set is true.
type NullString struct {
Value *string
Set bool
}
func (NullString) ImplementsGraphQLType(name string) bool {
return name == "String"
}
func (s *NullString) UnmarshalGraphQL(input interface{}) error {
s.Set = true
if input == nil {
return nil
}
switch v := input.(type) {
case string:
s.Value = &v
return nil
default:
return fmt.Errorf("wrong type for String: %T", v)
}
}
func (s *NullString) Nullable() {}
// NullBool is a string that can be null. Use it in input structs to
// differentiate a value explicitly set to null from an omitted value.
// When the value is defined (either null or a value) Set is true.
type NullBool struct {
Value *bool
Set bool
}
func (NullBool) ImplementsGraphQLType(name string) bool {
return name == "Boolean"
}
func (s *NullBool) UnmarshalGraphQL(input interface{}) error {
s.Set = true
if input == nil {
return nil
}
switch v := input.(type) {
case bool:
s.Value = &v
return nil
default:
return fmt.Errorf("wrong type for Boolean: %T", v)
}
}
func (s *NullBool) Nullable() {}
// NullInt is a string that can be null. Use it in input structs to
// differentiate a value explicitly set to null from an omitted value.
// When the value is defined (either null or a value) Set is true.
type NullInt struct {
Value *int32
Set bool
}
func (NullInt) ImplementsGraphQLType(name string) bool {
return name == "Int"
}
func (s *NullInt) UnmarshalGraphQL(input interface{}) error {
s.Set = true
if input == nil {
return nil
}
switch v := input.(type) {
case int32:
s.Value = &v
return nil
case float64:
coerced := int32(v)
if v < math.MinInt32 || v > math.MaxInt32 || float64(coerced) != v {
return fmt.Errorf("not a 32-bit integer")
}
s.Value = &coerced
return nil
default:
return fmt.Errorf("wrong type for Int: %T", v)
}
}
func (s *NullInt) Nullable() {}
// NullFloat is a string that can be null. Use it in input structs to
// differentiate a value explicitly set to null from an omitted value.
// When the value is defined (either null or a value) Set is true.
type NullFloat struct {
Value *float64
Set bool
}
func (NullFloat) ImplementsGraphQLType(name string) bool {
return name == "Float"
}
func (s *NullFloat) UnmarshalGraphQL(input interface{}) error {
s.Set = true
if input == nil {
return nil
}
switch v := input.(type) {
case float64:
s.Value = &v
return nil
case int32:
coerced := float64(v)
s.Value = &coerced
return nil
case int:
coerced := float64(v)
s.Value = &coerced
return nil
default:
return fmt.Errorf("wrong type for Float: %T", v)
}
}
func (s *NullFloat) Nullable() {}
// NullTime is a string that can be null. Use it in input structs to
// differentiate a value explicitly set to null from an omitted value.
// When the value is defined (either null or a value) Set is true.
type NullTime struct {
Value *Time
Set bool
}
func (NullTime) ImplementsGraphQLType(name string) bool {
return name == "Time"
}
func (s *NullTime) UnmarshalGraphQL(input interface{}) error {
s.Set = true
if input == nil {
return nil
}
s.Value = new(Time)
return s.Value.UnmarshalGraphQL(input)
}
func (s *NullTime) Nullable() {}