-
Notifications
You must be signed in to change notification settings - Fork 1
/
kind_string.go
96 lines (81 loc) · 2.05 KB
/
kind_string.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
package semantic
import (
"strings"
"github.com/darklab8/fl-configs/configs/configs_mapped/parserutils/inireader"
"github.com/darklab8/fl-configs/configs/settings/logus"
"github.com/darklab8/go-typelog/typelog"
)
type String struct {
*Value
remove_spaces bool
lowercase bool
}
type StringOption func(s *String)
func WithoutSpacesS() StringOption {
return func(s *String) { s.remove_spaces = true }
}
func WithLowercaseS() StringOption {
return func(s *String) { s.lowercase = true }
}
func OptsS(opts ...ValueOption) StringOption {
return func(s *String) {
for _, opt := range opts {
opt(s.Value)
}
}
}
func NewString(section *inireader.Section, key string, opts ...StringOption) *String {
v := NewValue(section, key)
s := &String{Value: v}
for _, opt := range opts {
opt(s)
}
return s
}
func (s *String) Get() string {
if s.optional && len(s.section.ParamMap[s.key]) == 0 {
return ""
}
value := s.section.ParamMap[s.key][s.index].Values[s.order].AsString()
if s.remove_spaces {
value = strings.ReplaceAll(value, " ", "")
}
if s.lowercase {
value = strings.ToLower(value)
}
return value
}
func (s *String) GetValue() (string, bool) {
var value string
var ok bool = true
func() {
defer func() {
if r := recover(); r != nil {
logus.Log.Debug("Recovered from int GetValue Error:\n", typelog.Any("recover", r))
ok = false
}
}()
value = s.Get()
}()
return value, ok
}
func (s *String) Set(value string) {
if s.isComment() {
s.Delete()
}
processed_value := inireader.UniParseStr(value)
if len(s.section.ParamMap[s.key]) == 0 {
s.section.AddParamToStart(s.key, (&inireader.Param{IsComment: s.isComment()}).AddValue(processed_value))
}
// implement SetValue in Section
s.section.ParamMap[s.key][0].First = processed_value
s.section.ParamMap[s.key][0].Values[0] = processed_value
}
func (s *String) Delete() {
delete(s.section.ParamMap, s.key)
for index, param := range s.section.Params {
if param.Key == s.key {
s.section.Params = append(s.section.Params[:index], s.section.Params[index+1:]...)
}
}
}