From 7274dddd2e5c9d1e4179e8a919a66ab64733abd8 Mon Sep 17 00:00:00 2001 From: Marty Schoch Date: Mon, 5 Jun 2017 10:08:10 -0400 Subject: [PATCH] fix nil ptr panic on newly introduced text marshaler support We recenlty introduced support for indexing the content of things implementing TextMarshaler. Since often times interfaces are implemented via pointer receivers, we added support to introspect pointers (previously we just dereferenceed them and traversed into their underlying structs). However, in doing so we neglected to consider the case where the pointer does implement the interface we care about, but happens to be nil. fixes #603 --- mapping/document.go | 26 +++++++++++++++----------- mapping/mapping_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/mapping/document.go b/mapping/document.go index 6b903889..d62675e5 100644 --- a/mapping/document.go +++ b/mapping/document.go @@ -513,21 +513,25 @@ func (dm *DocumentMapping) processProperty(property interface{}, path []string, } dm.walkDocument(property, path, indexes, context) case reflect.Ptr: - switch property := property.(type) { - case encoding.TextMarshaler: - txt, err := property.MarshalText() - if err == nil && subDocMapping != nil { - // index by explicit mapping - for _, fieldMapping := range subDocMapping.Fields { - if fieldMapping.Type == "text" { - fieldMapping.processString(string(txt), pathString, path, indexes, context) + if !propertyValue.IsNil() { + switch property := property.(type) { + case encoding.TextMarshaler: + + txt, err := property.MarshalText() + if err == nil && subDocMapping != nil { + // index by explicit mapping + for _, fieldMapping := range subDocMapping.Fields { + if fieldMapping.Type == "text" { + fieldMapping.processString(string(txt), pathString, path, indexes, context) + } } + } else { + dm.walkDocument(property, path, indexes, context) } - } else { + + default: dm.walkDocument(property, path, indexes, context) } - default: - dm.walkDocument(property, path, indexes, context) } default: dm.walkDocument(property, path, indexes, context) diff --git a/mapping/mapping_test.go b/mapping/mapping_test.go index e16052b1..5cd86015 100644 --- a/mapping/mapping_test.go +++ b/mapping/mapping_test.go @@ -19,6 +19,7 @@ import ( "fmt" "reflect" "testing" + "time" "github.com/blevesearch/bleve/analysis/tokenizer/exception" "github.com/blevesearch/bleve/analysis/tokenizer/regexp" @@ -966,3 +967,27 @@ func TestMappingForTextMarshaler(t *testing.T) { } } + +func TestMappingForNilTextMarshaler(t *testing.T) { + tm := struct { + Marshalable *time.Time + }{ + Marshalable: nil, + } + + // now verify that when a mapping explicity + m := NewIndexMapping() + txt := NewTextFieldMapping() + m.DefaultMapping.AddFieldMappingsAt("Marshalable", txt) + doc := document.NewDocument("x") + err := m.MapDocument(doc, tm) + if err != nil { + t.Fatal(err) + } + + if len(doc.Fields) != 0 { + t.Fatalf("expected 1 field, got: %d", len(doc.Fields)) + + } + +}