-
Notifications
You must be signed in to change notification settings - Fork 0
/
property.go
58 lines (47 loc) · 1.04 KB
/
property.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
package confi
import (
"bufio"
"io"
"strings"
"github.com/pkg/errors"
)
type Property struct {
Path []string
Value string
}
func (p Property) Key() string {
return strings.Join(p.Path, ".")
}
func getProperty(text, sep string, expandBools bool) (*Property, error) {
tokens := strings.SplitN(text, "=", 2)
if len(tokens[0]) == 0 {
return nil, errors.New("empty property name")
}
if len(tokens) == 1 {
if expandBools {
tokens = append(tokens, "true")
} else {
return nil, errors.Errorf("empty property value")
}
}
return &Property{
Path: strings.Split(tokens[0], sep),
Value: tokens[1],
}, nil
}
func readProperties(reader io.Reader) ([]Property, error) {
var props []Property
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := strings.Trim(scanner.Text(), " ")
if line == "" || strings.HasPrefix(line, "#") {
continue
}
prop, err := getProperty(line, ".", false)
if err != nil {
return nil, errors.Wrapf(err, `"%s""`, line)
}
props = append(props, *prop)
}
return props, nil
}