-
Notifications
You must be signed in to change notification settings - Fork 34
/
operator.go
92 lines (72 loc) · 1.75 KB
/
operator.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
package kit
import (
"fmt"
"net/url"
"reflect"
"github.com/machinefi/w3bstream/pkg/depends/base/types"
"github.com/machinefi/w3bstream/pkg/depends/x/reflectx"
)
func NewOperatorFactory(op Operator, last bool) *OperatorFactory {
opType := reflectx.DeRef(reflect.TypeOf(op))
if opType.Kind() != reflect.Struct {
panic(fmt.Errorf("operator must be a struct type, got %#v", op))
}
meta := &OperatorFactory{}
meta.IsLast = last
meta.Operator = op
if _, ok := op.(OperatorWithoutOutput); ok {
meta.NoOutput = true
}
meta.Type = reflectx.DeRef(reflect.TypeOf(op))
if operatorWithParams, ok := op.(OperatorWithParams); ok {
meta.Params = operatorWithParams.OperatorParams()
}
if !meta.IsLast {
if ctxKey, ok := op.(ContextProvider); ok {
meta.ContextKey = ctxKey.ContextKey()
} else {
if ctxKey, ok := op.(oldContextProvider); ok {
meta.ContextKey = ctxKey.ContextKey()
} else {
meta.ContextKey = meta.Type.String()
}
}
}
return meta
}
type oldContextProvider interface {
ContextKey() string
}
// TODO remove ContextKey?
type OperatorFactory struct {
Type reflect.Type
ContextKey interface{}
NoOutput bool
Params url.Values
IsLast bool
Operator Operator
}
func (o *OperatorFactory) String() string {
if o.Params != nil {
return o.Type.String() + "?" + o.Params.Encode()
}
return o.Type.String()
}
func (o *OperatorFactory) New() Operator {
var op Operator
if newer, ok := o.Operator.(OperatorNewer); ok {
op = newer.New()
} else {
op = reflect.New(o.Type).Interface().(Operator)
}
if init, ok := op.(OperatorInit); ok {
init.InitFrom(o.Operator)
}
if setter, ok := op.(types.DefaultSetter); ok {
setter.SetDefault()
}
return op
}
type EmptyOperator struct {
OperatorWithoutOutput
}