Skip to content

Commit

Permalink
chore: update some examples, update some for json driver
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 22, 2022
1 parent 808bb97 commit 167f619
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 13 deletions.
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# TODO

- remote `etcd` `consul`
- watch config files
- watch changed config files and reload
- set default value on binding struct. use tag `defalut`
66 changes: 66 additions & 0 deletions _examples/yamlv2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"fmt"

"github.com/gookit/config/v2"
"github.com/gookit/config/v2/yaml"
)

// go run ./examples/yamlv2.go
func main() {
config.WithOptions(config.ParseEnv)

// only add decoder
// config.SetDecoder(config.Yaml, yaml.Decoder)
// Or
config.AddDriver(yaml.Driver)

err := config.LoadFiles("testdata/yml_base.yml")
if err != nil {
panic(err)
}

fmt.Printf("config data: \n %#v\n", config.Data())

err = config.LoadFiles("testdata/yml_other.yml")
// config.LoadFiles("testdata/yml_base.yml", "testdata/yml_other.yml")
if err != nil {
panic(err)
}

fmt.Printf("config data: \n %#v\n", config.Data())
fmt.Print("get config example:\n")

name := config.String("name")
fmt.Printf("- get string\n val: %v\n", name)

arr1 := config.Strings("arr1")
fmt.Printf("- get array\n val: %#v\n", arr1)

val0 := config.String("arr1.0")
fmt.Printf("- get sub-value by path 'arr.index'\n val: %#v\n", val0)

map1 := config.StringMap("map1")
fmt.Printf("- get map\n val: %#v\n", map1)

val0 = config.String("map1.key")
fmt.Printf("- get sub-value by path 'map.key'\n val: %#v\n", val0)

// can parse env name(ParseEnv: true)
fmt.Printf("get env 'envKey' val: %s\n", config.String("envKey", ""))
fmt.Printf("get env 'envKey1' val: %s\n", config.String("envKey1", ""))

// set value
config.Set("name", "new name")
name = config.String("name")
fmt.Printf("- set string\n val: %v\n", name)

// if you want export config data
// buf := new(bytes.Buffer)
// _, err = config.DumpTo(buf, config.Yaml)
// if err != nil {
// panic(err)
// }
// fmt.Printf("export config:\n%s", buf.String())
}
42 changes: 30 additions & 12 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func (d *StdDriver) GetEncoder() Encoder {
return d.encoder
}

/*************************************************************
* json driver
*************************************************************/

var (
// JSONAllowComments support write comments on json file.
JSONAllowComments = true
Expand All @@ -68,24 +72,21 @@ var (

// JSONDecoder for json decode
var JSONDecoder Decoder = func(data []byte, v interface{}) (err error) {
if JSONAllowComments {
str := jsonutil.StripComments(string(data))
return json.Unmarshal([]byte(str), v)
}

return json.Unmarshal(data, v)
JSONDriver.ClearComments = JSONAllowComments
return JSONDriver.Decode(data, v)
}

// JSONEncoder for json encode
var JSONEncoder Encoder = func(v interface{}) (out []byte, err error) {
if len(JSONMarshalIndent) > 0 {
return json.MarshalIndent(v, "", JSONMarshalIndent)
}
return json.Marshal(v)
JSONDriver.MarshalIndent = JSONMarshalIndent
return JSONDriver.Encode(v)
}

// JSONDriver instance fot json
var JSONDriver = &jsonDriver{
ClearComments: JSONAllowComments,
MarshalIndent: JSONMarshalIndent,
// inject
StdDriver: StdDriver{
name: JSON,
decoder: JSONDecoder,
Expand All @@ -107,12 +108,29 @@ func (d *jsonDriver) Name() string {
return d.name
}

// Decode for the driver
func (d *jsonDriver) Decode(data []byte, v interface{}) error {
if d.ClearComments {
str := jsonutil.StripComments(string(data))
return json.Unmarshal([]byte(str), v)
}
return json.Unmarshal(data, v)
}

// GetDecoder for the driver
func (d *jsonDriver) GetDecoder() Decoder {
return JSONDecoder
return d.Decode
}

// Encode for the driver
func (d *jsonDriver) Encode(v interface{}) (out []byte, err error) {
if len(d.MarshalIndent) > 0 {
return json.MarshalIndent(v, "", JSONMarshalIndent)
}
return json.Marshal(v)
}

// GetEncoder for the driver
func (d *jsonDriver) GetEncoder() Encoder {
return JSONEncoder
return d.Encode
}

0 comments on commit 167f619

Please sign in to comment.