-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add new driver INI, add some demos, some logic update.
- Loading branch information
Showing
9 changed files
with
131 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// These are some sample code for YAML,TOML,JSON,INI,HCL | ||
package main | ||
|
||
import ( | ||
"github.com/gookit/config" | ||
"github.com/gookit/config/ini" | ||
"fmt" | ||
) | ||
|
||
// go run ./examples/ini.go | ||
func main() { | ||
config.SetOptions(&config.Options{ | ||
ParseEnv: true, | ||
}) | ||
|
||
// add Decoder and Encoder | ||
config.AddDriver(config.Ini, ini.Driver) | ||
// Or | ||
// config.DecoderEncoder(config.Ini, ini.Decoder, ini.Encoder) | ||
|
||
err := config.LoadFiles("testdata/ini_base.ini") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Printf("config data: \n %#v\n", config.Data()) | ||
|
||
err = config.LoadFiles("testdata/ini_other.ini") | ||
// config.LoadFiles("testdata/ini_base.ini", "testdata/ini_other.ini") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Printf("config data: \n %#v\n", config.Data()) | ||
fmt.Print("get config example:\n") | ||
|
||
name, ok := config.GetString("name") | ||
fmt.Printf("- get string\n ok: %v, val: %v\n", ok, name) | ||
|
||
// NOTICE: ini is not support array | ||
|
||
map1, ok := config.GetStringMap("map1") | ||
fmt.Printf("- get map\n ok: %v, val: %#v\n", ok, map1) | ||
|
||
val0, ok := config.GetString("map1.key") | ||
fmt.Printf("- get sub-value by path 'map.key'\n ok: %v, val: %v\n", ok, val0) | ||
|
||
// can parse env name(ParseEnv: true) | ||
fmt.Printf("get env 'envKey' val: %s\n", config.DefString("envKey", "")) | ||
fmt.Printf("get env 'envKey1' val: %s\n", config.DefString("envKey1", "")) | ||
|
||
// set value | ||
config.Set("name", "new name") | ||
name, ok = config.GetString("name") | ||
fmt.Printf("- set string\n ok: %v, val: %v\n", ok, name) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
This is driver use INI format content as config source | ||
about ini parse, please see https://github.com/gookit/ini/parser | ||
*/ | ||
package ini | ||
|
||
import ( | ||
"github.com/gookit/config" | ||
"github.com/gookit/ini/parser" | ||
"errors" | ||
) | ||
|
||
// Decoder the ini content decoder | ||
var Decoder config.Decoder = func(blob []byte, ptr interface{}) (err error) { | ||
return parser.Decode(blob, ptr) | ||
} | ||
|
||
// Encoder encode data to ini content | ||
var Encoder config.Encoder = func(ptr interface{}) (out []byte, err error) { | ||
err = errors.New("INI: is not support encode data to INI") | ||
return | ||
} | ||
|
||
// Driver | ||
var Driver = &iniDriver{config.Ini} | ||
|
||
// iniDriver for ini format content | ||
type iniDriver struct { | ||
name string | ||
} | ||
|
||
// Name | ||
func (d *iniDriver) Name() string { | ||
return d.name | ||
} | ||
|
||
// GetDecoder for ini | ||
func (d *iniDriver) GetDecoder() config.Decoder { | ||
return Decoder | ||
} | ||
|
||
// GetEncoder for ini | ||
func (d *iniDriver) GetEncoder() config.Encoder { | ||
return Encoder | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
name = app | ||
debug = false | ||
baseKey = value | ||
age = 123 | ||
envKey = ${SHELL} | ||
envKey1 = ${NotExist|defValue} | ||
|
||
[map1] | ||
key = val | ||
key1 = val1 | ||
key2 = val2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
name = app2 | ||
debug = false | ||
age = 12 | ||
baseKey = value2 | ||
|
||
[map1] | ||
key = val2 | ||
key2 = val20 |