-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for basic numeric types (#16)
- Loading branch information
Showing
10 changed files
with
1,023 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package yag | ||
|
||
import "strconv" | ||
|
||
type float32Value struct { | ||
dest *float32 | ||
} | ||
|
||
func (iv *float32Value) Set(val string) error { | ||
num, err := strconv.ParseFloat(val, 32) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*iv.dest = float32(num) | ||
return nil | ||
} | ||
|
||
func (iv *float32Value) String() string { | ||
return strconv.FormatFloat(float64(*iv.dest), 'G', -1, 32) | ||
} | ||
|
||
type float64Value struct { | ||
dest *float64 | ||
} | ||
|
||
func (iv *float64Value) Set(val string) error { | ||
num, err := strconv.ParseFloat(val, 64) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*iv.dest = float64(num) | ||
return nil | ||
} | ||
|
||
func (iv *float64Value) String() string { | ||
return strconv.FormatFloat(float64(*iv.dest), 'G', -1, 64) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package yag_test | ||
|
||
import ( | ||
"github.com/zoido/yag-config" | ||
) | ||
|
||
func (s *TypesTestSuite) TestFloat32() { | ||
// Given | ||
var num float32 = 6.626e-34 | ||
|
||
y := yag.New() | ||
y.Float32(&num, "float32", "") | ||
|
||
// When | ||
err := y.Parse([]string{"-float32=3.14"}) | ||
|
||
// Then | ||
s.Require().NoError(err) | ||
s.Require().Equal(float32(3.14), num) | ||
} | ||
|
||
func (s *TypesTestSuite) TestFloat32_DefaultValue() { | ||
// Given | ||
var num float32 = 2.72 | ||
|
||
y := yag.New() | ||
y.Float32(&num, "int", "") | ||
|
||
// When | ||
err := y.Parse([]string{}) | ||
|
||
// Then | ||
s.Require().NoError(err) | ||
s.Require().Equal(float32(2.72), num) | ||
} | ||
|
||
func (s *TypesTestSuite) TestFloat32_ParseError() { | ||
// Given | ||
var num float32 = 6.626e-34 | ||
|
||
y := yag.New() | ||
y.Float32(&num, "float32", "") | ||
|
||
// When | ||
err := y.Parse([]string{"-float32=3.14f"}) | ||
|
||
// Then | ||
s.Require().Error(err) | ||
s.Require().Contains(err.Error(), "invalid value") | ||
s.Require().Contains(err.Error(), "3.14") | ||
s.Require().Contains(err.Error(), "-float32") | ||
} | ||
|
||
func (s *TypesTestSuite) TestFloat32_ParseError_Overflow() { | ||
// Given | ||
var num float32 = 6.626e-34 | ||
|
||
y := yag.New() | ||
y.Float32(&num, "float32", "") | ||
|
||
// When | ||
err := y.Parse([]string{"-float32=3e40"}) | ||
|
||
// Then | ||
s.Require().Error(err) | ||
s.Require().Contains(err.Error(), "out of range") | ||
s.Require().Contains(err.Error(), "-float32") | ||
} | ||
|
||
func (s *TypesTestSuite) TestFloat64() { | ||
// Given | ||
var num float64 = 6.626e-34 | ||
|
||
y := yag.New() | ||
y.Float64(&num, "float64", "") | ||
|
||
// When | ||
err := y.Parse([]string{"-float64=3.14"}) | ||
|
||
// Then | ||
s.Require().NoError(err) | ||
s.Require().Equal(float64(3.14), num) | ||
} | ||
|
||
func (s *TypesTestSuite) TestFloat64_DefaultValue() { | ||
// Given | ||
var num float64 = 2.72 | ||
|
||
y := yag.New() | ||
y.Float64(&num, "int", "") | ||
|
||
// When | ||
err := y.Parse([]string{}) | ||
|
||
// Then | ||
s.Require().NoError(err) | ||
s.Require().Equal(float64(2.72), num) | ||
} | ||
|
||
func (s *TypesTestSuite) TestFloat64_ParseError() { | ||
// Given | ||
var num float64 = 6.626e-34 | ||
|
||
y := yag.New() | ||
y.Float64(&num, "float64", "") | ||
|
||
// When | ||
err := y.Parse([]string{"-float64=3.14f"}) | ||
|
||
// Then | ||
s.Require().Error(err) | ||
s.Require().Contains(err.Error(), "invalid value") | ||
s.Require().Contains(err.Error(), "3.14") | ||
s.Require().Contains(err.Error(), "-float64") | ||
} | ||
|
||
func (s *TypesTestSuite) TestFloat64_ParseError_Overflow() { | ||
// Given | ||
var num float64 = 6.626e-34 | ||
|
||
y := yag.New() | ||
y.Float64(&num, "float64", "") | ||
|
||
// When | ||
err := y.Parse([]string{"-float64=3e320"}) | ||
|
||
// Then | ||
s.Require().Error(err) | ||
s.Require().Contains(err.Error(), "out of range") | ||
s.Require().Contains(err.Error(), "-float64") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package yag | ||
|
||
import "strconv" | ||
|
||
type intValue struct { | ||
dest *int | ||
} | ||
|
||
func (iv *intValue) Set(val string) error { | ||
num, err := strconv.Atoi(val) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*iv.dest = num | ||
return nil | ||
} | ||
|
||
func (iv *intValue) String() string { | ||
return strconv.Itoa(*iv.dest) | ||
} | ||
|
||
type int8Value struct { | ||
dest *int8 | ||
} | ||
|
||
func (iv *int8Value) Set(val string) error { | ||
num, err := strconv.ParseInt(val, 10, 8) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*iv.dest = int8(num) | ||
return nil | ||
} | ||
|
||
func (iv *int8Value) String() string { | ||
return strconv.FormatInt(int64(*iv.dest), 10) | ||
} | ||
|
||
type int16Value struct { | ||
dest *int16 | ||
} | ||
|
||
func (iv *int16Value) Set(val string) error { | ||
num, err := strconv.ParseInt(val, 10, 16) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*iv.dest = int16(num) | ||
return nil | ||
} | ||
|
||
func (iv *int16Value) String() string { | ||
return strconv.FormatInt(int64(*iv.dest), 10) | ||
} | ||
|
||
type int32Value struct { | ||
dest *int32 | ||
} | ||
|
||
func (iv *int32Value) Set(val string) error { | ||
num, err := strconv.ParseInt(val, 10, 32) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*iv.dest = int32(num) | ||
return nil | ||
} | ||
|
||
func (iv *int32Value) String() string { | ||
return strconv.FormatInt(int64(*iv.dest), 10) | ||
} | ||
|
||
type int64Value struct { | ||
dest *int64 | ||
} | ||
|
||
func (iv *int64Value) Set(val string) error { | ||
num, err := strconv.ParseInt(val, 10, 64) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*iv.dest = num | ||
return nil | ||
} | ||
|
||
func (iv *int64Value) String() string { | ||
return strconv.FormatInt(*iv.dest, 10) | ||
} |
Oops, something went wrong.