Skip to content

Commit ceaf8bd

Browse files
committed
support for backticks and histograms
1 parent b23b5fb commit ceaf8bd

8 files changed

Lines changed: 368 additions & 13 deletions

File tree

dfl/Attribute.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (a Attribute) Dfl() string {
2020

2121
func (a Attribute) Map() map[string]interface{} {
2222
return map[string]interface{}{
23-
"attribute": a.Name,
23+
"attribute": "@" + a.Name,
2424
}
2525
}
2626

dfl/Counter.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@
77

88
package dfl
99

10+
import (
11+
"sort"
12+
)
13+
1014
// Counter is used for creating a frequency histogram of values
1115
type Counter map[string]int
1216

17+
func NewCounter() Counter {
18+
return Counter(map[string]int{})
19+
}
20+
1321
func (c Counter) Len() int {
1422
return len(c)
1523
}
@@ -26,3 +34,79 @@ func (c Counter) Increment(key string) {
2634
c[key] = 1
2735
}
2836
}
37+
38+
func (c Counter) Top(n int, min int) []string {
39+
40+
if n == 0 {
41+
return make([]string, 0)
42+
}
43+
44+
items := make([]struct {
45+
Value string
46+
Frequency int
47+
}, 0)
48+
for value, frequency := range c {
49+
if frequency >= min {
50+
items = append(items, struct {
51+
Value string
52+
Frequency int
53+
}{Value: value, Frequency: frequency})
54+
}
55+
}
56+
57+
sort.SliceStable(items, func(i, j int) bool {
58+
return items[i].Frequency > items[j].Frequency
59+
})
60+
61+
if n > 0 && n < len(items) {
62+
values := make([]string, 0, n)
63+
for _, item := range items {
64+
values = append(values, item.Value)
65+
}
66+
return values[:n]
67+
}
68+
69+
values := make([]string, 0, len(items))
70+
for _, item := range items {
71+
values = append(values, item.Value)
72+
}
73+
return values
74+
}
75+
76+
func (c Counter) Bottom(n int, max int) []string {
77+
78+
if n == 0 {
79+
return make([]string, 0)
80+
}
81+
82+
items := make([]struct {
83+
Value string
84+
Frequency int
85+
}, 0)
86+
for value, frequency := range c {
87+
if frequency >= max {
88+
items = append(items, struct {
89+
Value string
90+
Frequency int
91+
}{Value: value, Frequency: frequency})
92+
}
93+
}
94+
95+
sort.SliceStable(items, func(i, j int) bool {
96+
return items[i].Frequency < items[j].Frequency
97+
})
98+
99+
if n > 0 && n < len(items) {
100+
values := make([]string, 0, n)
101+
for _, item := range items {
102+
values = append(values, item.Value)
103+
}
104+
return values[:n]
105+
}
106+
107+
values := make([]string, 0, len(items))
108+
for _, item := range items {
109+
values = append(values, item.Value)
110+
}
111+
return values
112+
}

dfl/Function.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@ func (f Function) Compile() Node {
3434
}
3535

3636
func (f Function) Map() map[string]interface{} {
37+
arguments := make([]map[string]interface{}, 0, len(f.Arguments))
38+
for _, a := range f.Arguments {
39+
arguments = append(arguments, a.Map())
40+
}
3741
return map[string]interface{}{
3842
"op": "function",
3943
"name": f.Name,
40-
"arguments": f.Arguments,
44+
"arguments": arguments,
4145
}
4246
}
4347

