Skip to content

Commit 3091e67

Browse files
committedMar 3, 2018
schema: allow to customize behavior; resolves #694
·
v0.7.7v0.7.2
1 parent b74f4c7 commit 3091e67

File tree

7 files changed

+266
-107
lines changed

7 files changed

+266
-107
lines changed
 

‎examples/hello_schema/main.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ func main() {
5050
// All Coords objects will now generate a <id> <rdf:type> <ex:Coords> triple.
5151
schema.RegisterType(quad.IRI("ex:Coords"), Coords{})
5252

53+
sch := schema.NewConfig()
5354
// Override a function to generate IDs. Can be changed to generate UUIDs, for example.
54-
schema.GenerateID = func(_ interface{}) quad.Value {
55+
sch.GenerateID = func(_ interface{}) quad.Value {
5556
return quad.BNode(fmt.Sprintf("node%d", rand.Intn(1000)))
5657
}
5758

@@ -76,7 +77,7 @@ func main() {
7677
Name: "Bob", Age: 32,
7778
}
7879
fmt.Printf("saving: %+v\n", bob)
79-
id, err := schema.WriteAsQuads(qw, bob)
80+
id, err := sch.WriteAsQuads(qw, bob)
8081
checkErr(err)
8182
err = qw.Close()
8283
checkErr(err)
@@ -85,13 +86,13 @@ func main() {
8586

8687
// Get object by id
8788
var someone Person
88-
err = schema.LoadTo(nil, store, &someone, id)
89+
err = sch.LoadTo(nil, store, &someone, id)
8990
checkErr(err)
9091
fmt.Printf("loaded: %+v\n", someone)
9192

9293
// Or get all objects of type Person
9394
var people []Person
94-
err = schema.LoadTo(nil, store, &people)
95+
err = sch.LoadTo(nil, store, &people)
9596
checkErr(err)
9697
fmt.Printf("people: %+v\n", people)
9798

@@ -104,7 +105,7 @@ func main() {
104105
}
105106
qw = graph.NewWriter(store)
106107
for _, c := range coords {
107-
id, err = schema.WriteAsQuads(qw, c)
108+
id, err = sch.WriteAsQuads(qw, c)
108109
checkErr(err)
109110
fmt.Println("generated id:", id)
110111
}
@@ -113,7 +114,7 @@ func main() {
113114

114115
// Get coords back
115116
var newCoords []Coords
116-
err = schema.LoadTo(nil, store, &newCoords)
117+
err = sch.LoadTo(nil, store, &newCoords)
117118
checkErr(err)
118119
fmt.Printf("coords: %+v\n", newCoords)
119120

‎graph/graphtest/graphtest.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,15 +1008,17 @@ func TestSchema(t testing.TB, gen testutil.DatabaseFunc, conf *Config) {
10081008
Name: "Bob",
10091009
}
10101010

1011+
sch := schema.NewConfig()
1012+
10111013
qw := graph.NewWriter(w)
1012-
id, err := schema.WriteAsQuads(qw, p)
1014+
id, err := sch.WriteAsQuads(qw, p)
10131015
require.NoError(t, err)
10141016
err = qw.Close()
10151017
require.NoError(t, err)
10161018
require.Equal(t, p.ID, id)
10171019

10181020
var p2 Person
1019-
err = schema.LoadTo(nil, qs, &p2, id)
1021+
err = sch.LoadTo(nil, qs, &p2, id)
10201022
require.NoError(t, err)
10211023
require.Equal(t, p, p2)
10221024
}

‎query/gizmo/environ.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/cayleygraph/cayley/graph/path"
2828
"github.com/cayleygraph/cayley/graph/shape"
2929
"github.com/cayleygraph/cayley/quad"
30-
"github.com/cayleygraph/cayley/schema"
3130
"github.com/cayleygraph/cayley/voc"
3231
)
3332

@@ -58,7 +57,7 @@ func (g *graphObject) AddDefaultNamespaces() {
5857

5958
// LoadNamespaces loads all namespaces saved to graph.
6059
func (g *graphObject) LoadNamespaces() error {
61-
return schema.LoadNamespaces(g.s.ctx, g.s.qs, &g.s.ns)
60+
return g.s.sch.LoadNamespaces(g.s.ctx, g.s.qs, &g.s.ns)
6261
}
6362

6463
// V is a shorthand for Vertex.

‎query/gizmo/gizmo.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/cayleygraph/cayley/graph/iterator"
2727
"github.com/cayleygraph/cayley/quad"
2828
"github.com/cayleygraph/cayley/query"
29+
"github.com/cayleygraph/cayley/schema"
2930
"github.com/cayleygraph/cayley/voc"
3031
)
3132

@@ -49,6 +50,7 @@ func init() {
4950
func NewSession(qs graph.QuadStore) *Session {
5051
s := &Session{
5152
ctx: context.Background(),
53+
sch: schema.NewConfig(),
5254
qs: qs, limit: -1,
5355
}
5456
if err := s.buildEnv(); err != nil {
@@ -58,9 +60,10 @@ func NewSession(qs graph.QuadStore) *Session {
5860
}
5961

6062
type Session struct {
61-
qs graph.QuadStore
62-
vm *goja.Runtime
63-
ns voc.Namespaces
63+
qs graph.QuadStore
64+
vm *goja.Runtime
65+
ns voc.Namespaces
66+
sch *schema.Config
6467

6568
last string
6669
p *goja.Program

‎schema/global.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package schema
2+
3+
import (
4+
"context"
5+
"reflect"
6+
7+
"github.com/cayleygraph/cayley/graph"
8+
"github.com/cayleygraph/cayley/graph/path"
9+
"github.com/cayleygraph/cayley/quad"
10+
"github.com/cayleygraph/cayley/voc"
11+
)
12+
13+
// GenerateID is called when any object without an ID field is being saved.
14+
//
15+
// Deprecated: see Config.GenerateID
16+
var GenerateID = func(_ interface{}) quad.Value {
17+
return quad.RandomBlankNode()
18+
}
19+
20+
var global = NewConfig()
21+
22+
// Global returns a default global schema config.
23+
func Global() *Config {
24+
return global
25+
}
26+
27+
// PathForType builds a path (morphism) for a given Go type.
28+
//
29+
// Deprecated: see Config.PathForType
30+
func PathForType(rt reflect.Type) (*path.Path, error) {
31+
return global.PathForType(rt)
32+
}
33+
34+
// WriteAsQuads writes a single value in form of quads into specified quad writer.
35+
//
36+
// Deprecated: see Config.WriteAsQuads
37+
func WriteAsQuads(w quad.Writer, o interface{}) (quad.Value, error) {
38+
return global.WriteAsQuads(w, o)
39+
}
40+
41+
// WriteNamespaces will writes namespaces list into graph.
42+
//
43+
// Deprecated: see Config.WriteNamespaces
44+
func WriteNamespaces(w quad.Writer, n *voc.Namespaces) error {
45+
return global.WriteNamespaces(w, n)
46+
}
47+
48+
// LoadNamespaces will load namespaces stored in graph to a specified list.
49+
//
50+
// Deprecated: see Config.LoadNamespaces
51+
func LoadNamespaces(ctx context.Context, qs graph.QuadStore, dest *voc.Namespaces) error {
52+
return global.LoadNamespaces(ctx, qs, dest)
53+
}
54+
55+
// LoadIteratorToDepth is the same as LoadIteratorTo, but stops at a specified depth.
56+
//
57+
// Deprecated: see Config.LoadIteratorToDepth
58+
func LoadIteratorToDepth(ctx context.Context, qs graph.QuadStore, dst reflect.Value, depth int, list graph.Iterator) error {
59+
return global.LoadIteratorToDepth(ctx, qs, dst, depth, list)
60+
}
61+
62+
// LoadIteratorTo is a lower level version of LoadTo.
63+
//
64+
// Deprecated: see Config.LoadIteratorTo
65+
func LoadIteratorTo(ctx context.Context, qs graph.QuadStore, dst reflect.Value, list graph.Iterator) error {
66+
return global.LoadIteratorToDepth(ctx, qs, dst, -1, list)
67+
}
68+
69+
// LoadPathTo is the same as LoadTo, but starts loading objects from a given path.
70+
//
71+
// Deprecated: see Config.LoadPathTo
72+
func LoadPathTo(ctx context.Context, qs graph.QuadStore, dst interface{}, p *path.Path) error {
73+
return global.LoadIteratorTo(ctx, qs, reflect.ValueOf(dst), p.BuildIterator())
74+
}
75+
76+
// LoadTo will load a sub-graph of objects starting from ids (or from any nodes, if empty)
77+
// to a destination Go object. Destination can be a struct, slice or channel.
78+
//
79+
// Deprecated: see Config.LoadTo
80+
func LoadTo(ctx context.Context, qs graph.QuadStore, dst interface{}, ids ...quad.Value) error {
81+
return global.LoadTo(ctx, qs, dst, ids...)
82+
}
83+
84+
// LoadToDepth is the same as LoadTo, but stops at a specified depth.
85+
//
86+
// Deprecated: see Config.LoadToDepth
87+
func LoadToDepth(ctx context.Context, qs graph.QuadStore, dst interface{}, depth int, ids ...quad.Value) error {
88+
return global.LoadToDepth(ctx, qs, dst, depth, ids...)
89+
}

‎schema/schema.go

Lines changed: 152 additions & 90 deletions
Large diffs are not rendered by default.

‎schema/schema_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,11 @@ func (s *quadSlice) WriteQuad(q quad.Quad) error {
276276
}
277277

278278
func TestWriteAsQuads(t *testing.T) {
279+
sch := schema.NewConfig()
279280
for i, c := range testWriteValueCases {
280281
t.Run(c.name, func(t *testing.T) {
281282
var out quadSlice
282-
id, err := schema.WriteAsQuads(&out, c.obj)
283+
id, err := sch.WriteAsQuads(&out, c.obj)
283284
if err != c.err {
284285
t.Errorf("case %d failed: %v != %v", i, err, c.err)
285286
} else if c.err != nil {
@@ -550,6 +551,7 @@ var testFillValueCases = []struct {
550551
}
551552

552553
func TestLoadIteratorTo(t *testing.T) {
554+
sch := schema.NewConfig()
553555
for i, c := range testFillValueCases {
554556
t.Run(c.name, func(t *testing.T) {
555557
qs := memstore.New(c.quads...)
@@ -566,7 +568,7 @@ func TestLoadIteratorTo(t *testing.T) {
566568
if depth == 0 {
567569
depth = -1
568570
}
569-
if err := schema.LoadIteratorToDepth(nil, qs, out, depth, it); err != nil {
571+
if err := sch.LoadIteratorToDepth(nil, qs, out, depth, it); err != nil {
570572
t.Errorf("case %d failed: %v", i+1, err)
571573
return
572574
}
@@ -591,6 +593,7 @@ func TestLoadIteratorTo(t *testing.T) {
591593
}
592594

593595
func TestSaveNamespaces(t *testing.T) {
596+
sch := schema.NewConfig()
594597
save := []voc.Namespace{
595598
{Full: "http://example.org/", Prefix: "ex:"},
596599
{Full: "http://cayley.io/", Prefix: "c:"},
@@ -600,12 +603,12 @@ func TestSaveNamespaces(t *testing.T) {
600603
ns.Register(n)
601604
}
602605
qs := memstore.New()
603-
err := schema.WriteNamespaces(qs, &ns)
606+
err := sch.WriteNamespaces(qs, &ns)
604607
if err != nil {
605608
t.Fatal(err)
606609
}
607610
var ns2 voc.Namespaces
608-
err = schema.LoadNamespaces(context.TODO(), qs, &ns2)
611+
err = sch.LoadNamespaces(context.TODO(), qs, &ns2)
609612
if err != nil {
610613
t.Fatal(err)
611614
}

0 commit comments

Comments
 (0)
Please sign in to comment.