| 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 |
// Double type that implements ref.Val, comparison, and mathematical |
| 32 |
// operations. |
| 33 |
type Double float64 |
| 34 |
|
| 35 |
var ( |
| 36 |
// doubleWrapperType reflected type for protobuf double wrapper type. |
| 37 |
doubleWrapperType = reflect.TypeOf(&wrapperspb.DoubleValue{}) |
| 38 |
|
| 39 |
// floatWrapperType reflected type for protobuf float wrapper type. |
| 40 |
floatWrapperType = reflect.TypeOf(&wrapperspb.FloatValue{}) |
| 41 |
) |
| 42 |
|
| 43 |
// Add implements traits.Adder.Add. |
| 44 |
func (d Double) Add(other ref.Val) ref.Val { |
| 45 |
otherDouble, ok := other.(Double) |
| 46 |
if !ok { |
| 47 |
return MaybeNoSuchOverloadErr(other) |
| 48 |
} |
| 49 |
return d + otherDouble |
| 50 |
} |
| 51 |
|
| 52 |
// Compare implements traits.Comparer.Compare. |
| 53 |
func (d Double) Compare(other ref.Val) ref.Val { |
| 54 |
if math.IsNaN(float64(d)) { |
| 55 |
return NewErr("NaN values cannot be ordered") |
| 56 |
} |
| 57 |
switch ov := other.(type) { |
| 58 |
case Double: |
| 59 |
if math.IsNaN(float64(ov)) { |
| 60 |
return NewErr("NaN values cannot be ordered") |
| 61 |
} |
| 62 |
return compareDouble(d, ov) |
| 63 |
case Int: |
| 64 |
return compareDoubleInt(d, ov) |
| 65 |
case Uint: |
| 66 |
return compareDoubleUint(d, ov) |
| 67 |
default: |
| 68 |
return MaybeNoSuchOverloadErr(other) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
// ConvertToNative implements ref.Val.ConvertToNative. |
| 73 |
func (d Double) ConvertToNative(typeDesc reflect.Type) (any, error) { |
| 74 |
switch typeDesc.Kind() { |
| 75 |
case reflect.Float32: |
| 76 |
v := float32(d) |
| 77 |
return reflect.ValueOf(v).Convert(typeDesc).Interface(), nil |
| 78 |
case reflect.Float64: |
| 79 |
v := float64(d) |
| 80 |
return reflect.ValueOf(v).Convert(typeDesc).Interface(), nil |
| 81 |
case reflect.Ptr: |
| 82 |
switch typeDesc { |
| 83 |
case anyValueType: |
| 84 |
// Primitives must be wrapped before being set on an Any field. |
| 85 |
return anypb.New(wrapperspb.Double(float64(d))) |
| 86 |
case doubleWrapperType: |
| 87 |
// Convert to a wrapperspb.DoubleValue |
| 88 |
return wrapperspb.Double(float64(d)), nil |
| 89 |
case floatWrapperType: |
| 90 |
// Convert to a wrapperspb.FloatValue (with truncation). |
| 91 |
return wrapperspb.Float(float32(d)), nil |
| 92 |
case JSONValueType: |
| 93 |
// Note, there are special cases for proto3 to json conversion that |
| 94 |
// expect the floating point value to be converted to a NaN, |
| 95 |
// Infinity, or -Infinity string values, but the jsonpb string |
| 96 |
// marshaling of the protobuf.Value will handle this conversion. |
| 97 |
return structpb.NewNumberValue(float64(d)), nil |
| 98 |
} |
| 99 |
switch typeDesc.Elem().Kind() { |
| 100 |
case reflect.Float32: |
| 101 |
v := float32(d) |
| 102 |
p := reflect.New(typeDesc.Elem()) |
| 103 |
p.Elem().Set(reflect.ValueOf(v).Convert(typeDesc.Elem())) |
| 104 |
return p.Interface(), nil |
| 105 |
case reflect.Float64: |
| 106 |
v := float64(d) |
| 107 |
p := reflect.New(typeDesc.Elem()) |
| 108 |
p.Elem().Set(reflect.ValueOf(v).Convert(typeDesc.Elem())) |
| 109 |
return p.Interface(), nil |
| 110 |
} |
| 111 |
case reflect.Interface: |
| 112 |
dv := d.Value() |
| 113 |
if reflect.TypeOf(dv).Implements(typeDesc) { |
| 114 |
return dv, nil |
| 115 |
} |
| 116 |
if reflect.TypeOf(d).Implements(typeDesc) { |
| 117 |
return d, nil |
| 118 |
} |
| 119 |
} |
| 120 |
return nil, fmt.Errorf("type conversion error from Double to '%v'", typeDesc) |
| 121 |
} |
| 122 |
|
| 123 |
// ConvertToType implements ref.Val.ConvertToType. |
| 124 |
func (d Double) ConvertToType(typeVal ref.Type) ref.Val { |
| 125 |
switch typeVal { |
| 126 |
case IntType: |
| 127 |
i, err := doubleToInt64Checked(float64(d)) |
| 128 |
if err != nil { |
| 129 |
return WrapErr(err) |
| 130 |
} |
| 131 |
return Int(i) |
| 132 |
case UintType: |
| 133 |
i, err := doubleToUint64Checked(float64(d)) |
| 134 |
if err != nil { |
| 135 |
return WrapErr(err) |
| 136 |
} |
| 137 |
return Uint(i) |
| 138 |
case DoubleType: |
| 139 |
return d |
| 140 |
case StringType: |
| 141 |
return String(fmt.Sprintf("%g", float64(d))) |
| 142 |
case TypeType: |
| 143 |
return DoubleType |
| 144 |
} |
| 145 |
return NewErr("type conversion error from '%s' to '%s'", DoubleType, typeVal) |
| 146 |
} |
| 147 |
|
| 148 |
// Divide implements traits.Divider.Divide. |
| 149 |
func (d Double) Divide(other ref.Val) ref.Val { |
| 150 |
otherDouble, ok := other.(Double) |
| 151 |
if !ok { |
| 152 |
return MaybeNoSuchOverloadErr(other) |
| 153 |
} |
| 154 |
return d / otherDouble |
| 155 |
} |
| 156 |
|
| 157 |
// Equal implements ref.Val.Equal. |
| 158 |
func (d Double) Equal(other ref.Val) ref.Val { |
| 159 |
if math.IsNaN(float64(d)) { |
| 160 |
return False |
| 161 |
} |
| 162 |
switch ov := other.(type) { |
| 163 |
case Double: |
| 164 |
if math.IsNaN(float64(ov)) { |
| 165 |
return False |
| 166 |
} |
| 167 |
return Bool(d == ov) |
| 168 |
case Int: |
| 169 |
return Bool(compareDoubleInt(d, ov) == 0) |
| 170 |
case Uint: |
| 171 |
return Bool(compareDoubleUint(d, ov) == 0) |
| 172 |
default: |
| 173 |
return False |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
// IsZeroValue returns true if double value is 0.0 |
| 178 |
func (d Double) IsZeroValue() bool { |
| 179 |
return float64(d) == 0.0 |
| 180 |
} |
| 181 |
|
| 182 |
// Multiply implements traits.Multiplier.Multiply. |
| 183 |
func (d Double) Multiply(other ref.Val) ref.Val { |
| 184 |
otherDouble, ok := other.(Double) |
| 185 |
if !ok { |
| 186 |
return MaybeNoSuchOverloadErr(other) |
| 187 |
} |
| 188 |
return d * otherDouble |
| 189 |
} |
| 190 |
|
| 191 |
// Negate implements traits.Negater.Negate. |
| 192 |
func (d Double) Negate() ref.Val { |
| 193 |
return -d |
| 194 |
} |
| 195 |
|
| 196 |
// Subtract implements traits.Subtractor.Subtract. |
| 197 |
func (d Double) Subtract(subtrahend ref.Val) ref.Val { |
| 198 |
subtraDouble, ok := subtrahend.(Double) |
| 199 |
if !ok { |
| 200 |
return MaybeNoSuchOverloadErr(subtrahend) |
| 201 |
} |
| 202 |
return d - subtraDouble |
| 203 |
} |
| 204 |
|
| 205 |
// Type implements ref.Val.Type. |
| 206 |
func (d Double) Type() ref.Type { |
| 207 |
return DoubleType |
| 208 |
} |
| 209 |
|
| 210 |
// Value implements ref.Val.Value. |
| 211 |
func (d Double) Value() any { |
| 212 |
return float64(d) |
| 213 |
} |
| 214 |
|
| 215 |
func (d Double) format(sb *strings.Builder) { |
| 216 |
if math.IsNaN(float64(d)) { |
| 217 |
sb.WriteString(`double("NaN")`) |
| 218 |
return |
| 219 |
} |
| 220 |
if math.IsInf(float64(d), -1) { |
| 221 |
sb.WriteString(`double("-Infinity")`) |
| 222 |
return |
| 223 |
} |
| 224 |
if math.IsInf(float64(d), 1) { |
| 225 |
sb.WriteString(`double("Infinity")`) |
| 226 |
return |
| 227 |
} |
| 228 |
s := strconv.FormatFloat(float64(d), 'f', -1, 64) |
| 229 |
sb.WriteString(s) |
| 230 |
if !strings.ContainsRune(s, '.') { |
| 231 |
sb.WriteString(".0") |
| 232 |
} |
| 233 |
} |