forked from tliron/puccini
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvertex.go
73 lines (60 loc) · 1.62 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
package clout
import (
"encoding/json"
"github.com/tliron/kutil/ard"
)
//
// Vertex
//
type Vertex struct {
Clout *Clout `yaml:"-" cbor:"-"`
ID string `yaml:"-" cbor:"-"`
Metadata ard.StringMap `yaml:"metadata" cbor:"metadata"`
Properties ard.StringMap `yaml:"properties" cbor:"properties"`
EdgesOut Edges `yaml:"edgesOut" cbor:"edgesOut"`
EdgesIn Edges `yaml:"-" cbor:"-"`
}
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.EnsureStringMaps(self.Metadata),
Properties: ard.EnsureStringMaps(self.Properties),
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())
}
//
// Vertexes
//
type Vertexes map[string]*Vertex