-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_properties.go
More file actions
41 lines (37 loc) · 1.04 KB
/
parser_properties.go
File metadata and controls
41 lines (37 loc) · 1.04 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
package variables
import (
"strings"
"github.com/pkg/errors"
)
func (p *Variables) FromProperties(src, namespace string) error {
return p.FromPropertiesFilter(src, namespace, func(key string) bool {
return true
})
}
func (p *Variables) FromPropertiesFilter(src, namespace string, filter func(key string) bool) error {
if namespace != "" {
namespace = strings.Trim(namespace, ".") + "."
}
lines := strings.Split(src, "\n")
for i := 0; i < len(lines); i++ {
line := lines[i]
if strings.TrimSpace(line) == "" || strings.TrimSpace(line)[0] == '#' {
continue
}
key, value, found := strings.Cut(line, "=")
if !found {
return errors.Errorf("properties 格式错误,位于 %d 行", i)
}
for strings.HasSuffix(value, "\\") && i+1 < len(lines) {
i = i + 1
value = value[:len(value)-1] + lines[i]
}
value = strings.ReplaceAll(value, "\\n", "\n")
if filter(namespace + key) {
if err := p.Set(namespace+key, value); err != nil {
return errors.Wrapf(err, "properties 格式错误,位于 %d 行", i)
}
}
}
return nil
}