-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
74 lines (60 loc) · 1.86 KB
/
common.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package humus
//The common node type that is inherited. This differs from the DNode which is an interface while
//node is an embedded struct containing basic dgraph properties.
type Node struct {
Uid UID `json:"uid,omitempty" predicate:"uid,omitempty"`
Type []string `json:"dgraph.type,omitempty" predicate:"dgraph.type,omitempty"`
}
func (n *Node) Fields() Fields {
return nil
}
func (n *Node) Values() DNode {
return n
}
func (n *Node) Recurse(counter int) int {
return counter
}
func (n *Node) MapValues() Mapper {
return Mapper{"uid": n.Uid, "dgraph.type": n.Type}
}
func (n *Node) GetType() []string {
return nil
}
func (n *Node) SetUID(uid UID) {
n.Uid = uid
}
func (n *Node) SetType() {
panic("setType called on regular node")
}
func (n *Node) UID() UID {
return n.Uid
}
//CreateMutation creates a mutation object from the DNode. This can be used immediately
//as a value for Mutation. A simple syntax would be
//GetDB().Mutate(ctx, CreateMutation(node, MutateSet)) where node
//represents an arbitrary Node.
func CreateMutation(obj DNode, typ MutationType) SingleMutation {
return SingleMutation{
Object: obj,
MutationType: typ,
}
}
//UidFromVariable returns the proper uid mapping for
//upserts, of the form uid(variable).
func UIDVariable(variable string) UID {
return UID("uid(" + variable + ")")
}
//Here begins common queries.
//GetByUid is shorthand for generating a query for getting a node
//from its uid given by the fields.
func GetByUid(uid UID, fields Fields) *GeneratedQuery {
q := NewQuery(fields).Function(FunctionUid).Values(uid)
return q
}
//GetByPredicate is shorthand for generating a query for getting nodes
//from multiple predicate values given by the fields.
func GetByPredicate(pred Predicate, fields Fields, values ...interface{}) *GeneratedQuery {
q := NewQuery(fields).Function(Equals)
q.predValues(pred, values)
return q
}