Skip to content

Commit 2776739

Browse files
committed
chore: fix error handling
chore: fix linting errors chore: fix linting errors fix: linting error
1 parent 14f6d62 commit 2776739

File tree

5 files changed

+20
-41
lines changed

5 files changed

+20
-41
lines changed

cel.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import (
1010

1111
func GetCelEnv(environment map[string]any) []cel.EnvOption {
1212
// Generated functions
13-
opts := append(funcs.CelEnvOption, kubernetes.Library()...)
13+
var opts = funcs.CelEnvOption
14+
opts = append(opts, kubernetes.Library()...)
1415
opts = append(opts, ext.Strings(), ext.Encoders(), ext.Lists(), ext.Math(), ext.Sets())
1516
opts = append(opts, strings.Library...)
1617

funcs/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func convertMap(arg ref.Val) (map[string]any, error) {
1717
case map[string]any:
1818
return m, nil
1919
default:
20-
return nil, fmt.Errorf("Not a map %T\n", arg.Value())
20+
return nil, fmt.Errorf("not a map %T", arg.Value())
2121
}
2222
}
2323

kubernetes/health.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -165,33 +165,3 @@ func k8sIsHealthy2() cel.EnvOption {
165165
),
166166
)
167167
}
168-
169-
var k8sGetStatusGen = cel.Function("GetStatus",
170-
cel.Overload("GetStatus_interface{}",
171-
[]*cel.Type{
172-
cel.DynType,
173-
},
174-
cel.StringType,
175-
cel.FunctionBinding(func(args ...ref.Val) ref.Val {
176-
return types.DefaultTypeAdapter.NativeToValue(GetStatus(args[0].Value()))
177-
178-
}),
179-
),
180-
)
181-
182-
var k8sGetHealthGen = cel.Function("GetHealth",
183-
cel.Overload("GetHealth_interface{}",
184-
185-
[]*cel.Type{
186-
cel.DynType,
187-
},
188-
cel.DynType,
189-
cel.UnaryBinding(func(in ref.Val) ref.Val {
190-
v := GetHealth(in.Value())
191-
b, _ := json.Marshal(v)
192-
var r map[string]string
193-
_ = json.Unmarshal(b, &r)
194-
return types.DefaultTypeAdapter.NativeToValue(r)
195-
}),
196-
),
197-
)

strings/duration.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ import (
2020
"github.com/google/cel-go/common/types"
2121
)
2222

23+
type InvalidDuration struct {
24+
Duration string
25+
}
26+
27+
func (m InvalidDuration) Error() string {
28+
return "time: invalid duration " + m.Duration
29+
}
30+
2331
// Duration is a standard unit of time.
2432
type Duration time.Duration
2533

@@ -316,7 +324,7 @@ func parseDuration(s string) (Duration, error) {
316324
return 0, nil
317325
}
318326
if s == "" {
319-
return 0, errors.New("time: invalid duration " + orig)
327+
return 0, InvalidDuration{Duration: orig}
320328
}
321329
for s != "" {
322330
var (
@@ -328,13 +336,13 @@ func parseDuration(s string) (Duration, error) {
328336

329337
// The next character must be [0-9.]
330338
if !(s[0] == '.' || '0' <= s[0] && s[0] <= '9') {
331-
return 0, errors.New("time: invalid duration " + orig)
339+
return 0, InvalidDuration{Duration: orig}
332340
}
333341
// Consume [0-9]*
334342
pl := len(s)
335343
v, s, err = leadingInt(s)
336344
if err != nil {
337-
return 0, errors.New("time: invalid duration " + orig)
345+
return 0, InvalidDuration{Duration: orig}
338346
}
339347
pre := pl != len(s) // whether we consumed anything before a period
340348
// Consume (\.[0-9]*)?
@@ -344,7 +352,7 @@ func parseDuration(s string) (Duration, error) {
344352
pl := len(s)
345353
f, s, err = leadingInt(s)
346354
if err != nil {
347-
return 0, errors.New("time: invalid duration " + orig)
355+
return 0, InvalidDuration{Duration: orig}
348356
}
349357
for n := pl - len(s); n > 0; n-- {
350358
scale *= 10
@@ -353,7 +361,7 @@ func parseDuration(s string) (Duration, error) {
353361
}
354362
if !pre && !post {
355363
// no digits (e.g. ".s" or "-.s")
356-
return 0, errors.New("time: invalid duration " + orig)
364+
return 0, InvalidDuration{Duration: orig}
357365
}
358366

359367
// Consume unit.
@@ -375,7 +383,7 @@ func parseDuration(s string) (Duration, error) {
375383
}
376384
if v > (1<<63-1)/unit {
377385
// overflow
378-
return 0, errors.New("time: invalid duration " + orig)
386+
return 0, InvalidDuration{Duration: orig}
379387
}
380388
v *= unit
381389
if f > 0 {
@@ -384,13 +392,13 @@ func parseDuration(s string) (Duration, error) {
384392
v += int64(float64(f) * (float64(unit) / scale))
385393
if v < 0 {
386394
// overflow
387-
return 0, errors.New("time: invalid duration " + orig)
395+
return 0, InvalidDuration{Duration: orig}
388396
}
389397
}
390398
d += v
391399
if d < 0 {
392400
// overflow
393-
return 0, errors.New("time: invalid duration " + orig)
401+
return 0, InvalidDuration{Duration: orig}
394402
}
395403
}
396404

tests/cel_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func TestExtensions(t *testing.T) {
173173
}
174174
func TestStrings(t *testing.T) {
175175
tests := []Test{
176-
// {nil, "random.String(10, ['a','b','d'])", ""},
176+
{nil, `random.AlphaNum(10).size()`, "10"},
177177
{nil, "'abcdedf'.runeCount()", "7"},
178178
{map[string]interface{}{"hello": "world"}, "hello", "world"},
179179
{map[string]interface{}{"hello": "hello world ?"}, "urlencode(hello)", `hello+world+%3F`},

0 commit comments

Comments
 (0)