| 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 types |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"math" |
| 20 |
"reflect" |
| 21 |
"strconv" |
| 22 |
"strings" |
| 23 |
|
| 24 |
"github.com/google/cel-go/common/types/ref" |
| 25 |
|
| 26 |
anypb "google.golang.org/protobuf/types/known/anypb" |
| 27 |
structpb "google.golang.org/protobuf/types/known/structpb" |
| 28 |
wrapperspb "google.golang.org/protobuf/types/known/wrapperspb" |
| 29 |
) |
| 30 |
|
| 31 |
// Uint type implementation which supports comparison and math operators. |
| 32 |
type Uint uint64 |
| 33 |
|
| 34 |
var ( |
| 35 |
uint32WrapperType = reflect.TypeOf(&wrapperspb.UInt32Value{}) |
| 36 |
|
| 37 |
uint64WrapperType = reflect.TypeOf(&wrapperspb.UInt64Value{}) |
| 38 |
) |
| 39 |
|
| 40 |
// Uint constants |
| 41 |
const ( |
| 42 |
uintZero = Uint(0) |
| 43 |
) |
| 44 |
|
| 45 |
// Add implements traits.Adder.Add. |
| 46 |
func (i Uint) Add(other ref.Val) ref.Val { |
| 47 |
otherUint, ok := other.(Uint) |
| 48 |
if !ok { |
| 49 |
return MaybeNoSuchOverloadErr(other) |
| 50 |
} |
| 51 |
val, err := addUint64Checked(uint64(i), uint64(otherUint)) |
| 52 |
if err != nil { |
| 53 |
return WrapErr(err) |
| 54 |
} |
| 55 |
return Uint(val) |
| 56 |
} |
| 57 |
|
| 58 |
// Compare implements traits.Comparer.Compare. |
| 59 |
func (i Uint) Compare(other ref.Val) ref.Val { |
| 60 |
switch ov := other.(type) { |
| 61 |
case Double: |
| 62 |
if math.IsNaN(float64(ov)) { |
| 63 |
return NewErr("NaN values cannot be ordered") |
| 64 |
} |
| 65 |
return compareUintDouble(i, ov) |
| 66 |
case Int: |
| 67 |
return compareUintInt(i, ov) |
| 68 |
case Uint: |
| 69 |
return compareUint(i, ov) |
| 70 |
default: |
| 71 |
return MaybeNoSuchOverloadErr(other) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
// ConvertToNative implements ref.Val.ConvertToNative. |
| 76 |
func (i Uint) ConvertToNative(typeDesc reflect.Type) (any, error) { |
| 77 |
switch typeDesc.Kind() { |
| 78 |
case reflect.Uint, reflect.Uint32: |
| 79 |
v, err := uint64ToUint32Checked(uint64(i)) |
| 80 |
if err != nil { |
| 81 |
return 0, err |
| 82 |
} |
| 83 |
return reflect.ValueOf(v).Convert(typeDesc).Interface(), nil |
| 84 |
case reflect.Uint8: |
| 85 |
v, err := uint64ToUint8Checked(uint64(i)) |
| 86 |
if err != nil { |
| 87 |
return 0, err |
| 88 |
} |
| 89 |
return reflect.ValueOf(v).Convert(typeDesc).Interface(), nil |
| 90 |
case reflect.Uint16: |
| 91 |
v, err := uint64ToUint16Checked(uint64(i)) |
| 92 |
if err != nil { |
| 93 |
return 0, err |
| 94 |
} |
| 95 |
return reflect.ValueOf(v).Convert(typeDesc).Interface(), nil |
| 96 |
case reflect.Uint64: |
| 97 |
return reflect.ValueOf(i).Convert(typeDesc).Interface(), nil |
| 98 |
case reflect.Ptr: |
| 99 |
switch typeDesc { |
| 100 |
case anyValueType: |
| 101 |
// Primitives must be wrapped before being set on an Any field. |
| 102 |
return anypb.New(wrapperspb.UInt64(uint64(i))) |
| 103 |
case JSONValueType: |
| 104 |
// JSON can accurately represent 32-bit uints as floating point values. |
| 105 |
if i.isJSONSafe() { |
| 106 |
return structpb.NewNumberValue(float64(i)), nil |
| 107 |
} |
| 108 |
// Proto3 to JSON conversion requires string-formatted uint64 values |
| 109 |
// since the conversion to floating point would result in truncation. |
| 110 |
return structpb.NewStringValue(strconv.FormatUint(uint64(i), 10)), nil |
| 111 |
case uint32WrapperType: |
| 112 |
// Convert the value to a wrapperspb.UInt32Value, error on overflow. |
| 113 |
v, err := uint64ToUint32Checked(uint64(i)) |
| 114 |
if err != nil { |
| 115 |
return 0, err |
| 116 |
} |
| 117 |
return wrapperspb.UInt32(v), nil |
| 118 |
case uint64WrapperType: |
| 119 |
// Convert the value to a wrapperspb.UInt64Value. |
| 120 |
return wrapperspb.UInt64(uint64(i)), nil |
| 121 |
} |
| 122 |
switch typeDesc.Elem().Kind() { |
| 123 |
case reflect.Uint32: |
| 124 |
v, err := uint64ToUint32Checked(uint64(i)) |
| 125 |
if err != nil { |
| 126 |
return 0, err |
| 127 |
} |
| 128 |
p := reflect.New(typeDesc.Elem()) |
| 129 |
p.Elem().Set(reflect.ValueOf(v).Convert(typeDesc.Elem())) |
| 130 |
return p.Interface(), nil |
| 131 |
case reflect.Uint64: |
| 132 |
v := uint64(i) |
| 133 |
p := reflect.New(typeDesc.Elem()) |
| 134 |
p.Elem().Set(reflect.ValueOf(v).Convert(typeDesc.Elem())) |
| 135 |
return p.Interface(), nil |
| 136 |
} |
| 137 |
case reflect.Interface: |
| 138 |
iv := i.Value() |
| 139 |
if reflect.TypeOf(iv).Implements(typeDesc) { |
| 140 |
return iv, nil |
| 141 |
} |
| 142 |
if reflect.TypeOf(i).Implements(typeDesc) { |
| 143 |
return i, nil |
| 144 |
} |
| 145 |
} |
| 146 |
return nil, fmt.Errorf("unsupported type conversion from 'uint' to %v", typeDesc) |
| 147 |
} |
| 148 |
|
| 149 |
// ConvertToType implements ref.Val.ConvertToType. |
| 150 |
func (i Uint) ConvertToType(typeVal ref.Type) ref.Val { |
| 151 |
switch typeVal { |
| 152 |
case IntType: |
| 153 |
v, err := uint64ToInt64Checked(uint64(i)) |
| 154 |
if err != nil { |
| 155 |
return WrapErr(err) |
| 156 |
} |
| 157 |
return Int(v) |
| 158 |
case UintType: |
| 159 |
return i |
| 160 |
case DoubleType: |
| 161 |
return Double(i) |
| 162 |
case StringType: |
| 163 |
return String(fmt.Sprintf("%d", uint64(i))) |
| 164 |
case TypeType: |
| 165 |
return UintType |
| 166 |
} |
| 167 |
return NewErr("type conversion error from '%s' to '%s'", UintType, typeVal) |
| 168 |
} |
| 169 |
|
| 170 |
// Divide implements traits.Divider.Divide. |
| 171 |
func (i Uint) Divide(other ref.Val) ref.Val { |
| 172 |
otherUint, ok := other.(Uint) |
| 173 |
if !ok { |
| 174 |
return MaybeNoSuchOverloadErr(other) |
| 175 |
} |
| 176 |
div, err := divideUint64Checked(uint64(i), uint64(otherUint)) |
| 177 |
if err != nil { |
| 178 |
return WrapErr(err) |
| 179 |
} |
| 180 |
return Uint(div) |
| 181 |
} |
| 182 |
|
| 183 |
// Equal implements ref.Val.Equal. |
| 184 |
func (i Uint) Equal(other ref.Val) ref.Val { |
| 185 |
switch ov := other.(type) { |
| 186 |
case Double: |
| 187 |
if math.IsNaN(float64(ov)) { |
| 188 |
return False |
| 189 |
} |
| 190 |
return Bool(compareUintDouble(i, ov) == 0) |
| 191 |
case Int: |
| 192 |
return Bool(compareUintInt(i, ov) == 0) |
| 193 |
case Uint: |
| 194 |
return Bool(i == ov) |
| 195 |
default: |
| 196 |
return False |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
// IsZeroValue returns true if the uint is zero. |
| 201 |
func (i Uint) IsZeroValue() bool { |
| 202 |
return i == 0 |
| 203 |
} |
| 204 |
|
| 205 |
// Modulo implements traits.Modder.Modulo. |
| 206 |
func (i Uint) Modulo(other ref.Val) ref.Val { |
| 207 |
otherUint, ok := other.(Uint) |
| 208 |
if !ok { |
| 209 |
return MaybeNoSuchOverloadErr(other) |
| 210 |
} |
| 211 |
mod, err := moduloUint64Checked(uint64(i), uint64(otherUint)) |
| 212 |
if err != nil { |
| 213 |
return WrapErr(err) |
| 214 |
} |
| 215 |
return Uint(mod) |
| 216 |
} |
| 217 |
|
| 218 |
// Multiply implements traits.Multiplier.Multiply. |
| 219 |
func (i Uint) Multiply(other ref.Val) ref.Val { |
| 220 |
otherUint, ok := other.(Uint) |
| 221 |
if !ok { |
| 222 |
return MaybeNoSuchOverloadErr(other) |
| 223 |
} |
| 224 |
val, err := multiplyUint64Checked(uint64(i), uint64(otherUint)) |
| 225 |
if err != nil { |
| 226 |
return WrapErr(err) |
| 227 |
} |
| 228 |
return Uint(val) |
| 229 |
} |
| 230 |
|
| 231 |
// Subtract implements traits.Subtractor.Subtract. |
| 232 |
func (i Uint) Subtract(subtrahend ref.Val) ref.Val { |
| 233 |
subtraUint, ok := subtrahend.(Uint) |
| 234 |
if !ok { |
| 235 |
return MaybeNoSuchOverloadErr(subtrahend) |
| 236 |
} |
| 237 |
val, err := subtractUint64Checked(uint64(i), uint64(subtraUint)) |
| 238 |
if err != nil { |
| 239 |
return WrapErr(err) |
| 240 |
} |
| 241 |
return Uint(val) |
| 242 |
} |
| 243 |
|
| 244 |
// Type implements ref.Val.Type. |
| 245 |
func (i Uint) Type() ref.Type { |
| 246 |
return UintType |
| 247 |
} |
| 248 |
|
| 249 |
// Value implements ref.Val.Value. |
| 250 |
func (i Uint) Value() any { |
| 251 |
return uint64(i) |
| 252 |
} |
| 253 |
|
| 254 |
func (i Uint) format(sb *strings.Builder) { |
| 255 |
sb.WriteString(strconv.FormatUint(uint64(i), 10)) |
| 256 |
sb.WriteString("u") |
| 257 |
} |
| 258 |
|
| 259 |
// isJSONSafe indicates whether the uint is safely representable as a floating point value in JSON. |
| 260 |
func (i Uint) isJSONSafe() bool { |
| 261 |
return i <= maxIntJSON |
| 262 |
} |