Skip to content

Commit

Permalink
Adding support for basic numeric types (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
zoido authored Aug 25, 2020
1 parent eda9b83 commit 0ab6757
Show file tree
Hide file tree
Showing 10 changed files with 1,023 additions and 68 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ fmt.Printf("config.Duration: %v\n", cfg.Duration)
## Supported types

- `str`
- `int`
- `int`, `int8`, `int16`, `int32`, `int64`
- `uint`, `uint8`, `uint16`, `uint32`, `uint64`
- `float32`, `float64`
- `bool`
- `time.Duration`
- any `flag.Value` implementation (e.g.[(github.com/sgreben/flagvar](https://github.com/sgreben/flagvar))
- any `flag.Value` implementation (e.g. [(github.com/sgreben/flagvar](https://github.com/sgreben/flagvar))
- more to come…

## Credits
Expand Down
18 changes: 0 additions & 18 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,6 @@ func (sv *stringValue) String() string {
return *sv.dest
}

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 boolValue struct {
dest *bool
}
Expand Down
39 changes: 39 additions & 0 deletions types_floats.go
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)
}
131 changes: 131 additions & 0 deletions types_floats_test.go
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")
}
93 changes: 93 additions & 0 deletions types_ints.go
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)
}
Loading

0 comments on commit 0ab6757

Please sign in to comment.