Skip to content

Commit 7028852

Browse files
authored
add support for float variables (#8)
1 parent 98fcc0e commit 7028852

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

env.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,50 @@ func (v *int64Value) String() string {
9090
return strconv.FormatInt(int64(*v), 10)
9191
}
9292

93+
type float32Value float32
94+
95+
func newFloat32Value(x float32, p *float32) *float32Value {
96+
*p = x
97+
return (*float32Value)(p)
98+
}
99+
100+
func (v *float32Value) Set(x string) error {
101+
n, err := strconv.ParseFloat(x, 32)
102+
*v = float32Value(n)
103+
if err != nil {
104+
if ne, ok := err.(*strconv.NumError); ok {
105+
return errors.New("invalid 32-bit float " + strconv.Quote(ne.Num) + ": " + ne.Err.Error())
106+
}
107+
}
108+
return err
109+
}
110+
111+
func (v *float32Value) String() string {
112+
return strconv.FormatFloat(float64(*v), 'g', -1, 32)
113+
}
114+
115+
type float64Value float64
116+
117+
func newFloat64Value(x float64, p *float64) *float64Value {
118+
*p = x
119+
return (*float64Value)(p)
120+
}
121+
122+
func (v *float64Value) Set(x string) error {
123+
n, err := strconv.ParseFloat(x, 64)
124+
*v = float64Value(n)
125+
if err != nil {
126+
if ne, ok := err.(*strconv.NumError); ok {
127+
return errors.New("invalid 64-bit float " + strconv.Quote(ne.Num) + ": " + ne.Err.Error())
128+
}
129+
}
130+
return err
131+
}
132+
133+
func (v *float64Value) String() string {
134+
return strconv.FormatFloat(float64(*v), 'g', -1, 64)
135+
}
136+
93137
type durationValue time.Duration
94138

95139
func newDurationValue(x time.Duration, p *time.Duration) *durationValue {
@@ -234,6 +278,22 @@ func (v *VarSet) Int64(name string, usage string) *int64 {
234278
return p
235279
}
236280

281+
// Float32 defines an float32 variable with specified name, usage string and validation checks.
282+
// The return value is the address of an float32 variable that stores the value of the variable.
283+
func (v *VarSet) Float32(name string, usage string) *float32 {
284+
p := new(float32)
285+
v.Var(newFloat32Value(0, p), name, usage)
286+
return p
287+
}
288+
289+
// Float64 defines an float64 variable with specified name, usage string and validation checks.
290+
// The return value is the address of an float64 variable that stores the value of the variable.
291+
func (v *VarSet) Float64(name string, usage string) *float64 {
292+
p := new(float64)
293+
v.Var(newFloat64Value(0, p), name, usage)
294+
return p
295+
}
296+
237297
// Bool defines a bool variable with specified name, usage string and validation checks.
238298
// The return value is the address of a bool variable that stores the value of the variable.
239299
func (v *VarSet) Bool(name string, usage string) *bool {
@@ -414,6 +474,18 @@ func Int64(name string, usage string) *int64 {
414474
return CmdVar.Int64(name, usage)
415475
}
416476

477+
// Float32 defines an float32 variable with specified name and usage string.
478+
// The return value is the address of an int variable that stores the value of the variable.
479+
func Float32(name string, usage string) *float32 {
480+
return CmdVar.Float32(name, usage)
481+
}
482+
483+
// Float64 defines an float64 variable with specified name and usage string.
484+
// The return value is the address of an int variable that stores the value of the variable.
485+
func Float64(name string, usage string) *float64 {
486+
return CmdVar.Float64(name, usage)
487+
}
488+
417489
// Bool defines a bool variable with specified name and usage string.
418490
// The return value is the address of a bool variable that stores the value of the variable.
419491
func Bool(name string, usage string) *bool {

env_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ func TestAll(t *testing.T) {
2323
env.URL("URL", "URL test")
2424
env.String("STRING", "string test")
2525
env.Duration("TIMEOUT", "timeout test")
26+
env.Float32("FLOAT32", "float32 test")
27+
env.Float64("FLOAT64", "float64 test")
2628

2729
tg := testGetter{
2830
"TEST_BOOL": "true",
@@ -32,6 +34,8 @@ func TestAll(t *testing.T) {
3234
"TEST_URL": "http://localhost:1234/api",
3335
"TEST_STRING": "name",
3436
"TEST_TIMEOUT": "1m1s",
37+
"TEST_FLOAT32": "1.23",
38+
"TEST_FLOAT64": "1.24",
3539
}
3640

3741
if err := env.CmdVar.Parse(tg); err != nil {

0 commit comments

Comments
 (0)