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

schema: allow to customize behavior #700

Merged
merged 1 commit into from
Mar 3, 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
13 changes: 7 additions & 6 deletions examples/hello_schema/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ func main() {
// All Coords objects will now generate a <id> <rdf:type> <ex:Coords> triple.
schema.RegisterType(quad.IRI("ex:Coords"), Coords{})

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

Expand All @@ -76,7 +77,7 @@ func main() {
Name: "Bob", Age: 32,
}
fmt.Printf("saving: %+v\n", bob)
id, err := schema.WriteAsQuads(qw, bob)
id, err := sch.WriteAsQuads(qw, bob)
checkErr(err)
err = qw.Close()
checkErr(err)
Expand All @@ -85,13 +86,13 @@ func main() {

// Get object by id
var someone Person
err = schema.LoadTo(nil, store, &someone, id)
err = sch.LoadTo(nil, store, &someone, id)
checkErr(err)
fmt.Printf("loaded: %+v\n", someone)

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

Expand All @@ -104,7 +105,7 @@ func main() {
}
qw = graph.NewWriter(store)
for _, c := range coords {
id, err = schema.WriteAsQuads(qw, c)
id, err = sch.WriteAsQuads(qw, c)
checkErr(err)
fmt.Println("generated id:", id)
}
Expand All @@ -113,7 +114,7 @@ func main() {

// Get coords back
var newCoords []Coords
err = schema.LoadTo(nil, store, &newCoords)
err = sch.LoadTo(nil, store, &newCoords)
checkErr(err)
fmt.Printf("coords: %+v\n", newCoords)

Expand Down
6 changes: 4 additions & 2 deletions graph/graphtest/graphtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -1008,15 +1008,17 @@ func TestSchema(t testing.TB, gen testutil.DatabaseFunc, conf *Config) {
Name: "Bob",
}

sch := schema.NewConfig()

qw := graph.NewWriter(w)
id, err := schema.WriteAsQuads(qw, p)
id, err := sch.WriteAsQuads(qw, p)
require.NoError(t, err)
err = qw.Close()
require.NoError(t, err)
require.Equal(t, p.ID, id)

var p2 Person
err = schema.LoadTo(nil, qs, &p2, id)
err = sch.LoadTo(nil, qs, &p2, id)
require.NoError(t, err)
require.Equal(t, p, p2)
}
3 changes: 1 addition & 2 deletions query/gizmo/environ.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/cayleygraph/cayley/graph/path"
"github.com/cayleygraph/cayley/graph/shape"
"github.com/cayleygraph/cayley/quad"
"github.com/cayleygraph/cayley/schema"
"github.com/cayleygraph/cayley/voc"
)

Expand Down Expand Up @@ -58,7 +57,7 @@ func (g *graphObject) AddDefaultNamespaces() {

// LoadNamespaces loads all namespaces saved to graph.
func (g *graphObject) LoadNamespaces() error {
return schema.LoadNamespaces(g.s.ctx, g.s.qs, &g.s.ns)
return g.s.sch.LoadNamespaces(g.s.ctx, g.s.qs, &g.s.ns)
}

// V is a shorthand for Vertex.
Expand Down
9 changes: 6 additions & 3 deletions query/gizmo/gizmo.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/cayleygraph/cayley/graph/iterator"
"github.com/cayleygraph/cayley/quad"
"github.com/cayleygraph/cayley/query"
"github.com/cayleygraph/cayley/schema"
"github.com/cayleygraph/cayley/voc"
)

Expand All @@ -49,6 +50,7 @@ func init() {
func NewSession(qs graph.QuadStore) *Session {
s := &Session{
ctx: context.Background(),
sch: schema.NewConfig(),
qs: qs, limit: -1,
}
if err := s.buildEnv(); err != nil {
Expand All @@ -58,9 +60,10 @@ func NewSession(qs graph.QuadStore) *Session {
}

type Session struct {
qs graph.QuadStore
vm *goja.Runtime
ns voc.Namespaces
qs graph.QuadStore
vm *goja.Runtime
ns voc.Namespaces
sch *schema.Config

last string
p *goja.Program
Expand Down
89 changes: 89 additions & 0 deletions schema/global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package schema

import (
"context"
"reflect"

"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/graph/path"
"github.com/cayleygraph/cayley/quad"
"github.com/cayleygraph/cayley/voc"
)

// GenerateID is called when any object without an ID field is being saved.
//
// Deprecated: see Config.GenerateID
var GenerateID = func(_ interface{}) quad.Value {
return quad.RandomBlankNode()
}

var global = NewConfig()

// Global returns a default global schema config.
func Global() *Config {
return global
}

// PathForType builds a path (morphism) for a given Go type.
//
// Deprecated: see Config.PathForType
func PathForType(rt reflect.Type) (*path.Path, error) {
return global.PathForType(rt)
}

// WriteAsQuads writes a single value in form of quads into specified quad writer.
//
// Deprecated: see Config.WriteAsQuads
func WriteAsQuads(w quad.Writer, o interface{}) (quad.Value, error) {
return global.WriteAsQuads(w, o)
}

// WriteNamespaces will writes namespaces list into graph.
//
// Deprecated: see Config.WriteNamespaces
func WriteNamespaces(w quad.Writer, n *voc.Namespaces) error {
return global.WriteNamespaces(w, n)
}

// LoadNamespaces will load namespaces stored in graph to a specified list.
//
// Deprecated: see Config.LoadNamespaces
func LoadNamespaces(ctx context.Context, qs graph.QuadStore, dest *voc.Namespaces) error {
return global.LoadNamespaces(ctx, qs, dest)
}

// LoadIteratorToDepth is the same as LoadIteratorTo, but stops at a specified depth.
//
// Deprecated: see Config.LoadIteratorToDepth
func LoadIteratorToDepth(ctx context.Context, qs graph.QuadStore, dst reflect.Value, depth int, list graph.Iterator) error {
return global.LoadIteratorToDepth(ctx, qs, dst, depth, list)
}

// LoadIteratorTo is a lower level version of LoadTo.
//
// Deprecated: see Config.LoadIteratorTo
func LoadIteratorTo(ctx context.Context, qs graph.QuadStore, dst reflect.Value, list graph.Iterator) error {
return global.LoadIteratorToDepth(ctx, qs, dst, -1, list)
}

// LoadPathTo is the same as LoadTo, but starts loading objects from a given path.
//
// Deprecated: see Config.LoadPathTo
func LoadPathTo(ctx context.Context, qs graph.QuadStore, dst interface{}, p *path.Path) error {
return global.LoadIteratorTo(ctx, qs, reflect.ValueOf(dst), p.BuildIterator())
}

// LoadTo will load a sub-graph of objects starting from ids (or from any nodes, if empty)
// to a destination Go object. Destination can be a struct, slice or channel.
//
// Deprecated: see Config.LoadTo
func LoadTo(ctx context.Context, qs graph.QuadStore, dst interface{}, ids ...quad.Value) error {
return global.LoadTo(ctx, qs, dst, ids...)
}

// LoadToDepth is the same as LoadTo, but stops at a specified depth.
//
// Deprecated: see Config.LoadToDepth
func LoadToDepth(ctx context.Context, qs graph.QuadStore, dst interface{}, depth int, ids ...quad.Value) error {
return global.LoadToDepth(ctx, qs, dst, depth, ids...)
}
Loading