Skip to content

Commit 27ab2aa

Browse files
authored
Merge pull request #23 from spatialcurrent/numeric_arrays
Equal/NotEqual support for numeric arrays
2 parents 450c254 + ca9ba43 commit 27ab2aa

16 files changed

Lines changed: 725 additions & 33 deletions

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ script:
2626
- gometalinter --misspell-locale=US --disable-all --enable=misspell --enable=vet ./cmd/dfljs/
2727
- cd cmd/dfl
2828
- go fmt
29-
- go test
3029
- go build
30+
- go test

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ import (
5555
</html>
5656
```
5757

58-
5958
# Examples:
6059

6160
**Environment**
@@ -66,6 +65,22 @@ With the `-env` flag you can use DFL filters against the current environment.
6665
./dfl -env -f '@SHELL in [/bin/sh, /bin/bash]' && echo "Shell is set to sh or bash"
6766
```
6867

68+
**Arrays**
69+
70+
You can use DFL to filter integer arrays.
71+
72+
```
73+
./dfl -verbose -f '@a == [1, 2, 3, 4]' 'a=[1, 2, 3, 4]'
74+
# returns true as exit code 0
75+
```
76+
77+
You can also use DFl to compare byte arrays.
78+
79+
```
80+
./dfl -f '@a == [137, 80, 78, 71]' 'a=[0x89, 0x50, 0x4E, 0X47]'
81+
# returns true as exit code 0
82+
```
83+
6984
**IP Address Information**
7085

7186
You can use DFL to filter IP addresses.

cmd/dfl/main.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,32 @@ func main() {
101101
}
102102

103103
ctx := map[string]interface{}{}
104+
104105
if load_env {
105106
for _, e := range os.Environ() {
106-
pair := strings.Split(e, "=")
107-
ctx[pair[0]] = dfl.TryConvertString(pair[1])
107+
pair := strings.SplitN(e, "=", 2)
108+
ctx[strings.TrimSpace(pair[0])] = dfl.TryConvertString(strings.TrimSpace(pair[1]))
108109
}
109110
}
111+
110112
for _, a := range flag.Args() {
111113
if !strings.Contains(a, "=") {
112114
fmt.Println("Context attribute \"" + a + "\" does not contain \"=\".")
113115
os.Exit(1)
114116
}
115117
pair := strings.SplitN(a, "=", 2)
116-
ctx[pair[0]] = dfl.TryConvertString(pair[1])
118+
value, err := dfl.Parse(strings.TrimSpace(pair[1]))
119+
if err != nil {
120+
fmt.Println(errors.Wrap(err, "Could not parse context variable"))
121+
os.Exit(1)
122+
}
123+
value = value.Compile()
124+
switch value.(type) {
125+
case dfl.Literal:
126+
ctx[strings.TrimSpace(pair[0])] = value.(dfl.Literal).Value
127+
default:
128+
ctx[strings.TrimSpace(pair[0])] = dfl.TryConvertString(pair[1])
129+
}
117130
}
118131

119132
root, err := dfl.Parse(filter_text)
@@ -123,6 +136,17 @@ func main() {
123136
os.Exit(1)
124137
}
125138

139+
if verbose {
140+
fmt.Println("******************* Context *******************")
141+
out, err := yaml.Marshal(ctx)
142+
if err != nil {
143+
fmt.Println("Error marshaling context to yaml.")
144+
fmt.Println(err)
145+
os.Exit(1)
146+
}
147+
fmt.Println(string(out))
148+
}
149+
126150
if verbose {
127151
fmt.Println("******************* Parsed *******************")
128152
out, err := yaml.Marshal(root.Map())

dfl/Array.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (a Array) Map() map[string]interface{} {
3535
}
3636

3737
// Compile returns a compiled version of this node.
38-
// If all the values of an Set are literals, returns a single Literal with the corresponding Set/slice as its value.
38+
// If all the values of an Set are literals, returns a single Literal with the corresponding array as its value.
3939
// Otherwise returns the original node..
4040
func (a Array) Compile() Node {
4141
values := make([]interface{}, len(a.Nodes))
@@ -49,7 +49,7 @@ func (a Array) Compile() Node {
4949
return a
5050
}
5151
}
52-
return Literal{Value: values}
52+
return Literal{Value: TryConvertArray(values)}
5353
}
5454

5555
func (a Array) Evaluate(ctx Context, funcs FunctionMap) (interface{}, error) {
@@ -61,7 +61,7 @@ func (a Array) Evaluate(ctx Context, funcs FunctionMap) (interface{}, error) {
6161
}
6262
values[i] = v
6363
}
64-
return values, nil
64+
return TryConvertArray(values), nil
6565
}
6666

