-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstl.go
413 lines (353 loc) · 9.06 KB
/
stl.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package stl
import (
"bytes"
"encoding/json"
"fmt"
"github.com/xpsuper/stl/adapter"
"github.com/xpsuper/stl/canvas"
"github.com/xpsuper/stl/dispatcher"
"github.com/xpsuper/stl/eval"
"github.com/xpsuper/stl/excel"
"github.com/xpsuper/stl/helper"
"github.com/xpsuper/stl/htmlparser"
"github.com/xpsuper/stl/jwt"
"github.com/xpsuper/stl/linq"
"github.com/xpsuper/stl/memorycache"
"github.com/xpsuper/stl/objassigner"
"github.com/xpsuper/stl/srvmanager"
"github.com/xpsuper/stl/taskbus"
"image"
"io"
"reflect"
"unsafe"
)
var (
S *XPStringImpl
N *XPNumberImpl
Random *XPRandomImpl
DateTime *XPDateTimeImpl
Array *XPArrayImpl
Async *XPAsyncImpl
Config *XPConfigImpl
Encrypt *XPEncryptImpl
IPAddress *XPIPImpl
Queue *XPQueueImpl
Regexp *XPRegexpImpl
Scheduler *XPSchedulerImpl
Zip *XPZipImpl
Jwt *jwt.XPJwtImpl
IdCard *XPIdCardImpl
)
func init() {
S = &XPStringImpl{}
N = &XPNumberImpl{}
Random = &XPRandomImpl{}
Array = &XPArrayImpl{}
DateTime = &XPDateTimeImpl{}
Async = NewAsync()
Config = NewXPConfig(nil)
Encrypt = &XPEncryptImpl{}
IPAddress = NewIPAddress()
Queue = NewXPQueue(500)
Regexp = &XPRegexpImpl{}
Scheduler = NewScheduler()
Zip = &XPZipImpl{}
Jwt = &jwt.XPJwtImpl{}
IdCard = &XPIdCardImpl{}
}
// IsEmpty 判断是否为空
func IsEmpty(data interface{}) bool {
if data == nil {
return true
}
dataRef := reflect.ValueOf(data)
for dataRef.Kind() == reflect.Ptr {
dataRef = dataRef.Elem()
}
switch dataRef.Kind() {
case reflect.Array, reflect.Map, reflect.Slice:
return dataRef.Len() == 0
case reflect.String:
return S.IsEmpty(data.(string))
}
//switch data.(type) {
//case string:
// return S.IsEmpty(data.(string))
//case []interface{}:
// return len(data.([]interface{})) == 0
//}
return false
}
// ToJson 转为 Json 字符串
func ToJson(data interface{}) string {
jsonByte, err := json.Marshal(data)
if err != nil {
fmt.Printf("Marshal with error: %+v\n", err)
return "{}"
}
return string(jsonByte)
}
func FromJson(jsonStr string, data interface{}) error {
return json.Unmarshal([]byte(jsonStr), data)
}
// ToJsonIndent 转为 Json 格式化字符串
func ToJsonIndent(data interface{}) string {
b, err := json.Marshal(data)
if err != nil {
fmt.Printf("Marshal with error: %+v\n", err)
return "{}"
}
var out bytes.Buffer
err = json.Indent(&out, b, "", " ")
return out.String()
}
// ToMap 转为 map
func ToMap(data interface{}) map[string]interface{} {
dataRef := reflect.ValueOf(data)
for dataRef.Kind() == reflect.Ptr {
dataRef = dataRef.Elem()
}
var jsonByte []byte
var err error
if dataRef.Kind() == reflect.String {
jsonByte = []byte(dataRef.String())
} else {
jsonByte, err = json.Marshal(data)
}
if err != nil {
fmt.Printf("Marshal with error: %+v\n", err)
return nil
}
m := make(map[string]interface{})
err = json.Unmarshal(jsonByte, &m)
if err != nil {
fmt.Printf("Unmarshal with error: %+v\n", err)
return nil
}
return m
}
// ToArray 转为 slice
func ToArray(data interface{}) []interface{} {
dataRef := reflect.ValueOf(data)
for dataRef.Kind() == reflect.Ptr {
dataRef = dataRef.Elem()
}
var jsonByte []byte
var err error
if dataRef.Kind() == reflect.String {
jsonByte = []byte(dataRef.String())
} else {
jsonByte, err = json.Marshal(data)
}
if err != nil {
fmt.Printf("Marshal with error: %+v\n", err)
return nil
}
m := make([]interface{}, 0)
err = json.Unmarshal(jsonByte, &m)
if err != nil {
fmt.Printf("Unmarshal with error: %+v\n", err)
return nil
}
return m
}
// StringToBytes string 转为 []byte
func StringToBytes(s string) []byte {
return *(*[]byte)(unsafe.Pointer(
&struct {
string
Cap int
}{s, len(s)},
))
}
// BytesToString []byte 转为 string
func BytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
// MapValue 获取 map 的值
func MapValue[K comparable, V any](m map[K]interface{}, key K, def V) V {
if m == nil {
return def
}
if v, has := m[key]; has {
if value, ok := v.(V); ok {
return value
}
}
return def
}
// MapKeys 获取 map 的所有 key
func MapKeys[K comparable, V any](m map[K]V) []K {
keys := make([]K, len(m))
var i int
for k := range m {
keys[i] = k
i++
}
return keys
}
// MapValues 获取 map 的所有 value
func MapValues[K comparable, V any](m map[K]V) []V {
values := make([]V, len(m))
var i int
for _, v := range m {
values[i] = v
i++
}
return values
}
// AdapterDecode 对象转换适配器
func AdapterDecode(input, output interface{}) error {
return adapter.WeakDecode(input, output)
}
// AdapterDecodeByTag 对象转换-自定义标签适配器
func AdapterDecodeByTag(input, output interface{}, tag string) error {
return adapter.WeakDecodeByTag(input, output, tag)
}
// ConfigIpAddress 纯真IP库配置
func ConfigIpAddress(keyFileUrl, dataFileUrl string) {
KeyFileUrl = keyFileUrl
DataFileUrl = dataFileUrl
}
// Dispatcher 任务分发器
func Dispatcher(cnt int) (*dispatcher.Dispatcher, error) {
return dispatcher.NewDispatcher(cnt)
}
// FilePath 获取文件路径对象
func FilePath(path string) (filepath *XPFilePathImpl, err error) {
return NewFilePath(path)
}
func FilePathCurrent() (filepath *XPFilePathImpl, err error) {
return NewFilePathFromCurrentPath()
}
// IdGenerator 唯一ID生成器
func IdGenerator(workerId int64) (idGenerator *XPIdGeneratorImpl, err error) {
return NewIdGenerator(workerId)
}
// LINQ 集成查询
func LINQ(source interface{}) linq.LinQuery {
return linq.From(source)
}
// Helper 帮助类
func Helper(v interface{}) helper.Helper {
return helper.Chain(v)
}
func HelperLazy(v interface{}) helper.Helper {
return helper.LazyChain(v)
}
// Http HttpClient
func Http() *XPHttpImpl {
return NewHttp()
}
// JsonValid Json对象验证器
func JsonValid(json string) bool {
return Valid(json)
}
func JsonDataValid(json []byte) bool {
return ValidBytes(json)
}
func JsonParse(json string) JsonItem {
return Parse(json)
}
func JsonDataParse(json []byte) JsonItem {
return ParseBytes(json)
}
func JsonGet(json, path string) JsonItem {
return Get(json, path)
}
// Cache MemoryCache
func Cache(config *memorycache.Configuration) *memorycache.Cache {
return memorycache.NewCache(config)
}
// MapWithOrder 排序的map
func MapWithOrder(less OrderMapKeyLess) *OrderMap {
return NewOrderMap(less)
}
// Promise 异步执行
func Promise(executor func(resolve func(interface{}), reject func(error))) *XPPromiseImpl {
return NewPromise(executor)
}
func Resolve(resolution interface{}) *XPPromiseImpl {
return ResolvePromise(resolution)
}
func Reject(err error) *XPPromiseImpl {
return RejectPromise(err)
}
func All(promises ...*XPPromiseImpl) *XPPromiseImpl {
return PromiseAll(promises...)
}
func Race(promises ...*XPPromiseImpl) *XPPromiseImpl {
return PromiseRace(promises...)
}
// SpinLocker 自旋锁
func SpinLocker() *SpinLock {
return &SpinLock{}
}
// Canvas 画布
func Canvas(width, height int) *canvas.Context {
return canvas.NewContext(width, height)
}
func CanvasForImage(img image.Image) *canvas.Context {
return canvas.NewContextForImage(img)
}
func CanvasForRGBA(rgba *image.RGBA) *canvas.Context {
return canvas.NewContextForRGBA(rgba)
}
// ServiceBind ServiceManager 服务管理器
func ServiceBind(fn func()) {
srvmanager.InitManage()
srvmanager.Bind(fn)
}
// ServiceWait ServiceManager 服务管理器
func ServiceWait() {
srvmanager.Wait()
}
// TaskBusChain TaskBus 异步任务总线
func TaskBusChain(stack taskbus.Tasks, firstArgs ...interface{}) ([]interface{}, error) {
return taskbus.Chain(stack, firstArgs...)
}
// TaskBusMax TaskBus 异步任务总线
func TaskBusMax(stack taskbus.Taskier) (taskbus.Results, error) {
return taskbus.Max(stack)
}
// TaskBusAll TaskBus 异步任务总线
func TaskBusAll(stack taskbus.Taskier) (taskbus.Results, error) {
return taskbus.All(stack)
}
// TaskTicker 定时任务
func TaskTicker(scanInterval int, execOnStart bool) *TickerTasks {
return NewTicker(scanInterval, execOnStart)
}
// Eval 执行表达式
func Eval(expression string, parameter interface{}, opts ...eval.Language) (interface{}, error) {
return eval.Evaluate(expression, parameter, opts...)
}
func EvalFull(extensions ...eval.Language) eval.Language {
return eval.Full(extensions...)
}
func EvalConstant(name string, value interface{}) eval.Language {
return eval.Constant(name, value)
}
func EvalFunction(name string, value interface{}) eval.Language {
return eval.Function(name, value)
}
// HtmlParser 解析html
func HtmlParser(r io.Reader) (*htmlparser.Node, error) {
return htmlparser.Parse(r)
}
// ExcelParser 解析Excel
func ExcelParser(filePath string, container interface{}) error {
return excel.UnmarshalXLSX(filePath, container)
}
// ObjDeepCopy Interface Deep Copy
func ObjDeepCopy(src interface{}) *XPDeepCPImpl {
return DeepCopy(src)
}
// ObjAssign Interface Assign
func ObjAssign(target, source interface{}) error {
return objassigner.Assign(target, source)
}
// ObjAssignWithOption Interface Assign With Option
func ObjAssignWithOption(target, source interface{}, opt objassigner.Option) error {
return objassigner.AssignWithOption(target, source, opt)
}