From f6ae0cef47502a24b9d354b9b76903e5f6fd9541 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 7 Aug 2024 11:08:25 +0800 Subject: [PATCH] [add] Empty expression helper --- sui/core/data.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/sui/core/data.go b/sui/core/data.go index 8a761d26..f06c5b23 100644 --- a/sui/core/data.go +++ b/sui/core/data.go @@ -49,6 +49,7 @@ var options = []expr.Option{ expr.Function("P_", _process), expr.Function("True", _true), expr.Function("False", _false), + expr.Function("Empty", _empty), expr.AllowUndefinedVariables(), } @@ -273,6 +274,31 @@ func _true(args ...any) (interface{}, error) { 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) { if len(args) < 1 {