0
0

fix indexing of primitives not inside map/struct

fixes #389
This commit is contained in:
Marty Schoch 2016-06-21 21:15:36 -04:00
parent 54b06ce0f6
commit 7e02e616ce
2 changed files with 45 additions and 0 deletions

View File

@ -362,7 +362,18 @@ func (dm *DocumentMapping) walkDocument(data interface{}, path []string, indexes
if ptrElem.IsValid() && ptrElem.CanInterface() {
dm.processProperty(ptrElem.Interface(), path, indexes, context)
}
case reflect.String:
dm.processProperty(val.String(), path, indexes, context)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
dm.processProperty(float64(val.Int()), path, indexes, context)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
dm.processProperty(float64(val.Uint()), path, indexes, context)
case reflect.Float32, reflect.Float64:
dm.processProperty(float64(val.Float()), path, indexes, context)
case reflect.Bool:
dm.processProperty(val.Bool(), path, indexes, context)
}
}
func (dm *DocumentMapping) processProperty(property interface{}, path []string, indexes []uint64, context *walkContext) {

View File

@ -724,3 +724,37 @@ func TestAnonymousStructFieldWithJSONStructTagEmptString(t *testing.T) {
t.Errorf("expected field named 'key', got '%s'", doc.Fields[0].Name())
}
}
func TestMappingPrimitives(t *testing.T) {
tests := []struct {
data interface{}
}{
{data: "marty"},
{data: int(1)},
{data: int8(2)},
{data: int16(3)},
{data: int32(4)},
{data: int64(5)},
{data: uint(6)},
{data: uint8(7)},
{data: uint16(8)},
{data: uint32(9)},
{data: uint64(10)},
{data: float32(11.0)},
{data: float64(12.0)},
{data: false},
}
m := NewIndexMapping()
for _, test := range tests {
doc := document.NewDocument("x")
err := m.mapDocument(doc, test.data)
if err != nil {
t.Fatal(err)
}
if len(doc.Fields) != 1 {
t.Errorf("expected 1 field, got %d for %v", len(doc.Fields), test.data)
}
}
}