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

LinkedQL: Project Step #929

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
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
40 changes: 21 additions & 19 deletions query/linkedql/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,34 +187,36 @@ func Unmarshal(data []byte) (RegistryItem, error) {
}
fv.Set(reflect.ValueOf(s))
case reflect.Slice:
el := f.Type.Elem()
if el.Kind() != reflect.Interface {
err := json.Unmarshal(v, fv.Addr().Interface())
if err != nil {
var arr []json.RawMessage
err := json.Unmarshal(v, &arr)
if err != nil {
var i json.RawMessage
iErr := json.Unmarshal(v, &i)
if iErr != nil {
return nil, err
}
} else {
var arr []json.RawMessage
err := json.Unmarshal(v, &arr)
if err != nil {
var i json.RawMessage
iErr := json.Unmarshal(v, &i)
if iErr != nil {
return nil, err
}
arr = []json.RawMessage{i}
}
if arr != nil {
va := reflect.MakeSlice(f.Type, len(arr), len(arr))
for i, v := range arr {
arr = []json.RawMessage{i}
}
if arr != nil {
el := f.Type.Elem()
va := reflect.MakeSlice(f.Type, len(arr), len(arr))
for i, v := range arr {
if el.Kind() != reflect.Interface {
new := reflect.New(el)
err := json.Unmarshal(v, new.Interface())
if err != nil {
return nil, err
}
va.Index(i).Set(new.Elem())
} else {
s, err := Unmarshal(v)
if err != nil {
return nil, err
}
va.Index(i).Set(reflect.ValueOf(s))
}
fv.Set(va)
}
fv.Set(va)
}
default:
err := json.Unmarshal(v, fv.Addr().Interface())
Expand Down
34 changes: 0 additions & 34 deletions query/linkedql/steps/as.go

This file was deleted.

47 changes: 47 additions & 0 deletions query/linkedql/steps/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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(&Project{})
}

var _ linkedql.IteratorStep = (*Project)(nil)
var _ linkedql.PathStep = (*Project)(nil)

// Project corresponds to .project().
type Project struct {
From linkedql.PathStep `json:"from"`
Name quad.IRI `json:"name"`
Step linkedql.PathStep `json:"step"`
}

// Description implements Step.
func (s *Project) Description() string {
return "Sets the result of step to name"
}

// BuildIterator implements linkedql.IteratorStep.
func (s *Project) BuildIterator(qs graph.QuadStore, ns *voc.Namespaces) (query.Iterator, error) {
return linkedql.NewValueIteratorFromPathStep(s, qs, ns)
}

// BuildPath implements linkedql.PathStep.
func (s *Project) BuildPath(qs graph.QuadStore, ns *voc.Namespaces) (*path.Path, error) {
fromPath, err := s.From.BuildPath(qs, ns)
if err != nil {
return nil, err
}
step, err := s.Step.BuildPath(qs, ns)
if err != nil {
return nil, err
}
return fromPath.Follow(step).Tag(string(s.Name)).And(fromPath), nil
}
34 changes: 0 additions & 34 deletions query/linkedql/steps/property_names_as.go

This file was deleted.

34 changes: 0 additions & 34 deletions query/linkedql/steps/reverse_property_names_as.go

This file was deleted.

3 changes: 3 additions & 0 deletions query/linkedql/steps/steps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ func TestLinkedQL(t *testing.T) {
for iterator.Next(ctx) {
results = append(results, iterator.Result())
}
if testName == "project" {
fmt.Printf("Results: %#v\n", results)
}
require.NoError(t, iterator.Err())
require.Equal(t, nil, isomorphic(c.Results, results))
})
Expand Down
29 changes: 14 additions & 15 deletions query/linkedql/steps/test-cases/Select.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
"data": [
{
"@id": "http://example.com/alice",
"http://example.com/likes": [
{
"@id": "http://example.com/bob"
}
]
"http://example.com/likes": {
"@id": "http://example.com/bob"
},
"http://example.com/name": "Alice"
}
],
"query": {
Expand All @@ -15,21 +14,21 @@
},
"@type": "Select",
"from": {
"@type": "Visit",
"@type": "Properties",
"names": ["http://example.com/likes", "http://example.com/name"],
"from": {
"@type": "As",
"from": { "@type": "Match", "pattern": {} },
"name": "http://example.com/liker"
},
"properties": "http://example.com/likes"
"@type": "Match",
"pattern": {}
}
},
"tags": []
"properties": []
},
"results": [
{
"http://example.com/liker": {
"@id": "http://example.com/alice"
}
"http://example.com/likes": {
"@id": "http://example.com/bob"
},
"http://example.com/name": "Alice"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,30 @@
{
"@id": "http://example.com/bob"
}
]
],
"http://example.com/name": "Alice"
}
],
"query": {
"@context": {
"@vocab": "http://cayley.io/linkedql#"
},
"@type": "Select",
"from": {
"@type": "ReversePropertyNamesAs",
"from": { "@type": "Match", "pattern": {} },
"tag": "property"
"@type": "Project",
"name": "http://example.com/total",
"step": {
"@type": "Count",
"from": {
"@type": "Placeholder"
}
},
"tags": []
"from": {
"@type": "Match",
"pattern": {}
}
},
"results": [
{
"property": {
"@id": "http://example.com/likes"
}
"http://example.com/total": 7
}
]
}
31 changes: 0 additions & 31 deletions query/linkedql/steps/test-cases/property-names-as.json

This file was deleted.

6 changes: 5 additions & 1 deletion query/linkedql/steps/test-cases/property-names.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
{
"@id": "http://example.com/bob"
}
]
],
"http://example.com/name": "Alice"
}
],
"query": {
Expand All @@ -19,6 +20,9 @@
"results": [
{
"@id": "http://example.com/likes"
},
{
"@id": "http://example.com/name"
}
]
}
33 changes: 33 additions & 0 deletions query/linkedql/steps/test-cases/select-with-properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"data": [
{
"@id": "http://example.com/alice",
"http://example.com/likes": {
"@id": "http://example.com/bob"
},
"http://example.com/name": "Alice"
}
],
"query": {
"@context": {
"@vocab": "http://cayley.io/linkedql#"
},
"@type": "Select",
"from": {
"@type": "Properties",
"names": ["http://example.com/likes", "http://example.com/name"],
"from": {
"@type": "Match",
"pattern": {}
}
},
"properties": ["http://example.com/likes"]
},
"results": [
{
"http://example.com/likes": {
"@id": "http://example.com/bob"
}
}
]
}
Loading