forked from 99designs/gqlgen
/
type.go
208 lines (176 loc) · 4.79 KB
/
type.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
207
208
package codegen
import (
"strconv"
"strings"
)
type NamedTypes map[string]*NamedType
type NamedType struct {
Ref
IsScalar bool
IsInterface bool
IsInput bool
IsPointer bool
GQLType string // Name of the graphql type
Marshaler *Ref // If this type has an external marshaler this will be set
Unmarshaler *Ref // If this type has an external unmarshaler this will be set
}
type Ref struct {
GoType string // Name of the go type
Package string // the package the go type lives in
Import *Import // the resolved import with alias
IsUserDefined bool // does the type exist in the typemap
}
type Type struct {
*NamedType
Modifiers []string
AliasedType *Ref
}
const (
modList = "[]"
modPtr = "*"
)
func (t Ref) FullName() string {
return t.PkgDot() + t.GoType
}
func (t Ref) PkgDot() string {
if t.Import == nil || t.Import.Alias() == "" {
return ""
}
return t.Import.Alias() + "."
}
func (t Type) Signature() string {
if t.AliasedType != nil {
return strings.Join(t.Modifiers, "") + t.AliasedType.FullName()
}
return strings.Join(t.Modifiers, "") + t.FullName()
}
func (t Type) FullSignature() string {
pkg := ""
if t.Package != "" {
pkg = t.Package + "."
}
return strings.Join(t.Modifiers, "") + pkg + t.GoType
}
func (t *Type) RealModifiers() []string {
if t.IsPointer {
for i := 0; i < len(t.Modifiers); i++ {
if t.Modifiers[i] == modPtr {
return append(t.Modifiers[:i], t.Modifiers[i+1:]...)
}
}
}
return t.Modifiers
}
func (t Type) UnmarshaledSignature() string {
if t.Unmarshaler == nil {
return t.Signature()
}
return strings.Join(t.RealModifiers(), "") + t.Unmarshaler.FullName()
}
func (t Type) FullUnmarshaledSignature() string {
if t.Unmarshaler == nil {
return ""
}
pkg := ""
if t.Unmarshaler.Package != "" {
pkg = t.Unmarshaler.Package + "."
}
return strings.Join(t.RealModifiers(), "") + pkg + t.Unmarshaler.GoType
}
func (t Type) IsPtr() bool {
return len(t.Modifiers) > 0 && t.Modifiers[0] == modPtr
}
func (t *Type) StripPtr() {
if !t.IsPtr() {
return
}
t.Modifiers = t.Modifiers[0 : len(t.Modifiers)-1]
}
func (t Type) IsSlice() bool {
return len(t.Modifiers) > 0 && t.Modifiers[0] == modList ||
len(t.Modifiers) > 1 && t.Modifiers[0] == modPtr && t.Modifiers[1] == modList
}
func (t NamedType) IsMarshaled() bool {
return t.Marshaler != nil
}
func (t Type) Unmarshal(result, raw string) string {
return t.unmarshal(result, raw, t.RealModifiers(), 1)
}
func (t Type) unmarshal(result, raw string, remainingMods []string, depth int) string {
switch {
case len(remainingMods) > 0 && remainingMods[0] == modPtr:
ptr := "ptr" + strconv.Itoa(depth)
return tpl(`var {{.ptr}} {{.mods}}{{.t.FullName}}
if {{.raw}} != nil {
{{.next}}
{{.result}} = &{{.ptr -}}
}
`, map[string]interface{}{
"ptr": ptr,
"t": t,
"raw": raw,
"result": result,
"mods": strings.Join(remainingMods[1:], ""),
"next": t.unmarshal(ptr, raw, remainingMods[1:], depth+1),
})
case len(remainingMods) > 0 && remainingMods[0] == modList:
var rawIf = "rawIf" + strconv.Itoa(depth)
var index = "idx" + strconv.Itoa(depth)
var fullName string
if t.Unmarshaler != nil {
fullName = t.Unmarshaler.FullName()
} else {
fullName = t.FullName()
}
return tpl(`var {{.rawSlice}} []interface{}
if {{.raw}} != nil {
if tmp1, ok := {{.raw}}.([]interface{}); ok {
{{.rawSlice}} = tmp1
} else {
{{.rawSlice}} = []interface{}{ {{.raw}} }
}
}
{{.result}} = make({{.type}}, len({{.rawSlice}}))
for {{.index}} := range {{.rawSlice}} {
{{ .next -}}
}`, map[string]interface{}{
"raw": raw,
"rawSlice": rawIf,
"index": index,
"result": result,
"type": strings.Join(remainingMods, "") + fullName,
"next": t.unmarshal(result+"["+index+"]", rawIf+"["+index+"]", remainingMods[1:], depth+1),
})
}
realResult := result
if t.AliasedType != nil {
result = "castTmp"
}
return tpl(`{{- if .t.AliasedType }}
var castTmp {{.t.UnmarshaledSignature}}
{{ end }}
{{- if eq .t.GoType "map[string]interface{}" }}
{{- .result }} = {{.raw}}.(map[string]interface{})
{{- else if .t.Marshaler }}
{{- .result }}, err = {{ .t.Marshaler.PkgDot }}Unmarshal{{.t.Marshaler.GoType}}({{.raw}})
{{- else -}}
err = (&{{.result}}).UnmarshalGQL({{.raw}})
{{- end }}
{{- if .t.AliasedType }}
{{ .realResult }} = {{.t.AliasedType.FullName}}(castTmp)
{{- end }}`, map[string]interface{}{
"realResult": realResult,
"result": result,
"raw": raw,
"t": t,
})
}
func (t Type) Marshal(val string) string {
if t.AliasedType != nil {
val = t.GoType + "(" + val + ")"
}
if t.Marshaler != nil {
return "return " + t.Marshaler.PkgDot() + "Marshal" + t.Marshaler.GoType + "(" + val + ")"
}
return "return " + val
}