Skip to content

Commit

Permalink
String and bool ptr support improved
Browse files Browse the repository at this point in the history
  • Loading branch information
markdicksonjr committed Feb 28, 2020
1 parent a185c1a commit 27aef62
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/markdicksonjr/config
go 1.12

require (
github.com/markdicksonjr/dot v0.6.15
github.com/markdicksonjr/dot v0.8.0
github.com/stretchr/testify v1.4.0 // indirect
gopkg.in/yaml.v2 v2.2.2
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/markdicksonjr/dot v0.6.15 h1:qAw8NhyGzmC0mbu3Imxxx2wtgGT6qs3tk6PHUwpKWCQ=
github.com/markdicksonjr/dot v0.6.15/go.mod h1:56w2FRih96QqJky5Wu1dn5bJcJKEaq5HSOo//x+y1rw=
github.com/markdicksonjr/dot v0.8.0 h1:tf9/zUpl17duOukWjOGPALwjWpLwNynU0OKAfnlRf7Y=
github.com/markdicksonjr/dot v0.8.0/go.mod h1:56w2FRih96QqJky5Wu1dn5bJcJKEaq5HSOo//x+y1rw=
github.com/oleiade/reflections v1.0.0 h1:0ir4pc6v8/PJ0yw5AEtMddfXpWBXg9cnG7SgSoJuCgY=
github.com/oleiade/reflections v1.0.0/go.mod h1:RbATFBbKYkVdqmSFtx13Bb/tVhR0lgOBXunWTZKeL4w=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
36 changes: 36 additions & 0 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ type stringVar struct {
func applyFlags(flagCfg interface{}) error {
keys := dot.KeysRecursiveLeaves(flagCfg)
var boolFlags []boolVar
var boolPtrFlags []boolVar
var stringFlags []stringVar
var stringPtrFlags []stringVar

for _, key := range keys {
k := strings.ReplaceAll(key, ".", "-")
Expand Down Expand Up @@ -155,6 +157,28 @@ func applyFlags(flagCfg interface{}) error {
continue
}

if dvp, ok := dotVal.(*bool); ok {
boolFlag := boolVar{
Name: k,
Key: key,
Ptr: dvp,
}
flag.BoolVar(dvp, boolFlag.Name, false, "")
boolPtrFlags = append(boolPtrFlags, boolFlag)
continue
}

if svp, ok := dotVal.(*string); ok {
strFlag := stringVar{
Name: k,
Key: key,
Ptr: svp,
}
flag.StringVar(svp, strFlag.Name, "", "")
stringPtrFlags = append(stringPtrFlags, strFlag)
continue
}

// TODO: more types
}

Expand All @@ -167,13 +191,25 @@ func applyFlags(flagCfg interface{}) error {
}
}

for _, boolPtrKey := range boolPtrFlags {
if err := dot.Set(flagCfg, boolPtrKey.Key, boolPtrKey.Ptr); err != nil {
return err
}
}

// loop through strings and apply them
for _, stringKey := range stringFlags {
if err := dot.Set(flagCfg, stringKey.Key, *stringKey.Ptr); err != nil {
return err
}
}

for _, strPtrKey := range stringPtrFlags {
if err := dot.Set(flagCfg, strPtrKey.Key, strPtrKey.Ptr); err != nil {
return err
}
}

return nil
}

Expand Down
3 changes: 2 additions & 1 deletion sample/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"Primary": {
"A": "BC"
}
},
"name": "bob"
}
7 changes: 6 additions & 1 deletion sample/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
type TestConfig struct {
config.BaseConfiguration
Text string
Debug bool `json:"debug"`
Debug bool `json:"debug"`
Version *bool `json:"version,omitempty"`
Name *string `json:"name,omitempty"`
Primary Classification
Secondary *Classification `json:"Secondary,omitempty"`
}
Expand All @@ -24,6 +26,7 @@ type Classification struct {
// main will start the test app - to test, provide either a flag "-Primary-A BC" or a config file with Primary.A = "BC"
// this app asserts that the precedence of configs is what we expect
func main() {
isTrue := true
tc := TestConfig{
BaseConfiguration: config.BaseConfiguration{
//ConfigFile: "./config.json",
Expand All @@ -32,6 +35,8 @@ func main() {
Primary: Classification{
A: "AC",
},
Name: nil,
Version: &isTrue,
}
fg, err := config.Load(&tc)
if err != nil {
Expand Down

0 comments on commit 27aef62

Please sign in to comment.