@@ -20,6 +20,14 @@ import (
20
20
"github.com/google/cel-go/common/types"
21
21
)
22
22
23
+ type InvalidDuration struct {
24
+ Duration string
25
+ }
26
+
27
+ func (m InvalidDuration ) Error () string {
28
+ return "time: invalid duration " + m .Duration
29
+ }
30
+
23
31
// Duration is a standard unit of time.
24
32
type Duration time.Duration
25
33
@@ -316,7 +324,7 @@ func parseDuration(s string) (Duration, error) {
316
324
return 0 , nil
317
325
}
318
326
if s == "" {
319
- return 0 , errors . New ( "time: invalid duration " + orig )
327
+ return 0 , InvalidDuration { Duration : orig }
320
328
}
321
329
for s != "" {
322
330
var (
@@ -328,13 +336,13 @@ func parseDuration(s string) (Duration, error) {
328
336
329
337
// The next character must be [0-9.]
330
338
if ! (s [0 ] == '.' || '0' <= s [0 ] && s [0 ] <= '9' ) {
331
- return 0 , errors . New ( "time: invalid duration " + orig )
339
+ return 0 , InvalidDuration { Duration : orig }
332
340
}
333
341
// Consume [0-9]*
334
342
pl := len (s )
335
343
v , s , err = leadingInt (s )
336
344
if err != nil {
337
- return 0 , errors . New ( "time: invalid duration " + orig )
345
+ return 0 , InvalidDuration { Duration : orig }
338
346
}
339
347
pre := pl != len (s ) // whether we consumed anything before a period
340
348
// Consume (\.[0-9]*)?
@@ -344,7 +352,7 @@ func parseDuration(s string) (Duration, error) {
344
352
pl := len (s )
345
353
f , s , err = leadingInt (s )
346
354
if err != nil {
347
- return 0 , errors . New ( "time: invalid duration " + orig )
355
+ return 0 , InvalidDuration { Duration : orig }
348
356
}
349
357
for n := pl - len (s ); n > 0 ; n -- {
350
358
scale *= 10
@@ -353,7 +361,7 @@ func parseDuration(s string) (Duration, error) {
353
361
}
354
362
if ! pre && ! post {
355
363
// no digits (e.g. ".s" or "-.s")
356
- return 0 , errors . New ( "time: invalid duration " + orig )
364
+ return 0 , InvalidDuration { Duration : orig }
357
365
}
358
366
359
367
// Consume unit.
@@ -375,7 +383,7 @@ func parseDuration(s string) (Duration, error) {
375
383
}
376
384
if v > (1 << 63 - 1 )/ unit {
377
385
// overflow
378
- return 0 , errors . New ( "time: invalid duration " + orig )
386
+ return 0 , InvalidDuration { Duration : orig }
379
387
}
380
388
v *= unit
381
389
if f > 0 {
@@ -384,13 +392,13 @@ func parseDuration(s string) (Duration, error) {
384
392
v += int64 (float64 (f ) * (float64 (unit ) / scale ))
385
393
if v < 0 {
386
394
// overflow
387
- return 0 , errors . New ( "time: invalid duration " + orig )
395
+ return 0 , InvalidDuration { Duration : orig }
388
396
}
389
397
}
390
398
d += v
391
399
if d < 0 {
392
400
// overflow
393
- return 0 , errors . New ( "time: invalid duration " + orig )
401
+ return 0 , InvalidDuration { Duration : orig }
394
402
}
395
403
}
396
404
0 commit comments