-
Notifications
You must be signed in to change notification settings - Fork 16
/
parse.go
82 lines (66 loc) · 1.78 KB
/
parse.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
package ini
import (
"strings"
"github.com/gookit/ini/v2/parser"
)
// parse and load data
func (c *Ini) parse(data string) (err error) {
if strings.TrimSpace(data) == "" {
return
}
p := parser.NewLite()
p.Collector = c.valueCollector
p.IgnoreCase = c.opts.IgnoreCase
p.DefSection = c.opts.DefSection
return p.ParseString(data)
}
// collect value form parser
func (c *Ini) valueCollector(section, key, val string, isSlice bool) {
if c.opts.IgnoreCase {
key = strings.ToLower(key)
section = strings.ToLower(section)
}
// if opts.ParseEnv is true. will parse like: "${SHELL}".
// CHANGE: parse ENV on get value
// - if parse on there, will loss vars on exported data.
// if c.opts.ParseEnv {
// val = c.parseEnvValue(val)
// }
if c.opts.ReplaceNl {
val = strings.ReplaceAll(val, `\n`, "\n")
}
if sec, ok := c.data[section]; ok {
sec[key] = val
c.data[section] = sec
} else {
// create the section if it does not exist
c.data[section] = Section{key: val}
}
}
// parse var reference
func (c *Ini) parseVarReference(key, valStr string, sec Section) string {
if c.opts.VarOpen != "" && strings.Index(valStr, c.opts.VarOpen) == -1 {
return valStr
}
// http://%(host)s:%(port)s/Portal
// %(section:key)s key in the section
vars := c.varRegex.FindAllString(valStr, -1)
if len(vars) == 0 {
return valStr
}
varOLen, varCLen := len(c.opts.VarOpen), len(c.opts.VarClose)
var name string
var oldNew []string
for _, fVar := range vars {
realVal := fVar
name = fVar[varOLen : len(fVar)-varCLen]
// first, find from current section
if val, ok := sec[name]; ok && key != name {
realVal = val
} else if val, ok := c.getValue(name); ok {
realVal = val
}
oldNew = append(oldNew, fVar, realVal)
}
return strings.NewReplacer(oldNew...).Replace(valStr)
}