forked from cayleygraph/cayley
-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespaces.go
57 lines (52 loc) · 1.41 KB
/
namespaces.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package schema
import (
"context"
"fmt"
"reflect"
"github.com/aperturerobotics/cayley/graph"
"github.com/aperturerobotics/cayley/quad"
"github.com/aperturerobotics/cayley/quad/voc"
)
type namespace struct {
_ struct{} `quad:"@type > cayley:namespace"`
Full quad.IRI `quad:"@id"`
Prefix quad.IRI `quad:"cayley:prefix"`
}
// WriteNamespaces will writes namespaces list into graph.
func (c *Config) WriteNamespaces(ctx context.Context, w quad.Writer, n *voc.Namespaces) error {
rules, err := c.rulesFor(reflect.TypeOf(namespace{}))
if err != nil {
return fmt.Errorf("can't load rules: %v", err)
}
wr := c.newWriter(w)
for _, ns := range n.List() {
obj := namespace{
Full: quad.IRI(ns.Full),
Prefix: quad.IRI(ns.Prefix),
}
rv := reflect.ValueOf(obj)
if err = wr.writeValueAs(ctx, obj.Full, rv, "", rules); err != nil {
return err
}
}
return nil
}
// LoadNamespaces will load namespaces stored in graph to a specified list.
// If destination list is empty, global namespace registry will be used.
func (c *Config) LoadNamespaces(ctx context.Context, qs graph.QuadStore, dest *voc.Namespaces) error {
var list []namespace
if err := c.LoadTo(ctx, qs, &list); err != nil {
return err
}
register := dest.Register
if dest == nil {
register = voc.Register
}
for _, ns := range list {
register(voc.Namespace{
Prefix: string(ns.Prefix),
Full: string(ns.Full),
})
}
return nil
}