From 5484d7eb3589233c18d17ab22d4dbcfbe62d8665 Mon Sep 17 00:00:00 2001 From: kortschak Date: Tue, 23 Sep 2014 08:56:14 +0930 Subject: [PATCH 1/2] Type switch on otto.Value Ugh, this is not documented in otto. Leaving a panic for future cases where dynamic typing will jump out at us. Fixes issue #160. --- query/gremlin/session.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/query/gremlin/session.go b/query/gremlin/session.go index 887027e..16e54de 100644 --- a/query/gremlin/session.go +++ b/query/gremlin/session.go @@ -191,9 +191,17 @@ func (s *Session) ToText(result interface{}) string { } else { if data.val.IsObject() { export, _ := data.val.Export() - mapExport := export.(map[string]string) - for k, v := range mapExport { - out += fmt.Sprintf("%s : %v\n", k, v) + switch export := export.(type) { + case map[string]string: + for k, v := range export { + out += fmt.Sprintf("%s : %s\n", k, v) + } + case map[string]interface{}: + for k, v := range export { + out += fmt.Sprintf("%s : %v\n", k, v) + } + default: + panic(fmt.Sprintf("unexpected type: %T", export)) } } else { strVersion, _ := data.val.ToString() From d6191ba46840b5fbfd0f5f086ef0a72b76f714b1 Mon Sep 17 00:00:00 2001 From: kortschak Date: Tue, 23 Sep 2014 09:17:44 +0930 Subject: [PATCH 2/2] Add test for issue 160 --- query/gremlin/gremlin_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/query/gremlin/gremlin_test.go b/query/gremlin/gremlin_test.go index f96af65..4a22f24 100644 --- a/query/gremlin/gremlin_test.go +++ b/query/gremlin/gremlin_test.go @@ -280,3 +280,44 @@ func TestGremlin(t *testing.T) { } } } + +var issue160TestGraph = []quad.Quad{ + {"alice", "follows", "bob", ""}, + {"bob", "follows", "alice", ""}, + {"charlie", "follows", "bob", ""}, + {"dani", "follows", "charlie", ""}, + {"dani", "follows", "alice", ""}, + {"alice", "is", "cool", ""}, + {"bob", "is", "not cool", ""}, + {"charlie", "is", "cool", ""}, + {"danie", "is", "not cool", ""}, +} + +func TestIssue160(t *testing.T) { + query := `g.V().Tag('query').Out('follows').Out('follows').ForEach(function (item) { if (item.id !== item.query) g.Emit({ id: item.id }); })` + expect := []string{ + "****\nid : alice\n", + "****\nid : bob\n", + "****\nid : bob\n", + "=> \n", + } + + ses := makeTestSession(issue160TestGraph) + c := make(chan interface{}, 5) + go ses.ExecInput(query, c, 100) + var got []string + for res := range c { + func() { + defer func() { + if r := recover(); r != nil { + t.Errorf("Unexpected panic: %v", r) + } + }() + got = append(got, ses.ToText(res)) + }() + } + sort.Strings(got) + if !reflect.DeepEqual(got, expect) { + t.Errorf("Unexpected result, got: %q expected: %q", got, expect) + } +}