| 1 | // Copyright 2018 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package typesscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/ |
| 16 | |
| 17 | import ( |
| 18 | "errorsscip-go gomod github.com/golang/go/src go1.23.0 errors/" |
| 19 | "fmtscip-go gomod github.com/golang/go/src go1.23.0 fmt/" |
| 20 | "reflectscip-go gomod github.com/golang/go/src go1.23.0 reflect/" |
| 21 | |
| 22 | "github.com/google/cel-go/common/types/refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/" |
| 23 | ) |
| 24 | |
| 25 | // Error interface which allows types types.Err values to be treated as error values. |
| 26 | type Errorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Error# interface { |
| 27 | error |
| 28 | refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val# |
| 29 | } |
| 30 | |
| 31 | // Err type which extends the built-in go error and implements ref.Val. |
| 32 | type Errscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err# struct { |
| 33 | errorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#error. |
| 34 | idscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#id. int64 |
| 35 | } |
| 36 | |
| 37 | var ( |
| 38 | // ErrType singleton. |
| 39 | ErrTypescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/ErrType. = NewOpaqueTypescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/NewOpaqueType().("error") |
| 40 | |
| 41 | // errDivideByZero is an error indicating a division by zero of an integer value. |
| 42 | errDivideByZeroscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/errDivideByZero. = errorsscip-go gomod github.com/golang/go/src go1.23.0 errors/.Newscip-go gomod github.com/golang/go/src go1.23.0 errors/New().("division by zero") |
| 43 | // errModulusByZero is an error indicating a modulus by zero of an integer value. |
| 44 | errModulusByZeroscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/errModulusByZero. = errorsscip-go gomod github.com/golang/go/src go1.23.0 errors/.Newscip-go gomod github.com/golang/go/src go1.23.0 errors/New().("modulus by zero") |
| 45 | // errIntOverflow is an error representing integer overflow. |
| 46 | errIntOverflowscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/errIntOverflow. = errorsscip-go gomod github.com/golang/go/src go1.23.0 errors/.Newscip-go gomod github.com/golang/go/src go1.23.0 errors/New().("integer overflow") |
| 47 | // errUintOverflow is an error representing unsigned integer overflow. |
| 48 | errUintOverflowscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/errUintOverflow. = errorsscip-go gomod github.com/golang/go/src go1.23.0 errors/.Newscip-go gomod github.com/golang/go/src go1.23.0 errors/New().("unsigned integer overflow") |
| 49 | // errDurationOverflow is an error representing duration overflow. |
| 50 | errDurationOverflowscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/errDurationOverflow. = errorsscip-go gomod github.com/golang/go/src go1.23.0 errors/.Newscip-go gomod github.com/golang/go/src go1.23.0 errors/New().("duration overflow") |
| 51 | // errTimestampOverflow is an error representing timestamp overflow. |
| 52 | errTimestampOverflowscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/errTimestampOverflow. = errorsscip-go gomod github.com/golang/go/src go1.23.0 errors/.Newscip-go gomod github.com/golang/go/src go1.23.0 errors/New().("timestamp overflow") |
| 53 | celErrTimestampOverflowscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/celErrTimestampOverflow. = &Errscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#{errorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#error.: errTimestampOverflowscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/errTimestampOverflow.} |
| 54 | |
| 55 | // celErrNoSuchOverload indicates that the call arguments did not match a supported method signature. |
| 56 | celErrNoSuchOverloadscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/celErrNoSuchOverload. = NewErrscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/NewErr().("no such overload") |
| 57 | ) |
| 58 | |
| 59 | // NewErr creates a new Err described by the format string and args. |
| 60 | // TODO: Audit the use of this function and standardize the error messages and codes. |
| 61 | func NewErrscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/NewErr().(format string, args ...any) refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val# { |
| 62 | return &Errscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#{errorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#error.: fmtscip-go gomod github.com/golang/go/src go1.23.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.23.0 fmt/Errorf().(format, args...)} |
| 63 | } |
| 64 | |
| 65 | // NewErrFromString creates a new Err with the provided message. |
| 66 | // TODO: Audit the use of this function and standardize the error messages and codes. |
| 67 | func NewErrFromStringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/NewErrFromString().(message string) refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val# { |
| 68 | return &Errscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#{errorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#error.: errorsscip-go gomod github.com/golang/go/src go1.23.0 errors/.Newscip-go gomod github.com/golang/go/src go1.23.0 errors/New().(message)} |
| 69 | } |
| 70 | |
| 71 | // NewErrWithNodeID creates a new Err described by the format string and args. |
| 72 | // TODO: Audit the use of this function and standardize the error messages and codes. |
| 73 | func NewErrWithNodeIDscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/NewErrWithNodeID().(id int64, format string, args ...any) refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val# { |
| 74 | return &Errscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#{errorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#error.: fmtscip-go gomod github.com/golang/go/src go1.23.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.23.0 fmt/Errorf().(format, args...), idscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#id.: id} |
| 75 | } |
| 76 | |
| 77 | // LabelErrNode returns val unaltered it is not an Err or if the error has a non-zero |
| 78 | // AST node ID already present. Otherwise the id is added to the error for |
| 79 | // recovery with the Err.NodeID method. |
| 80 | func LabelErrNodescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/LabelErrNode().(id int64, val refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#) refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val# { |
| 81 | if err, ok := val.(*Errscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#); ok && err.idscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#id. == 0 { |
| 82 | err.idscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#id. = id |
| 83 | return err |
| 84 | } |
| 85 | return val |
| 86 | } |
| 87 | |
| 88 | // NoSuchOverloadErr returns a new types.Err instance with a no such overload message. |
| 89 | func NoSuchOverloadErrscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/NoSuchOverloadErr().() refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val# { |
| 90 | return celErrNoSuchOverloadscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/celErrNoSuchOverload. |
| 91 | } |
| 92 | |
| 93 | // UnsupportedRefValConversionErr returns a types.NewErr instance with a no such conversion |
| 94 | // message that indicates that the native value could not be converted to a CEL ref.Val. |
| 95 | func UnsupportedRefValConversionErrscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/UnsupportedRefValConversionErr().(val any) refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val# { |
| 96 | return NewErrscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/NewErr().("unsupported conversion to ref.Val: (%T)%v", val, val) |
| 97 | } |
| 98 | |
| 99 | // MaybeNoSuchOverloadErr returns the error or unknown if the input ref.Val is one of these types, |
| 100 | // else a new no such overload error. |
| 101 | func MaybeNoSuchOverloadErrscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/MaybeNoSuchOverloadErr().(val refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#) refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val# { |
| 102 | return ValOrErrscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/ValOrErr().(val, "no such overload") |
| 103 | } |
| 104 | |
| 105 | // ValOrErr either returns the existing error or creates a new one. |
| 106 | // TODO: Audit the use of this function and standardize the error messages and codes. |
| 107 | func ValOrErrscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/ValOrErr().(val refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#, format string, args ...any) refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val# { |
| 108 | if val == nil || !IsUnknownOrErrorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/IsUnknownOrError().(val) { |
| 109 | return NewErrscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/NewErr().(format, args...) |
| 110 | } |
| 111 | return val |
| 112 | } |
| 113 | |
| 114 | // WrapErr wraps an existing Go error value into a CEL Err value. |
| 115 | func WrapErrscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/WrapErr().(err error) refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val# { |
| 116 | return &Errscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#{errorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#error.: err} |
| 117 | } |
| 118 | |
| 119 | // ConvertToNative implements ref.Val.ConvertToNative. |
| 120 | func (e *Errscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#) ConvertToNativescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#ConvertToNative().(typeDesc reflectscip-go gomod github.com/golang/go/src go1.23.0 reflect/.Typescip-go gomod github.com/golang/go/src go1.23.0 reflect/Type#) (any, error) { |
| 121 | return nil, e.errorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#error. |
| 122 | } |
| 123 | |
| 124 | // ConvertToType implements ref.Val.ConvertToType. |
| 125 | func (e *Errscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#) ConvertToTypescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#ConvertToType().(typeVal refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Typescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Type#) refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val# { |
| 126 | // Errors are not convertible to other representations. |
| 127 | return e |
| 128 | } |
| 129 | |
| 130 | // Equal implements ref.Val.Equal. |
| 131 | func (e *Errscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#) Equalscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#Equal().(other refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#) refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val# { |
| 132 | // An error cannot be equal to any other value, so it returns itself. |
| 133 | return e |
| 134 | } |
| 135 | |
| 136 | // String implements fmt.Stringer. |
| 137 | func (e *Errscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#) Stringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#String().() string { |
| 138 | return e.errorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#error..Error() |
| 139 | } |
| 140 | |
| 141 | // Type implements ref.Val.Type. |
| 142 | func (e *Errscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#) Typescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#Type().() refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Typescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Type# { |
| 143 | return ErrTypescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/ErrType. |
| 144 | } |
| 145 | |
| 146 | // Value implements ref.Val.Value. |
| 147 | func (e *Errscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#) Valuescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#Value().() any { |
| 148 | return e.errorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#error. |
| 149 | } |
| 150 | |
| 151 | // NodeID returns the AST node ID of the expression that returned the error. |
| 152 | func (e *Errscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#) NodeIDscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#NodeID().() int64 { |
| 153 | return e.idscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#id. |
| 154 | } |
| 155 | |
| 156 | // Is implements errors.Is. |
| 157 | func (e *Errscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#) Isscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#Is().(target error) bool { |
| 158 | return e.errorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#error..Error() == target.Error() |
| 159 | } |
| 160 | |
| 161 | // Unwrap implements errors.Unwrap. |
| 162 | func (e *Errscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#) Unwrapscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#Unwrap().() error { |
| 163 | return e.errorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#error. |
| 164 | } |
| 165 | |
| 166 | // IsError returns whether the input element ref.Type or ref.Val is equal to |
| 167 | // the ErrType singleton. |
| 168 | func IsErrorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/IsError().(val refscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/.Valscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types/ref`/Val#) bool { |
| 169 | switch val.(type) { |
| 170 | case *Errscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common/types`/Err#: |
| 171 | return true |
| 172 | default: |
| 173 | return false |
| 174 | } |
| 175 | } |