/
types.go
231 lines (195 loc) · 4.25 KB
/
types.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io/ioutil"
"path"
"time"
"github.com/Songmu/prompter"
"github.com/lucsky/cuid"
"gopkg.in/yaml.v2"
)
type state struct {
here string
Nodes map[string]*Node
Rels map[string]*Rel
schema schema
}
type representable interface {
repr() string
}
func nodeMapToList(m map[string]*Node) []representable {
list := make([]representable, len(m))
i := 0
for _, el := range m {
list[i] = el
i++
}
return list
}
func relMapToList(m map[string]*Rel) []representable {
list := make([]representable, len(m))
i := 0
for _, el := range m {
list[i] = el
i++
}
return list
}
type byRepr []representable
func (o byRepr) Len() int { return len(o) }
func (o byRepr) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
func (o byRepr) Less(i, j int) bool { return o[i].repr() < o[j].repr() }
type Node struct {
path string
state *state
Id string `json:"id"`
Name string `json:"name"`
Attrs map[string]interface{} `json:"attrs,omitempty"`
Timestamp int64 `json:"timestamp"`
}
func (n Node) MarshalYAML() (interface{}, error) {
raw := map[string]interface{}{
"name": n.Name,
"id": n.Id,
"timestamp": n.Timestamp,
}
for k, v := range n.Attrs {
raw[k] = v
}
outgoing := map[string][]string{}
incoming := map[string][]string{}
neutral := map[string][]string{}
for _, r := range n.state.Rels {
var other string
var out bool
if r.From.Id == n.Id {
other = r.To.Id
out = true
} else if r.To.Id == n.Id {
other = r.From.Id
out = false
} else {
// this node is not participating in current rel
continue
}
if r.Directed {
if out {
outgoing[r.Kind] = append(outgoing[r.Kind], other)
} else {
incoming[r.Kind] = append(incoming[r.Kind], other)
}
} else {
neutral[r.Kind] = append(neutral[r.Kind], other)
}
}
if len(outgoing) > 0 {
raw["outgoing"] = outgoing
}
if len(incoming) > 0 {
raw["incoming"] = incoming
}
if len(neutral) > 0 {
raw["neutral"] = neutral
}
return raw, nil
}
func (n *Node) UnmarshalYAML(unmarshal func(interface{}) error) error {
var raw map[string]interface{}
if err := unmarshal(&raw); err != nil {
return err
}
if name, ok := raw["name"].(string); ok {
n.Name = name
}
if id, ok := raw["id"].(string); ok {
n.Id = id
}
if tm, ok := raw["timestamp"].(int64); ok {
n.Timestamp = tm
}
if n.Name == "" || n.Id == "" {
return fmt.Errorf("invalid file, missing name and/or id.")
}
n.Attrs = make(map[string]interface{})
for k, v := range raw {
if k != "name" && k != "id" &&
k != "outgoing" && k != "incoming" && k != "neutral" &&
k != "timestamp" {
n.Attrs[k] = v
}
}
return nil
}
func (n Node) repr() string {
return n.Name + " (" + n.Id + ")"
}
func addNode(s *state, name string) *Node {
id := cuid.Slug()
for _, n := range s.Nodes {
if n.Name == name {
dup := prompter.YN(
"There's already a node named '"+name+"', create a duplicate?",
false,
)
if dup {
break
} else {
return nil
}
}
}
n := &Node{
path: path.Join(s.here, id+".yaml"),
state: s,
Id: id,
Name: name,
Timestamp: time.Now().Unix(),
}
s.Nodes[id] = n
return n
}
func (n Node) write() error {
contents, err := yaml.Marshal(n)
if err != nil {
return err
}
return ioutil.WriteFile(n.path, contents, 0777)
}
type Rel struct {
Kind string `json:"kind"`
Directed bool `json:"directed"`
From *Node `json:"from"`
To *Node `json:"to"`
}
func (r Rel) key() string {
var prekey string
if r.Directed {
prekey = r.From.Id + "-" + r.Kind + ">" + r.To.Id
} else {
// alphabetic
if r.From.Id < r.To.Id {
prekey = r.From.Id + "-" + r.Kind + "-" + r.To.Id
} else {
prekey = r.To.Id + "-" + r.Kind + "-" + r.From.Id
}
}
hasher := md5.New()
hasher.Write([]byte(prekey))
return hex.EncodeToString(hasher.Sum(nil))[:5]
}
func (r Rel) repr() string {
if r.Directed {
return "[" + r.From.Name + "] ={" + r.Kind + "}=> [" + r.To.Name + "]"
} else {
// alphabetic
if r.From.Name < r.To.Name {
return "[" + r.From.Name + "] ={" + r.Kind + "}= [" + r.To.Name + "]"
} else {
return "[" + r.To.Name + "] ={" + r.Kind + "}= [" + r.From.Name + "]"
}
}
}
type schema struct {
}