From fc7243e5c924c57521e5e2b8ff13e64cae37c698 Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Fri, 12 Jan 2018 03:15:03 -0700 Subject: [PATCH] Add map and slice support to IsEmpty --- types/convert/convert.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/types/convert/convert.go b/types/convert/convert.go index 43ba4774..a2a6c49a 100644 --- a/types/convert/convert.go +++ b/types/convert/convert.go @@ -104,7 +104,16 @@ func LowerTitle(input string) string { } func IsEmpty(v interface{}) bool { - return v == nil || v == "" || v == 0 || v == false + if v == nil || v == "" || v == 0 || v == false { + return true + } + if m, ok := v.(map[string]interface{}); ok { + return len(m) == 0 + } + if s, ok := v.([]interface{}); ok { + return len(s) == 0 + } + return false } func ToMapInterface(obj interface{}) map[string]interface{} { @@ -112,6 +121,13 @@ func ToMapInterface(obj interface{}) map[string]interface{} { return v } +func ToInterfaceSlice(obj interface{}) []interface{} { + if v, ok := obj.([]interface{}); ok { + return v + } + return nil +} + func ToMapSlice(obj interface{}) []map[string]interface{} { if v, ok := obj.([]map[string]interface{}); ok { return v