-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattributes.go
More file actions
56 lines (47 loc) · 1.01 KB
/
Copy pathattributes.go
File metadata and controls
56 lines (47 loc) · 1.01 KB
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
package smutje
import (
"github.com/gfrey/smutje/parser"
"github.com/pkg/errors"
)
type Attributes map[string]string
func newAttributes(n *parser.AstNode) (Attributes, error) {
if n.Type != parser.AstAttributes {
return nil, errors.Errorf("expected attributes node, got %s", n.Type)
}
attrs := Attributes{}
if raw, ok := n.Value.([]*parser.Attribute); !ok {
return nil, errors.Errorf("expected attributes on node, got %T", n.Value)
} else {
for _, a := range raw {
attrs[a.Key] = a.Val
}
}
return attrs, nil
}
func (a Attributes) Copy() Attributes {
c := Attributes{}
for k, v := range a {
c[k] = v
}
return c
}
func (a Attributes) Merge(b Attributes) (Attributes, error) {
c := Attributes{}
for k, v := range a {
c[k] = v
}
return c, c.MergeInplace(b)
}
func (a Attributes) MergeInplace(b Attributes) error {
var err error
for k, v := range b {
if _, found := a[k]; found {
continue
}
a[k], err = renderString("key_"+k, v, a)
if err != nil {
return err
}
}
return nil
}