-
Notifications
You must be signed in to change notification settings - Fork 20
/
vertex.go
114 lines (97 loc) · 2.68 KB
/
vertex.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
package clout
import (
"encoding/json"
"github.com/tliron/go-ard"
)
//
// Vertex
//
type Vertex struct {
Clout *Clout `json:"-" yaml:"-"`
ID string `json:"-" yaml:"-"`
Metadata ard.StringMap `json:"metadata" yaml:"metadata"`
Properties ard.StringMap `json:"properties" yaml:"properties"`
EdgesOut Edges `json:"edgesOut" yaml:"edgesOut"`
EdgesIn Edges `json:"-" yaml:"-"`
}
func (self *Clout) NewVertex(id string) *Vertex {
vertex := &Vertex{
Clout: self,
ID: id,
Metadata: make(ard.StringMap),
Properties: make(ard.StringMap),
EdgesOut: make(Edges, 0),
EdgesIn: make(Edges, 0),
}
self.Vertexes[id] = vertex
return vertex
}
func (self *Vertex) Remove() {
delete(self.Clout.Vertexes, self.ID)
}
// Entity interface
func (self *Vertex) GetMetadata() ard.StringMap {
return self.Metadata
}
// Entity interface
func (self *Vertex) GetProperties() ard.StringMap {
return self.Properties
}
type MarshalableVertexStringMaps struct {
Metadata ard.StringMap `json:"metadata"`
Properties ard.StringMap `json:"properties"`
EdgesOut Edges `json:"edgesOut"`
}
func (self *Vertex) MarshalableStringMaps() any {
return &MarshalableVertexStringMaps{
Metadata: ard.CopyMapsToStringMaps(self.Metadata).(ard.StringMap),
Properties: ard.CopyMapsToStringMaps(self.Properties).(ard.StringMap),
EdgesOut: self.EdgesOut,
}
}
// ([json.Marshaler] interface)
func (self *Vertex) MarshalJSON() ([]byte, error) {
// JavaScript requires keys to be strings, so we would lose complex keys
return json.Marshal(self.MarshalableStringMaps())
}
func (self *Vertex) copy(toArd bool) (*Vertex, error) {
vertex := Vertex{
ID: self.ID,
EdgesIn: make(Edges, 0),
}
var err error
if vertex.EdgesOut, err = self.EdgesOut.copy(toArd); err == nil {
if toArd {
if metadata, err := ard.ValidCopyMapsToStringMaps(self.Metadata, nil); err == nil {
if properties, err := ard.ValidCopyMapsToStringMaps(self.Properties, nil); err == nil {
vertex.Metadata = metadata.(ard.StringMap)
vertex.Properties = properties.(ard.StringMap)
} else {
return nil, err
}
} else {
return nil, err
}
} else {
vertex.Metadata = ard.CopyMapsToStringMaps(self.Metadata).(ard.StringMap)
vertex.Properties = ard.CopyMapsToStringMaps(self.Properties).(ard.StringMap)
}
} else {
return nil, err
}
return &vertex, nil
}
//
// Vertexes
//
type Vertexes map[string]*Vertex
func (self Vertexes) copy(toArd bool) (Vertexes, error) {
vertexes := make(Vertexes)
var err error
for id, vertex := range self {
if vertexes[id], err = vertex.copy(toArd); err != nil {
return nil, err
}
}
return vertexes, nil
}