| 1 | // Copyright 2021 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 | "math" |
| 19 | |
| 20 | "github.com/google/cel-go/common/types/ref" |
| 21 | ) |
| 22 | |
| 23 | func compareDoubleInt(d Double, i Int) Int { |
| 24 | if d < math.MinInt64 { |
| 25 | return IntNegOne |
| 26 | } |
| 27 | if d > math.MaxInt64 { |
| 28 | return IntOne |
| 29 | } |
| 30 | return compareDouble(d, Double(i)) |
| 31 | } |
| 32 | |
| 33 | func compareIntDouble(i Int, d Double) Int { |
| 34 | return -compareDoubleInt(d, i) |
| 35 | } |
| 36 | |
| 37 | func compareDoubleUint(d Double, u Uint) Int { |
| 38 | if d < 0 { |
| 39 | return IntNegOne |
| 40 | } |
| 41 | if d > math.MaxUint64 { |
| 42 | return IntOne |
| 43 | } |
| 44 | return compareDouble(d, Double(u)) |
| 45 | } |
| 46 | |
| 47 | func compareUintDouble(u Uint, d Double) Int { |
| 48 | return -compareDoubleUint(d, u) |
| 49 | } |
| 50 | |
| 51 | func compareIntUint(i Int, u Uint) Int { |
| 52 | if i < 0 || u > math.MaxInt64 { |
| 53 | return IntNegOne |
| 54 | } |
| 55 | cmp := i - Int(u) |
| 56 | if cmp < 0 { |
| 57 | return IntNegOne |
| 58 | } |
| 59 | if cmp > 0 { |
| 60 | return IntOne |
| 61 | } |
| 62 | return IntZero |
| 63 | } |
| 64 | |
| 65 | func compareUintInt(u Uint, i Int) Int { |
| 66 | return -compareIntUint(i, u) |
| 67 | } |
| 68 | |
| 69 | func compareDouble(a, b Double) Int { |
| 70 | if a < b { |
| 71 | return IntNegOne |
| 72 | } |
| 73 | if a > b { |
| 74 | return IntOne |
| 75 | } |
| 76 | return IntZero |
| 77 | } |
| 78 | |
| 79 | func compareInt(a, b Int) ref.Val { |
| 80 | if a < b { |
| 81 | return IntNegOne |
| 82 | } |
| 83 | if a > b { |
| 84 | return IntOne |
| 85 | } |
| 86 | return IntZero |
| 87 | } |
| 88 | |
| 89 | func compareUint(a, b Uint) ref.Val { |
| 90 | if a < b { |
| 91 | return IntNegOne |
| 92 | } |
| 93 | if a > b { |
| 94 | return IntOne |
| 95 | } |
| 96 | return IntZero |
| 97 | } |