0
0

fix panic on mapping value containing null

added testcase to reproduce initial issue
closes #153
This commit is contained in:
Marty Schoch 2015-02-05 16:15:05 -05:00
parent 41cd64337b
commit 15139b8fa5
2 changed files with 36 additions and 0 deletions

View File

@ -270,6 +270,10 @@ func (dm *DocumentMapping) processProperty(property interface{}, path []string,
}
propertyValue := reflect.ValueOf(property)
if !propertyValue.IsValid() {
// cannot do anything with the zero value
return
}
propertyType := propertyValue.Type()
switch propertyType.Kind() {
case reflect.String:

View File

@ -183,3 +183,35 @@ func TestMappingStructWithPointerToString(t *testing.T) {
t.Errorf("expected to find 1 find, found %d", count)
}
}
func TestMappingJSONWithNull(t *testing.T) {
mapping := NewIndexMapping()
jsonbytes := []byte(`{"name":"marty", "age": null}`)
var jsondoc interface{}
err := json.Unmarshal(jsonbytes, &jsondoc)
if err != nil {
t.Fatal(err)
}
doc := document.NewDocument("1")
err = mapping.mapDocument(doc, jsondoc)
if err != nil {
t.Fatal(err)
}
found := false
count := 0
for _, f := range doc.Fields {
if f.Name() == "name" {
found = true
}
count++
}
if !found {
t.Errorf("expected to find field named 'name'")
}
if count != 1 {
t.Errorf("expected to find 1 find, found %d", count)
}
}