-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.go
188 lines (161 loc) · 4.28 KB
/
converter.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
package converter
import "fmt"
type ConversionFn func(interface{}, *interface{}) error
type StreamConvFn func(chan interface{}) chan interface{}
type ConverterChain []*Converter
func NewConverterChain(filterNames []string) (ConverterChain, error) {
if len(filterNames) == 0 {
return nil, fmt.Errorf("you should have at least one filter")
}
chain := ConverterChain{}
for _, name := range filterNames {
converter, err := GetConverterByName(name)
if err != nil {
return nil, err
}
chain = append(chain, converter)
}
return chain, nil
}
func GetTypeConversionFunc(inType, outType string) ConversionFn {
if inType == outType {
return nil
}
if inType == "interface{}" || outType == "interface{}" {
return nil
}
for _, converter := range RegisteredConverters {
if converter.InputType == inType && converter.OutputType == outType && converter.IsDefaultTypeConverter {
return converter.ConversionFunc
}
}
return nil
}
func (chain *ConverterChain) ConversionFunc(inType, outType string) (ConversionFn, error) {
if len(*chain) == 0 {
return nil, fmt.Errorf("you should have at least one converter")
}
lastRealInType := inType
fn := (*chain)[0].ConversionFunc
if convertFn := GetTypeConversionFunc(inType, (*chain)[0].InputType); convertFn != nil {
fn = Pipe(convertFn, fn)
}
if len(*chain) == 1 {
return fn, nil
}
inType = (*chain)[0].OutputType
for _, right := range (*chain)[1:] {
if inType != "interface{}" {
lastRealInType = inType
}
if convertFn := GetTypeConversionFunc(lastRealInType, right.InputType); convertFn != nil {
fn = Pipe(fn, convertFn)
}
fn = Pipe(fn, right.ConversionFunc)
inType = right.OutputType
}
return fn, nil
}
func (chain *ConverterChain) Convert(input interface{}, output *interface{}) error {
fn, err := chain.ConversionFunc("interface{}", "interface{}")
if err != nil {
return err
}
return fn(input, output)
}
func GetConverterByName(name string) (*Converter, error) {
for _, converter := range RegisteredConverters {
if converter.Name == name {
return converter, nil
}
}
return nil, fmt.Errorf("no such filter %q", name)
}
func Chain(left ConversionFn, rights ...ConversionFn) ConversionFn {
fn := left
for _, right := range rights {
fn = Pipe(fn, right)
}
return fn
}
func Pipe(left, right ConversionFn) ConversionFn {
return func(in interface{}, output *interface{}) error {
var tmpOutput interface{}
if err := left(in, &tmpOutput); err != nil {
return err
}
return right(tmpOutput, output)
}
}
func ConversionToStreamConv(conversionFn ConversionFn) StreamConvFn {
return func(in chan interface{}) chan interface{} {
out := make(chan interface{})
go func() {
for {
select {
case input := <-in:
var output interface{}
_ = conversionFn(input, &output)
// FIXME: check err
out <- output
}
}
}()
return out
}
}
func StreamPipe(left, right StreamConvFn) StreamConvFn {
return func(in chan interface{}) chan interface{} {
return right(left(in))
}
}
func StreamChain(streamFuncs ...StreamConvFn) StreamConvFn {
return func(in chan interface{}) chan interface{} {
left := in
for _, right := range streamFuncs {
left = right(left)
}
return left
}
}
type Converter struct {
InputType string
OutputType string
Name string
ConversionFunc ConversionFn
StreamConvFunc StreamConvFn
IsDefaultTypeConverter bool
}
func (conv *Converter) SetType(ioType string) *Converter {
conv.InputType = ioType
conv.OutputType = ioType
return conv
}
func (conv *Converter) SetTypes(inType, outType string) *Converter {
conv.InputType = inType
conv.OutputType = outType
return conv
}
func (conv *Converter) SetConversionFunc(fn ConversionFn) *Converter {
conv.ConversionFunc = fn
return conv
}
func (conv *Converter) SetStreamConvFunc(fn StreamConvFn) *Converter {
conv.StreamConvFunc = fn
return conv
}
func (conv *Converter) SetDefaultTypeConverter() *Converter {
conv.IsDefaultTypeConverter = true
return conv
}
func NewConverter(name string) *Converter {
return &Converter{
InputType: "interface{}",
OutputType: "interface{}",
Name: name,
}
}
var RegisteredConverters []*Converter
func RegisterConverter(converter *Converter) {
RegisteredConverters = append(RegisteredConverters, converter)
}