-
Notifications
You must be signed in to change notification settings - Fork 210
/
node.go
234 lines (193 loc) · 4.29 KB
/
node.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
232
233
234
package diagram
import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
graphviz "github.com/awalterschulze/gographviz"
"github.com/blushft/go-diagrams/nodes/assets"
"github.com/blushft/go-diagrams/pkg/rand"
)
type Node struct {
id string
conn Connector
Options NodeOptions
}
func NewNode(opts ...NodeOption) *Node {
options := DefaultNodeOptions(opts...)
return &Node{
id: rand.String(8),
Options: options,
}
}
func (n *Node) ID() string {
return n.id
}
func (n *Node) Label(l string) *Node {
n.Options.Label = l
return n
}
func (n *Node) setConnector(c Connector) {
n.conn = c
}
func (n *Node) render(parent string, path string, graph *graphviz.Escape) error {
if n.Options.Image != "" {
img, err := assets.ReadFile(n.Options.Image)
if err != nil {
return err
}
outDir := filepath.Join(path, filepath.Dir(n.Options.Image))
if err := os.MkdirAll(outDir, os.ModePerm); err != nil {
return err
}
outFile := filepath.Join(outDir, filepath.Base(n.Options.Image))
if err := ioutil.WriteFile(outFile, img, os.ModePerm); err != nil {
return err
}
}
return graph.AddNode(parent, n.id, n.attrs())
}
func (n *Node) attrs() map[string]string {
if n.Options.Image != "" {
labelH := float64(len(strings.Split(n.Options.Label, "/n")))
n.Options.Height = n.Options.Height + (0.4 * labelH)
n.Options.Shape = "none"
}
attrs := map[string]string{
"label": n.Options.Label,
"image": n.Options.Image,
"height": strconv.FormatFloat(n.Options.Height, 'f', -1, 64),
"width": strconv.FormatFloat(n.Options.Width, 'f', -1, 64),
"shape": n.Options.Shape,
"style": n.Options.Style,
"labelloc": n.Options.LabelLocation,
"imagescale": strconv.FormatBool(n.Options.ImageScale),
"fixedsize": strconv.FormatBool(n.Options.FixedSize),
"fontsize": strconv.FormatInt(int64(n.Options.Font.Size), 10),
"fontname": n.Options.Font.Name,
"fontcolor": n.Options.Font.Color,
}
for k, v := range n.Options.Attributes {
attrs[k] = v
}
return trimAttrs(attrs)
}
func renderNodes(name, out string, graph *graphviz.Escape, nodes ...*Node) error {
for _, n := range nodes {
err := n.render(name, out, graph)
if err != nil {
return err
}
}
return nil
}
type NodeOptions struct {
Name string
Label string
Provider string
Image string
ImageScale bool
Shape string
Style string
FixedSize bool
Width float64
Height float64
LabelLocation string
Font Font
Attributes map[string]string
}
type NodeOption func(*NodeOptions)
type OptionSet []NodeOption
func DefaultNodeOptions(opts ...NodeOption) NodeOptions {
nopts := NodeOptions{
Name: "node",
Label: "node",
Shape: "box",
Style: "rounded",
FixedSize: true,
Width: 1.4,
Height: 1.4,
LabelLocation: "b",
ImageScale: true,
Font: Font{
Name: "Sans-Serif",
Size: 13,
Color: "#2D3436",
},
Attributes: make(map[string]string),
}
for _, o := range opts {
o(&nopts)
}
return nopts
}
func MergeOptionSets(sets ...OptionSet) OptionSet {
ns := OptionSet{}
for _, set := range sets {
for _, opt := range set {
ns = append(ns, opt)
}
}
return ns
}
func Name(n string) NodeOption {
return func(o *NodeOptions) {
o.Name = n
}
}
func NodeLabel(l string) NodeOption {
return func(o *NodeOptions) {
o.Label = l
}
}
func Provider(p string) NodeOption {
return func(o *NodeOptions) {
o.Provider = p
}
}
func Icon(i string) NodeOption {
return func(o *NodeOptions) {
o.Image = i
}
}
func NodeShape(s string) NodeOption {
return func(o *NodeOptions) {
o.Shape = s
}
}
func NodeStyle(s string) NodeOption {
return func(o *NodeOptions) {
o.Style = s
}
}
func FixedSize(b bool) NodeOption {
return func(o *NodeOptions) {
o.FixedSize = b
}
}
func Width(w float64) NodeOption {
return func(o *NodeOptions) {
o.Width = w
}
}
func Height(h float64) NodeOption {
return func(o *NodeOptions) {
o.Height = h
}
}
func LabelLocation(l string) NodeOption {
return func(o *NodeOptions) {
o.LabelLocation = l
}
}
func ImageScale(b bool) NodeOption {
return func(o *NodeOptions) {
o.ImageScale = b
}
}
func SetFontOptions(f Font) NodeOption {
return func(o *NodeOptions) {
o.Font = f
}
}