forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregates.go
68 lines (58 loc) · 1.35 KB
/
aggregates.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
// Copyright 2016 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package topdown
import (
"fmt"
"github.com/open-policy-agent/opa/ast"
"github.com/pkg/errors"
)
func evalCount(ctx *Context, expr *ast.Expr, iter Iterator) error {
ops := expr.Terms.([]*ast.Term)
src, dst := ops[1].Value, ops[2].Value
s, err := ValueToInterface(src, ctx)
if err != nil {
return errors.Wrapf(err, "count")
}
var count ast.Number
switch s := s.(type) {
case []interface{}:
count = ast.Number(len(s))
case map[string]interface{}:
count = ast.Number(len(s))
default:
return fmt.Errorf("count: source must be a collection: %v", src)
}
switch dst := dst.(type) {
case ast.Var:
ctx = ctx.BindVar(dst, count)
return iter(ctx)
default:
if dst.Equal(count) {
return iter(ctx)
}
return nil
}
}
func evalSum(ctx *Context, expr *ast.Expr, iter Iterator) error {
ops := expr.Terms.([]*ast.Term)
src, dst := ops[1].Value, ops[2].Value
s, err := ValueToSlice(src, ctx)
if err != nil {
return errors.Wrapf(err, "sum")
}
sum := ast.Number(0)
for _, x := range s {
sum += ast.Number(x.(float64))
}
switch dst := dst.(type) {
case ast.Var:
ctx = ctx.BindVar(dst, sum)
return iter(ctx)
default:
if dst.Equal(sum) {
return iter(ctx)
}
return nil
}
}