-
Notifications
You must be signed in to change notification settings - Fork 20
/
edge.go
214 lines (183 loc) · 5 KB
/
edge.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package clout
import (
"encoding/json"
"github.com/fxamacker/cbor/v2"
"github.com/tliron/go-ard"
"github.com/vmihailenco/msgpack/v5"
)
//
// Edge
//
type Edge struct {
Metadata ard.StringMap
Properties ard.StringMap
Source *Vertex
TargetID string
Target *Vertex
}
func (self *Vertex) NewEdgeTo(target *Vertex) *Edge {
edge := &Edge{
Metadata: make(ard.StringMap),
Properties: make(ard.StringMap),
Source: self,
TargetID: target.ID,
Target: target,
}
self.EdgesOut = append(self.EdgesOut, edge)
target.EdgesIn = append(target.EdgesIn, edge)
return edge
}
func (self *Vertex) NewEdgeToID(targetId string) *Edge {
edge := &Edge{
Metadata: make(ard.StringMap),
Properties: make(ard.StringMap),
Source: self,
TargetID: targetId,
}
self.EdgesOut = append(self.EdgesOut, edge)
return edge
}
func (self *Edge) Remove() {
self.Source.EdgesOut = self.Source.EdgesOut.remove(self)
self.Target.EdgesIn = self.Target.EdgesIn.remove(self)
}
// Entity interface
func (self *Edge) GetMetadata() ard.StringMap {
return self.Metadata
}
// Entity interface
func (self *Edge) GetProperties() ard.StringMap {
return self.Properties
}
type MarshalableEdge struct {
Metadata ard.StringMap `json:"metadata" yaml:"metadata" ard:"metadata"`
Properties ard.StringMap `json:"properties" yaml:"properties" ard:"properties"`
TargetID string `json:"targetID" yaml:"targetID" ard:"targetID"`
}
type MarshalableEdgeStringMaps struct {
Metadata ard.StringMap `json:"metadata"`
Properties ard.StringMap `json:"properties"`
TargetID string `json:"targetID"`
}
func (self *Edge) Marshalable(stringMaps bool) any {
var targetID string
if self.Target != nil {
targetID = self.Target.ID
}
if stringMaps {
return &MarshalableEdgeStringMaps{
Metadata: ard.CopyMapsToStringMaps(self.Metadata).(ard.StringMap),
Properties: ard.CopyMapsToStringMaps(self.Properties).(ard.StringMap),
TargetID: targetID,
}
} else {
return &MarshalableEdge{
Metadata: self.Metadata,
Properties: self.Properties,
TargetID: targetID,
}
}
}
func (self *Edge) Unmarshal(f func(m *MarshalableEdge) error) error {
var m MarshalableEdge
if err := f(&m); err != nil {
return err
}
self.Metadata = m.Metadata
self.Properties = m.Properties
self.TargetID = m.TargetID
return nil
}
// ([json.Marshaler] interface)
func (self *Edge) MarshalJSON() ([]byte, error) {
// JavaScript requires keys to be strings, so we would lose complex keys
return json.Marshal(self.Marshalable(true))
}
// ([yaml.Marshaler] interface)
func (self *Edge) MarshalYAML() (any, error) {
return self.Marshalable(false), nil
}
// ([cbor.Marshaler] interface)
func (self *Edge) MarshalCBOR() ([]byte, error) {
return cbor.Marshal(self.Marshalable(false))
}
// ([msgpack.Marshaler] interface)
func (self *Edge) MarshalMsgpack() ([]byte, error) {
return msgpack.Marshal(self.Marshalable(false))
}
// ([ard.ToARD] interface)
func (self *Edge) ToARD(reflector *ard.Reflector) (any, error) {
return reflector.Unpack(self.Marshalable(false))
}
// json.Unmarshaler interface
func (self *Edge) UnmarshalJSON(data []byte) error {
return self.Unmarshal(func(m *MarshalableEdge) error {
return json.Unmarshal(data, m)
})
}
// yaml.Unmarshaler interface
func (self *Edge) UnmarshalYAML(unmarshal func(any) error) error {
return self.Unmarshal(func(m *MarshalableEdge) error {
return unmarshal(m)
})
}
// cbor.Unmarshaler interface
func (self *Edge) UnmarshalCBOR(data []byte) error {
return self.Unmarshal(func(m *MarshalableEdge) error {
return cbor.Unmarshal(data, m)
})
}
// msgpack.Unmarshaler interface
func (self *Edge) UnmarshalMsgpack(data []byte) error {
return self.Unmarshal(func(m *MarshalableEdge) error {
return ard.UnmarshalMessagePack(data, m)
})
}
func (self *Edge) copy(toArd bool) (*Edge, error) {
edge := Edge{
TargetID: self.TargetID,
}
if toArd {
if metadata, err := ard.ValidCopyMapsToStringMaps(self.Metadata, nil); err == nil {
if properties, err := ard.ValidCopyMapsToStringMaps(self.Properties, nil); err == nil {
edge.Metadata = metadata.(ard.StringMap)
edge.Properties = properties.(ard.StringMap)
} else {
return nil, err
}
} else {
return nil, err
}
} else {
edge.Metadata = ard.CopyMapsToStringMaps(self.Metadata).(ard.StringMap)
edge.Properties = ard.CopyMapsToStringMaps(self.Properties).(ard.StringMap)
}
return &edge, nil
}
//
// Edges
//
type Edges []*Edge
// Note: ".length" will not work reliably in JavaScript because once the value
// is bound it will not reflect changes to the struct's field
func (self Edges) Size() int {
return len(self)
}
func (self Edges) copy(toArd bool) (Edges, error) {
edges := make(Edges, len(self))
var err error
for index, edge := range self {
if edges[index], err = edge.copy(toArd); err != nil {
return nil, err
}
}
return edges, nil
}
func (self Edges) remove(edge *Edge) Edges {
for index, e := range self {
if e == edge {
return append(self[:index], self[index+1:]...)
}
}
return self
}