-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextdirect.go
More file actions
219 lines (179 loc) · 5.51 KB
/
extdirect.go
File metadata and controls
219 lines (179 loc) · 5.51 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
package extdirect
import (
"reflect"
"encoding/json"
"bytes"
"strings"
"fmt"
)
// DirectMethodTags serves to host tags for some direct method.
// Example: UpdateBasicInfoTags DirectMethodTags `formhandler:"true"`
// means tag `formhandler:"true"` targets UpdateBasicInfo direct method.
type DirectMethodTags struct{}
type directServiceProviderType string
const (
// RemotingProvider is remoting provider type.
RemotingProvider directServiceProviderType = "remoting"
// PollingProvider is polling provider type.
PollingProvider directServiceProviderType = "polling"
)
// DirectServiceProvider represents Ext Direct service settings.
type DirectServiceProvider struct {
ID *string `json:"id,omitempty"`
Type directServiceProviderType `json:"type"`
URL string `json:"url"`
Namespace string `json:"namespace"`
Timeout int `json:"timeout"`
Actions map[string]directAction `json:"actions"`
actionsInfo map[string]directActionInfo
debug bool
profile bool
}
type directAction []directMethod
type directMethod struct {
Name string `json:"name"`
// Method declaration MUST have one of the following mutually exclusive properties that describe the Method’s calling convention:
Len *int `json:"len,omitempty"`
FormHandler *bool `json:"formHander,omitempty"`
}
// DirectFormHandlerResult is a result of form handler execution.
type DirectFormHandlerResult struct {
Errors map[string]string `json:"errors,omitempty"`
Success bool `json:"success"`
}
type directActionInfo struct {
Type reflect.Type
Methods map[string]reflect.Method
DirectMethods map[string]directMethod
}
// JSON returns provider as JSON string.
func (provider DirectServiceProvider) JSON() (string, error) {
jsonText, err := json.Marshal(provider);
if err != nil {
return "", err
}
return string(jsonText), nil
}
// Debug enables/disables debugging for provider.
func (provider *DirectServiceProvider) Debug(debug bool) {
provider.debug = debug
}
// Profile enables/disables profiling for provider.
func (provider *DirectServiceProvider) Profile(profile bool) {
provider.profile = profile
}
// JavaScript returns javascript declaration of the provider.
func (provider DirectServiceProvider) JavaScript() (string, error) {
apiJSON, err := provider.JSON();
if err != nil {
return "", err
}
return fmt.Sprintf("Ext.ns(\"%s\");%s.REMOTE_API=%s", provider.Namespace, provider.Namespace, apiJSON), nil
}
// RegisterAction registers action.
func (provider *DirectServiceProvider) RegisterAction(typeInfo reflect.Type) {
actionTypeName := typeInfo.Name()
debug := provider.debug
if _, ok := provider.Actions[actionTypeName]; ok {
return
}
if debug {
log.Print(fmt.Sprintf("Register action %v", actionTypeName))
}
methodsLen := typeInfo.NumMethod()
var directAction []directMethod
methods := make(map[string]reflect.Method, 0)
directMethods := make(map[string]directMethod, 0)
if debug {
log.Print(fmt.Sprintf("\twith %v method(s)", methodsLen))
}
for i := 0; i < methodsLen; i++ {
methodInfo := typeInfo.Method(i)
if debug {
log.Print(fmt.Sprintf("\tregister method %v", methodInfo.Name))
}
argsLen := methodInfo.Type.NumIn() - 1
directMethodName := firstCharToLower(methodInfo.Name)
directMethod := directMethod{Name: directMethodName}
if debug {
log.Print(fmt.Sprintf("\t\twith args len = %v", argsLen))
log.Print("\t\tget method tags")
}
// Get method tags.
if tagsField := getDirectMethodTags(typeInfo, methodInfo.Name, debug); tagsField != nil {
if debug {
log.Print("\t\t\ttags found")
}
if tagsField.Tag.Get("formhandler") == "true" {
directMethod.FormHandler = new(bool)
*directMethod.FormHandler = true
}
} else {
if debug {
log.Print("\t\t\tno tags found")
}
}
if directMethod.FormHandler == nil {
directMethod.Len = new(int)
*directMethod.Len = argsLen
}
directAction = append(directAction, directMethod)
methods[directMethodName] = methodInfo
directMethods[directMethodName] = directMethod
}
provider.Actions[actionTypeName] = directAction
provider.actionsInfo[actionTypeName] = directActionInfo{
Type: typeInfo,
Methods: methods,
DirectMethods: directMethods,
}
}
// Provider is default provider.
var Provider *DirectServiceProvider
func init() {
Provider = NewProvider()
}
// NewProvider creates new provider with default configuration.
func NewProvider() (provider *DirectServiceProvider) {
provider = &DirectServiceProvider{
Type: RemotingProvider,
Namespace: "DirectApi",
URL: "/directapi",
Timeout: 30000,
Actions: make(map[string]directAction),
actionsInfo: make(map[string]directActionInfo),
}
return
}
func firstCharToLower(s string) string {
if len(s) < 2 {
return strings.ToLower(s)
}
bts := []byte(s)
lc := bytes.ToLower([]byte{bts[0]})
rest := bts[1:]
return string(bytes.Join([][]byte{lc, rest}, nil))
}
func getDirectMethodTags(t reflect.Type, methodName string, debug bool) *reflect.StructField {
fieldsLen := t.NumField()
dmt := reflect.TypeOf(DirectMethodTags{})
if debug {
log.Print(fmt.Sprintf("\t\t\tsearch tag among %v fields", fieldsLen))
}
for i := 0; i < fieldsLen; i++ {
f := t.Field(i)
if debug {
log.Print(fmt.Sprintf("\t\t\t\tfield %v of type %v", f.Name, f.Type))
}
if f.Name == (methodName + "Tags") && f.Type == dmt {
if debug {
log.Print("\t\t\t\t\tis a tag")
}
return &f;
}
if debug {
log.Print(fmt.Sprintf("\t\t\t\t\tis NOT a tag: nameOk=%v, typeOk=%v", f.Name == (methodName + "Tags"), f.Type == dmt))
}
}
return nil
}