Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gizmo: make methods lower case #822

Merged
merged 14 commits into from
Sep 22, 2019
60 changes: 30 additions & 30 deletions graph/graphtest/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ var queries = []struct {
{
message: "name predicate",
query: `
g.V("Humphrey Bogart").In("<name>").All()
g.v("Humphrey Bogart").in("<name>").all()
`,
expect: []interface{}{
map[string]string{"id": "</en/humphrey_bogart>"},
Expand All @@ -105,11 +105,11 @@ var queries = []struct {
{
message: "two large sets with no intersection",
query: `
function getId(x) { return g.V(x).In("<name>") }
var actor_to_film = g.M().In("</film/performance/actor>").In("</film/film/starring>")
function getId(x) { return g.v(x).in("<name>") }
var actor_to_film = g.m().in("</film/performance/actor>").in("</film/film/starring>")

getId("Oliver Hardy").Follow(actor_to_film).Out("<name>").Intersect(
getId("Mel Blanc").Follow(actor_to_film).Out("<name>")).All()
getId("Oliver Hardy").follow(actor_to_film).out("<name>").intersect(
getId("Mel Blanc").follow(actor_to_film).out("<name>")).all()
`,
expect: nil,
},
Expand All @@ -119,19 +119,19 @@ var queries = []struct {
message: "three huge sets with small intersection",
long: true,
query: `
function getId(x) { return g.V(x).In("<name>") }
var actor_to_film = g.M().In("</film/performance/actor>").In("</film/film/starring>")
function getId(x) { return g.v(x).in("<name>") }
var actor_to_film = g.m().in("</film/performance/actor>").in("</film/film/starring>")

var a = getId("Oliver Hardy").Follow(actor_to_film).FollowR(actor_to_film)
var b = getId("Mel Blanc").Follow(actor_to_film).FollowR(actor_to_film)
var c = getId("Billy Gilbert").Follow(actor_to_film).FollowR(actor_to_film)
var a = getId("Oliver Hardy").follow(actor_to_film).followR(actor_to_film)
var b = getId("Mel Blanc").follow(actor_to_film).followR(actor_to_film)
var c = getId("Billy Gilbert").follow(actor_to_film).followR(actor_to_film)

seen = {}

a.Intersect(b).Intersect(c).ForEach(function (d) {
a.intersect(b).intersect(c).forEach(function (d) {
if (!(d.id in seen)) {
seen[d.id] = true;
g.Emit(d)
g.emit(d)
}
})
`,
Expand All @@ -148,7 +148,7 @@ var queries = []struct {
message: "the helpless checker",
long: true,
query: `
g.V().As("person").In("<name>").In().In().Out("<name>").Is("Casablanca").All()
g.v().as("person").in("<name>").in().in().out("<name>").is("Casablanca").all()
`,
tag: "person",
expect: []interface{}{
Expand All @@ -175,7 +175,7 @@ var queries = []struct {
message: "the helpless checker, negated (films without Ingrid Bergman)",
long: true,
query: `
g.V().As("person").In("<name>").In().In().Out("<name>").Except(g.V("Ingrid Bergman").In("<name>").In().In().Out("<name>")).Is("Casablanca").All()
g.v().as("person").in("<name>").in().in().out("<name>").except(g.v("Ingrid Bergman").in("<name>").in().in().out("<name>")).is("Casablanca").all()
`,
tag: "person",
expect: nil,
Expand All @@ -184,7 +184,7 @@ var queries = []struct {
message: "the helpless checker, negated (without actors Ingrid Bergman)",
long: true,
query: `
g.V().As("person").In("<name>").Except(g.V("Ingrid Bergman").In("<name>")).In().In().Out("<name>").Is("Casablanca").All()
g.v().as("person").in("<name>").except(g.v("Ingrid Bergman").in("<name>")).in().in().out("<name>").is("Casablanca").all()
`,
tag: "person",
expect: []interface{}{
Expand All @@ -209,7 +209,7 @@ var queries = []struct {
//A: "Sandra Bullock"
{
message: "Net and Speed",
query: common + `m1_actors.Intersect(m2_actors).Out("<name>").All()
query: common + `m1_actors.intersect(m2_actors).out("<name>").all()
`,
expect: []interface{}{
map[string]string{"id": SandraB, "movie1": "The Net", "movie2": nSpeed},
Expand All @@ -220,7 +220,7 @@ var queries = []struct {
//A: No
{
message: "Keanu in The Net",
query: common + `actor2.Intersect(m1_actors).Out("<name>").All()
query: common + `actor2.intersect(m1_actors).out("<name>").all()
`,
expect: nil,
},
Expand All @@ -229,7 +229,7 @@ var queries = []struct {
//A: Yes
{
message: "Keanu in Speed",
query: common + `actor2.Intersect(m2_actors).Out("<name>").All()
query: common + `actor2.intersect(m2_actors).out("<name>").all()
`,
expect: []interface{}{
map[string]string{"id": KeanuR, "movie2": nSpeed},
Expand All @@ -242,7 +242,7 @@ var queries = []struct {
{
message: "Keanu with other in The Net",
long: true,
query: common + `actor2.Follow(coStars1).Intersect(m1_actors).Out("<name>").All()
query: common + `actor2.follow(coStars1).intersect(m1_actors).out("<name>").all()
`,
expect: []interface{}{
map[string]string{"id": SandraB, "movie1": "The Net", "costar1_movie": nSpeed},
Expand All @@ -256,7 +256,7 @@ var queries = []struct {
{
message: "Keanu and Bullock with other",
long: true,
query: common + `actor1.Save("<name>","costar1_actor").Follow(coStars1).Intersect(actor2.Save("<name>","costar2_actor").Follow(coStars2)).Out("<name>").All()
query: common + `actor1.save("<name>","costar1_actor").follow(coStars1).intersect(actor2.save("<name>","costar2_actor").follow(coStars2)).out("<name>").all()
`,
expect: []interface{}{
costarTag(SandraB, SandraB, "The Proposal", KeanuR, nSpeed),
Expand Down Expand Up @@ -430,7 +430,7 @@ var queries = []struct {
{
message: "Save a number of predicates around a set of nodes",
query: `
g.V("_:9037", "_:49278", "_:44112", "_:44709", "_:43382").Save("</film/performance/character>", "char").Save("</film/performance/actor>", "act").SaveR("</film/film/starring>", "film").All()
g.v("_:9037", "_:49278", "_:44112", "_:44709", "_:43382").save("</film/performance/character>", "char").save("</film/performance/actor>", "act").saveR("</film/film/starring>", "film").all()
`,
expect: []interface{}{
map[string]string{"act": "</en/humphrey_bogart>", "char": "Rick Blaine", "film": "</en/casablanca_1942>", "id": "_:9037"},
Expand All @@ -443,21 +443,21 @@ var queries = []struct {
}

const common = `
var movie1 = g.V().Has("<name>", "The Net")
var movie2 = g.V().Has("<name>", "Speed")
var actor1 = g.V().Has("<name>", "Sandra Bullock")
var actor2 = g.V().Has("<name>", "Keanu Reeves")
var movie1 = g.v().has("<name>", "The Net")
var movie2 = g.v().has("<name>", "Speed")
var actor1 = g.v().has("<name>", "Sandra Bullock")
var actor2 = g.v().has("<name>", "Keanu Reeves")

// (film) -> starring -> (actor)
var filmToActor = g.Morphism().Out("</film/film/starring>").Out("</film/performance/actor>")
var filmToActor = g.morphism().out("</film/film/starring>").out("</film/performance/actor>")

// (actor) -> starring -> [film -> starring -> (actor)]
var coStars1 = g.Morphism().In("</film/performance/actor>").In("</film/film/starring>").Save("<name>","costar1_movie").Follow(filmToActor)
var coStars2 = g.Morphism().In("</film/performance/actor>").In("</film/film/starring>").Save("<name>","costar2_movie").Follow(filmToActor)
var coStars1 = g.morphism().in("</film/performance/actor>").in("</film/film/starring>").save("<name>","costar1_movie").follow(filmToActor)
var coStars2 = g.morphism().in("</film/performance/actor>").in("</film/film/starring>").save("<name>","costar2_movie").follow(filmToActor)

// Stars for the movies "The Net" and "Speed"
var m1_actors = movie1.Save("<name>","movie1").Follow(filmToActor)
var m2_actors = movie2.Save("<name>","movie2").Follow(filmToActor)
var m1_actors = movie1.save("<name>","movie1").follow(filmToActor)
var m2_actors = movie2.save("<name>","movie2").follow(filmToActor)
`

func prepare(t testing.TB, gen testutil.DatabaseFunc) (graph.QuadStore, func()) {
Expand Down
20 changes: 20 additions & 0 deletions query/gizmo/gizmo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"context"
"fmt"
"sort"
"unicode"
"reflect"

"github.com/dop251/goja"

Expand Down Expand Up @@ -59,6 +61,23 @@ func NewSession(qs graph.QuadStore) *Session {
return s
}

func lcFirst(str string) string {
for i, v := range str {
iddan marked this conversation as resolved.
Show resolved Hide resolved
return string(unicode.ToLower(v)) + str[i+1:]
}
return ""
}

type fieldNameMapperToLower struct{}

func (fieldNameMapperToLower) FieldName(t reflect.Type, f reflect.StructField) string {
return lcFirst(f.Name)
}

func (fieldNameMapperToLower) MethodName(t reflect.Type, m reflect.Method) string {
return lcFirst(m.Name)
}

type Session struct {
qs graph.QuadStore
vm *goja.Runtime
Expand Down Expand Up @@ -88,6 +107,7 @@ func (s *Session) buildEnv() error {
return nil
}
s.vm = goja.New()
s.vm.SetFieldNameMapper(fieldNameMapperToLower{})
s.vm.Set("graph", &graphObject{s: s})
s.vm.Set("g", s.vm.Get("graph"))
for name, val := range defaultEnv {
Expand Down
Loading