Skip to content

Commit

Permalink
update some logic for add options
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 21, 2018
1 parent 0aa898f commit 9c0e29d
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 43 deletions.
77 changes: 66 additions & 11 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type Config struct {

// loaded config files
loadedFiles []string
initialized bool

// decoders["toml"] = func(blob []byte, v interface{}) (err error){}
// decoders["yaml"] = func(blob []byte, v interface{}) (err error){}
Expand All @@ -83,7 +84,7 @@ type Config struct {
sMapCache map[string]strMap
}

// New
// New config instance
func New(name string) *Config {
return &Config{
name: name,
Expand All @@ -98,11 +99,39 @@ func New(name string) *Config {
}
}

// NewEmpty config instance
func NewEmpty(name string) *Config {
return &Config{
name: name,
data: make(map[string]interface{}),

// empty options
opts: &Options{},

// don't add any drivers
encoders: map[string]Encoder{},
decoders: map[string]Decoder{},
}
}

// NewWithOptions config instance
func NewWithOptions(name string, opts ...func(*Options)) *Config {
c := New(name)
c.WithOptions(opts...)

return c
}

/*************************************************************
* config setting
*************************************************************/

// SetOptions
// Options get
func (c *Config) Options() (*Options) {
return c.opts
}

// SetOptions set options
func (c *Config) SetOptions(opts *Options) {
c.opts = opts

Expand All @@ -115,21 +144,37 @@ func (c *Config) SetOptions(opts *Options) {
}
}

// Name get config name
func (c *Config) Name() string {
return c.name
// WithParseEnv set parse env
func WithParseEnv(opts *Options) {
opts.ParseEnv = true
}

// Data get all config data
func (c *Config) Data() map[string]interface{} {
return c.data
// WithReadonly set readonly
func WithReadonly(opts *Options) {
opts.Readonly = true
}

// Readonly set readonly
func (c *Config) Readonly(readonly bool) {
c.opts.Readonly = readonly
// WithEnableCache set readonly
func WithEnableCache(opts *Options) {
opts.EnableCache = true
}

// WithOptions
func (c *Config) WithOptions(opts ...func(*Options)) {
if c.initialized {
panic("config: Cannot set options after initialization is complete")
}

// apply options
for _, opt := range opts {
opt(c.opts)
}
}

/*************************************************************
* config drivers
*************************************************************/

// AddDriver set a decoder and encoder driver for a format.
func (c *Config) AddDriver(format string, driver Driver) {
format = fixFormat(format)
Expand Down Expand Up @@ -199,6 +244,16 @@ func (c *Config) HasEncoder(format string) bool {
* helper methods
*************************************************************/

// Name get config name
func (c *Config) Name() string {
return c.name
}

// Data get all config data
func (c *Config) Data() map[string]interface{} {
return c.data
}

// ToJson string
func (c *Config) ToJson() string {
buf := &bytes.Buffer{}
Expand Down
10 changes: 5 additions & 5 deletions config_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ func SetOptions(opts *Options) {
dc.SetOptions(opts)
}

// WithOptions with options
func WithOptions(opts ...func(*Options)) {
dc.WithOptions(opts...)
}

// AddDriver set a decoder and encoder driver for a format.
func AddDriver(format string, driver Driver) {
dc.AddDriver(format, driver)
Expand Down Expand Up @@ -175,8 +180,3 @@ func Data() map[string]interface{} {
func ClearAll() {
dc.ClearAll()
}

// ClearData data
func ClearData() {
dc.ClearData()
}
28 changes: 25 additions & 3 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ var jsonStr = `{
}`

func Example() {
SetOptions(&Options{
ParseEnv: true,
})
WithOptions(WithParseEnv)

// add Decoder and Encoder
// use yaml github.com/gookit/config/yaml
Expand Down Expand Up @@ -166,6 +164,23 @@ func TestBasic(t *testing.T) {
c := Default()
st.True(c.HasDecoder(Json))
st.True(c.HasEncoder(Json))
st.Equal("default", c.Name())

c = NewWithOptions("test", WithReadonly)
opts := c.Options()
st.True(opts.Readonly)
st.Equal(Json, opts.DumpFormat)
st.Equal(Json, opts.ReadFormat)

// empty
c = NewEmpty("test")
st.False(c.HasDecoder(Json))
st.Panics(func() {
c.AddDriver("invalid", JsonDriver)
})
c.AddDriver(Json, JsonDriver)
st.True(c.HasDecoder(Json))
st.True(c.HasEncoder(Json))
}

func TestLoad(t *testing.T) {
Expand Down Expand Up @@ -200,3 +215,10 @@ func TestLoad(t *testing.T) {
st.Equal("defVal", def)
}
}

func TestJsonDriver(t *testing.T) {
st := assert.New(t)

st.Equal("json", JsonDriver.Name())
// st.IsType(new(Encoder), JsonDriver.GetEncoder())
}
15 changes: 11 additions & 4 deletions ini/ini_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package ini

import (
"github.com/gookit/config"
"fmt"
"github.com/gookit/config"
"testing"
"github.com/stretchr/testify/assert"
)

func Example() {
config.SetOptions(&config.Options{
ParseEnv: true,
})
config.WithOptions(config.WithParseEnv)

// add Decoder and Encoder
config.AddDriver(config.Ini, Driver)
Expand Down Expand Up @@ -51,3 +51,10 @@ func Example() {
name, ok = config.String("name")
fmt.Printf("set string\n - ok: %v, val: %v\n", ok, name)
}

func TestDriver(t *testing.T) {
st := assert.New(t)

st.Equal("ini", Driver.Name())
// st.IsType(new(Encoder), JsonDriver.GetEncoder())
}
17 changes: 12 additions & 5 deletions json/json_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package json

import (
"github.com/gookit/config"
"fmt"
"github.com/gookit/config"
"testing"
"github.com/stretchr/testify/assert"
)

func Example() {
config.SetOptions(&config.Options{
ParseEnv: true,
})
func Example() {
config.WithOptions(config.WithParseEnv)

// add Decoder and Encoder
config.AddDriver(config.Json, Driver)
Expand Down Expand Up @@ -63,3 +63,10 @@ func Example() {
// }
// fmt.Printf("export config:\n%s", buf.String())
}

func TestDriver(t *testing.T) {
st := assert.New(t)

st.Equal("json", Driver.Name())
// st.IsType(new(Encoder), Driver.GetEncoder())
}
11 changes: 5 additions & 6 deletions json_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ package config
import "encoding/json"

// JsonDecoder
var JsonDecoder Decoder = func(blob []byte, v interface{}) (err error) {
return json.Unmarshal(blob, v)
}
var JsonDecoder Decoder = json.Unmarshal

// JsonEncoder
var JsonEncoder Encoder = func(v interface{}) (out []byte, err error) {
return json.Marshal(v)
}
var JsonEncoder Encoder = json.Marshal
// var JsonEncoder Encoder = func(v interface{}) (out []byte, err error) {
// return json.Marshal(v)
// }

// JsonDriver
var JsonDriver = &jsonDriver{Json}
Expand Down
13 changes: 10 additions & 3 deletions toml/toml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package toml
import (
"fmt"
"github.com/gookit/config"
"testing"
"github.com/stretchr/testify/assert"
)

var tomlStr = `
Expand All @@ -23,9 +25,7 @@ org = "GitHub"
`

func Example() {
config.SetOptions(&config.Options{
ParseEnv: true,
})
config.WithOptions(config.WithParseEnv)

// add Decoder and Encoder
config.AddDriver(config.Toml, Driver)
Expand Down Expand Up @@ -87,3 +87,10 @@ func Example() {
// get env 'envKey' val: /bin/zsh
// get env 'envKey1' val: defValue
}

func TestDriver(t *testing.T) {
st := assert.New(t)

st.Equal("toml", Driver.Name())
// st.IsType(new(Encoder), JsonDriver.GetEncoder())
}
8 changes: 2 additions & 6 deletions yaml/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@ import (
)

// Decoder the yaml content decoder
var Decoder config.Decoder = func(blob []byte, ptr interface{}) (err error) {
return yaml.Unmarshal(blob, ptr)
}
var Decoder config.Decoder = yaml.Unmarshal

// Encoder the yaml content encoder
var Encoder config.Encoder = func(ptr interface{}) (out []byte, err error) {
return yaml.Marshal(ptr)
}
var Encoder config.Encoder = yaml.Marshal

// Driver for yaml
var Driver = &yamlDriver{config.Yaml}
Expand Down
12 changes: 12 additions & 0 deletions yaml/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"fmt"
"github.com/gookit/config"
"testing"
"github.com/stretchr/testify/assert"
)

var yamlStr = `
Expand All @@ -22,6 +24,8 @@ arr1:
`

func Example() {
config.WithOptions(config.WithParseEnv)

// add yaml decoder
// only add decoder
// config.SetDecoder(config.Yaml, Decoder)
Expand Down Expand Up @@ -95,3 +99,11 @@ func Example_exportConfig() {
// debug: false
// ... ...
}

func TestDriver(t *testing.T) {
st := assert.New(t)

st.Equal("yaml", Driver.Name())
// st.IsType(new(Encoder), JsonDriver.GetEncoder())
}

0 comments on commit 9c0e29d

Please sign in to comment.