Browse Source

Made sure ToValue() returns null for interface{} with nil values for map[string]interface{}, []interface{} and *[]interface{}. Fixes #110

Dmitry Panov 6 years ago
parent
commit
65ce6d6e24
2 changed files with 29 additions and 1 deletions
  1. 9 0
      runtime.go
  2. 20 1
      runtime_test.go

+ 9 - 0
runtime.go

@@ -995,6 +995,9 @@ func (r *Runtime) ToValue(i interface{}) Value {
 	case float64:
 		return floatToValue(i)
 	case map[string]interface{}:
+		if i == nil {
+			return _null
+		}
 		obj := &Object{runtime: r}
 		m := &objectGoMapSimple{
 			baseObject: baseObject{
@@ -1007,6 +1010,9 @@ func (r *Runtime) ToValue(i interface{}) Value {
 		m.init()
 		return obj
 	case []interface{}:
+		if i == nil {
+			return _null
+		}
 		obj := &Object{runtime: r}
 		a := &objectGoSlice{
 			baseObject: baseObject{
@@ -1018,6 +1024,9 @@ func (r *Runtime) ToValue(i interface{}) Value {
 		a.init()
 		return obj
 	case *[]interface{}:
+		if i == nil {
+			return _null
+		}
 		obj := &Object{runtime: r}
 		a := &objectGoSlice{
 			baseObject: baseObject{

+ 20 - 1
runtime_test.go

@@ -787,8 +787,27 @@ func TestToValueNil(t *testing.T) {
 	var a *T
 	vm := New()
 
+	if v := vm.ToValue(nil); !IsNull(v) {
+		t.Fatalf("nil: %v", v)
+	}
+
 	if v := vm.ToValue(a); !IsNull(v) {
-		t.Fatalf("Expected null, got: %v", v)
+		t.Fatalf("struct ptr: %v", v)
+	}
+
+	var m map[string]interface{}
+	if v := vm.ToValue(m); !IsNull(v) {
+		t.Fatalf("map[string]interface{}: %v", v)
+	}
+
+	var ar []interface{}
+	if v := vm.ToValue(ar); !IsNull(v) {
+		t.Fatalf("[]interface{}: %v", v)
+	}
+
+	var arptr *[]interface{}
+	if v := vm.ToValue(arptr); !IsNull(v) {
+		t.Fatalf("*[]interface{}: %v", v)
 	}
 }