Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 21389214ea | |||
| c2e107f1af |
+27
-5
@@ -642,8 +642,23 @@ func isNil(a interface{}) bool {
|
||||
return vi.IsNil()
|
||||
}
|
||||
|
||||
func isNilOrZero(value interface{}) bool {
|
||||
if value == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
v := reflect.ValueOf(value)
|
||||
switch v.Kind() {
|
||||
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Slice:
|
||||
return v.IsNil()
|
||||
default:
|
||||
// Check for zero value
|
||||
return reflect.DeepEqual(value, reflect.Zero(v.Type()).Interface())
|
||||
}
|
||||
}
|
||||
|
||||
func Default(inputValue interface{}, defaultValue interface{}) (_result interface{}) {
|
||||
if IsNil(inputValue) {
|
||||
if isNilOrZero(inputValue) {
|
||||
_result = defaultValue
|
||||
return _result
|
||||
}
|
||||
@@ -714,10 +729,17 @@ func ToMap(args ...interface{}) map[string]interface{} {
|
||||
}
|
||||
default:
|
||||
val := reflect.ValueOf(obj)
|
||||
res := structToMap(val)
|
||||
for key, value := range res {
|
||||
if value != nil {
|
||||
finalArg[key] = value
|
||||
if val.Kind().String() == "map" {
|
||||
tmp := val.MapKeys()
|
||||
for _, key := range tmp {
|
||||
finalArg[key.String()] = val.MapIndex(key).Interface()
|
||||
}
|
||||
} else {
|
||||
res := structToMap(val)
|
||||
for key, value := range res {
|
||||
if value != nil {
|
||||
finalArg[key] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+44
-1
@@ -361,11 +361,27 @@ type Test struct {
|
||||
}
|
||||
|
||||
func TestToMap(t *testing.T) {
|
||||
inStr := map[string]string{
|
||||
"tea": "test",
|
||||
"test": "test2",
|
||||
}
|
||||
result := ToMap(inStr)
|
||||
utils.AssertEqual(t, "test", result["tea"])
|
||||
utils.AssertEqual(t, "test2", result["test"])
|
||||
|
||||
inInt := map[string]int{
|
||||
"tea": 12,
|
||||
"test": 13,
|
||||
}
|
||||
result = ToMap(inInt)
|
||||
utils.AssertEqual(t, 12, result["tea"])
|
||||
utils.AssertEqual(t, 13, result["test"])
|
||||
|
||||
in := map[string]*string{
|
||||
"tea": String("test"),
|
||||
"nil": nil,
|
||||
}
|
||||
result := ToMap(in)
|
||||
result = ToMap(in)
|
||||
utils.AssertEqual(t, "test", result["tea"])
|
||||
utils.AssertNil(t, result["nil"])
|
||||
|
||||
@@ -995,6 +1011,33 @@ func Test_Default(t *testing.T) {
|
||||
|
||||
b := ToInt32(a)
|
||||
utils.AssertEqual(t, Int32Value(b), int32(10))
|
||||
|
||||
// Testing Default with nil values
|
||||
if result := Default(nil, "default"); result != "default" {
|
||||
t.Errorf("expected 'default', got '%v'", result)
|
||||
}
|
||||
|
||||
// Testing Default with zero values
|
||||
if result := Default("", "default"); result != "default" {
|
||||
t.Errorf("expected 'default', got '%v'", result)
|
||||
}
|
||||
|
||||
if result := Default(0, 42); result != 42 {
|
||||
t.Errorf("expected 42, got %v", result)
|
||||
}
|
||||
|
||||
if result := Default(false, true); result != true {
|
||||
t.Errorf("expected true, got %v", result)
|
||||
}
|
||||
|
||||
// Testing Default with non-zero values
|
||||
if result := Default("value", "default"); result != "value" {
|
||||
t.Errorf("expected 'value', got '%v'", result)
|
||||
}
|
||||
|
||||
if result := Default([]int{1, 2, 3}, []int{}); !reflect.DeepEqual(result, []int{1, 2, 3}) {
|
||||
t.Errorf("expected [1 2 3], got '%v'", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToBytes(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user