-
Notifications
You must be signed in to change notification settings - Fork 16
/
options.go
99 lines (84 loc) · 2.09 KB
/
options.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
package parser
// mode of parse data
//
// ModeFull - will parse array value and inline array
// ModeLite/ModeSimple - don't parse array value line
const (
ModeFull parseMode = 1
ModeLite parseMode = 2
ModeSimple parseMode = 2 // alias of ModeLite
)
// DefSection default section key name
const DefSection = "__default"
type parseMode uint8
// Unit8 mode value to uint8
func (m parseMode) Unit8() uint8 {
return uint8(m)
}
// TagName default tag-name of mapping data to struct
var TagName = "ini"
// OptFunc define
type OptFunc func(opt *Options)
// UserCollector custom data collector.
//
// Notice: in lite mode, isSlice always is false.
type UserCollector func(section, key, val string, isSlice bool)
// Options for parser
type Options struct {
// TagName of mapping data to struct
TagName string
// ParseMode setting. default is ModeLite
ParseMode parseMode
// Ignore case for key name
IgnoreCase bool
// ReplaceNl replace the "\n" to newline
ReplaceNl bool
// default section name. default is "__default"
DefSection string
// NoDefSection setting. only for full parse mode
NoDefSection bool
// InlineComment support parse inline comments. default is false
InlineComment bool
// Collector allow you custom the value collector.
//
// Notice: in lite mode, isSlice always is false.
Collector UserCollector
}
// NewOptions instance
func NewOptions(fns ...OptFunc) *Options {
opt := &Options{
TagName: TagName,
ParseMode: ModeLite,
DefSection: DefSection,
}
for _, fn := range fns {
fn(opt)
}
return opt
}
// WithParseMode name for parse
func WithParseMode(mode parseMode) OptFunc {
return func(opt *Options) {
opt.ParseMode = mode
}
}
// InlineComment for parse
func InlineComment(opt *Options) {
opt.InlineComment = true
}
// WithReplaceNl for parse
func WithReplaceNl(opt *Options) {
opt.ReplaceNl = true
}
// WithDefSection name for parse
func WithDefSection(name string) OptFunc {
return func(opt *Options) {
opt.DefSection = name
}
}
// WithTagName for decode data
func WithTagName(name string) OptFunc {
return func(opt *Options) {
opt.TagName = name
}
}