Skip to content

Commit

Permalink
int64 support (#31)
Browse files Browse the repository at this point in the history
* add testing time.Duration, int64

* added support for int64

* goenv int64 support

* goflags int64 support

* goenv test int64

* goflags test for int64
  • Loading branch information
Puppollo authored and crgimenes committed Oct 24, 2018
1 parent c1f2e88 commit 4e9292a
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 7 deletions.
1 change: 1 addition & 0 deletions goenv/goenv.go
Expand Up @@ -26,6 +26,7 @@ func Setup(tag string, tagDefault string) {
SetTag(tag)
SetTagDefault(tagDefault)

structtag.ParseMap[reflect.Int64] = reflectInt
structtag.ParseMap[reflect.Int] = reflectInt
structtag.ParseMap[reflect.Float64] = reflectFloat
structtag.ParseMap[reflect.String] = reflectString
Expand Down
24 changes: 21 additions & 3 deletions goenv/goenv_test.go
Expand Up @@ -3,16 +3,20 @@ package goenv
import (
"os"
"testing"
"time"
)

type testStruct struct {
A int `cfg:"A" cfgDefault:"100"`
B string `cfg:"B" cfgDefault:"200"`
C string
D bool `cfg:"D" cfgDefault:"true"`
D bool `cfg:"D" cfgDefault:"true"`
E time.Duration `cfg:"E"`
F float64
G float64 `cfg:"G" cfgDefault:"3.05"`
N string `cfg:"-"`
G float64 `cfg:"G" cfgDefault:"3.05"`
H int64 `cfg:"H"`
I time.Duration `cfg:"I" cfgDefault:"5000"`
N string `cfg:"-"`
M int
p string
S testSub `cfg:"S"`
Expand All @@ -37,6 +41,8 @@ func TestParse(t *testing.T) {
os.Setenv("B", "TEST")
os.Setenv("D", "true")
os.Setenv("F", "23.6")
os.Setenv("E", "500")
os.Setenv("H", "1000")

s := &testStruct{A: 1, F: 1.0, S: testSub{A: 1, B: "2"}}
err := Parse(s)
Expand All @@ -56,6 +62,18 @@ func TestParse(t *testing.T) {
t.Fatal("s.D == true, s.D:", s.D)
}

if s.E != time.Nanosecond*500 {
t.Fatal("s.E != 500ns, s.E:", s.E)
}

if s.H != 1000 {
t.Fatal("s.H != 1000, s.H:", s.H)
}

if s.I != time.Nanosecond*5000 {
t.Fatal("s.I != 5000ns, s.I:", s.I)
}

if s.F != 23.6 {
t.Fatal("s.F != 23.6, s.F:", s.F)
}
Expand Down
1 change: 1 addition & 0 deletions goflags/goflags.go
Expand Up @@ -42,6 +42,7 @@ func Setup(tag string, tagDefault string) {
SetTag(tag)
SetTagDefault(tagDefault)

structtag.ParseMap[reflect.Int64] = reflectInt
structtag.ParseMap[reflect.Int] = reflectInt
structtag.ParseMap[reflect.Float64] = reflectFloat
structtag.ParseMap[reflect.String] = reflectString
Expand Down
29 changes: 26 additions & 3 deletions goflags/goflags_test.go
Expand Up @@ -3,15 +3,18 @@ package goflags
import (
"os"
"testing"
"time"
)

type testStruct struct {
A int `flag:"A" flagDefault:"100"`
B string `flag:"B" flagDefault:"200"`
C string
D bool `cfg:"D" cfgDefault:"true"`
D bool `cfg:"D" cfgDefault:"true"`
E time.Duration `flag:"E"`
F float64
G float64 `cfg:"G" cfgDefault:"3.05"`
H int64 `flag:"H" flagDefault:"500"`
N string `flag:"-"`
M int
p string
Expand All @@ -21,12 +24,15 @@ type testStruct struct {
type testSub struct {
A int `flag:"A" flagDefault:"300"`
B string `flag:"C" flagDefault:"400"`
C int64 `flag:"D" flagDefault:"500"`
S testSubSub `flag:"S"`
}

type testSubSub struct {
A int `flag:"A" flagDefault:"500"`
B string `flag:"S" flagDefault:"600"`
A int `flag:"A" flagDefault:"500"`
B string `flag:"S" flagDefault:"600"`
C int64 `flag:"C" flagDefault:"100"`
E time.Duration `flag:"E" flagDefault:"1000"`
}

func TestParse(t *testing.T) {
Expand All @@ -42,7 +48,9 @@ func TestParse(t *testing.T) {
"-b=TEST",
"-d=true",
"-s_s_a=99999",
"-s_s_e=5000",
"-f=23.6",
"-e=1000",
}

s := &testStruct{A: 1, S: testSub{A: 1, B: "2"}}
Expand All @@ -69,10 +77,25 @@ func TestParse(t *testing.T) {
t.Fatal("s.F != 23.6, s.F:", s.F)
}

if s.E != time.Nanosecond*1000 {
t.Fatal("s.E != 1000ns, s.E:", s.E)
}

if s.H != 500 {
t.Fatal("s.H != 500, s.H:", s.H)
}

if s.S.C != 500 {
t.Fatal("s.S.C != 500, s.S.C :", s.S.C)
}

if s.S.S.A != 99999 {
t.Fatal("s.S.S.A != 99999, s.S.S.A :", s.S.S.A)
}

if s.S.S.E != time.Nanosecond*5000 {
t.Fatal("s.E != 5000ns, s.E:", s.E)
}
}

func TestPreserve(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions validate/validate.go
Expand Up @@ -22,6 +22,7 @@ func Setup(tag string, tagDefault string) {
SetTag(tag)
SetTagDefault(tagDefault)

structtag.ParseMap[reflect.Int64] = reflectInt
structtag.ParseMap[reflect.Int] = reflectInt
structtag.ParseMap[reflect.Float64] = reflectFloat
structtag.ParseMap[reflect.String] = reflectString
Expand Down
5 changes: 4 additions & 1 deletion validate/validate_test.go
Expand Up @@ -2,6 +2,7 @@ package validate

import (
"testing"
"time"
)

type testStruct struct {
Expand All @@ -11,7 +12,9 @@ type testStruct struct {
D bool `cfg:"D" cfgDefault:"true"`
F float64
G float64 `cfg:"G" cfgDefault:"3.05"`
N string `cfg:"-"`
H time.Duration
I int64
N string `cfg:"-"`
M int
p string
S testSub `cfg:"S"`
Expand Down

0 comments on commit 4e9292a

Please sign in to comment.