-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Collect Step * Update description * Correct path construct by collect * Test collect and make it work like properties
- Loading branch information
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package steps | ||
|
||
import ( | ||
"github.com/cayleygraph/cayley/graph" | ||
"github.com/cayleygraph/cayley/query" | ||
"github.com/cayleygraph/cayley/query/linkedql" | ||
"github.com/cayleygraph/cayley/query/path" | ||
"github.com/cayleygraph/quad" | ||
"github.com/cayleygraph/quad/voc" | ||
) | ||
|
||
func init() { | ||
linkedql.Register(&Collect{}) | ||
} | ||
|
||
var _ linkedql.IteratorStep = (*Collect)(nil) | ||
var _ linkedql.PathStep = (*Collect)(nil) | ||
|
||
// Collect corresponds to .view(). | ||
type Collect struct { | ||
From linkedql.PathStep `json:"from"` | ||
Name quad.IRI `json:"name"` | ||
} | ||
|
||
// Description implements Step. | ||
func (s *Collect) Description() string { | ||
return "Recursively resolves values of a list (also known as RDF collection)" | ||
} | ||
|
||
// BuildIterator implements linkedql.IteratorStep. | ||
func (s *Collect) BuildIterator(qs graph.QuadStore, ns *voc.Namespaces) (query.Iterator, error) { | ||
return linkedql.NewValueIteratorFromPathStep(s, qs, ns) | ||
} | ||
|
||
var ( | ||
first = quad.IRI("rdf:first").Full() | ||
rest = quad.IRI("rdf:rest").Full() | ||
rdfNil = quad.IRI("rdf:nil").Full() | ||
) | ||
|
||
// BuildPath implements linkedql.PathStep. | ||
func (s *Collect) BuildPath(qs graph.QuadStore, ns *voc.Namespaces) (*path.Path, error) { | ||
fromPath, err := s.From.BuildPath(qs, ns) | ||
if err != nil { | ||
return nil, err | ||
} | ||
p := fromPath. | ||
Out(s.Name). | ||
Save(first, string(first)). | ||
Save(rest, string(rest)). | ||
Or( | ||
fromPath.Out(s.Name).FollowRecursive(rest, -1, nil). | ||
Save(first, string(first)). | ||
Save(rest, string(rest)), | ||
). | ||
Or(fromPath.Save(s.Name, string(s.Name))) | ||
return p, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"data": { | ||
"http://example.com/friends": { | ||
"@list": [ | ||
{ "@id": "http://example.com/alice" }, | ||
{ "@id": "http://example.com/bob" } | ||
] | ||
} | ||
}, | ||
"query": { | ||
"@context": { | ||
"linkedql": "http://cayley.io/linkedql" | ||
}, | ||
"@type": "linkedql:Documents", | ||
"linkedql:from": { | ||
"@type": "linkedql:Collect", | ||
"linkedql:name": "http://example.com/friends", | ||
"linkedql:from": { | ||
"@type": "linkedql:Vertex", | ||
"linkedql:values": [] | ||
} | ||
} | ||
}, | ||
"results": [ | ||
{ | ||
"http://example.com/friends": { | ||
"@list": [ | ||
{ "@id": "http://example.com/alice" }, | ||
{ "@id": "http://example.com/bob" } | ||
] | ||
} | ||
} | ||
] | ||
} |