/
knf_parser.go
146 lines (109 loc) · 3.79 KB
/
knf_parser.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
package knf
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
import (
"bufio"
"fmt"
"io"
"regexp"
"strings"
"sync"
)
// ////////////////////////////////////////////////////////////////////////////////// //
const (
_SYMBOL_COMMENT = "#"
_SYMBOL_SECTION_START = "["
_SYMBOL_SECTION_END = "["
_SYMBOL_DELIMITER = ":"
_SYMBOL_MACRO_START = "{"
_SYMBOL_MACRO_END = "}"
)
// ////////////////////////////////////////////////////////////////////////////////// //
// macroRE is a regexp for extracting macro
var macroRE = regexp.MustCompile(`\{([\w\-]+):([\w\-]+)\}`)
// ////////////////////////////////////////////////////////////////////////////////// //
// readData reads data from given reader
func readData(r io.Reader) (*Config, error) {
config := &Config{
data: make(map[string]string),
mx: &sync.RWMutex{},
}
var isDataRead bool
var section string
var lineNum int
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := strings.Trim(scanner.Text(), " \t")
lineNum++
if line == "" || strings.HasPrefix(line, _SYMBOL_COMMENT) {
continue
}
isDataRead = true
if strings.HasPrefix(line, _SYMBOL_SECTION_START) &&
strings.HasPrefix(line, _SYMBOL_SECTION_END) {
section = strings.Trim(line, "[]")
config.data[strings.ToLower(section)] = "!"
config.sections = append(config.sections, section)
continue
}
if section == "" {
return nil, fmt.Errorf("Error at line %d: Data defined before section", lineNum)
}
propName, propValue, err := parseProperty(line, config)
if err != nil {
return nil, fmt.Errorf("Error at line %d: %w", lineNum, err)
}
fullPropName := genPropName(section, propName)
if config.HasProp(fullPropName) {
return nil, fmt.Errorf("Error at line %d: Property %q defined more than once", lineNum, propName)
}
config.props = append(config.props, fullPropName)
config.data[strings.ToLower(fullPropName)] = propValue
}
if !isDataRead {
return nil, fmt.Errorf("Configuration file doesn't contain any valid data")
}
return config, scanner.Err()
}
// parseProperty parses line with property name and value
func parseProperty(line string, config *Config) (string, string, error) {
di := strings.Index(line, _SYMBOL_DELIMITER)
if di == -1 {
return "", "", fmt.Errorf(`Property must have ":" as a delimiter`)
}
name, value := line[:di], line[di+1:]
name = strings.Trim(name, " \t")
value = strings.Trim(value, " \t")
if !strings.Contains(value, _SYMBOL_MACRO_START) &&
!strings.Contains(value, _SYMBOL_MACRO_END) {
return name, value, nil
}
var err error
value, err = evalMacros(value, config)
if err != nil {
return "", "", err
}
return name, value, nil
}
// evalMacros evaluates all macros in given string
func evalMacros(value string, config *Config) (string, error) {
macros := macroRE.FindAllStringSubmatch(value, -1)
for _, macro := range macros {
full, section, prop := macro[0], macro[1], macro[2]
if !config.HasProp(genPropName(section, prop)) {
return "", fmt.Errorf("Unknown property %s", full)
}
propVal := config.GetS(genPropName(section, prop))
value = strings.ReplaceAll(value, full, propVal)
}
return value, nil
}
// genPropName generates "full property name" which contains section and
// property name
func genPropName(section, prop string) string {
return section + _SYMBOL_DELIMITER + prop
}