Repository navigation
Expand file tree
/
Copy pathtags.go
More file actions
66 lines (51 loc) · 1.4 KB
/
Copy pathtags.go
File metadata and controls
66 lines (51 loc) · 1.4 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
57
58
59
60
61
62
63
64
65
66
package pluginsdk
import (
"fmt"
"regexp"
"strings"
)
// TagsConfig is a string slice that supports interpolation
type TagsConfig []string
// RegEx for matching interpolated variables, e.g ${myvar}
var interpolationRegex = regexp.MustCompile(`\${([^}]+)}`)
// InterpolateTags processes tags and replaces variables with values from metadata
func InterpolateTags(tags TagsConfig, metadata map[string]interface{}) []string {
result := make([]string, 0, len(tags))
for _, tag := range tags {
if tag == "" {
continue
}
if !strings.Contains(tag, "${") {
result = append(result, tag)
continue
}
interpolated := interpolationRegex.ReplaceAllStringFunc(tag, func(match string) string {
varName := match[2 : len(match)-1]
if value, ok := getNestedValue(metadata, varName); ok {
return fmt.Sprintf("%v", value)
}
return ""
})
if interpolated != "" {
result = append(result, interpolated)
}
}
return result
}
// getNestedValue retrieves a value from nested metadata using dot notation
func getNestedValue(metadata map[string]interface{}, path string) (interface{}, bool) {
parts := strings.Split(path, ".")
current := metadata
for i := 0; i < len(parts)-1; i++ {
next, ok := current[parts[i]]
if !ok {
return nil, false
}
current, ok = next.(map[string]interface{})
if !ok {
return nil, false
}
}
value, ok := current[parts[len(parts)-1]]
return value, ok
}