Skip to content

Commit

Permalink
ptr tests and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jpughcs authored and iamolegga committed Dec 14, 2020
1 parent dae64c0 commit 2e1108f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ type barry struct {
type bazzy struct {
Baz bool
}
type quxxy struct {
Qux string
}
type config struct {
Foo string
Barry barry
Barries map[string]barry
Bazzy bazzy `mapstructure:",squash"`
Foo string
Barry barry
Barries map[string]barry
Bazzy bazzy `mapstructure:",squash"`
Quxxy *quxxy
}

// For example this kind of structure can be unmarshaled with next yaml:
Expand All @@ -56,18 +60,22 @@ type config struct {
// Bar: 255
// key2:
// Bar: 256
// Quxxy:
// Qux: "lorem"
//
// And then it could be overriden by next env variables:
// FOO=foo
// BARRY_BAR=42
// BAZ=true
// BARRIES_KEY1_BAR=42
// QUXXY_QUX=ipsum
//
// Or with prefix:
// MYAPP_FOO=foo
// MYAPP_BARRY_BAR=42
// MYAPP_BAZ=true
// MYAPP_BARRIES_KEY1_BAR=42
// MYAPP_QUXXY_QUX=ipsum

func main() {
var c config
Expand Down
10 changes: 10 additions & 0 deletions enviper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func (s *UnmarshalSuite) TestWithoutEnvs() {
s.Equal("foo", c.Foo)
s.Equal(1, c.Bar.BAZ)
s.Equal(true, c.QUX.Quuux)
s.Equal("testptr1", c.FooPtr.Value)
s.Equal("testptr2", c.QUX.QuuuxPtr.Value)
}

func (s *UnmarshalSuite) TestWithEnvs() {
Expand All @@ -84,6 +86,8 @@ func (s *UnmarshalSuite) TestWithEnvs() {
s.Equal(false, c.QUX.Quuux)
s.Equal(true, c.QuxMap["key1"].Quuux)
s.Equal(false, c.QuxMap["key2"].Quuux)
s.Equal("testptr1", c.FooPtr.Value)
s.Equal("testptr2", c.QUX.QuuuxPtr.Value)

s.tearDownEnvConfig()
}
Expand Down Expand Up @@ -138,6 +142,7 @@ func (s *UnmarshalSuite) loadEnvVars() {

type Config struct {
Foo string
FooPtr *PtrTest
Bar struct {
BAZ int `mapstructure:"baz"`
} `mapstructure:"bar"`
Expand All @@ -149,6 +154,11 @@ type Config struct {

type QUX struct {
Quuux bool
QuuuxPtr *PtrTest `mapstructure:"quuux_ptr"`
}

type PtrTest struct {
Value string
}

func TestNew(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions fixture.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Foo: foo
FooPtr:
Value: testptr1
bar:
baz: 1
QuxMap:
Expand All @@ -7,3 +9,5 @@ QuxMap:
key2:
Quuux: false
Quuux: true
quuux_ptr:
Value: testptr2

0 comments on commit 2e1108f

Please sign in to comment.