forked from go-kratos/kratos
/
template.go
212 lines (183 loc) · 4.61 KB
/
template.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
209
210
211
212
package sdl
import (
"bytes"
"html/template"
"strings"
)
//nolint:lll
var messageTemplate = `
{{- /* delete empty line */ -}}
# Code generated by kratos proto types. DO NOT EDIT.
# source: {{ .Source }}
{{- if .QueryMethods }}
type {{ .Module }}Query {
{{- range .QueryMethods }}
{{ .Name }}(in: {{ .Request }}!): {{ .Reply }}!
{{- end }}
}
{{- end }}
{{- if .MutationMethods }}
type {{ .Module }}Mutation {
{{- range .MutationMethods }}
{{ .Name }}(in: {{ .Request }}!): {{ .Reply }}!
{{- end }}
}
{{- end }}
{{- if .SubscriptionMethods }}
type {{ .Module }}Subscription {
{{- range .SubscriptionMethods }}
{{ .Name }}(in: {{ .Request }}!): {{ .Reply }}!
{{- end }}
}
{{- end }}
{{- range .InputMessage }}
input {{ .Name }} {
{{- range .Fields }}
{{ .Name }}: {{ if .Repeated }}[{{ .Type }}{{ if .Required }}!{{ end }}]!{{ else }}{{ .Type }}{{ if .Required }}!{{ end }}{{ end }}
{{- end }}
}
{{- end }}
{{- range .OutputMessage }}
type {{ .Name }} {
{{- range .Fields }}
{{ .Name }}: {{ if .Repeated }}[{{ .Type }}{{ if .Required }}!{{ end }}]!{{ else }}{{ .Type }}{{ if .Required }}!{{ end }}{{ end }}
{{- end }}
}
{{- end }}
`
type FieldMode string
type MethodMode string
const (
input FieldMode = "input"
output FieldMode = "output"
)
const (
query MethodMode = "query"
mutation MethodMode = "mutation"
subscription MethodMode = "subscription"
)
// Method is a proto method.
type Method struct {
Name string
Request string
Reply string
}
type Field struct {
Name string
Type string
Repeated bool
Required bool
}
type Message struct {
Name string
Fields []*Field
}
type Msg struct {
Source string
Module string
MutationMethods []*Method
QueryMethods []*Method
SubscriptionMethods []*Method
InputMessage []*Message
OutputMessage []*Message
}
func (l *Msg) execute() ([]byte, error) {
buf := new(bytes.Buffer)
tmpl, err := template.New("msg").Parse(messageTemplate)
if err != nil {
return nil, err
}
if err := tmpl.Execute(buf, l); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// addInputMessage adds a input message.
func (l *Msg) addInputMessage(message *Message) {
l.InputMessage = append(l.InputMessage, message)
}
// addOutputMessage adds a output message.
func (l *Msg) addOutputMessage(message *Message) {
l.OutputMessage = append(l.OutputMessage, message)
}
// addQueryMethod adds a query method.
func (l *Msg) addQueryMethod(method *Method) {
l.QueryMethods = append(l.QueryMethods, method)
}
// addMutationMethod adds a mutation method.
func (l *Msg) addMutationMethod(method *Method) {
l.MutationMethods = append(l.MutationMethods, method)
}
// addSubscriptionMethod adds a subscription method.
func (l *Msg) addSubscriptionMethod(method *Method) {
l.SubscriptionMethods = append(l.SubscriptionMethods, method)
}
// addField adds a field.
func (m *Message) addField(field *Field) {
m.Fields = append(m.Fields, field)
}
// addMessage adds a message.
func (l *Msg) addMessage(mode FieldMode, message *Message) {
switch mode {
case output:
l.OutputMessage = append(l.OutputMessage, message)
case input:
l.InputMessage = append(l.InputMessage, message)
default:
l.OutputMessage = append(l.OutputMessage, message)
}
}
// addMethod adds a method.
func (l *Msg) addMethod(mode MethodMode, method *Method) {
switch mode {
case query:
l.QueryMethods = append(l.QueryMethods, method)
case mutation:
l.MutationMethods = append(l.MutationMethods, method)
case subscription:
l.SubscriptionMethods = append(l.SubscriptionMethods, method)
default:
l.QueryMethods = append(l.QueryMethods, method)
}
}
func parseComment(s, tag string) string {
tagStr := tag + ":"
if strings.Contains(s, tagStr) {
ss := strings.Split(s, " ")
for index, v := range ss {
if v == tagStr && index+1 < len(ss) {
res := ss[index+1]
// 去掉双引号
res = strings.ReplaceAll(res, "\"", "")
return res
}
}
}
return ""
}
// 获取Comment @sdl: "input|output|subscription"
func parseFieldMode(s string) FieldMode {
return FieldMode(parseComment(s, "@sdl"))
}
// 获取Comment @sdl_type: "query|mutation|subscription"
func parseMethodMode(s string) MethodMode {
return MethodMode(parseComment(s, "@sdl"))
}
// 首字母大写
func upperFirst(s string) string {
if len(s) == 0 {
return s
}
return strings.ToUpper(s[:1]) + s[1:]
}
// 类型存在.情况去最后一级
func parseFieldType(s string) string {
if strings.Contains(s, ".") {
ss := strings.Split(s, ".")
return upperFirst(ss[len(ss)-1])
}
return upperFirst(s)
}
func parseSdlType(s string) string {
return parseInlineComment(s, "@sdl_type")
}