forked from webrpc/webrpc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfuncmap.go
276 lines (235 loc) · 6.33 KB
/
funcmap.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package typescript
import (
"errors"
"fmt"
"strings"
"github.com/webrpc/webrpc/schema"
)
var fieldTypeMap = map[schema.DataType]string{
schema.T_Uint: "number",
schema.T_Uint8: "number",
schema.T_Uint16: "number",
schema.T_Uint32: "number",
schema.T_Uint64: "number",
schema.T_Int: "number",
schema.T_Int8: "number",
schema.T_Int16: "number",
schema.T_Int32: "number",
schema.T_Int64: "number",
schema.T_Float32: "number",
schema.T_Float64: "number",
schema.T_String: "string",
schema.T_Timestamp: "string",
schema.T_Null: "null",
schema.T_Any: "any",
schema.T_Byte: "string",
schema.T_Bool: "boolean",
}
func jsFieldType(in *schema.VarType) (string, error) {
switch in.Type {
case schema.T_Map:
return "object", nil
case schema.T_List:
z, err := fieldType(in.List.Elem)
if err != nil {
return "", err
}
return z + "[]", nil
case schema.T_Struct:
return in.Struct.Name, nil
default:
if fieldTypeMap[in.Type] != "" {
return fieldTypeMap[in.Type], nil
}
}
return "", fmt.Errorf("could not represent type: %#v", in)
}
func fieldType(in *schema.VarType) (string, error) {
switch in.Type {
case schema.T_Map:
typK, ok := fieldTypeMap[in.Map.Key]
if !ok {
return "", fmt.Errorf("unknown type mapping %v", in.Map.Key)
}
typV, err := fieldType(in.Map.Value)
if err != nil {
return "", err
}
return fmt.Sprintf("{[key: %s]: %s}", typK, typV), nil
case schema.T_List:
z, err := fieldType(in.List.Elem)
if err != nil {
return "", err
}
return "Array<" + z + ">", nil
case schema.T_Struct:
return in.Struct.Name, nil
default:
if fieldTypeMap[in.Type] != "" {
return fieldTypeMap[in.Type], nil
}
}
return "", fmt.Errorf("could not represent type: %#v", in)
}
func constPathPrefix(in schema.VarName) (string, error) {
return string(in) + "PathPrefix", nil
}
func methodInputName(in *schema.MethodArgument) string {
name := string(in.Name)
if name != "" {
return name
}
if in.Type != nil {
return in.Type.String()
}
return ""
}
func methodInputType(in *schema.MethodArgument) string {
z, _ := fieldType(in.Type)
return z
}
func methodArgumentInputInterfaceName(in *schema.Method) string {
if len(in.Service.Schema.Services) == 1 {
return fmt.Sprintf("%s%s", in.Name, "Args")
} else {
return fmt.Sprintf("%s%s%s", in.Service.Name, in.Name, "Args")
}
}
func methodArgumentOutputInterfaceName(in *schema.Method) string {
if len(in.Service.Schema.Services) == 1 {
return fmt.Sprintf("%s%s", in.Name, "Return")
} else {
return fmt.Sprintf("%s%s%s", in.Service.Name, in.Name, "Return")
}
}
func methodInputs(in *schema.Method) (string, error) {
inputs := []string{}
if len(in.Inputs) > 0 {
inputs = append(inputs, fmt.Sprintf("args: %s", methodArgumentInputInterfaceName(in)))
}
inputs = append(inputs, "headers?: object")
inputs = append(inputs, "signal?: AbortSignal")
return strings.Join(inputs, ", "), nil
}
func methodOutputs(in *schema.Method) (string, error) {
return fmt.Sprintf("Promise<%s>", methodArgumentOutputInterfaceName(in)), nil
}
func methodName(in interface{}) string {
v, _ := downcaseName(in)
return v
}
func isStruct(t schema.MessageType) bool {
return t == "struct"
}
func exportedField(in schema.VarName) (string, error) {
return string(in), nil
}
func exportableField(in schema.MessageField) bool {
for _, meta := range in.Meta {
for k := range meta {
if k == "json" {
if meta[k] == "-" {
return false
}
}
}
}
return true
}
func exportedJSONField(in schema.MessageField) (string, error) {
for _, meta := range in.Meta {
for k := range meta {
if k == "json" {
s := strings.Split(fmt.Sprintf("%v", meta[k]), ",")
return s[0], nil
}
}
}
return string(in.Name), nil
}
func interfaceName(in schema.VarName) (string, error) {
s := string(in)
return s, nil
}
func isEnum(t schema.MessageType) bool {
return t == "enum"
}
func downcaseName(v interface{}) (string, error) {
downFn := func(s string) string {
if s == "" {
return ""
}
return strings.ToLower(s[0:1]) + s[1:]
}
switch t := v.(type) {
case schema.VarName:
return downFn(string(t)), nil
case string:
return downFn(t), nil
default:
return "", errors.New("downcaseFieldName, unknown arg type")
}
}
func listComma(item int, count int) string {
if item+1 < count {
return ", "
}
return ""
}
func serviceInterfaceName(in schema.VarName) (string, error) {
s := string(in)
return s, nil
}
func newOutputArgResponse(in *schema.MethodArgument) (string, error) {
z, err := fieldType(in.Type)
if err != nil {
return "", err
}
typ := fmt.Sprintf("<%s>", z)
line := fmt.Sprintf("%s: %s(_data.%s)", in.Name, typ, in.Name)
return line, nil
}
func serverServiceName(in schema.VarName) (string, error) {
s := string(in)
return strings.ToLower(s[0:1]) + s[1:] + "Server", nil
}
func methodArgType(in *schema.MethodArgument) string {
z, err := fieldType(in.Type)
if err != nil {
panic(err.Error())
}
var prefix string
typ := in.Type.Type
if in.Optional {
prefix = "*"
}
if typ == schema.T_Struct {
prefix = "" // noop, as already pointer applied elsewhere
}
if typ == schema.T_List || typ == schema.T_Map {
prefix = ""
}
return prefix + z
}
var templateFuncMap = map[string]interface{}{
"fieldType": fieldType,
"constPathPrefix": constPathPrefix,
"interfaceName": interfaceName,
"methodName": methodName,
"methodInputs": methodInputs,
"methodOutputs": methodOutputs,
"methodArgumentInputInterfaceName": methodArgumentInputInterfaceName,
"methodArgumentOutputInterfaceName": methodArgumentOutputInterfaceName,
"isStruct": isStruct,
"isEnum": isEnum,
"listComma": listComma,
"serviceInterfaceName": serviceInterfaceName,
"exportableField": exportableField,
"exportedField": exportedField,
"exportedJSONField": exportedJSONField,
"newOutputArgResponse": newOutputArgResponse,
"downcaseName": downcaseName,
"serverServiceName": serverServiceName,
"methodArgType": methodArgType,
"jsFieldType": jsFieldType,
}