Merge branch 'melbania-fix-json-nearby-fields'

This commit is contained in:
tidwall 2019-05-28 12:36:12 -07:00
commit f01389d181
2 changed files with 48 additions and 8 deletions

View File

@ -355,13 +355,10 @@ func (sw *scanWriter) writeObject(opts ScanWriterParams) bool {
sw.mu.Lock()
defer sw.mu.Unlock()
}
var fieldVals []float64
var ok bool
keepGoing := true
if opts.skipTesting {
fieldVals = sw.fvals
} else {
ok, keepGoing, fieldVals = sw.testObject(opts.id, opts.o, opts.fields, opts.ignoreGlobMatch)
if !opts.skipTesting {
ok, keepGoing, _ = sw.testObject(opts.id, opts.o, opts.fields, opts.ignoreGlobMatch)
if !ok {
return keepGoing
}
@ -403,11 +400,15 @@ func (sw *scanWriter) writeObject(opts ScanWriterParams) bool {
} else if len(sw.farr) > 0 {
jsfields = `,"fields":[`
for i, field := range fieldVals {
for i := range sw.farr {
if i > 0 {
jsfields += ","
jsfields += `,`
}
if len(opts.fields) > i {
jsfields += strconv.FormatFloat(opts.fields[i], 'f', -1, 64)
} else {
jsfields += "0"
}
jsfields += strconv.FormatFloat(field, 'f', -1, 64)
}
jsfields += `]`
}

View File

@ -18,6 +18,7 @@ func subTestSearch(t *testing.T, mc *mockServer) {
runStep(t, mc, "SCAN_CURSOR", keys_SCAN_CURSOR_test)
runStep(t, mc, "SEARCH_CURSOR", keys_SEARCH_CURSOR_test)
runStep(t, mc, "MATCH", keys_MATCH_test)
runStep(t, mc, "FIELDS", keys_FIELDS_search_test)
}
func keys_KNN_test(mc *mockServer) error {
@ -355,6 +356,44 @@ func keys_MATCH_test(mc *mockServer) error {
})
}
func keys_FIELDS_search_test(mc *mockServer) error {
return mc.DoBatch([][]interface{}{
{"SET", "mykey", "1", "FIELD", "field1", 10, "FIELD", "field2", 11 /* field3 undefined */, "OBJECT", `{"type":"Point","coordinates":[-112.2791,33.5220]}`}, {"OK"},
{"SET", "mykey", "2", "FIELD", "field1", 20, "FIELD", "field2", 10 /* field3 undefined */, "OBJECT", `{"type":"Point","coordinates":[-112.2793,33.5222]}`}, {"OK"},
{"SET", "mykey", "3", "FIELD", "field1", 30, "FIELD", "field2", 13 /* field3 undefined */, "OBJECT", `{"type":"Point","coordinates":[-112.2795,33.5224]}`}, {"OK"},
{"SET", "mykey", "4", "FIELD", "field1", 40, "FIELD", "field2", 14 /* field3 undefined */, "OBJECT", `{"type":"Point","coordinates":[-112.2797,33.5226]}`}, {"OK"},
{"SET", "mykey", "5" /* field1 undefined */, "FIELD", "field2", 15, "FIELD", "field3", 28, "OBJECT", `{"type":"Point","coordinates":[-112.2799,33.5228]}`}, {"OK"},
{"SET", "mykey", "6" /* field1 & field2 undefined */, "FIELD", "field3", 29, "OBJECT", `{"type":"Point","coordinates":[-112.2801,33.5230]}`}, {"OK"},
{"SET", "mykey", "7" /* field1, field2, & field3 undefined */, "OBJECT", `{"type":"Point","coordinates":[-112.2803,33.5232]}`}, {"OK"},
{"OUTPUT", "json"}, {`{"ok":true}`},
{"NEARBY", "mykey", "WHERE", "field2", 11, "+inf", "POINT", 33.462, -112.268, 60000}, {
`{"ok":true,"fields":["field1","field2","field3"],"objects":[` +
`{"id":"1","object":{"type":"Point","coordinates":[-112.2791,33.522]},"fields":[10,11,0]},` +
`{"id":"3","object":{"type":"Point","coordinates":[-112.2795,33.5224]},"fields":[30,13,0]},` +
`{"id":"4","object":{"type":"Point","coordinates":[-112.2797,33.5226]},"fields":[40,14,0]},` +
`{"id":"5","object":{"type":"Point","coordinates":[-112.2799,33.5228]},"fields":[0,15,28]}` +
`],"count":4,"cursor":0}`},
{"NEARBY", "mykey", "WHERE", "field2", 0, 2, "POINT", 33.462, -112.268, 60000}, {
`{"ok":true,"fields":["field1","field2","field3"],"objects":[` +
`{"id":"6","object":{"type":"Point","coordinates":[-112.2801,33.523]},"fields":[0,0,29]},` +
`{"id":"7","object":{"type":"Point","coordinates":[-112.2803,33.5232]},"fields":[0,0,0]}` +
`],"count":2,"cursor":0}`},
{"WITHIN", "mykey", "WHERE", "field2", 11, "+inf", "CIRCLE", 33.462, -112.268, 60000}, {
`{"ok":true,"fields":["field1","field2","field3"],"objects":[` +
`{"id":"1","object":{"type":"Point","coordinates":[-112.2791,33.522]},"fields":[10,11,0]},` +
`{"id":"3","object":{"type":"Point","coordinates":[-112.2795,33.5224]},"fields":[30,13,0]},` +
`{"id":"4","object":{"type":"Point","coordinates":[-112.2797,33.5226]},"fields":[40,14,0]},` +
`{"id":"5","object":{"type":"Point","coordinates":[-112.2799,33.5228]},"fields":[0,15,28]}` +
`],"count":4,"cursor":0}`},
{"WITHIN", "mykey", "WHERE", "field2", 0, 2, "CIRCLE", 33.462, -112.268, 60000}, {
`{"ok":true,"fields":["field1","field2","field3"],"objects":[` +
`{"id":"6","object":{"type":"Point","coordinates":[-112.2801,33.523]},"fields":[0,0,29]},` +
`{"id":"7","object":{"type":"Point","coordinates":[-112.2803,33.5232]},"fields":[0,0,0]}` +
`],"count":2,"cursor":0}`},
})
}
// match sorts the response and compares to the expected input
func match(expectIn string) func(org, v interface{}) (resp, expect interface{}) {
return func(v, org interface{}) (resp, expect interface{}) {