-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextensions.go
More file actions
708 lines (613 loc) · 15.6 KB
/
extensions.go
File metadata and controls
708 lines (613 loc) · 15.6 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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
package fxjson
import (
"fmt"
"reflect"
"sort"
"strconv"
"strings"
)
// FieldMapper 字段映射配置
type FieldMapper struct {
Rules map[string]string `json:"rules"` // 字段映射规则
DefaultValues map[string]interface{} `json:"default_values"` // 默认值
TypeCast map[string]string `json:"type_cast"` // 类型转换
}
// QueryBuilder 查询构建器
type QueryBuilder struct {
node Node
conditions []Condition
sortFields []SortField
limitCount int
offsetVal int
}
// Condition 查询条件
type Condition struct {
Field string `json:"field"`
Operator string `json:"operator"` // =, !=, >, <, >=, <=, in, not_in, contains
Value interface{} `json:"value"`
}
// SortField 排序字段
type SortField struct {
Field string `json:"field"`
Order string `json:"order"` // asc, desc
}
// Aggregator 聚合器
type Aggregator struct {
operations []AggOperation
groupBy []string
}
// AggOperation 聚合操作
type AggOperation struct {
Type string `json:"type"` // count, sum, avg, max, min
Field string `json:"field"` // 操作字段
Alias string `json:"alias"` // 结果别名
}
// ValidationRule 验证规则
type ValidationRule struct {
Required bool `json:"required"`
Type string `json:"type"` // string, number, boolean, array, object
MinLength int `json:"min_length"`
MaxLength int `json:"max_length"`
Min float64 `json:"min"`
Max float64 `json:"max"`
Pattern string `json:"pattern"`
Default interface{} `json:"default"`
Sanitize func(interface{}) interface{} `json:"-"`
}
// DataValidator 数据验证器
type DataValidator struct {
Rules map[string]ValidationRule `json:"rules"`
}
// Transform 数据变换
func (n Node) Transform(mapper FieldMapper) (map[string]interface{}, error) {
result := make(map[string]interface{})
// 应用默认值
for key, value := range mapper.DefaultValues {
result[key] = value
}
// 应用字段映射规则
for sourceField, targetField := range mapper.Rules {
sourceNode := n.Get(sourceField)
if sourceNode.Exists() {
// 获取原始值
var value interface{}
switch sourceNode.Type() {
case 's':
value, _ = sourceNode.String()
case 'n':
// 检查是否需要类型转换
if castType, exists := mapper.TypeCast[targetField]; exists {
switch castType {
case "int":
value, _ = sourceNode.Int()
case "float":
value, _ = sourceNode.Float()
default:
value, _ = sourceNode.Float()
}
} else {
value, _ = sourceNode.Float()
}
case 'b':
value, _ = sourceNode.Bool()
case 'a', 'o':
value = sourceNode.Raw()
}
result[targetField] = value
}
}
return result, nil
}
// Query 创建查询构建器
func (n Node) Query() *QueryBuilder {
return &QueryBuilder{
node: n,
conditions: make([]Condition, 0),
sortFields: make([]SortField, 0),
limitCount: -1,
offsetVal: 0,
}
}
// Where 添加查询条件
func (qb *QueryBuilder) Where(field, operator string, value interface{}) *QueryBuilder {
qb.conditions = append(qb.conditions, Condition{
Field: field,
Operator: operator,
Value: value,
})
return qb
}
// WhereIn 检查字段值是否在指定列表中
func (qb *QueryBuilder) WhereIn(field string, values []interface{}) *QueryBuilder {
return qb.Where(field, "in", values)
}
// WhereNotIn 检查字段值是否不在指定列表中
func (qb *QueryBuilder) WhereNotIn(field string, values []interface{}) *QueryBuilder {
return qb.Where(field, "not_in", values)
}
// WhereContains 检查字符串字段是否包含指定内容
func (qb *QueryBuilder) WhereContains(field, substring string) *QueryBuilder {
return qb.Where(field, "contains", substring)
}
// SortBy 添加排序
func (qb *QueryBuilder) SortBy(field, order string) *QueryBuilder {
qb.sortFields = append(qb.sortFields, SortField{
Field: field,
Order: order,
})
return qb
}
// Limit 限制结果数量
func (qb *QueryBuilder) Limit(count int) *QueryBuilder {
qb.limitCount = count
return qb
}
// Offset 设置偏移量
func (qb *QueryBuilder) Offset(offset int) *QueryBuilder {
qb.offsetVal = offset
return qb
}
// ToSlice 执行查询并返回结果
func (qb *QueryBuilder) ToSlice() ([]Node, error) {
if qb.node.Type() != 'a' {
return nil, fmt.Errorf("node is not an array")
}
var results []Node
// 遍历数组元素
for i := 0; i < qb.node.Len(); i++ {
item := qb.node.Index(i)
// 检查是否满足所有条件
if qb.matchesConditions(item) {
results = append(results, item)
}
}
// 排序
if len(qb.sortFields) > 0 {
qb.sortResults(results)
}
// 应用偏移和限制
start := qb.offsetVal
if start < 0 {
start = 0
}
if start >= len(results) {
return []Node{}, nil
}
end := len(results)
if qb.limitCount > 0 && start+qb.limitCount < end {
end = start + qb.limitCount
}
return results[start:end], nil
}
// Count 计算匹配条件的数量
func (qb *QueryBuilder) Count() (int, error) {
results, err := qb.ToSlice()
if err != nil {
return 0, err
}
return len(results), nil
}
// First 返回第一个匹配的元素
func (qb *QueryBuilder) First() (Node, error) {
qb.limitCount = 1
results, err := qb.ToSlice()
if err != nil {
return Node{}, err
}
if len(results) == 0 {
return Node{}, fmt.Errorf("no matching elements found")
}
return results[0], nil
}
// matchesConditions 检查节点是否满足所有条件
func (qb *QueryBuilder) matchesConditions(node Node) bool {
for _, condition := range qb.conditions {
if !qb.evaluateCondition(node, condition) {
return false
}
}
return true
}
// evaluateCondition 评估单个条件
func (qb *QueryBuilder) evaluateCondition(node Node, condition Condition) bool {
fieldNode := node.Get(condition.Field)
if !fieldNode.Exists() {
return condition.Operator == "!=" || condition.Operator == "not_in"
}
fieldValue := qb.getNodeValue(fieldNode)
switch condition.Operator {
case "=":
return qb.compareValues(fieldValue, condition.Value) == 0
case "!=":
return qb.compareValues(fieldValue, condition.Value) != 0
case ">":
return qb.compareValues(fieldValue, condition.Value) > 0
case "<":
return qb.compareValues(fieldValue, condition.Value) < 0
case ">=":
return qb.compareValues(fieldValue, condition.Value) >= 0
case "<=":
return qb.compareValues(fieldValue, condition.Value) <= 0
case "in":
if values, ok := condition.Value.([]interface{}); ok {
for _, v := range values {
if qb.compareValues(fieldValue, v) == 0 {
return true
}
}
}
return false
case "not_in":
if values, ok := condition.Value.([]interface{}); ok {
for _, v := range values {
if qb.compareValues(fieldValue, v) == 0 {
return false
}
}
}
return true
case "contains":
if fieldStr, ok := fieldValue.(string); ok {
if searchStr, ok := condition.Value.(string); ok {
return strings.Contains(fieldStr, searchStr)
}
}
return false
}
return false
}
// getNodeValue 获取节点的值
func (qb *QueryBuilder) getNodeValue(node Node) interface{} {
switch node.Type() {
case 's':
if val, err := node.String(); err == nil {
return val
}
case 'n':
if val, err := node.Float(); err == nil {
return val
}
case 'b':
if val, err := node.Bool(); err == nil {
return val
}
}
return nil
}
// compareValues 比较两个值
func (qb *QueryBuilder) compareValues(a, b interface{}) int {
// 类型转换和比较逻辑
aVal := qb.normalizeValue(a)
bVal := qb.normalizeValue(b)
// 字符串比较
if aStr, aOk := aVal.(string); aOk {
if bStr, bOk := bVal.(string); bOk {
return strings.Compare(aStr, bStr)
}
}
// 数值比较
if aNum, aOk := aVal.(float64); aOk {
if bNum, bOk := bVal.(float64); bOk {
if aNum < bNum {
return -1
} else if aNum > bNum {
return 1
}
return 0
}
}
// 布尔值比较
if aBool, aOk := aVal.(bool); aOk {
if bBool, bOk := bVal.(bool); bOk {
if aBool == bBool {
return 0
} else if aBool {
return 1
}
return -1
}
}
return 0
}
// normalizeValue 标准化值类型
func (qb *QueryBuilder) normalizeValue(value interface{}) interface{} {
switch v := value.(type) {
case int, int8, int16, int32, int64:
return float64(reflect.ValueOf(v).Int())
case uint, uint8, uint16, uint32, uint64:
return float64(reflect.ValueOf(v).Uint())
case float32:
return float64(v)
case string:
// 尝试转换为数字
if num, err := strconv.ParseFloat(v, 64); err == nil {
return num
}
return v
default:
return value
}
}
// sortResults 对结果进行排序
func (qb *QueryBuilder) sortResults(results []Node) {
sort.Slice(results, func(i, j int) bool {
for _, sortField := range qb.sortFields {
iVal := qb.getNodeValue(results[i].Get(sortField.Field))
jVal := qb.getNodeValue(results[j].Get(sortField.Field))
cmp := qb.compareValues(iVal, jVal)
if cmp != 0 {
if sortField.Order == "desc" {
return cmp > 0
}
return cmp < 0
}
}
return false
})
}
// Aggregate 创建聚合器
func (n Node) Aggregate() *Aggregator {
return &Aggregator{
operations: make([]AggOperation, 0),
groupBy: make([]string, 0),
}
}
// Count 计数聚合
func (agg *Aggregator) Count(alias string) *Aggregator {
agg.operations = append(agg.operations, AggOperation{
Type: "count",
Alias: alias,
})
return agg
}
// Sum 求和聚合
func (agg *Aggregator) Sum(field, alias string) *Aggregator {
agg.operations = append(agg.operations, AggOperation{
Type: "sum",
Field: field,
Alias: alias,
})
return agg
}
// Avg 平均值聚合
func (agg *Aggregator) Avg(field, alias string) *Aggregator {
agg.operations = append(agg.operations, AggOperation{
Type: "avg",
Field: field,
Alias: alias,
})
return agg
}
// Max 最大值聚合
func (agg *Aggregator) Max(field, alias string) *Aggregator {
agg.operations = append(agg.operations, AggOperation{
Type: "max",
Field: field,
Alias: alias,
})
return agg
}
// Min 最小值聚合
func (agg *Aggregator) Min(field, alias string) *Aggregator {
agg.operations = append(agg.operations, AggOperation{
Type: "min",
Field: field,
Alias: alias,
})
return agg
}
// GroupBy 分组
func (agg *Aggregator) GroupBy(fields ...string) *Aggregator {
agg.groupBy = append(agg.groupBy, fields...)
return agg
}
// Execute 执行聚合操作
func (agg *Aggregator) Execute(node Node) (map[string]interface{}, error) {
if node.Type() != 'a' {
return nil, fmt.Errorf("node must be an array for aggregation")
}
result := make(map[string]interface{})
// 如果没有分组,直接对所有数据聚合
if len(agg.groupBy) == 0 {
return agg.executeSimpleAggregation(node)
}
// 分组聚合
groups := make(map[string][]Node)
for i := 0; i < node.Len(); i++ {
item := node.Index(i)
groupKey := agg.buildGroupKey(item)
groups[groupKey] = append(groups[groupKey], item)
}
// 对每个分组执行聚合
for groupKey, groupItems := range groups {
groupResult := make(map[string]interface{})
for _, op := range agg.operations {
value, err := agg.executeOperation(op, groupItems)
if err != nil {
return nil, err
}
groupResult[op.Alias] = value
}
result[groupKey] = groupResult
}
return result, nil
}
// executeSimpleAggregation 执行简单聚合(无分组)
func (agg *Aggregator) executeSimpleAggregation(node Node) (map[string]interface{}, error) {
result := make(map[string]interface{})
// 转换为Node切片
items := make([]Node, node.Len())
for i := 0; i < node.Len(); i++ {
items[i] = node.Index(i)
}
for _, op := range agg.operations {
value, err := agg.executeOperation(op, items)
if err != nil {
return nil, err
}
result[op.Alias] = value
}
return result, nil
}
// buildGroupKey 构建分组键
func (agg *Aggregator) buildGroupKey(item Node) string {
var keyParts []string
for _, field := range agg.groupBy {
value := item.Get(field)
if valueStr, err := value.String(); err == nil {
keyParts = append(keyParts, valueStr)
} else {
keyParts = append(keyParts, "null")
}
}
return strings.Join(keyParts, "|")
}
// executeOperation 执行单个聚合操作
func (agg *Aggregator) executeOperation(op AggOperation, items []Node) (interface{}, error) {
switch op.Type {
case "count":
return len(items), nil
case "sum":
var sum float64
for _, item := range items {
if val, err := item.Get(op.Field).Float(); err == nil {
sum += val
}
}
return sum, nil
case "avg":
var sum float64
var count int
for _, item := range items {
if val, err := item.Get(op.Field).Float(); err == nil {
sum += val
count++
}
}
if count == 0 {
return 0, nil
}
return sum / float64(count), nil
case "max":
var max float64
var hasValue bool
for _, item := range items {
if val, err := item.Get(op.Field).Float(); err == nil {
if !hasValue || val > max {
max = val
hasValue = true
}
}
}
if !hasValue {
return nil, nil
}
return max, nil
case "min":
var min float64
var hasValue bool
for _, item := range items {
if val, err := item.Get(op.Field).Float(); err == nil {
if !hasValue || val < min {
min = val
hasValue = true
}
}
}
if !hasValue {
return nil, nil
}
return min, nil
default:
return nil, fmt.Errorf("unknown aggregation operation: %s", op.Type)
}
}
// Validate 数据验证
func (n Node) Validate(validator *DataValidator) (map[string]interface{}, []error) {
result := make(map[string]interface{})
var errors []error
for fieldName, rule := range validator.Rules {
fieldNode := n.Get(fieldName)
// 检查必填字段
if rule.Required && !fieldNode.Exists() {
errors = append(errors, fmt.Errorf("field '%s' is required", fieldName))
continue
}
// 应用默认值
if !fieldNode.Exists() && rule.Default != nil {
result[fieldName] = rule.Default
continue
}
if !fieldNode.Exists() {
continue
}
// 验证和转换值
value, err := validateAndConvertField(fieldNode, rule)
if err != nil {
errors = append(errors, fmt.Errorf("field '%s': %w", fieldName, err))
continue
}
// 应用清理函数
if rule.Sanitize != nil {
value = rule.Sanitize(value)
}
result[fieldName] = value
}
return result, errors
}
// validateAndConvertField 验证和转换字段值
func validateAndConvertField(node Node, rule ValidationRule) (interface{}, error) {
switch rule.Type {
case "string":
value, err := node.String()
if err != nil {
return nil, err
}
if rule.MinLength > 0 && len(value) < rule.MinLength {
return nil, fmt.Errorf("string too short, minimum length is %d", rule.MinLength)
}
if rule.MaxLength > 0 && len(value) > rule.MaxLength {
return nil, fmt.Errorf("string too long, maximum length is %d", rule.MaxLength)
}
return value, nil
case "number":
value, err := node.Float()
if err != nil {
return nil, err
}
if rule.Min != 0 && value < rule.Min {
return nil, fmt.Errorf("number too small, minimum is %f", rule.Min)
}
if rule.Max != 0 && value > rule.Max {
return nil, fmt.Errorf("number too large, maximum is %f", rule.Max)
}
return value, nil
case "boolean":
return node.Bool()
default:
// 原样返回
switch node.Type() {
case 's':
return node.String()
case 'n':
return node.Float()
case 'b':
return node.Bool()
default:
return node.Raw(), nil
}
}
}
// Stream 流式处理
func (n Node) Stream(processor func(Node, int) bool) error {
if n.Type() != 'a' {
return fmt.Errorf("node must be an array for streaming")
}
for i := 0; i < n.Len(); i++ {
item := n.Index(i)
if !processor(item, i) {
break
}
}
return nil
}