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

Support loading object with loops #746

Merged
merged 2 commits into from
Oct 20, 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
525 changes: 525 additions & 0 deletions schema/loader.go

Large diffs are not rendered by default.

360 changes: 360 additions & 0 deletions schema/loader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,360 @@
package schema_test

import (
"reflect"
"testing"

"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/graph/iterator"
"github.com/cayleygraph/cayley/graph/memstore"
"github.com/cayleygraph/cayley/quad"
"github.com/cayleygraph/cayley/schema"
)

func TestLoadLoop(t *testing.T) {
sch := schema.NewConfig()

a := &NodeLoop{ID: iri("A"), Name: "Node A"}
a.Next = a

qs := memstore.New([]quad.Quad{
{a.ID, iri("name"), quad.String(a.Name), nil},
{a.ID, iri("next"), a.ID, nil},
}...)

b := &NodeLoop{}
if err := sch.LoadIteratorTo(nil, qs, reflect.ValueOf(b), nil); err != nil {
t.Error(err)
return
}
if a.ID != b.ID || a.Name != b.Name {
t.Fatalf("%#v vs %#v", a, b)
}
if b != b.Next {
t.Fatalf("loop is broken: %p vs %p", b, b.Next)
}

a = &NodeLoop{ID: iri("A"), Name: "Node A"}
b = &NodeLoop{ID: iri("B"), Name: "Node B"}
c := &NodeLoop{ID: iri("C"), Name: "Node C"}
a.Next = b
b.Next = c
c.Next = a

qs = memstore.New([]quad.Quad{
{a.ID, iri("name"), quad.String(a.Name), nil},
{b.ID, iri("name"), quad.String(b.Name), nil},
{c.ID, iri("name"), quad.String(c.Name), nil},
{a.ID, iri("next"), b.ID, nil},
{b.ID, iri("next"), c.ID, nil},
{c.ID, iri("next"), a.ID, nil},
}...)

a1 := &NodeLoop{}
if err := sch.LoadIteratorTo(nil, qs, reflect.ValueOf(a1), nil); err != nil {
t.Error(err)
return
}
if a.ID != a1.ID || a.Name != a1.Name {
t.Fatalf("%#v vs %#v", a, b)
}
b1 := a1.Next
c1 := b1.Next
if b.ID != b1.ID || b.Name != b1.Name {
t.Fatalf("%#v vs %#v", a, b)
}
if c.ID != c1.ID || c.Name != c1.Name {
t.Fatalf("%#v vs %#v", a, b)
}
if a1 != c1.Next {
t.Fatalf("loop is broken: %p vs %p", a1, c1.Next)
}
}

func TestLoadIteratorTo(t *testing.T) {
sch := schema.NewConfig()
for i, c := range testFillValueCases {
t.Run(c.name, func(t *testing.T) {
qs := memstore.New(c.quads...)
rt := reflect.TypeOf(c.expect)
var out reflect.Value
if rt.Kind() == reflect.Ptr {
out = reflect.New(rt.Elem())
} else {
out = reflect.New(rt)
}
var it graph.Iterator
if c.from != nil {
fixed := iterator.NewFixed()
for _, id := range c.from {
fixed.Add(qs.ValueOf(id))
}
it = fixed
}
depth := c.depth
if depth == 0 {
depth = -1
}
if err := sch.LoadIteratorToDepth(nil, qs, out, depth, it); err != nil {
t.Errorf("case %d failed: %v", i+1, err)
return
}
var got interface{}
if rt.Kind() == reflect.Ptr {
got = out.Interface()
} else {
got = out.Elem().Interface()
}
if s, ok := got.(interface {
Sort()
}); ok {
s.Sort()
}
if s, ok := c.expect.(interface {
Sort()
}); ok {
s.Sort()
}
if !reflect.DeepEqual(got, c.expect) {
t.Errorf("case %d failed: objects are different\n%#v\n%#v",
i+1, got, c.expect,
)
}
})
}
}

