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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package types
import (
"fmt"
"math"
"reflect"
"strconv"
"github.com/google/cel-go/common/types/ref"
anypb "google.golang.org/protobuf/types/known/anypb"
structpb "google.golang.org/protobuf/types/known/structpb"
wrapperspb "google.golang.org/protobuf/types/known/wrapperspb"
)
// Uint type implementation which supports comparison and math operators.
type Uint uint64
var (
uint32WrapperType = reflect.TypeOf(&wrapperspb.UInt32Value{})
uint64WrapperType = reflect.TypeOf(&wrapperspb.UInt64Value{})
)
// Uint constants
const (
uintZero = Uint(0)
)
// Add implements traits.Adder.Add.
func (i Uint) Add(other ref.Val) ref.Val {
otherUint, ok := other.(Uint)
if !ok {
return MaybeNoSuchOverloadErr(other)
}
val, err := addUint64Checked(uint64(i), uint64(otherUint))
if err != nil {
return WrapErr(err)
}
return Uint(val)
}
// Compare implements traits.Comparer.Compare.
func (i Uint) Compare(other ref.Val) ref.Val {
switch ov := other.(type) {
case Double:
if math.IsNaN(float64(ov)) {
return NewErr("NaN values cannot be ordered")
}
return compareUintDouble(i, ov)
case Int:
return compareUintInt(i, ov)
case Uint:
return compareUint(i, ov)
default:
return MaybeNoSuchOverloadErr(other)
}
}
// ConvertToNative implements ref.Val.ConvertToNative.
func (i Uint) ConvertToNative(typeDesc reflect.Type) (any, error) {
switch typeDesc.Kind() {
case reflect.Uint, reflect.Uint32:
v, err := uint64ToUint32Checked(uint64(i))
if err != nil {
return 0, err
}
return reflect.ValueOf(v).Convert(typeDesc).Interface(), nil
case reflect.Uint64:
return reflect.ValueOf(i).Convert(typeDesc).Interface(), nil
case reflect.Ptr:
switch typeDesc {
case anyValueType:
// Primitives must be wrapped before being set on an Any field.
return anypb.New(wrapperspb.UInt64(uint64(i)))
case jsonValueType:
// JSON can accurately represent 32-bit uints as floating point values.
if i.isJSONSafe() {
return structpb.NewNumberValue(float64(i)), nil
}
// Proto3 to JSON conversion requires string-formatted uint64 values
// since the conversion to floating point would result in truncation.
return structpb.NewStringValue(strconv.FormatUint(uint64(i), 10)), nil
case uint32WrapperType:
// Convert the value to a wrapperspb.UInt32Value, error on overflow.
v, err := uint64ToUint32Checked(uint64(i))
if err != nil {
return 0, err
}
return wrapperspb.UInt32(v), nil
case uint64WrapperType:
// Convert the value to a wrapperspb.UInt64Value.
return wrapperspb.UInt64(uint64(i)), nil
}
switch typeDesc.Elem().Kind() {
case reflect.Uint32:
v, err := uint64ToUint32Checked(uint64(i))
if err != nil {
return 0, err
}
p := reflect.New(typeDesc.Elem())
p.Elem().Set(reflect.ValueOf(v).Convert(typeDesc.Elem()))
return p.Interface(), nil
case reflect.Uint64:
v := uint64(i)
p := reflect.New(typeDesc.Elem())
p.Elem().Set(reflect.ValueOf(v).Convert(typeDesc.Elem()))
return p.Interface(), nil
}
case reflect.Interface:
iv := i.Value()
if reflect.TypeOf(iv).Implements(typeDesc) {
return iv, nil
}
if reflect.TypeOf(i).Implements(typeDesc) {
return i, nil
}
}
return nil, fmt.Errorf("unsupported type conversion from 'uint' to %v", typeDesc)
}
// ConvertToType implements ref.Val.ConvertToType.
func (i Uint) ConvertToType(typeVal ref.Type) ref.Val {
switch typeVal {
case IntType:
v, err := uint64ToInt64Checked(uint64(i))
if err != nil {
return WrapErr(err)
}
return Int(v)
case UintType:
return i
case DoubleType:
return Double(i)
case StringType:
return String(fmt.Sprintf("%d", uint64(i)))
case TypeType:
return UintType
}
return NewErr("type conversion error from '%s' to '%s'", UintType, typeVal)
}
// Divide implements traits.Divider.Divide.
func (i Uint) Divide(other ref.Val) ref.Val {
otherUint, ok := other.(Uint)
if !ok {
return MaybeNoSuchOverloadErr(other)
}
div, err := divideUint64Checked(uint64(i), uint64(otherUint))
if err != nil {
return WrapErr(err)
}
return Uint(div)
}
// Equal implements ref.Val.Equal.
func (i Uint) Equal(other ref.Val) ref.Val {
switch ov := other.(type) {
case Double:
if math.IsNaN(float64(ov)) {
return False
}
return Bool(compareUintDouble(i, ov) == 0)
case Int:
return Bool(compareUintInt(i, ov) == 0)
case Uint:
return Bool(i == ov)
default:
return False
}
}
// IsZeroValue returns true if the uint is zero.
func (i Uint) IsZeroValue() bool {
return i == 0
}
// Modulo implements traits.Modder.Modulo.
func (i Uint) Modulo(other ref.Val) ref.Val {
otherUint, ok := other.(Uint)
if !ok {
return MaybeNoSuchOverloadErr(other)
}
mod, err := moduloUint64Checked(uint64(i), uint64(otherUint))
if err != nil {
return WrapErr(err)
}
return Uint(mod)
}
// Multiply implements traits.Multiplier.Multiply.
func (i Uint) Multiply(other ref.Val) ref.Val {
otherUint, ok := other.(Uint)
if !ok {
return MaybeNoSuchOverloadErr(other)
}
val, err := multiplyUint64Checked(uint64(i), uint64(otherUint))
if err != nil {
return WrapErr(err)
}
return Uint(val)
}
// Subtract implements traits.Subtractor.Subtract.
func (i Uint) Subtract(subtrahend ref.Val) ref.Val {
subtraUint, ok := subtrahend.(Uint)
if !ok {
return MaybeNoSuchOverloadErr(subtrahend)
}
val, err := subtractUint64Checked(uint64(i), uint64(subtraUint))
if err != nil {
return WrapErr(err)
}
return Uint(val)
}
// Type implements ref.Val.Type.
func (i Uint) Type() ref.Type {
return UintType
}
// Value implements ref.Val.Value.
func (i Uint) Value() any {
return uint64(i)
}
// isJSONSafe indicates whether the uint is safely representable as a floating point value in JSON.
func (i Uint) isJSONSafe() bool {
return i <= maxIntJSON
}
|