ppl: add support for additional data (#2696)

* ppl: add support for additional data

* remove unused NewCriterionDeviceRule
This commit is contained in:
Caleb Doxsey 2021-10-22 12:32:20 -06:00 committed by GitHub
parent 0638b07f4d
commit 6e48627b4d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 205 additions and 59 deletions

View file

@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"math"
"github.com/open-policy-agent/opa/ast"
)
@ -137,6 +138,29 @@ func (o Object) Clone() Value {
return no
}
// Falsy returns true if the value is considered Javascript falsy:
// https://developer.mozilla.org/en-US/docs/Glossary/Falsy.
// If the field is not found in the object it is *not* falsy.
func (o Object) Falsy(field string) bool {
v, ok := o[field]
if !ok {
return false
}
switch v := v.(type) {
case Boolean:
return !bool(v)
case Number:
return v.Float64() == 0 || math.IsNaN(v.Float64())
case String:
return v == ""
case Null:
return true
default:
return false
}
}
// RegoValue returns the Object as a rego Value.
func (o Object) RegoValue() ast.Value {
kvps := make([][2]*ast.Term, 0, len(o))
@ -158,6 +182,16 @@ func (o Object) String() string {
return string(bs)
}
// Truthy returns the opposite of Falsy, however if the field is not found in the object it is neither truthy nor falsy.
func (o Object) Truthy(field string) bool {
_, ok := o[field]
if !ok {
return false
}
return !o.Falsy(field)
}
// An Array is a slice of values.
type Array []Value
@ -216,6 +250,18 @@ func (n Number) Clone() Value {
return n
}
// Float64 returns the number as a float64.
func (n Number) Float64() float64 {
v, _ := json.Number(n).Float64()
return v
}
// Int64 returns the number as an int64.
func (n Number) Int64() int64 {
v, _ := json.Number(n).Int64()
return v
}
// RegoValue returns the Number as a rego Value.
func (n Number) RegoValue() ast.Value {
return ast.Number(n)