-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopic_trees.go
143 lines (108 loc) · 2.55 KB
/
topic_trees.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
package main
import (
"strings"
)
type TopicTreeLeaf struct {
Topic string
Value []byte
Children TopicTree
Parent *TopicTreeLeaf
}
type TopicTree map[string]*TopicTreeLeaf
type TopicMap map[string][]byte
func (tm *TopicMap) IntoTree() *TopicTree {
t := make(TopicTree)
for k, v := range *tm {
t.Add(k, v)
}
return &t
}
func (t *TopicTree) Add(topic string, value []byte) *TopicTreeLeaf {
return t.AddRecursive(topic, value, nil)
}
func (t *TopicTree) AddRecursive(topic string, value []byte, parent *TopicTreeLeaf) *TopicTreeLeaf {
parts := strings.Split(topic, "/")
if len(parts) == 0 {
return nil
}
// log.Default().Println(parts)
currentPart := parts[0]
reminder := parts[1:]
// if reminder is empty, we are at the end of the topic
if len(reminder) == 0 {
(*t)[currentPart] = &TopicTreeLeaf{
Topic: currentPart,
Value: value,
Parent: parent,
Children: make(TopicTree),
}
return (*t)[currentPart]
}
// if reminder is not empty, we need to go deeper
if _, ok := (*t)[currentPart]; !ok {
(*t)[currentPart] = &TopicTreeLeaf{
Topic: currentPart,
Value: nil,
Parent: parent,
Children: make(TopicTree),
}
}
(*t)[currentPart].Children.AddRecursive(strings.Join(reminder, "/"), value, (*t)[currentPart])
return (*t)[currentPart]
}
func (t *TopicTreeLeaf) TopicCanonical() string {
if t.Parent == nil {
return t.Topic
}
return t.Parent.TopicCanonical() + "/" + t.Topic
}
func truncateString(s string, length int) string {
isLenExceeded := len(s) > length
if isLenExceeded {
return s[:length] + "..."
}
return s
}
func (t *TopicTreeLeaf) RenderString(depth int) string {
const INDENT = " "
sb := strings.Builder{}
for i := 0; i < depth; i++ {
sb.WriteString(INDENT)
}
sb.WriteString(t.Topic)
if t.Value != nil {
sb.WriteString(" = ")
sb.WriteString(truncateString(string(t.Value), 48))
}
for _, v := range t.Children {
sb.WriteString("\n")
sb.WriteString(INDENT)
sb.WriteString(v.RenderString(depth + 1))
}
return sb.String()
}
func (t *TopicTreeLeaf) String() string {
return t.RenderString(0)
}
func (t *TopicTree) RenderString() string {
sb := strings.Builder{}
for _, v := range *t {
sb.WriteString(v.RenderString(0))
sb.WriteString("\n")
}
return sb.String()
}
func (t *TopicTree) String() string {
return t.RenderString()
}
func (t *TopicTreeLeaf) ContainsCanonicalTopic(topic string) bool {
if t.TopicCanonical() == topic {
return true
}
for _, v := range t.Children {
if v.ContainsCanonicalTopic(topic) {
return true
}
}
return false
}