-
Notifications
You must be signed in to change notification settings - Fork 0
/
reducer.go
127 lines (103 loc) · 2.13 KB
/
reducer.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
package dscope
import (
"errors"
"fmt"
"reflect"
"strings"
)
// reducer types
type CustomReducer interface {
Reduce(Scope, []reflect.Value) reflect.Value
}
var customReducerType = reflect.TypeFor[CustomReducer]()
type Reducer interface {
IsReducer()
}
var reducerType = reflect.TypeFor[Reducer]()
func getReducerKind(t reflect.Type) reducerKind {
if t.Implements(reducerType) {
return isReducer
}
if t.Implements(customReducerType) {
return isCustomReducer
}
return notReducer
}
// reducer mark type
type reducerMark struct{}
var reducerMarkType = reflect.TypeFor[reducerMark]()
var reducerMarkTypes = NewCowMap[_TypeID, reflect.Type]()
func getReducerMarkType(t reflect.Type, id _TypeID) reflect.Type {
if v, ok := reducerMarkTypes.Get(id); ok {
return v
}
markType := reflect.FuncOf(
[]reflect.Type{
reducerMarkType,
},
[]reflect.Type{
t,
},
false,
)
reducerMarkTypes.Set(id, markType)
return markType
}
// reduce func
var ErrNoValues = errors.New("no values")
func Reduce(vs []reflect.Value) reflect.Value {
if len(vs) == 0 {
panic(ErrNoValues)
}
t := vs[0].Type()
ret := reflect.New(t).Elem()
switch t.Kind() {
case reflect.Slice:
for _, v := range vs {
var elems []reflect.Value
for i := 0; i < v.Len(); i++ {
elems = append(elems, v.Index(i))
}
ret = reflect.Append(ret, elems...)
}
case reflect.String:
var b strings.Builder
for _, v := range vs {
b.WriteString(v.String())
}
ret = reflect.New(t)
ret.Elem().SetString(b.String())
ret = ret.Elem()
case reflect.Func:
ret = reflect.MakeFunc(
t,
func(args []reflect.Value) (rets []reflect.Value) {
for _, v := range vs {
if v.IsNil() {
continue
}
rets = v.Call(args)
}
return
},
)
case reflect.Map:
ret = reflect.MakeMap(t)
for _, v := range vs {
iter := v.MapRange()
for iter.Next() {
ret.SetMapIndex(iter.Key(), iter.Value())
}
}
case reflect.Int:
var i int64
for _, v := range vs {
i += v.Int()
}
ret = reflect.New(t).Elem()
ret.SetInt(i)
default: // NOCOVER
panic(fmt.Errorf("don't know how to reduce %v", t))
}
return ret
}