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: expose SaveOptional #706

Merged
merged 1 commit into from
Apr 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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