This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
scalar_id.go
174 lines (142 loc) · 4.2 KB
/
scalar_id.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
package gqlmodel
import (
"errors"
"io"
"strconv"
"github.com/99designs/gqlgen/graphql"
"github.com/reearth/reearth-backend/pkg/id"
"github.com/reearth/reearth-backend/pkg/id/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(""), errors.New("invalid ID")
}
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 {
return id.DatasetFieldIDFrom(tmpStr)
}
return id.NewDatasetFieldID(), errors.New("invalid ID")
}
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 {
return id.PluginIDFrom(tmpStr)
}
return id.PluginID{}, errors.New("invalid ID")
}
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(""), errors.New("invalid ID")
}
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 {
return id.PropertySchemaIDFrom(tmpStr)
}
return id.PropertySchemaID{}, errors.New("invalid ID")
}
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(""), errors.New("invalid ID")
}
func IDFrom[T idx.Type](i idx.ID[T]) ID {
return ID(i.String())
}
func IDFromRef[T idx.Type](i *idx.ID[T]) *ID {
return (*ID)(i.StringRef())
}
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) {
return idx.From[A](string(a))
}
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 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))
}