Support for structs in document fields

This commit is contained in:
Amadeo Casas 2014-08-17 13:24:21 -07:00
parent 15921ffa9c
commit bfa1530af9
2 changed files with 19 additions and 6 deletions

23
goes.go
View File

@ -13,6 +13,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"reflect"
"strconv" "strconv"
"strings" "strings"
) )
@ -139,13 +140,25 @@ func (c *Connection) BulkSend(documents []Document) (Response, error) {
bulkData[i] = action bulkData[i] = action
i++ i++
if len(doc.Fields) > 0 { if doc.Fields != nil {
fields := make(map[string]interface{}, len(doc.Fields)) if docFields, ok := doc.Fields.(map[string]interface{}); ok {
for fieldName, fieldValue := range doc.Fields { if len(docFields) == 0 {
fields[fieldName] = fieldValue continue
}
} else {
typeOfFields := reflect.TypeOf(doc.Fields)
if typeOfFields.Kind() == reflect.Ptr {
typeOfFields = typeOfFields.Elem()
}
if typeOfFields.Kind() != reflect.Struct {
return Response{}, fmt.Errorf("Document fields not in struct or map[string]interface{} format")
}
if typeOfFields.NumField() == 0 {
continue
}
} }
sources, err := json.Marshal(fields) sources, err := json.Marshal(doc.Fields)
if err != nil { if err != nil {
return Response{}, err return Response{}, err
} }

View File

@ -102,7 +102,7 @@ type Document struct {
Type string Type string
Id interface{} Id interface{}
BulkCommand string BulkCommand string
Fields map[string]interface{} Fields interface{}
} }
// Represents the "items" field in a _bulk response // Represents the "items" field in a _bulk response