Skip to content

Commit 2372f1c

Browse files
authored
add float option (#8)
Signed-off-by: William Huang <ai.william@outlook.com>
1 parent 2169a70 commit 2372f1c

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

config_opts.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ func (c *ConfigOpts) RegisterOpt(group *OptGroup, opt interface{}) {
7171
c.registerBoolOpt(section, v)
7272
case *IntOpt:
7373
c.registerIntOpt(section, v)
74+
case *FloatOpt:
75+
c.registerFloatOpt(section, v)
7476
case *ListOpt:
7577
c.registerListOpt(section, v)
7678
case *URIOpt:
@@ -139,6 +141,23 @@ func (c *ConfigOpts) GetInt(group, option string) (int, error) {
139141
return c.cfg.Section(group).Key(option).Int()
140142
}
141143

144+
func (c *ConfigOpts) registerFloatOpt(section *ini.Section, opt *FloatOpt) {
145+
if section == nil || opt == nil {
146+
return
147+
}
148+
149+
if section.HasKey(opt.Name()) {
150+
return
151+
}
152+
153+
value := strconv.FormatFloat(opt.Default(), "f", -1, 64)
154+
_, _ = section.NewKey(opt.Name(), value)
155+
}
156+
157+
func (c *ConfigOpts) GetFloat(group, option string) (float64, error) {
158+
return c.cfg.Section(group).Key(option).Float64()
159+
}
160+
142161
func (c *ConfigOpts) registerListOpt(section *ini.Section, opt *ListOpt) {
143162
if section == nil || opt == nil {
144163
return

float_opt.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2021 TFCloud Co.,Ltd. All rights reserved.
2+
// This source code is licensed under Apache-2.0 license
3+
// that can be found in the LICENSE file.
4+
5+
package config
6+
7+
type FloatOpt struct {
8+
name string
9+
defaultValue float64
10+
}
11+
12+
func NewFloatOpt(name string) *FloatOpt {
13+
return &FloatOpt{
14+
name: name,
15+
}
16+
}
17+
18+
func (o *FloatOpt) WithDefault(value float64) *FloatOpt {
19+
o.defaultValue = value
20+
return o
21+
}
22+
23+
func (o *FloatOpt) Name() string {
24+
return o.name
25+
}
26+
27+
func (o *FloatOpt) Default() float64 {
28+
return o.defaultValue
29+
}

0 commit comments

Comments
 (0)