diff --git a/query/query.go b/query/query.go index f1b0d97078cca9b3cfd77f892feaae01fa6b642d..d1470131662bdfeba8eb7bb43e537031cc9a0e21 100644 --- a/query/query.go +++ b/query/query.go @@ -247,15 +247,19 @@ func (g *SubGraph) ToJson(l *Latency) (js []byte, rerr error) { log.Fatal("We don't currently support more than 1 uid at root.") } - ival := r[0] - var m map[string]interface{} - if ival != nil { - m = ival.(map[string]interface{}) - } else { - m = make(map[string]interface{}) + // r is a map, and we don't know it's key. So iterate over it, even though it only has 1 result. + for _, ival := range r { + var m map[string]interface{} + if ival != nil { + m = ival.(map[string]interface{}) + } else { + m = make(map[string]interface{}) + } + m["server_latency"] = l.ToMap() + return json.Marshal(m) } - m["server_latency"] = l.ToMap() - return json.Marshal(m) + log.Fatal("Runtime should never reach here.") + return []byte(""), fmt.Errorf("Runtime should never reach here.") } // This function performs a binary search on the uids slice and returns the diff --git a/query/query_test.go b/query/query_test.go index 2e3474d1abf16a469800770be97d53652170788c..23b12604194097b28e2b90523e3a88d5f569e384 100644 --- a/query/query_test.go +++ b/query/query_test.go @@ -19,9 +19,9 @@ package query import ( "bytes" "encoding/gob" - "fmt" "io/ioutil" "os" + "strings" "testing" "time" @@ -324,7 +324,10 @@ func TestToJson(t *testing.T) { if err != nil { t.Error(err) } - fmt.Printf(string(js)) + s := string(js) + if !strings.Contains(s, "Michonne") { + t.Errorf("Unable to find Michonne in this result: %v", s) + } } func getProperty(properties []*graph.Property, prop string) []byte {