dfl/FunctionMap.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@ type FunctionMap map[string]func(FunctionMap, interface{}, []interface{}) (inter
2323
func NewFuntionMapWithDefaults() FunctionMap {
2424
funcs := FunctionMap{}
2525

26+
funcs["flatten"] = flattenArray
2627
funcs["filter"] = filterArray
2728
funcs["map"] = mapArray
2829
funcs["sort"] = sortArray
2930
funcs["limit"] = limitArray
3031
funcs["array"] = setToArray
3132
funcs["set"] = arrayToSet
3233
funcs["len"] = getLength
34+
funcs["hist"] = histArray
35+
funcs["top"] = topCounter
3336
funcs["bytes"] = convertToBytes
3437
funcs["int16"] = convertToInt16
3538
funcs["int32"] = convertToInt32

dfl/Parse.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func Parse(in string) (Node, error) {
3232
squarebrackets := 0
3333
singlequotes := 0
3434
doublequotes := 0
35+
backticks := 0
3536

3637
for i, c := range in {
3738

@@ -43,7 +44,7 @@ func Parse(in string) (Node, error) {
4344
leftparentheses += 1
4445
} else if c == ')' {
4546
rightparentheses += 1
46-
} else if singlequotes == 0 && doublequotes == 0 {
47+
} else if singlequotes == 0 && doublequotes == 0 && backticks == 0 {
4748
if squarebrackets == 0 && c == '[' {
4849
squarebrackets += 1
4950
} else if squarebrackets == 1 && c == ']' {
@@ -56,18 +57,23 @@ func Parse(in string) (Node, error) {
5657
singlequotes += 1
5758
} else if c == '"' {
5859
doublequotes += 1
60+
} else if c == '`' {
61+
backticks += 1
5962
}
6063
} else if singlequotes == 1 && c == '\'' {
6164
singlequotes -= 1
6265
} else if doublequotes == 1 && c == '"' {
6366
doublequotes -= 1
67+
} else if backticks == 1 && c == '`' {
68+
backticks -= 1
6469
}
6570

6671
if leftparentheses == rightparentheses &&
6772
squarebrackets == 0 &&
6873
curlybrackets == 0 &&
6974
singlequotes == 0 &&
70-
doublequotes == 0 {
75+
doublequotes == 0 &&
76+
backticks == 0 {
7177
if s_lc == "?:" {
7278
right, err := Parse(remainder)
7379
if err != nil {
@@ -153,7 +159,7 @@ func Parse(in string) (Node, error) {
153159
return &Pipe{&BinaryOperator{Right: right}}, nil
154160

155161
} else if len(remainder) == 0 || in[i+1] == ' ' || in[i+1] == '\n' {
156-
if len(s) >= 2 && ((strings.HasPrefix(s, "'") && strings.HasSuffix(s, "'")) || (strings.HasPrefix(s, "\"") && strings.HasSuffix(s, "\""))) {
162+
if len(s) >= 2 && ((strings.HasPrefix(s, "'") && strings.HasSuffix(s, "'")) || (strings.HasPrefix(s, "\"") && strings.HasSuffix(s, "\"")) || (strings.HasPrefix(s, "`") && strings.HasSuffix(s, "`"))) {
157163
return ParseLiteral(s[1:len(s)-1], remainder)
158164
} else if strings.HasPrefix(s, "@") {
159165
return ParseAttribute(s, remainder)

dfl/ParseFunction.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func ParseFunction(in string, remainder string) (Node, error) {
2626
rightparentheses := 0
2727
singlequotes := 0
2828
doublequotes := 0
29+
backticks := 0
2930

3031
in = strings.TrimSpace(in)
3132
s := ""
@@ -36,10 +37,12 @@ func ParseFunction(in string, remainder string) (Node, error) {
3637

3738
s += string(c)
3839

39-
if c == '"' && singlequotes == 0 {
40+
if c == '"' && singlequotes == 0 && backticks == 0 {
4041
doublequotes ^= 1
41-
} else if c == '\'' && doublequotes == 0 {
42+
} else if c == '\'' && doublequotes == 0 && backticks == 0 {
4243
singlequotes ^= 1
44+
} else if c == '`' && singlequotes == 0 && doublequotes == 0 {
45+
backticks ^= 1
4346
} else if c == '(' {
4447
leftparentheses += 1
4548

@@ -52,7 +55,7 @@ func ParseFunction(in string, remainder string) (Node, error) {
5255

5356
rightparentheses += 1
5457

55-
if singlequotes == 0 && doublequotes == 0 {
58+
if singlequotes == 0 && doublequotes == 0 && backticks == 0 {
5659
if leftparentheses-rightparentheses == 1 {
5760
// if leftparentheses-rightparentheses > 0 {
5861
subFunction, err := ParseFunction(strings.TrimSpace(s), "")
@@ -64,7 +67,7 @@ func ParseFunction(in string, remainder string) (Node, error) {
6467
} else if leftparentheses == rightparentheses {
6568
s = strings.TrimSpace(s[0 : len(s)-1])
6669
if len(s) > 0 {
67-
if len(s) >= 2 && ((strings.HasPrefix(s, "'") && strings.HasSuffix(s, "'")) || (strings.HasPrefix(s, "\"") && strings.HasSuffix(s, "\""))) {
70+
if len(s) >= 2 && ((strings.HasPrefix(s, "'") && strings.HasSuffix(s, "'")) || (strings.HasPrefix(s, "\"") && strings.HasSuffix(s, "\"")) || (strings.HasPrefix(s, "`") && strings.HasSuffix(s, "`"))) {
6871
arguments = append(arguments, &Literal{Value: s[1 : len(s)-1]})
6972
} else if strings.HasPrefix(strings.TrimLeftFunc(s, unicode.IsSpace), "@") {
7073
arguments = append(arguments, &Attribute{Name: strings.TrimLeftFunc(s, unicode.IsSpace)[1:]})
@@ -84,10 +87,10 @@ func ParseFunction(in string, remainder string) (Node, error) {
8487

8588
//s = ""
8689

87-
} else if singlequotes == 0 && doublequotes == 0 && (leftparentheses-rightparentheses) == 1 && c == ',' {
90+
} else if singlequotes == 0 && doublequotes == 0 && backticks == 0 && (leftparentheses-rightparentheses) == 1 && c == ',' {
8891
s = strings.TrimSpace(s[0 : len(s)-1])
8992
if len(s) > 0 {
90-
if len(s) >= 2 && ((strings.HasPrefix(s, "'") && strings.HasSuffix(s, "'")) || (strings.HasPrefix(s, "\"") && strings.HasSuffix(s, "\""))) {
93+
if len(s) >= 2 && ((strings.HasPrefix(s, "'") && strings.HasSuffix(s, "'")) || (strings.HasPrefix(s, "\"") && strings.HasSuffix(s, "\"")) || (strings.HasPrefix(s, "`") && strings.HasSuffix(s, "`"))) {
9194
arguments = append(arguments, &Literal{Value: s[1 : len(s)-1]})
9295
} else if strings.HasPrefix(strings.TrimLeftFunc(s, unicode.IsSpace), "@") {
9396
arguments = append(arguments, &Attribute{Name: strings.TrimLeftFunc(s, unicode.IsSpace)[1:]})

dfl/Version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77

88
package dfl
99

10-
var VERSION = "0.0.5"
10+
var VERSION = "0.0.6"

0 commit comments

Comments
 (0)