-
Notifications
You must be signed in to change notification settings - Fork 19
/
attr.go
58 lines (51 loc) · 1.64 KB
/
attr.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
package dot
// HTML renders the provided content as graphviz HTML. Use of this
// type is only valid for some attributes, like the 'label' attribute.
type HTML string
// Literal renders the provided value as is, without adding enclosing
// quotes, escaping newlines, quotations marks or any other characters.
// For example:
//
// node.Attr("label", Literal(`"left-justified text\l"`))
//
// allows you to left-justify the label (due to the \l at the end).
// The caller is responsible for enclosing the value in quotes and for
// proper escaping of special characters.
type Literal string
// AttributesMap holds attribute=value pairs.
type AttributesMap struct {
attributes map[string]interface{}
}
// Attrs sets multiple values for attributes (unless empty) taking a label,value list
// E.g Attrs("style","filled","fillcolor","red")
func (a AttributesMap) Attrs(labelvalues ...interface{}) {
if len(labelvalues)%2 != 0 {
panic("missing label or value ; must provide pairs")
}
for i := 0; i < len(labelvalues); i += 2 {
label := labelvalues[i].(string)
value := labelvalues[i+1]
a.Attr(label, value)
}
}
// Attr sets the value for an attribute (unless empty).
func (a AttributesMap) Attr(label string, value interface{}) {
if len(label) == 0 || value == nil {
return
}
if s, ok := value.(string); ok {
if len(s) > 0 {
a.attributes[label] = s
return
}
}
a.attributes[label] = value
}
// Value return the value added for this label.
func (a AttributesMap) Value(label string) interface{} {
return a.attributes[label]
}
// Delete removes the attribute value at key, if any
func (a AttributesMap) Delete(key string) {
delete(a.attributes, key)
}