This repository has been archived by the owner on Oct 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 215
/
skipper.go
111 lines (99 loc) · 2.24 KB
/
skipper.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
package skip
import (
"fmt"
"math"
"reflect"
"regexp"
"strconv"
"github.com/compose/transporter/function"
"github.com/compose/transporter/message"
)
type unknownOperatorError struct {
Op string
}
func (e unknownOperatorError) Error() string {
return fmt.Sprintf("unkown operator, %s", e.Op)
}
type wrongTypeError struct {
Wanted string
Got string
}
func (e wrongTypeError) Error() string {
return fmt.Sprintf("value is of incompatible type, wanted %s, got %s", e.Wanted, e.Got)
}
func init() {
function.Add(
"skip",
func() function.Function {
return &skip{}
},
)
}
type skip struct {
Field string `json:"field"`
Operator string `json:"operator"`
Match interface{} `json:"match"`
}
func (s *skip) Apply(msg message.Msg) (message.Msg, error) {
val := msg.Data().Get(s.Field)
switch s.Operator {
case "==", "eq", "$eq":
if reflect.DeepEqual(val, s.Match) {
return msg, nil
}
case "=~":
if ok, err := regexp.MatchString(s.Match.(string), val.(string)); err != nil || ok {
return msg, err
}
case ">", "gt", "$gt":
v, m, err := convertForComparison(val, s.Match)
if err == nil && v > m {
return msg, err
}
return nil, err
case ">=", "gte", "$gte":
v, m, err := convertForComparison(val, s.Match)
if err == nil && v >= m {
return msg, err
}
return nil, err
case "<", "lt", "$lt":
v, m, err := convertForComparison(val, s.Match)
if err == nil && v < m {
return msg, err
}
return nil, err
case "<=", "lte", "$lte":
v, m, err := convertForComparison(val, s.Match)
if err == nil && v <= m {
return msg, err
}
return nil, err
default:
return nil, unknownOperatorError{s.Operator}
}
return nil, nil
}
func convertForComparison(in1, in2 interface{}) (float64, float64, error) {
float1, err := convertToFloat(in1)
if err != nil {
return math.NaN(), math.NaN(), err
}
float2, err := convertToFloat(in2)
if err != nil {
return math.NaN(), math.NaN(), err
}
return float1, float2, nil
}
func convertToFloat(in interface{}) (float64, error) {
switch i := in.(type) {
case float64:
return i, nil
case int:
return float64(i), nil
case string:
return strconv.ParseFloat(i, 64)
default:
return math.NaN(), wrongTypeError{"float64 or int", fmt.Sprintf("%T", i)}
}
}