Skip to content

Commit

Permalink
Merge pull request mattbaird#61 from dukex/fix/marshal-json
Browse files Browse the repository at this point in the history
Fixed "json: error calling MarshalJSON for type json.RawMessage"
  • Loading branch information
mattbaird committed Mar 5, 2014
2 parents e72ba32 + 48595f2 commit d2bd63c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
12 changes: 6 additions & 6 deletions core/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ func (h *Hits) Len() int {
}

type Hit struct {
Index string `json:"_index"`
Type string `json:"_type,omitempty"`
Id string `json:"_id"`
Score Float32Nullable `json:"_score,omitempty"` // Filters (no query) dont have score, so is null
Source json.RawMessage `json:"_source"` // marshalling left to consumer
Fields json.RawMessage `json:"fields"` // when a field arg is passed to ES, instead of _source it returns fields
Index string `json:"_index"`
Type string `json:"_type,omitempty"`
Id string `json:"_id"`
Score Float32Nullable `json:"_score,omitempty"` // Filters (no query) dont have score, so is null
Source *json.RawMessage `json:"_source"` // marshalling left to consumer
Fields *json.RawMessage `json:"fields"` // when a field arg is passed to ES, instead of _source it returns fields
}

// Elasticsearch returns some invalid (according to go) json, with floats having...
Expand Down
20 changes: 20 additions & 0 deletions core/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package core

import (
"encoding/json"
"fmt"
"github.com/bmizerany/assert"
"testing"
Expand All @@ -29,3 +30,22 @@ func TestSearchRequest(t *testing.T) {
assert.T(t, out.Hits.Len() == 10, fmt.Sprintf("Should have 10 docs but was %v", out.Hits.Len()))
assert.T(t, CloseInt(out.Hits.Total, 588), fmt.Sprintf("Should have 588 hits but was %v", out.Hits.Total))
}

func TestSearchResultToJSON(t *testing.T) {
qry := map[string]interface{}{
"query": map[string]interface{}{
"wildcard": map[string]string{"actor": "a*"},
},
}
out, err := SearchRequest(true, "github", "", qry, "", 0)

if err != nil {
t.Error(err)
}

_, err = json.Marshal(out.Hits.Hits)

if err != nil {
t.Error(err)
}
}

0 comments on commit d2bd63c

Please sign in to comment.