-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuilder.go
55 lines (49 loc) · 1.28 KB
/
builder.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
package store4
// Builder provides a fluent (chainable) convenience API for creating quads.
//
// If no quad store is provided, a new one will be created.
type Builder struct {
QuadStore *QuadStore
graph string
subject string
}
// Graph sets the name of the current graph in which to add new statements.
func (b *Builder) Graph(g string) *Builder {
b.graph = g
b.subject = ""
return b
}
// DefaultGraph sets the current graph in which to add new statements to be
// the default graph.
func (b *Builder) DefaultGraph() *Builder {
return b.Graph("")
}
// Subject sets the current subject about which statements are to be added.
func (b *Builder) Subject(s string) *Builder {
b.subject = s
return b
}
// Add a statement to the underlying quad store, using the current subject and graph.
func (b *Builder) Add(p string, o interface{}) *Builder {
if len(b.subject) == 0 {
panic("Undefined subject term")
}
if len(p) == 0 {
panic("Undefined predicate term")
}
if o == nil {
panic("Undefined object term")
}
if b.QuadStore == nil {
b.QuadStore = NewQuadStore()
}
b.QuadStore.Add(b.subject, p, o, b.graph)
return b
}
// Build returns the underlying quad store.
func (b *Builder) Build() *QuadStore {
if b.QuadStore == nil {
b.QuadStore = NewQuadStore()
}
return b.QuadStore
}