diff --git a/mapping/document.go b/mapping/document.go index d4c9a8f9..6ec0c66b 100644 --- a/mapping/document.go +++ b/mapping/document.go @@ -504,7 +504,7 @@ func (dm *DocumentMapping) processProperty(property interface{}, path []string, } dm.walkDocument(property, path, indexes, context) } - case reflect.Map: + case reflect.Map, reflect.Slice: if subDocMapping != nil { for _, fieldMapping := range subDocMapping.Fields { if fieldMapping.Type == "geopoint" { diff --git a/mapping/mapping_test.go b/mapping/mapping_test.go index 5d7527e0..1a770904 100644 --- a/mapping/mapping_test.go +++ b/mapping/mapping_test.go @@ -869,37 +869,65 @@ func TestMappingForGeo(t *testing.T) { mapping := NewIndexMapping() mapping.DefaultMapping = thingMapping - x := struct { + geopoints := []interface{}{} + + // geopoint as a struct + geopoints = append(geopoints, struct { Name string `json:"name"` Location *Location `json:"location"` }{ - Name: "marty", + Name: "struct", Location: &Location{ Lon: -180, Lat: -90, }, - } + }) - doc := document.NewDocument("1") - err := mapping.MapDocument(doc, x) - if err != nil { - t.Fatal(err) - } + // geopoint as a map + geopoints = append(geopoints, struct { + Name string `json:"name"` + Location map[string]interface{} `json:"location"` + }{ + Name: "map", + Location: map[string]interface{}{ + "lon": -180, + "lat": -90, + }, + }) - var foundGeo bool - for _, f := range doc.Fields { - if f.Name() == "location" { - foundGeo = true - got := f.Value() - expect := []byte(numeric.MustNewPrefixCodedInt64(0, 0)) - if !reflect.DeepEqual(got, expect) { - t.Errorf("expected geo value: %v, got %v", expect, got) + // geopoint as a slice + geopoints = append(geopoints, struct { + Name string `json:"name"` + Location []interface{} `json:"location"` + }{ + Name: "slice", + Location: []interface{}{ + -180, -90, + }, + }) + + for i, geopoint := range geopoints { + doc := document.NewDocument(string(i)) + err := mapping.MapDocument(doc, geopoint) + if err != nil { + t.Fatal(err) + } + + var foundGeo bool + for _, f := range doc.Fields { + if f.Name() == "location" { + foundGeo = true + got := f.Value() + expect := []byte(numeric.MustNewPrefixCodedInt64(0, 0)) + if !reflect.DeepEqual(got, expect) { + t.Errorf("expected geo value: %v, got %v", expect, got) + } } } - } - if !foundGeo { - t.Errorf("expected to find geo point, did not") + if !foundGeo { + t.Errorf("expected to find geo point, did not") + } } }