Bump cel-go to v0.11.2

This commit is contained in:
cici37
2022-05-04 18:32:06 -07:00
parent cb7beb5912
commit a86dd29157
83 changed files with 653 additions and 974 deletions

View File

@@ -27,7 +27,7 @@ type SourceLocation struct {
}
var (
// Location implements the SourcceLocation interface.
// Location implements the SourceLocation interface.
_ Location = &SourceLocation{}
// NoLocation is a particular illegal location.
NoLocation = &SourceLocation{-1, -1}

View File

@@ -14,7 +14,7 @@
// Package operators defines the internal function names of operators.
//
// ALl operators in the expression language are modelled as function calls.
// All operators in the expression language are modelled as function calls.
package operators
// String "names" for CEL operators.

View File

@@ -73,6 +73,7 @@ go_test(
"timestamp_test.go",
"type_test.go",
"uint_test.go",
"util_test.go",
],
embed = [":go_default_library"],
deps = [

View File

@@ -130,12 +130,11 @@ func (b Bool) Value() interface{} {
}
// IsBool returns whether the input ref.Val or ref.Type is equal to BoolType.
func IsBool(elem interface{}) bool {
switch elem := elem.(type) {
case ref.Type:
return elem == BoolType
case ref.Val:
return IsBool(elem.Type())
func IsBool(elem ref.Val) bool {
switch elem.(type) {
case Bool:
return true
default:
return false
}
return false
}

View File

@@ -53,7 +53,7 @@ func (b Bytes) Add(other ref.Val) ref.Val {
return append(b, otherBytes...)
}
// Compare implments traits.Comparer interface method by lexicographic ordering.
// Compare implements traits.Comparer interface method by lexicographic ordering.
func (b Bytes) Compare(other ref.Val) ref.Val {
otherBytes, ok := other.(Bytes)
if !ok {

View File

@@ -16,6 +16,8 @@ package types
import (
"math"
"github.com/google/cel-go/common/types/ref"
)
func compareDoubleInt(d Double, i Int) Int {
@@ -74,7 +76,7 @@ func compareDouble(a, b Double) Int {
return IntZero
}
func compareInt(a, b Int) Int {
func compareInt(a, b Int) ref.Val {
if a < b {
return IntNegOne
}
@@ -84,7 +86,7 @@ func compareInt(a, b Int) Int {
return IntZero
}
func compareUint(a, b Uint) Int {
func compareUint(a, b Uint) ref.Val {
if a < b {
return IntNegOne
}

View File

@@ -78,12 +78,10 @@ func ValOrErr(val ref.Val, format string, args ...interface{}) ref.Val {
if val == nil {
return NewErr(format, args...)
}
switch val.Type() {
case ErrType, UnknownType:
if IsUnknownOrError(val) {
return val
default:
return NewErr(format, args...)
}
return NewErr(format, args...)
}
// wrapErr wraps an existing Go error value into a CEL Err value.
@@ -126,5 +124,10 @@ func (e *Err) Value() interface{} {
// IsError returns whether the input element ref.Type or ref.Val is equal to
// the ErrType singleton.
func IsError(val ref.Val) bool {
return val.Type() == ErrType
switch val.(type) {
case *Err:
return true
default:
return false
}
}

View File

@@ -96,14 +96,16 @@ func NewJSONList(adapter ref.TypeAdapter, l *structpb.ListValue) traits.Lister {
}
// NewMutableList creates a new mutable list whose internal state can be modified.
//
// The mutable list only handles `Add` calls correctly as it is intended only for use within
// comprehension loops which generate an immutable result upon completion.
func NewMutableList(adapter ref.TypeAdapter) traits.Lister {
func NewMutableList(adapter ref.TypeAdapter) traits.MutableLister {
var mutableValues []ref.Val
return &mutableList{
TypeAdapter: adapter,
baseList: nil,
mutableValues: []ref.Val{},
baseList: &baseList{
TypeAdapter: adapter,
value: mutableValues,
size: 0,
get: func(i int) interface{} { return mutableValues[i] },
},
mutableValues: mutableValues,
}
}
@@ -238,16 +240,14 @@ func (l *baseList) Equal(other ref.Val) ref.Val {
// Get implements the traits.Indexer interface method.
func (l *baseList) Get(index ref.Val) ref.Val {
i, ok := index.(Int)
if !ok {
return ValOrErr(index, "unsupported index type '%s' in list", index.Type())
ind, err := indexOrError(index)
if err != nil {
return ValOrErr(index, err.Error())
}
iv := int(i)
if iv < 0 || iv >= l.size {
return NewErr("index '%d' out of range in list size '%d'", i, l.Size())
if ind < 0 || ind >= l.size {
return NewErr("index '%d' out of range in list size '%d'", ind, l.Size())
}
elem := l.get(iv)
return l.NativeToValue(elem)
return l.NativeToValue(l.get(ind))
}
// Iterator implements the traits.Iterable interface method.
@@ -272,20 +272,25 @@ func (l *baseList) Value() interface{} {
// mutableList aggregates values into its internal storage. For use with internal CEL variables only.
type mutableList struct {
ref.TypeAdapter
*baseList
mutableValues []ref.Val
}
// Add copies elements from the other list into the internal storage of the mutable list.
// The ref.Val returned by Add is the receiver.
func (l *mutableList) Add(other ref.Val) ref.Val {
otherList, ok := other.(traits.Lister)
if !ok {
switch otherList := other.(type) {
case *mutableList:
l.mutableValues = append(l.mutableValues, otherList.mutableValues...)
l.size += len(otherList.mutableValues)
case traits.Lister:
for i := IntZero; i < otherList.Size().(Int); i++ {
l.size++
l.mutableValues = append(l.mutableValues, otherList.Get(i))
}
default:
return MaybeNoSuchOverloadErr(otherList)
}
for i := IntZero; i < otherList.Size().(Int); i++ {
l.mutableValues = append(l.mutableValues, otherList.Get(i))
}
return l
}
@@ -323,7 +328,7 @@ func (l *concatList) Add(other ref.Val) ref.Val {
nextList: otherList}
}
// Contains implments the traits.Container interface method.
// Contains implements the traits.Container interface method.
func (l *concatList) Contains(elem ref.Val) ref.Val {
// The concat list relies on the IsErrorOrUnknown checks against the input element to be
// performed by the `prevList` and/or `nextList`.
@@ -391,10 +396,11 @@ func (l *concatList) Equal(other ref.Val) ref.Val {
// Get implements the traits.Indexer interface method.
func (l *concatList) Get(index ref.Val) ref.Val {
i, ok := index.(Int)
if !ok {
return MaybeNoSuchOverloadErr(index)
ind, err := indexOrError(index)
if err != nil {
return ValOrErr(index, err.Error())
}
i := Int(ind)
if i < l.prevList.Size().(Int) {
return l.prevList.Get(i)
}
@@ -462,3 +468,22 @@ func (it *listIterator) Next() ref.Val {
}
return nil
}
func indexOrError(index ref.Val) (int, error) {
switch iv := index.(type) {
case Int:
return int(iv), nil
case Double:
if ik, ok := doubleToInt64Lossless(float64(iv)); ok {
return int(ik), nil
}
return -1, fmt.Errorf("unsupported index value %v in list", index)
case Uint:
if ik, ok := uint64ToInt64Lossless(uint64(iv)); ok {
return int(ik), nil
}
return -1, fmt.Errorf("unsupported index value %v in list", index)
default:
return -1, fmt.Errorf("unsupported index type '%s' in list", index.Type())
}
}

View File

@@ -105,7 +105,7 @@ var (
// This interface implements portions of the API surface area required by the traits.Mapper
// interface.
type mapAccessor interface {
// Find returns a value, if one exists, for the inpput key.
// Find returns a value, if one exists, for the input key.
//
// If the key is not found the function returns (nil, false).
Find(ref.Val) (ref.Val, bool)
@@ -429,7 +429,7 @@ func (a *refValMapAccessor) Find(key ref.Val) (ref.Val, bool) {
case Double:
if ik, ok := doubleToInt64Lossless(float64(k)); ok {
if keyVal, found := a.mapVal[Int(ik)]; found {
return keyVal, true
return keyVal, found
}
}
if uk, ok := doubleToUint64Lossless(float64(k)); ok {

View File

@@ -316,7 +316,7 @@ func doubleToUint64Checked(v float64) (uint64, error) {
return uint64(v), nil
}
// int64toUint64Checked converts an int64 to a uint64 value.
// int64ToUint64Checked converts an int64 to a uint64 value.
//
// If the conversion fails due to overflow the error return value will be non-nil.
func int64ToUint64Checked(v int64) (uint64, error) {
@@ -326,7 +326,7 @@ func int64ToUint64Checked(v int64) (uint64, error) {
return uint64(v), nil
}
// int64toInt32Checked converts an int64 to an int32 value.
// int64ToInt32Checked converts an int64 to an int32 value.
//
// If the conversion fails due to overflow the error return value will be non-nil.
func int64ToInt32Checked(v int64) (int32, error) {
@@ -336,7 +336,7 @@ func int64ToInt32Checked(v int64) (int32, error) {
return int32(v), nil
}
// uint64toUint32Checked converts a uint64 to a uint32 value.
// uint64ToUint32Checked converts a uint64 to a uint32 value.
//
// If the conversion fails due to overflow the error return value will be non-nil.
func uint64ToUint32Checked(v uint64) (uint32, error) {
@@ -346,7 +346,7 @@ func uint64ToUint32Checked(v uint64) (uint32, error) {
return uint32(v), nil
}
// uint64toInt64Checked converts a uint64 to an int64 value.
// uint64ToInt64Checked converts a uint64 to an int64 value.
//
// If the conversion fails due to overflow the error return value will be non-nil.
func uint64ToInt64Checked(v uint64) (int64, error) {

View File

@@ -167,7 +167,7 @@ func (s String) Match(pattern ref.Val) ref.Val {
return Bool(matched)
}
// Receive implements traits.Reciever.Receive.
// Receive implements traits.Receiver.Receive.
func (s String) Receive(function string, overload string, args []ref.Val) ref.Val {
switch len(args) {
case 1:

View File

@@ -138,7 +138,7 @@ func (t Timestamp) Equal(other ref.Val) ref.Val {
return Bool(ok && t.Time.Equal(otherTime.Time))
}
// Receive implements traits.Reciever.Receive.
// Receive implements traits.Receiver.Receive.
func (t Timestamp) Receive(function string, overload string, args []ref.Val) ref.Val {
switch len(args) {
case 0:

View File

@@ -28,5 +28,6 @@ type Lister interface {
// MutableLister interface which emits an immutable result after an intermediate computation.
type MutableLister interface {
Lister
ToImmutableList() Lister
}

View File

@@ -34,12 +34,12 @@ func (u Unknown) ConvertToNative(typeDesc reflect.Type) (interface{}, error) {
return u.Value(), nil
}
// ConvertToType implements ref.Val.ConvertToType.
// ConvertToType is an identity function since unknown values cannot be modified.
func (u Unknown) ConvertToType(typeVal ref.Type) ref.Val {
return u
}
// Equal implements ref.Val.Equal.
// Equal is an identity function since unknown values cannot be modified.
func (u Unknown) Equal(other ref.Val) ref.Val {
return u
}
@@ -57,5 +57,10 @@ func (u Unknown) Value() interface{} {
// IsUnknown returns whether the element ref.Type or ref.Val is equal to the
// UnknownType singleton.
func IsUnknown(val ref.Val) bool {
return val.Type() == UnknownType
switch val.(type) {
case Unknown:
return true
default:
return false
}
}

View File

@@ -18,10 +18,10 @@ import (
"github.com/google/cel-go/common/types/ref"
)
// IsUnknownOrError returns whether the input element ref.Val is an ErrType or UnknonwType.
// IsUnknownOrError returns whether the input element ref.Val is an ErrType or UnknownType.
func IsUnknownOrError(val ref.Val) bool {
switch val.Type() {
case UnknownType, ErrType:
switch val.(type) {
case Unknown, *Err:
return true
}
return false