Skip to content

Commit 30489ff

Browse files
committed
filter: outputs as array vs. map
1 parent 8877381 commit 30489ff

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

benchmarks_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func benchmarkLayer(layer *Layer, ctx *filter.Context) {
117117

118118
// check the outputs
119119
for _, o := range f.Output {
120-
o.Eval(ctx)
120+
o.Expr.Eval(ctx)
121121
}
122122
}
123123
}

filter/filter.go

+23-9
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@ type Filter struct {
1515
RawMinZoom interface{} `yaml:"min_zoom"`
1616
Table string `yaml:"table"`
1717

18-
MinZoom NumExpression `yaml:"-"`
19-
Filter Condition `yaml:"-"`
20-
Output map[string]Expression `yaml:"-"`
18+
MinZoom NumExpression `yaml:"-"`
19+
Filter Condition `yaml:"-"`
20+
// Output map[string]Expression `yaml:"-"`
21+
Output []OutputExpression `yaml:"-"`
22+
}
23+
24+
// OutputExpression has the key and expression value for the outputs.
25+
// Using a slice vs a key value map was much cleaner.
26+
type OutputExpression struct {
27+
Key string
28+
Expr Expression
2129
}
2230

2331
// Compile will compile the parsed yaml into the expressions
@@ -52,16 +60,22 @@ func (f *Filter) Compile() error {
5260
}
5361

5462
if f.RawOutput != nil {
55-
f.Output = make(map[string]Expression, len(f.RawOutput))
63+
f.Output = make([]OutputExpression, 0, len(f.RawOutput))
5664
for k, v := range f.RawOutput {
57-
var err error
58-
f.Output[k], err = CompileExpression(v)
65+
expr, err := CompileExpression(v)
5966
if err != nil {
6067
return &CompileError{
6168
Cause: errors.WithMessage(err, fmt.Sprintf("output %s", k)),
6269
Input: v,
6370
}
6471
}
72+
73+
if _, ok := expr.(*nilExpr); expr != nil && !ok {
74+
f.Output = append(f.Output, OutputExpression{
75+
Key: k,
76+
Expr: expr,
77+
})
78+
}
6579
}
6680
}
6781

@@ -82,10 +96,10 @@ func (f *Filter) Match(ctx *Context) bool {
8296
// Must call Compile() first to initialize the output expressions.
8397
func (f *Filter) Properties(ctx *Context) map[string]interface{} {
8498
result := make(map[string]interface{}, len(f.Output))
85-
for k, expr := range f.Output {
86-
o := expr.Eval(ctx)
99+
for i := range f.Output {
100+
o := f.Output[i].Expr.Eval(ctx)
87101
if o != nil {
88-
result[k] = o
102+
result[f.Output[i].Key] = o
89103
}
90104
}
91105

yaml_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func checkLayer(layer *Layer, feature *geojson.Feature) {
9999

100100
// check the outputs
101101
for _, e := range f.Output {
102-
e.Eval(ctx)
102+
e.Expr.Eval(ctx)
103103
}
104104
}
105105
}

0 commit comments

Comments
 (0)