-
Notifications
You must be signed in to change notification settings - Fork 11
/
variables.go
206 lines (174 loc) · 3.78 KB
/
variables.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package client
import (
"github.com/Twingate/terraform-provider-twingate/v3/twingate/internal/client/query"
"github.com/Twingate/terraform-provider-twingate/v3/twingate/internal/utils"
"github.com/hasura/go-graphql-client"
)
func newVars(options ...gqlVarOption) map[string]interface{} {
values := make(map[string]interface{})
for _, opt := range options {
values = opt(values)
}
return values
}
type gqlVarOption func(values map[string]interface{}) map[string]interface{}
func pageLimit(limit int) gqlVarOption {
return gqlVar(limit, query.PageLimit)
}
func cursor(name string) gqlVarOption {
return gqlNullable("", name)
}
func gqlID(val interface{}, name ...string) gqlVarOption {
key := "id"
if len(name) > 0 {
key = name[0]
}
return func(values map[string]interface{}) map[string]interface{} {
switch value := val.(type) {
case string:
values[key] = graphql.ID(value)
case graphql.ID:
values[key] = value
default:
values[key] = graphql.ToID(val)
}
return values
}
}
func gqlIDs(ids []string, name string) gqlVarOption {
gqlValues := utils.Map[string, graphql.ID](ids,
func(val string) graphql.ID {
return graphql.ID(val)
})
return func(values map[string]interface{}) map[string]interface{} {
values[name] = gqlValues
return values
}
}
func gqlVar(val interface{}, name string) gqlVarOption {
return func(values map[string]interface{}) map[string]interface{} {
if val != nil {
values[name] = val
}
return values
}
}
func gqlNullable(val interface{}, name string) gqlVarOption {
return func(values map[string]interface{}) map[string]interface{} {
var gqlValue interface{}
if isZeroValue(val) {
gqlValue = getNullableValue(val)
} else {
gqlValue = getValue(val)
}
values[name] = gqlValue
return values
}
}
func getValue(val any) any {
switch value := val.(type) {
case *bool:
return *value
case *string:
return *value
case *int:
return *value
case *int32:
return *value
case *int64:
return *value
case *float32:
return *value
case *float64:
return *value
default:
return val
}
}
//nolint:unparam
func gqlNullableID(val interface{}, name string) gqlVarOption {
return func(values map[string]interface{}) map[string]interface{} {
var (
gqlValue interface{}
defaultID *graphql.ID
)
if value, ok := val.(*string); ok && value != nil {
val = *value
}
if isZeroValue(val) {
gqlValue = defaultID
} else {
gqlValue = graphql.ToID(val)
}
values[name] = gqlValue
return values
}
}
//nolint:cyclop
func isZeroValue(val interface{}) bool {
if val == nil {
return true
}
var (
defaultString string
defaultInt int
defaultInt32 int32
defaultInt64 int64
defaultBool bool
defaultFloat64 float64
defaultFloat32 float32
)
switch value := val.(type) {
case string:
return value == defaultString
case bool:
return value == defaultBool
case int:
return value == defaultInt
case int32:
return value == defaultInt32
case int64:
return value == defaultInt64
case float32:
return value == defaultFloat32
case float64:
return value == defaultFloat64
case *bool:
return value == nil
case *string:
return value == nil
case *int:
return value == nil
case *int32:
return value == nil
case *int64:
return value == nil
case *float32:
return value == nil
case *float64:
return value == nil
}
return false
}
func getNullableValue(val interface{}) interface{} {
if val == nil {
return nil
}
var (
defaultString *string
defaultInt *int
defaultBool *bool
defaultFloat *float64
)
switch val.(type) {
case string, *string:
return defaultString
case bool, *bool:
return defaultBool
case int, int32, int64, *int, *int32, *int64:
return defaultInt
case float32, float64, *float32, *float64:
return defaultFloat
}
return nil
}