-
Notifications
You must be signed in to change notification settings - Fork 1
/
kind_float.go
73 lines (62 loc) · 1.65 KB
/
kind_float.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
package semantic
import (
"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 Precision int
type Float struct {
*Value
precision Precision
}
func NewFloat(section *inireader.Section, key string, precision Precision, opts ...ValueOption) *Float {
v := NewValue(section, key)
for _, opt := range opts {
opt(v)
}
s := &Float{
Value: v,
precision: precision,
}
return s
}
func (s *Float) Get() float64 {
if s.optional && len(s.section.ParamMap[s.key]) == 0 {
return 0
}
return s.section.ParamMap[s.key][s.index].Values[s.order].(inireader.ValueNumber).Value
}
func (s *Float) GetValue() (float64, bool) {
var value float64
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 *Float) Set(value float64) {
if s.isComment() {
s.Delete()
}
processed_value := inireader.UniParseFloat(value, int(s.precision))
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 *Float) 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:]...)
}
}
}