builtin-base: bool() supports lists and dicts, int() supports fractions
This commit is contained in:
parent
7b93c5b4ac
commit
92bd366380
@ -72,6 +72,10 @@ func boolFunc(ctx ExprContext, name string, args map[string]any) (result any, er
|
||||
result = v
|
||||
case string:
|
||||
result = len(v) > 0
|
||||
case *ListType:
|
||||
result = len(*v) > 0
|
||||
case *DictType:
|
||||
result = len(*v) > 0
|
||||
default:
|
||||
err = ErrCantConvert(name, v, "bool")
|
||||
}
|
||||
@ -95,6 +99,8 @@ func intFunc(ctx ExprContext, name string, args map[string]any) (result any, err
|
||||
if i, err = strconv.Atoi(v); err == nil {
|
||||
result = int64(i)
|
||||
}
|
||||
case *FractionType:
|
||||
result = int64(v.num / v.den)
|
||||
default:
|
||||
err = ErrCantConvert(name, v, "int")
|
||||
}
|
||||
|
||||
@ -52,16 +52,21 @@ func TestFuncBase(t *testing.T) {
|
||||
/* 38 */ {`bool(1.0)`, true, nil},
|
||||
/* 39 */ {`bool("1")`, true, nil},
|
||||
/* 40 */ {`bool(false)`, false, nil},
|
||||
/* 41 */ {`bool([1])`, nil, `bool(): can't convert list to bool`},
|
||||
/* 42 */ {`dec(false)`, float64(0), nil},
|
||||
/* 43 */ {`dec(1:2)`, float64(0.5), nil},
|
||||
/* 44 */ {`dec([1])`, nil, `dec(): can't convert list to float`},
|
||||
/* 45 */ {`eval("a=3"); a`, int64(3), nil},
|
||||
/* 41 */ {`bool([1])`, true, nil},
|
||||
/* 42 */ {`bool([])`, false, nil},
|
||||
/* 43 */ {`bool({})`, false, nil},
|
||||
/* 44 */ {`bool({1:"one"})`, true, nil},
|
||||
/* 45 */ {`dec(false)`, float64(0), nil},
|
||||
/* 46 */ {`dec(1:2)`, float64(0.5), nil},
|
||||
/* 47 */ {`dec([1])`, nil, `dec(): can't convert list to float`},
|
||||
/* 48 */ {`eval("a=3"); a`, int64(3), nil},
|
||||
/* 49 */ {`int(5:2)`, int64(2), nil},
|
||||
|
||||
// /* 45 */ {`string([1])`, nil, `string(): can't convert list to string`},
|
||||
}
|
||||
|
||||
t.Setenv("EXPR_PATH", ".")
|
||||
|
||||
// runTestSuiteSpec(t, section, inputs, 45)
|
||||
// runTestSuiteSpec(t, section, inputs, 49)
|
||||
runTestSuite(t, section, inputs)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user