Merge pull request #2 from ido50/master

Bugfix: Run() fails for queries, add MarshalJSON()
This commit is contained in:
Oran Moshai 2020-02-20 16:54:57 +02:00 committed by GitHub
commit 42eaef3f2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 49 additions and 2 deletions

View File

@ -40,6 +40,10 @@ func (req *AggregationRequest) Map() map[string]interface{} {
} }
} }
func (req *AggregationRequest) MarshalJSON() ([]byte, error) {
return json.Marshal(req.Map())
}
func (req *AggregationRequest) Run( func (req *AggregationRequest) Run(
api *elasticsearch.Client, api *elasticsearch.Client,
o ...func(*esapi.SearchRequest), o ...func(*esapi.SearchRequest),

View File

@ -4,6 +4,8 @@ import (
"encoding/json" "encoding/json"
"reflect" "reflect"
"testing" "testing"
"github.com/jgroeneveld/trial/assert"
) )
type mapTest struct { type mapTest struct {
@ -38,3 +40,20 @@ func sameJSON(a, b map[string]interface{}) (aJSON, bJSON []byte, ok bool) {
ok = reflect.DeepEqual(aJSON, bJSON) ok = reflect.DeepEqual(aJSON, bJSON)
return aJSON, bJSON, ok return aJSON, bJSON, ok
} }
type jsonTest struct {
name string
q json.Marshaler
expJSON string
expErr error
}
func runJSONTests(t *testing.T, tests []jsonTest) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
b, err := test.q.MarshalJSON()
assert.Equal(t, test.expErr, err)
assert.Equal(t, test.expJSON, string(b))
})
}
}

2
go.mod
View File

@ -5,4 +5,6 @@ go 1.13
require ( require (
github.com/elastic/go-elasticsearch/v7 v7.6.0 github.com/elastic/go-elasticsearch/v7 v7.6.0
github.com/fatih/structs v1.1.0 github.com/fatih/structs v1.1.0
github.com/jgroeneveld/schema v1.0.0 // indirect
github.com/jgroeneveld/trial v2.0.0+incompatible
) )

4
go.sum
View File

@ -6,3 +6,7 @@ github.com/elastic/go-elasticsearch/v8 v8.0.0-20200210103600-aff00e5adfde h1:Y9S
github.com/elastic/go-elasticsearch/v8 v8.0.0-20200210103600-aff00e5adfde/go.mod h1:xe9a/L2aeOgFKKgrO3ibQTnMdpAeL0GC+5/HpGScSa4= github.com/elastic/go-elasticsearch/v8 v8.0.0-20200210103600-aff00e5adfde/go.mod h1:xe9a/L2aeOgFKKgrO3ibQTnMdpAeL0GC+5/HpGScSa4=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo= github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/jgroeneveld/schema v1.0.0 h1:J0E10CrOkiSEsw6dfb1IfrDJD14pf6QLVJ3tRPl/syI=
github.com/jgroeneveld/schema v1.0.0/go.mod h1:M14lv7sNMtGvo3ops1MwslaSYgDYxrSmbzWIQ0Mr5rs=
github.com/jgroeneveld/trial v2.0.0+incompatible h1:d59ctdgor+VqdZCAiUfVN8K13s0ALDioG5DWwZNtRuQ=
github.com/jgroeneveld/trial v2.0.0+incompatible/go.mod h1:I6INLW96EN8WysNBXUFI3M4RIC8ePg9ntAc/Wy+U/+M=

View File

@ -22,12 +22,16 @@ func (req *QueryRequest) Map() map[string]interface{} {
} }
} }
func (req *QueryRequest) MarshalJSON() ([]byte, error) {
return json.Marshal(req.Map())
}
func (req *QueryRequest) Run( func (req *QueryRequest) Run(
api *elasticsearch.Client, api *elasticsearch.Client,
o ...func(*esapi.SearchRequest), o ...func(*esapi.SearchRequest),
) (res *esapi.Response, err error) { ) (res *esapi.Response, err error) {
var b bytes.Buffer var b bytes.Buffer
err = json.NewEncoder(&b).Encode(req.Query.Map()) err = json.NewEncoder(&b).Encode(req.Map())
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -4,7 +4,7 @@ import (
"testing" "testing"
) )
func TestQueries(t *testing.T) { func TestQueryMaps(t *testing.T) {
runMapTests(t, []mapTest{ runMapTests(t, []mapTest{
{ {
"a simple match_all query", "a simple match_all query",
@ -66,3 +66,17 @@ func TestQueries(t *testing.T) {
}, },
}) })
} }
func TestQueryJSONs(t *testing.T) {
runJSONTests(t, []jsonTest{
{
"simple query",
Query(
Bool().
Must(Term("account_id", "bla")),
),
`{"query":{"bool":{"must":[{"term":{"account_id":{"value":"bla"}}}]}}}`,
nil,
},
})
}