var testFillValueCases = []struct {
name string
expect interface{}
quads []quad.Quad
depth int
from []quad.Value
}{
{
name: "complex object",
expect: struct {
rdfType struct{} `quad:"rdf:type > some:Type"`
ID quad.IRI `quad:"@id"`
Name string `quad:"name"`
Values []string `quad:"values"`
Items []item `quad:"items"`
Sub *item `quad:"sub"`
Val int `quad:"val"`
}{
ID: "1234",
Name: "some item",
Values: []string{"val1", "val2"},
Items: []item{
{ID: "sub1", Name: "Sub 1"},
{ID: "sub2", Name: "Sub 2"},
},
Sub: &item{ID: "sub3", Name: "Sub 3"},
Val: 123,
},
quads: []quad.Quad{
{iri("1234"), typeIRI, iri("some:Type"), nil},
{iri("1234"), iri("name"), quad.String("some item"), nil},
{iri("1234"), iri("values"), quad.String("val1"), nil},
{iri("1234"), iri("values"), quad.String("val2"), nil},
{iri("sub1"), typeIRI, iri("some:item"), nil},
{iri("sub1"), iri("name"), quad.String("Sub 1"), nil},
{iri("1234"), iri("items"), iri("sub1"), nil},
{iri("sub2"), typeIRI, iri("some:item"), nil},
{iri("sub2"), iri("name"), quad.String("Sub 2"), nil},
{iri("1234"), iri("items"), iri("sub2"), nil},
{iri("sub3"), typeIRI, iri("some:item"), nil},
{iri("sub3"), iri("name"), quad.String("Sub 3"), nil},
{iri("1234"), iri("sub"), iri("sub3"), nil},
{iri("1234"), iri("val"), quad.Int(123), nil},
},
},
{
name: "complex object (id value)",
expect: struct {
rdfType struct{} `quad:"rdf:type > some:Type"`
ID quad.Value `quad:"@id"`
Name string `quad:"name"`
Values []string `quad:"values"`
Items []item `quad:"items"`
}{
ID: quad.BNode("1234"),
Name: "some item",
Values: []string{"val1", "val2"},
Items: []item{
{ID: "sub1", Name: "Sub 1"},
{ID: "sub2", Name: "Sub 2"},
},
},
quads: []quad.Quad{
{quad.BNode("1234"), typeIRI, iri("some:Type"), nil},
{quad.BNode("1234"), iri("name"), quad.String("some item"), nil},
{quad.BNode("1234"), iri("values"), quad.String("val1"), nil},
{quad.BNode("1234"), iri("values"), quad.String("val2"), nil},
{iri("sub1"), typeIRI, iri("some:item"), nil},
{iri("sub1"), iri("name"), quad.String("Sub 1"), nil},
{quad.BNode("1234"), iri("items"), iri("sub1"), nil},
{iri("sub2"), typeIRI, iri("some:item"), nil},
{iri("sub2"), iri("name"), quad.String("Sub 2"), nil},
{quad.BNode("1234"), iri("items"), iri("sub2"), nil},
},
},
{
name: "embedded object",
expect: struct {
rdfType struct{} `quad:"rdf:type > some:Type"`
item2
ID quad.IRI `quad:"@id"`
Values []string `quad:"values"`
}{
item2: item2{Name: "Sub 1", Spec: "special"},
ID: "1234",
Values: []string{"val1", "val2"},
},
quads: []quad.Quad{
{iri("1234"), typeIRI, iri("some:Type"), nil},
{iri("1234"), iri("name"), quad.String("Sub 1"), nil},
{iri("1234"), iri("spec"), quad.String("special"), nil},
{iri("1234"), iri("values"), quad.String("val1"), nil},
{iri("1234"), iri("values"), quad.String("val2"), nil},
},
},
{
name: "type shorthand",
expect: struct {
rdfType struct{} `quad:"@type > some:Type"`
item2
ID quad.IRI `quad:"@id"`
Values []string `quad:"values"`
}{
item2: item2{Name: "Sub 1", Spec: "special"},
ID: "1234",
Values: []string{"val1", "val2"},
},
quads: []quad.Quad{
{iri("1234"), typeIRI, iri("some:Type"), nil},
{iri("1234"), iri("name"), quad.String("Sub 1"), nil},
{iri("1234"), iri("spec"), quad.String("special"), nil},
{iri("1234"), iri("values"), quad.String("val1"), nil},
{iri("1234"), iri("values"), quad.String("val2"), nil},
},
},
{
name: "tree",
expect: treeItem{
ID: iri("n1"),
Name: "Node 1",
Children: []treeItem{
{
ID: iri("n2"),
Name: "Node 2",
},
{
ID: iri("n3"),
Name: "Node 3",
Children: []treeItem{
{
ID: iri("n4"),
Name: "Node 4",
},
},
},
},
},
quads: treeQuads,
from: []quad.Value{iri("n1")},
},
{
name: "tree with depth limit 1",
expect: treeItem{
ID: iri("n1"),
Name: "Node 1",
Children: []treeItem{
{
ID: iri("n2"),
Name: "Node 2",
},
{
ID: iri("n3"),
Name: "Node 3",
Children: []treeItem{
{
ID: iri("n4"),
},
},
},
},
},
depth: 1,
quads: treeQuads,
from: []quad.Value{iri("n1")},
},
{
name: "tree with depth limit 2",
expect: treeItemOpt{
ID: iri("n1"),
Name: "Node 1",
Children: []treeItemOpt{
{
ID: iri("n2"),
Name: "Node 2",
},
{
ID: iri("n3"),
Name: "Node 3",
Children: []treeItemOpt{
{
ID: iri("n4"),
Name: "Node 4",
},
},
},
},
},
depth: 2,
quads: treeQuads,
from: []quad.Value{iri("n1")},
},
{
name: "tree with required children",
expect: treeItemReq{
ID: iri("n1"),
Name: "Node 1",
Children: []treeItemReq{
{
ID: iri("n3"),
Name: "Node 3",
// TODO(dennwc): a strange behavior: this field is required, but it's empty for current object,
// because all it's children are missing the same field. Leaving this as-is for now because
// it's weird to set Children field as required in a tree.
Children: nil,
},
},
},
quads: treeQuads,
from: []quad.Value{iri("n1")},
},
{
name: "simple object",
expect: subObject{
genObject: genObject{
ID: "1234",
Name: "Obj",
},
Num: 3,
},
quads: []quad.Quad{
{iri("1234"), iri("name"), quad.String("Obj"), nil},
{iri("1234"), iri("num"), quad.Int(3), nil},
},
},
{
name: "coords",
expect: Coords{Lat: 12.3, Lng: 34.5},
quads: []quad.Quad{
{iri("c1"), typeIRI, iri("ex:Coords"), nil},
{iri("c1"), iri("ex:lat"), quad.Float(12.3), nil},
{iri("c1"), iri("ex:lng"), quad.Float(34.5), nil},
},
},
}
Loading