File: labels_decode.go

package info (click to toggle)
golang-github-traefik-paerser 0.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 612 kB
  • sloc: makefile: 14
file content (97 lines) | stat: -rw-r--r-- 2,141 bytes parent folder | download
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
package parser

import (
	"fmt"
	"sort"
	"strings"
)

// DecodeToNode converts the labels to a tree of nodes.
// If any filters are present, labels which do not match the filters are skipped.
func DecodeToNode(labels map[string]string, rootName string, filters ...string) (*Node, error) {
	sortedKeys := sortKeys(labels, filters)

	var node *Node
	for i, key := range sortedKeys {
		split := strings.Split(key, ".")

		if split[0] != rootName {
			return nil, fmt.Errorf("invalid label root %s", split[0])
		}

		var parts []string
		for _, v := range split {
			if v == "" {
				return nil, fmt.Errorf("invalid element: %s", key)
			}

			if v[0] == '[' {
				return nil, fmt.Errorf("invalid leading character '[' in field name (bracket is a slice delimiter): %s", v)
			}

			if strings.HasSuffix(v, "]") && v[0] != '[' {
				indexLeft := strings.Index(v, "[")
				parts = append(parts, v[:indexLeft], v[indexLeft:])
			} else {
				parts = append(parts, v)
			}
		}

		if i == 0 {
			node = &Node{}
		}
		decodeToNode(node, parts, labels[key])
	}

	return node, nil
}

func decodeToNode(root *Node, path []string, value string) {
	if len(root.Name) == 0 {
		root.Name = path[0]
	}

	// it's a leaf or not -> children
	if len(path) > 1 {
		if n := containsNode(root.Children, path[1]); n != nil {
			// the child already exists
			decodeToNode(n, path[1:], value)
		} else {
			// new child
			child := &Node{Name: path[1]}
			decodeToNode(child, path[1:], value)
			root.Children = append(root.Children, child)
		}
	} else {
		root.Value = value
	}
}

func containsNode(nodes []*Node, name string) *Node {
	for _, n := range nodes {
		if strings.EqualFold(name, n.Name) {
			return n
		}
	}
	return nil
}

func sortKeys(labels map[string]string, filters []string) []string {
	var sortedKeys []string
	for key := range labels {
		if len(filters) == 0 {
			sortedKeys = append(sortedKeys, key)
			continue
		}

		for _, filter := range filters {
			if len(key) >= len(filter) && strings.EqualFold(key[:len(filter)], filter) {
				sortedKeys = append(sortedKeys, key)
				continue
			}
		}
	}
	sort.Strings(sortedKeys)

	return sortedKeys
}