-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscalar_id.go
More file actions
230 lines (194 loc) · 5.45 KB
/
Copy pathscalar_id.go
File metadata and controls
230 lines (194 loc) · 5.45 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package gqlmodel
import (
"fmt"
"io"
"strconv"
"github.com/99designs/gqlgen/graphql"
"github.com/reearth/reearth/server/pkg/id"
"github.com/reearth/reearthx/idx"
)
type ID string
func MarshalPropertyFieldID(t id.PropertyFieldID) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
_, _ = io.WriteString(w, strconv.Quote(t.String()))
})
}
func UnmarshalPropertyFieldID(v interface{}) (id.PropertyFieldID, error) {
if tmpStr, ok := v.(string); ok {
return id.PropertyFieldID(tmpStr), nil
}
return id.PropertyFieldID(""), fmt.Errorf("invalid propertyField ID: %v", v)
}
func MarshalDatasetFieldID(t id.DatasetFieldID) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
_, _ = io.WriteString(w, strconv.Quote(t.String()))
})
}
func UnmarshalDatasetFieldID(v interface{}) (id.DatasetFieldID, error) {
if tmpStr, ok := v.(string); ok {
res, err := id.DatasetFieldIDFrom(tmpStr)
if err != nil {
return res, fmt.Errorf("invalid datasetField ID: %s", tmpStr)
}
return res, nil
}
return id.NewDatasetFieldID(), fmt.Errorf("invalid datasetField ID: %v", v)
}
func MarshalPluginID(t id.PluginID) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
_, _ = io.WriteString(w, strconv.Quote(t.String()))
})
}
func UnmarshalPluginID(v interface{}) (id.PluginID, error) {
if tmpStr, ok := v.(string); ok {
res, err := id.PluginIDFrom(tmpStr)
if err != nil {
return res, fmt.Errorf("invalid plugin ID: %s", tmpStr)
}
return res, nil
}
return id.PluginID{}, fmt.Errorf("invalid plugin ID: %v", v)
}
func MarshalPluginExtensionID(t id.PluginExtensionID) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
_, _ = io.WriteString(w, strconv.Quote(t.String()))
})
}
func UnmarshalPluginExtensionID(v interface{}) (id.PluginExtensionID, error) {
if tmpStr, ok := v.(string); ok {
return id.PluginExtensionID(tmpStr), nil
}
return id.PluginExtensionID(""), fmt.Errorf("invalid pluginExtension ID: %v", v)
}
func MarshalPropertySchemaID(t id.PropertySchemaID) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
_, _ = io.WriteString(w, strconv.Quote(t.String()))
})
}
func UnmarshalPropertySchemaID(v interface{}) (id.PropertySchemaID, error) {
if tmpStr, ok := v.(string); ok {
res, err := id.PropertySchemaIDFrom(tmpStr)
if err != nil {
return res, fmt.Errorf("invalid propertySchema ID: %s", tmpStr)
}
return res, nil
}
return id.PropertySchemaID{}, fmt.Errorf("invalid propertySchema ID: %v", v)
}
func MarshalPropertySchemaGroupID(t id.PropertySchemaGroupID) graphql.Marshaler {
return graphql.WriterFunc(func(w io.Writer) {
_, _ = io.WriteString(w, strconv.Quote(t.String()))
})
}
func UnmarshalPropertySchemaGroupID(v interface{}) (id.PropertySchemaGroupID, error) {
if tmpStr, ok := v.(string); ok {
return id.PropertySchemaGroupID(tmpStr), nil
}
return id.PropertySchemaGroupID(""), fmt.Errorf("invalid propertySchemaGroup ID: %v", v)
}
func IDFrom[T idx.Type](i idx.ID[T]) ID {
return ID(i.String())
}
func IDFromList[T idx.Type](i idx.List[T]) []ID {
if i == nil {
return nil
}
res := make([]ID, len(i))
for i, v := range i {
res[i] = IDFrom[T](v)
}
return res
}
func IDFromRef[T idx.Type](i *idx.ID[T]) *ID {
return (*ID)(i.StringRef())
}
func IDFromString[T idx.Type](i idx.StringID[T]) ID {
return (ID)(i)
}
func IDFromStringRef[T idx.Type](i *idx.StringID[T]) *ID {
return (*ID)(i)
}
func IDFromPluginID(i id.PluginID) ID {
return ID(i.String())
}
func IDFromPluginIDRef(i *id.PluginID) *ID {
return (*ID)(i.StringRef())
}
func IDFromPropertySchemaID(i id.PropertySchemaID) ID {
return ID(i.String())
}
func IDFromPropertySchemaIDRef(i *id.PropertySchemaID) *ID {
return (*ID)(i.StringRef())
}
func ToID[A idx.Type](a ID) (idx.ID[A], error) {
res, err := idx.From[A](string(a))
if err != nil {
var t A
return res, fmt.Errorf("invalid %s ID: %s", t.Type(), string(a))
}
return res, nil
}
func ToIDs[A idx.Type](a []ID) (*[]idx.ID[A], error) {
if a == nil {
return nil, nil
}
res := make([]idx.ID[A], len(a))
for i, v := range a {
r, err := ToID[A](v)
if err != nil {
return nil, err
}
res[i] = r
}
return &res, nil
}
func ToID2[A, B idx.Type](a, b ID) (ai idx.ID[A], bi idx.ID[B], err error) {
ai, err = ToID[A](a)
if err != nil {
return
}
bi, err = ToID[B](b)
return
}
func ToID3[A, B, C idx.Type](a, b, c ID) (ai idx.ID[A], bi idx.ID[B], ci idx.ID[C], err error) {
ai, bi, err = ToID2[A, B](a, b)
if err != nil {
return
}
ci, err = ToID[C](c)
return
}
func ToID4[A, B, C, D idx.Type](a, b, c, d ID) (ai idx.ID[A], bi idx.ID[B], ci idx.ID[C], di idx.ID[D], err error) {
ai, bi, err = ToID2[A, B](a, b)
if err != nil {
return
}
ci, di, err = ToID2[C, D](c, d)
return
}
func ToIDRef[A idx.Type](a *ID) *idx.ID[A] {
return idx.FromRef[A]((*string)(a))
}
func ToStringIDRef[T idx.Type](a *ID) *idx.StringID[T] {
return idx.StringIDFromRef[T]((*string)(a))
}
func ToPropertySchemaID(a ID) (id.PropertySchemaID, error) {
return id.PropertySchemaIDFrom((string)(a))
}
func ToPluginID(a ID) (id.PluginID, error) {
return id.PluginIDFrom((string)(a))
}
func ToPluginID2(a, b ID) (ai id.PluginID, bi id.PluginID, err error) {
ai, err = id.PluginIDFrom((string)(a))
if err != nil {
return
}
bi, err = ToPluginID(b)
return ai, bi, err
}
func ToPropertySchemaIDRef(a *ID) *id.PropertySchemaID {
return id.PropertySchemaIDFromRef((*string)(a))
}
func ToPluginIDRef(a *ID) *id.PluginID {
return id.PluginIDFromRef((*string)(a))
}