[add] Empty expression helper

This commit is contained in:
Max 2024-08-07 11:08:25 +08:00
parent c5cf0babf9
commit f6ae0cef47

View file

@ -49,6 +49,7 @@ var options = []expr.Option{
expr.Function("P_", _process), expr.Function("P_", _process),
expr.Function("True", _true), expr.Function("True", _true),
expr.Function("False", _false), expr.Function("False", _false),
expr.Function("Empty", _empty),
expr.AllowUndefinedVariables(), expr.AllowUndefinedVariables(),
} }
@ -273,6 +274,31 @@ func _true(args ...any) (interface{}, error) {
return false, nil return false, nil
} }
func _empty(args ...any) (interface{}, error) {
if len(args) < 1 {
return true, nil
}
if args[0] == nil {
return true, nil
}
if v, ok := args[0].(string); ok {
return v == "", nil
}
if v, ok := args[0].(int); ok {
return v == 0, nil
}
if v, ok := args[0].(bool); ok {
return !v, nil
}
return true, nil
}
func _process(args ...any) (interface{}, error) { func _process(args ...any) (interface{}, error) {
if len(args) < 1 { if len(args) < 1 {