diff --git a/index_op_test.go b/index_op_test.go index 5f752da..589c2e4 100644 --- a/index_op_test.go +++ b/index_op_test.go @@ -95,7 +95,7 @@ func TestArrayInStruct(t *testing.T) { } type StructWithMap struct { - Map map[string]string + Map map[string]int64 } func TestMapField(t *testing.T) { @@ -110,8 +110,8 @@ func TestMapField(t *testing.T) { } inputs := make(map[string]interface{}) - mapField := make(map[string]string) - mapField["key"] = "value" + mapField := make(map[string]int64) + mapField["key"] = 42 inputs["s"] = &StructWithMap{ Map: mapField, } @@ -128,7 +128,46 @@ func TestMapField(t *testing.T) { t.Fatal("Experiment run failed") } - if elem := exp.Outputs["element"]; elem != "value" { + if elem := exp.Outputs["element"]; elem != int64(42) { + t.Fail() + } + + if exp.Outputs["empty"] != nil { + t.Fail() + } +} + +type StructWithNilField struct { + None interface{} +} + +func TestStructWithNilField(t *testing.T) { + data, err := ioutil.ReadFile("test/struct_with_nil_field.json") + if err != nil { + t.Fatal(err) + } + var js map[string]interface{} + err = json.Unmarshal(data, &js) + if (err != nil) { + t.Fatal(err) + } + + inputs := make(map[string]interface{}) + inputs["struct"] = &StructWithNilField{} + + exp := &Interpreter{ + Name: "struct with nil field", + Salt: "safasdf", + Inputs: inputs, + Outputs: make(map[string]interface{}), + Code: js, + } + + if _, ok := exp.Run(); !ok { + t.Fatal("Experiment run failed") + } + + if exp.Outputs["nil"] != nil { t.Fail() } } \ No newline at end of file diff --git a/operators.go b/operators.go index 1a57175..cf2865e 100644 --- a/operators.go +++ b/operators.go @@ -213,7 +213,11 @@ func unwrapValue(value reflect.Value) interface{} { case reflect.Bool: return value.Bool() default: - return value.Interface() + if value.IsValid() { + return value.Interface() + } else { + return nil + } } } diff --git a/test/map_index_test.json b/test/map_index_test.json index 7ce7cd1..9481e0c 100644 --- a/test/map_index_test.json +++ b/test/map_index_test.json @@ -32,6 +32,18 @@ }, "index": "key" } + }, + { + "op": "set", + "var": "empty", + "value": { + "op": "index", + "base": { + "op": "get", + "var": "map" + }, + "index": "not there" + } } ] } \ No newline at end of file diff --git a/test/struct_with_nil_field.json b/test/struct_with_nil_field.json new file mode 100644 index 0000000..ad8e14b --- /dev/null +++ b/test/struct_with_nil_field.json @@ -0,0 +1,12 @@ +{ + "op": "set", + "var": "nil", + "value": { + "op": "index", + "base": { + "op": "get", + "var": "struct" + }, + "index": "None" + } +} \ No newline at end of file