Skip to content

Commit

Permalink
gizmo: expose SaveOptional; resolves cayleygraph#702
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Apr 14, 2018
1 parent 4c75257 commit 19932b7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
10 changes: 10 additions & 0 deletions docs/GizmoAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,16 @@ g.V("<bob>").SaveInPredicates("pred").All()
```


### `path.SaveOpt(*)`

SaveOpt is the same as Save, but returns empty tags if predicate does not exists.


### `path.SaveOptR(*)`

SaveOptR is the same as SaveOpt, but tags values via reverse predicate.


### `path.SaveOutPredicates(tag)`

SaveOutPredicates tags the list of predicates that are pointing out from a node.
Expand Down
8 changes: 8 additions & 0 deletions query/gizmo/gizmo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,14 @@ var testQueries = []struct {
tag: "somecool",
expect: []string{"cool_person", "cool_person", "cool_person", "smart_person", "smart_person"},
},
{
message: "show a simple save optional",
query: `
g.V("<bob>","<charlie>").Out("<follows>").SaveOpt("<status>", "somecool").All()
`,
tag: "somecool",
expect: []string{"cool_person", "cool_person"},
},
{
message: "show a simple saveR",
query: `
Expand Down
30 changes: 24 additions & 6 deletions query/gizmo/traversals.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ func (p *pathObject) has(call goja.FunctionCall, rev bool) goja.Value {
}
return p.newVal(np)
}
func (p *pathObject) save(call goja.FunctionCall, rev bool) goja.Value {
func (p *pathObject) save(call goja.FunctionCall, rev, opt bool) goja.Value {
args := exportArgs(call.Arguments)
if len(args) > 2 || len(args) == 0 {
return throwErr(p.s.vm, errArgCount{Got: len(args)})
Expand All @@ -461,10 +461,18 @@ func (p *pathObject) save(call goja.FunctionCall, rev bool) goja.Value {
}
}
np := p.clonePath()
if rev {
np = np.SaveReverse(via, tag)
if opt {
if rev {
np = np.SaveOptionalReverse(via, tag)
} else {
np = np.SaveOptional(via, tag)
}
} else {
np = np.Save(via, tag)
if rev {
np = np.SaveReverse(via, tag)
} else {
np = np.Save(via, tag)
}
}
return p.newVal(np)
}
Expand All @@ -486,12 +494,22 @@ func (p *pathObject) save(call goja.FunctionCall, rev bool) goja.Value {
// // {"id" : "<dani>", "target": "<greg>" }
// g.V("<dani>", "<bob>").Save("<follows>", "target").All()
func (p *pathObject) Save(call goja.FunctionCall) goja.Value {
return p.save(call, false)
return p.save(call, false, false)
}

// SaveR is the same as Save, but tags values via reverse predicate.
func (p *pathObject) SaveR(call goja.FunctionCall) goja.Value {
return p.save(call, true)
return p.save(call, true, false)
}

// SaveOpt is the same as Save, but returns empty tags if predicate does not exists.
func (p *pathObject) SaveOpt(call goja.FunctionCall) goja.Value {
return p.save(call, false, true)
}

// SaveOptR is the same as SaveOpt, but tags values via reverse predicate.
func (p *pathObject) SaveOptR(call goja.FunctionCall) goja.Value {
return p.save(call, true, true)
}

// Except removes all paths which match query from current path.
Expand Down

0 comments on commit 19932b7

Please sign in to comment.