Skip to content

Commit

Permalink
add new driver INI, add some demos, some logic update.
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 19, 2018
1 parent eb5811a commit 7eb39c4
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 45 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

[![GoDoc](https://godoc.org/github.com/gookit/config?status.svg)](https://godoc.org/github.com/gookit/config)

golang application config manage implement.
golang application config manage tool library.

- support multi format: `JSON`(default), `YAML`, `TOML`, `HCL`
- support multi format: `JSON`(default), `INI`, `YAML`, `TOML`, `HCL`
- support multi file/data load
- support data override merge
- support get sub value by path, like `map.key` `arr.2`
Expand Down
3 changes: 2 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ const Version = "1.0.3"

// supported config format
const (
Json = "json"
Ini = "ini"
Hcl = "hcl"
Yml = "yml"
Json = "json"
Yaml = "yaml"
Toml = "toml"
)
Expand Down
4 changes: 4 additions & 0 deletions config_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ func (c *Config) parseSourceCode(format string, blob []byte) (err error) {
var decoder Decoder

switch format {
case Hcl:
decoder, ok = c.decoders[Hcl]
case Ini:
decoder, ok = c.decoders[Ini]
case Json:
decoder, ok = c.decoders[Json]
case Yaml, Yml:
Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
golang application config manage implement. support YAML,TOML,JSON,HCL format.
golang application config manage implement. support YAML,TOML,JSON,INI,HCL format.
Source code and other details for the project are available at GitHub:
Expand Down
41 changes: 0 additions & 41 deletions examples/ini-parse-packages.md

This file was deleted.

57 changes: 57 additions & 0 deletions examples/ini.go
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)

}
46 changes: 46 additions & 0 deletions ini/ini.go
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
}
11 changes: 11 additions & 0 deletions testdata/ini_base.ini
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
8 changes: 8 additions & 0 deletions testdata/ini_other.ini
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

0 comments on commit 7eb39c4

Please sign in to comment.