| 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 |
"reflect" |
| 20 |
"strconv" |
| 21 |
"strings" |
| 22 |
"time" |
| 23 |
|
| 24 |
"github.com/google/cel-go/common/overloads" |
| 25 |
"github.com/google/cel-go/common/types/ref" |
| 26 |
|
| 27 |
anypb "google.golang.org/protobuf/types/known/anypb" |
| 28 |
dpb "google.golang.org/protobuf/types/known/durationpb" |
| 29 |
structpb "google.golang.org/protobuf/types/known/structpb" |
| 30 |
) |
| 31 |
|
| 32 |
// Duration type that implements ref.Val and supports add, compare, negate, |
| 33 |
// and subtract operators. This type is also a receiver which means it can |
| 34 |
// participate in dispatch to receiver functions. |
| 35 |
type Duration struct { |
| 36 |
time.Duration |
| 37 |
} |
| 38 |
|
| 39 |
func durationOf(d time.Duration) Duration { |
| 40 |
return Duration{Duration: d} |
| 41 |
} |
| 42 |
|
| 43 |
var ( |
| 44 |
durationValueType = reflect.TypeOf(&dpb.Duration{}) |
| 45 |
|
| 46 |
durationZeroArgOverloads = map[string]func(ref.Val) ref.Val{ |
| 47 |
overloads.TimeGetHours: DurationGetHours, |
| 48 |
overloads.TimeGetMinutes: DurationGetMinutes, |
| 49 |
overloads.TimeGetSeconds: DurationGetSeconds, |
| 50 |
overloads.TimeGetMilliseconds: DurationGetMilliseconds, |
| 51 |
} |
| 52 |
) |
| 53 |
|
| 54 |
// Add implements traits.Adder.Add. |
| 55 |
func (d Duration) Add(other ref.Val) ref.Val { |
| 56 |
switch other.Type() { |
| 57 |
case DurationType: |
| 58 |
dur2 := other.(Duration) |
| 59 |
val, err := addDurationChecked(d.Duration, dur2.Duration) |
| 60 |
if err != nil { |
| 61 |
return WrapErr(err) |
| 62 |
} |
| 63 |
return durationOf(val) |
| 64 |
case TimestampType: |
| 65 |
ts := other.(Timestamp).Time |
| 66 |
val, err := addTimeDurationChecked(ts, d.Duration) |
| 67 |
if err != nil { |
| 68 |
return WrapErr(err) |
| 69 |
} |
| 70 |
return timestampOf(val) |
| 71 |
} |
| 72 |
return MaybeNoSuchOverloadErr(other) |
| 73 |
} |
| 74 |
|
| 75 |
// Compare implements traits.Comparer.Compare. |
| 76 |
func (d Duration) Compare(other ref.Val) ref.Val { |
| 77 |
otherDur, ok := other.(Duration) |
| 78 |
if !ok { |
| 79 |
return MaybeNoSuchOverloadErr(other) |
| 80 |
} |
| 81 |
d1 := d.Duration |
| 82 |
d2 := otherDur.Duration |
| 83 |
switch { |
| 84 |
case d1 < d2: |
| 85 |
return IntNegOne |
| 86 |
case d1 > d2: |
| 87 |
return IntOne |
| 88 |
default: |
| 89 |
return IntZero |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
// ConvertToNative implements ref.Val.ConvertToNative. |
| 94 |
func (d Duration) ConvertToNative(typeDesc reflect.Type) (any, error) { |
| 95 |
// If the duration is already assignable to the desired type return it. |
| 96 |
if reflect.TypeOf(d.Duration).AssignableTo(typeDesc) { |
| 97 |
return d.Duration, nil |
| 98 |
} |
| 99 |
if reflect.TypeOf(d).AssignableTo(typeDesc) { |
| 100 |
return d, nil |
| 101 |
} |
| 102 |
switch typeDesc { |
| 103 |
case anyValueType: |
| 104 |
// Pack the duration as a dpb.Duration into an Any value. |
| 105 |
return anypb.New(dpb.New(d.Duration)) |
| 106 |
case durationValueType: |
| 107 |
// Unwrap the CEL value to its underlying proto value. |
| 108 |
return dpb.New(d.Duration), nil |
| 109 |
case JSONValueType: |
| 110 |
// CEL follows the proto3 to JSON conversion. |
| 111 |
// Note, using jsonpb would wrap the result in extra double quotes. |
| 112 |
v := d.ConvertToType(StringType) |
| 113 |
if IsError(v) { |
| 114 |
return nil, v.(*Err) |
| 115 |
} |
| 116 |
return structpb.NewStringValue(string(v.(String))), nil |
| 117 |
} |
| 118 |
return nil, fmt.Errorf("type conversion error from 'Duration' to '%v'", typeDesc) |
| 119 |
} |
| 120 |
|
| 121 |
// ConvertToType implements ref.Val.ConvertToType. |
| 122 |
func (d Duration) ConvertToType(typeVal ref.Type) ref.Val { |
| 123 |
switch typeVal { |
| 124 |
case StringType: |
| 125 |
return String(strconv.FormatFloat(d.Seconds(), 'f', -1, 64) + "s") |
| 126 |
case IntType: |
| 127 |
return Int(d.Duration) |
| 128 |
case DurationType: |
| 129 |
return d |
| 130 |
case TypeType: |
| 131 |
return DurationType |
| 132 |
} |
| 133 |
return NewErr("type conversion error from '%s' to '%s'", DurationType, typeVal) |
| 134 |
} |
| 135 |
|
| 136 |
// Equal implements ref.Val.Equal. |
| 137 |
func (d Duration) Equal(other ref.Val) ref.Val { |
| 138 |
otherDur, ok := other.(Duration) |
| 139 |
return Bool(ok && d.Duration == otherDur.Duration) |
| 140 |
} |
| 141 |
|
| 142 |
// IsZeroValue returns true if the duration value is zero |
| 143 |
func (d Duration) IsZeroValue() bool { |
| 144 |
return d.Duration == 0 |
| 145 |
} |
| 146 |
|
| 147 |
// Negate implements traits.Negater.Negate. |
| 148 |
func (d Duration) Negate() ref.Val { |
| 149 |
val, err := negateDurationChecked(d.Duration) |
| 150 |
if err != nil { |
| 151 |
return WrapErr(err) |
| 152 |
} |
| 153 |
return durationOf(val) |
| 154 |
} |
| 155 |
|
| 156 |
// Receive implements traits.Receiver.Receive. |
| 157 |
func (d Duration) Receive(function string, overload string, args []ref.Val) ref.Val { |
| 158 |
if len(args) == 0 { |
| 159 |
if f, found := durationZeroArgOverloads[function]; found { |
| 160 |
return f(d) |
| 161 |
} |
| 162 |
} |
| 163 |
return NoSuchOverloadErr() |
| 164 |
} |
| 165 |
|
| 166 |
// Subtract implements traits.Subtractor.Subtract. |
| 167 |
func (d Duration) Subtract(subtrahend ref.Val) ref.Val { |
| 168 |
subtraDur, ok := subtrahend.(Duration) |
| 169 |
if !ok { |
| 170 |
return MaybeNoSuchOverloadErr(subtrahend) |
| 171 |
} |
| 172 |
val, err := subtractDurationChecked(d.Duration, subtraDur.Duration) |
| 173 |
if err != nil { |
| 174 |
return WrapErr(err) |
| 175 |
} |
| 176 |
return durationOf(val) |
| 177 |
} |
| 178 |
|
| 179 |
// Type implements ref.Val.Type. |
| 180 |
func (d Duration) Type() ref.Type { |
| 181 |
return DurationType |
| 182 |
} |
| 183 |
|
| 184 |
// Value implements ref.Val.Value. |
| 185 |
func (d Duration) Value() any { |
| 186 |
return d.Duration |
| 187 |
} |
| 188 |
|
| 189 |
func (d Duration) format(sb *strings.Builder) { |
| 190 |
fmt.Fprintf(sb, `duration("%ss")`, strconv.FormatFloat(d.Seconds(), 'f', -1, 64)) |
| 191 |
} |
| 192 |
|
| 193 |
// DurationGetHours returns the duration in hours. |
| 194 |
func DurationGetHours(val ref.Val) ref.Val { |
| 195 |
dur, ok := val.(Duration) |
| 196 |
if !ok { |
| 197 |
return MaybeNoSuchOverloadErr(val) |
| 198 |
} |
| 199 |
return Int(dur.Hours()) |
| 200 |
} |
| 201 |
|
| 202 |
// DurationGetMinutes returns duration in minutes. |
| 203 |
func DurationGetMinutes(val ref.Val) ref.Val { |
| 204 |
dur, ok := val.(Duration) |
| 205 |
if !ok { |
| 206 |
return MaybeNoSuchOverloadErr(val) |
| 207 |
} |
| 208 |
return Int(dur.Minutes()) |
| 209 |
} |
| 210 |
|
| 211 |
// DurationGetSeconds returns duration in seconds. |
| 212 |
func DurationGetSeconds(val ref.Val) ref.Val { |
| 213 |
dur, ok := val.(Duration) |
| 214 |
if !ok { |
| 215 |
return MaybeNoSuchOverloadErr(val) |
| 216 |
} |
| 217 |
return Int(dur.Seconds()) |
| 218 |
} |
| 219 |
|
| 220 |
// DurationGetMilliseconds returns duration in milliseconds. |
| 221 |
func DurationGetMilliseconds(val ref.Val) ref.Val { |
| 222 |
dur, ok := val.(Duration) |
| 223 |
if !ok { |
| 224 |
return MaybeNoSuchOverloadErr(val) |
| 225 |
} |
| 226 |
return Int(dur.Milliseconds()) |
| 227 |
} |