6767
func (a Array) Attributes() []string {

dfl/CompareNumbers.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ func CompareNumbers(a interface{}, b interface{}) (int, error) {
3939
} else {
4040
return 0, nil
4141
}
42+
case uint8:
43+
if a.(int) > int(b.(uint8)) {
44+
return 1, nil
45+
} else if a.(int) < int(b.(uint8)) {
46+
return -1, nil
47+
} else {
48+
return 0, nil
49+
}
4250
case float64:
4351
if float64(a.(int)) > b.(float64) {
4452
return 1, nil
@@ -66,6 +74,14 @@ func CompareNumbers(a interface{}, b interface{}) (int, error) {
6674
} else {
6775
return 0, nil
6876
}
77+
case uint8:
78+
if a.(int64) > int64(b.(uint8)) {
79+
return 1, nil
80+
} else if a.(int64) < int64(b.(uint8)) {
81+
return -1, nil
82+
} else {
83+
return 0, nil
84+
}
6985
case float64:
7086
if float64(a.(int64)) > b.(float64) {
7187
return 1, nil
@@ -75,6 +91,41 @@ func CompareNumbers(a interface{}, b interface{}) (int, error) {
7591
return 0, nil
7692
}
7793
}
94+
case uint8:
95+
switch b.(type) {
96+
case int:
97+
if int(a.(uint8)) > int(b.(int)) {
98+
return 1, nil
99+
} else if int(a.(uint8)) < int(b.(int)) {
100+
return -1, nil
101+
} else {
102+
return 0, nil
103+
}
104+
case int64:
105+
if int64(a.(uint8)) > b.(int64) {
106+
return 1, nil
107+
} else if int64(a.(uint8)) < b.(int64) {
108+
return -1, nil
109+
} else {
110+
return 0, nil
111+
}
112+
case uint8:
113+
if a.(uint8) > b.(uint8) {
114+
return 1, nil
115+
} else if a.(uint8) < b.(uint8) {
116+
return -1, nil
117+
} else {
118+
return 0, nil
119+
}
120+
case float64:
121+
if float64(a.(uint8)) > b.(float64) {
122+
return 1, nil
123+
} else if float64(a.(uint8)) < b.(float64) {
124+
return -1, nil
125+
} else {
126+
return 0, nil
127+
}
128+
}
78129
case float64:
79130
switch b.(type) {
80131
case int:
@@ -93,6 +144,14 @@ func CompareNumbers(a interface{}, b interface{}) (int, error) {
93144
} else {
94145
return 0, nil
95146
}
147+
case uint8:
148+
if a.(float64) > float64(b.(uint8)) {
149+
return 1, nil
150+
} else if a.(float64) < float64(b.(uint8)) {
151+
return -1, nil
152+
} else {
153+
return 0, nil
154+
}
96155
case float64:
97156
if a.(float64) > b.(float64) {
98157
return 1, nil

dfl/CompareStrings.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// =================================================================
2+
//
3+
// Copyright (C) 2018 Spatial Current, Inc. - All Rights Reserved
4+
// Released as open source under the MIT License. See LICENSE file.
5+
//
6+
// =================================================================
7+
8+
package dfl
9+
10+
import (
11+
"regexp"
12+
"strings"
13+
)
14+
15+
import (
16+
"github.com/pkg/errors"
17+
)
18+
19+
// CompareStrings compares parameter a and parameter b.
20+
// The parameters must be of type string.
21+
// Returns true if a like b.
22+
func CompareStrings(lvs string, rvs string) (bool, error) {
23+
24+
if len(rvs) == 0 {
25+
return len(lvs) == 0, nil
26+
}
27+
28+
pattern, err := regexp.Compile("^" + strings.Replace(rvs, "%", ".*", -1) + "$")
29+
if err != nil {
30+
return false, errors.Wrap(err, "Error comparing strings \""+lvs+"\" and \""+rvs+"\"")
31+
}
32+
33+
return pattern.MatchString(lvs), nil
34+
}

dfl/Counter.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package dfl
2+
3+
type Counter map[string]int
4+
5+
func (c Counter) Len() int {
6+
return len(c)
7+
}
8+
9+
func (c Counter) Has(key string) bool {
10+
_, ok := c[key]
11+
return ok
12+
}
13+
14+
func (c Counter) Increment(key string) {
15+
if count, ok := c[key]; ok {
16+
c[key] = count + 1
17+
} else {
18+
c[key] = 1
19+
}
20+
}

0 commit comments

Comments
 (0)