diff --git a/config_test.go b/config_test.go index 03539d9..6e6fb95 100644 --- a/config_test.go +++ b/config_test.go @@ -6,6 +6,7 @@ import ( "os" "testing" + "github.com/gookit/goutil/maputil" "github.com/gookit/goutil/testutil" "github.com/gookit/goutil/testutil/assert" ) @@ -163,11 +164,12 @@ func TestBasic(t *testing.T) { is.Eq("default", c.Name()) is.NoErr(c.Error()) - c = NewWithOptions("test", Readonly) + c = NewWithOptions("test", Readonly, WithTagName("mytag")) opts := c.Options() is.True(opts.Readonly) is.Eq(JSON, opts.DumpFormat) is.Eq(JSON, opts.ReadFormat) + is.Eq("mytag", opts.TagName) } func TestGetEnv(t *testing.T) { @@ -249,6 +251,16 @@ func TestJSONDriver(t *testing.T) { is.NoErr(err) }) is.Eq(2, c.Int("key1")) + + // test call JSONDriver.Encode + s := c.ToJSON() + is.StrContains(s, `{"key1":2}`) + + // set MarshalIndent + JSONDriver.MarshalIndent = " " + s = c.ToJSON() + is.StrContains(s, ` "key1": 2`) + JSONDriver.MarshalIndent = "" // reset } func TestDriver(t *testing.T) { @@ -282,6 +294,22 @@ func TestDriver(t *testing.T) { is.True(c.HasEncoder(JSON)) } +func TestStdDriver_methods(t *testing.T) { + d1 := NewDriver("my001", JSONDecoder, JSONEncoder) + d1.WithAlias("json") + assert.Eq(t, "my001", d1.Name()) + assert.Contains(t, d1.Aliases(), "json") + + s := `{"age": 245}` + m := make(maputil.Map) + err := d1.Decode([]byte(s), &m) + assert.NoErr(t, err) + + bs, err := d1.Encode(m) + assert.NoErr(t, err) + assert.StrContains(t, string(bs), `{"age":245}`) +} + func TestOptions(t *testing.T) { is := assert.New(t) @@ -447,4 +475,3 @@ func TestMapStringStringParseEnv(t *testing.T) { is.Eq(shellVal, sMap["key3"]) }) } - diff --git a/export.go b/export.go index d6e7c57..7f20b61 100644 --- a/export.go +++ b/export.go @@ -121,7 +121,7 @@ func (c *Config) Structure(key string, dst any) (err error) { return err } -// ToJSON string +// ToJSON string, will ignore error func (c *Config) ToJSON() string { buf := &bytes.Buffer{} @@ -129,7 +129,6 @@ func (c *Config) ToJSON() string { if err != nil { return "" } - return buf.String() } @@ -168,7 +167,6 @@ func (c *Config) DumpTo(out io.Writer, format string) (n int64, err error) { // write content to out num, _ := fmt.Fprintln(out, string(encoded)) - return int64(num), nil } diff --git a/export_test.go b/export_test.go index 10af544..485ed81 100644 --- a/export_test.go +++ b/export_test.go @@ -2,6 +2,7 @@ package config import ( "bytes" + "errors" "testing" "github.com/gookit/goutil/dump" @@ -11,7 +12,6 @@ import ( func TestExport(t *testing.T) { is := assert.New(t) - c := New("test") str := c.ToJSON() @@ -27,11 +27,10 @@ func TestExport(t *testing.T) { _, err = c.WriteTo(buf) is.Nil(err) + // test dump buf = &bytes.Buffer{} - _, err = c.DumpTo(buf, "invalid") is.Err(err) - _, err = c.DumpTo(buf, Yml) is.Err(err) @@ -39,6 +38,24 @@ func TestExport(t *testing.T) { is.Nil(err) } +func TestDumpTo_encode_error(t *testing.T) { + is := assert.New(t) + c := NewEmpty("test") + is.NoErr(c.Set("age", 34)) + + drv := NewDriver(JSON, JSONDecoder, func(v any) (out []byte, err error) { + return nil, errors.New("encode data error") + }) + c.WithDriver(drv) + + // encode error + buf := &bytes.Buffer{} + _, err := c.DumpTo(buf, JSON) + is.ErrMsg(err, "encode data error") + + is.Empty(c.ToJSON()) +} + func TestConfig_Structure(t *testing.T) { is := assert.New(t) diff --git a/json5/json5.go b/json5/json5.go index 4622b85..79f6559 100644 --- a/json5/json5.go +++ b/json5/json5.go @@ -8,12 +8,12 @@ import ( "github.com/titanous/json5" ) -// NAME for driver -const NAME = "json5" - // Name for driver const Name = "json5" +// NAME for driver +const NAME = Name + // JSONMarshalIndent if not empty, will use json.MarshalIndent for encode data. var JSONMarshalIndent string diff --git a/json5/json5_test.go b/json5/json5_test.go index ca5e973..faa0ded 100644 --- a/json5/json5_test.go +++ b/json5/json5_test.go @@ -76,6 +76,7 @@ func TestDriver(t *testing.T) { is.True(c.HasDecoder(json5.Name)) is.True(c.HasEncoder(json5.Name)) + // test use m := struct { N string }{} @@ -86,7 +87,26 @@ func TestDriver(t *testing.T) { is.Nil(err) is.Eq("v", m.N) - // will error on use + // load file err = c.LoadFiles("../testdata/json_base.json5") is.NoErr(err) + is.Eq("app", c.Get("name")) +} + +func TestEncode2JSON5(t *testing.T) { + is := assert.New(t) + + mp := map[string]any{ + "name": "app", + "age": 45, + } + bs, err := json5.Encoder(mp) + is.NoErr(err) + is.StrContains(string(bs), `"name":"app"`) + + json5.JSONMarshalIndent = " " + bs, err = json5.Encoder(mp) + is.NoErr(err) + s := string(bs) + is.StrContains(s, ` "name": "app"`) } diff --git a/options.go b/options.go index 3bc7922..f58e836 100644 --- a/options.go +++ b/options.go @@ -4,6 +4,7 @@ import ( "strings" "dario.cat/mergo" + "github.com/gookit/goutil" "github.com/mitchellh/mapstructure" ) @@ -149,15 +150,12 @@ func Delimiter(sep byte) func(*Options) { } } -// SaveFileOnSet set hook func +// SaveFileOnSet set hook func, will panic on save error func SaveFileOnSet(fileName string, format string) func(options *Options) { return func(opts *Options) { opts.HookFunc = func(event string, c *Config) { if strings.HasPrefix(event, "set.") { - err := c.DumpToFile(fileName, format) - if err != nil { - panic(err) - } + goutil.PanicErr(c.DumpToFile(fileName, format)) } } } diff --git a/testdata/issues59.ini b/testdata/issues59.ini index 8427fea..7f26ec9 100644 --- a/testdata/issues59.ini +++ b/testdata/issues59.ini @@ -1,4 +1,4 @@ -; exported at 2023-07-01 14:46:07 +; exported at 2024-01-13 12:00:15 age = 123 baseKey = value diff --git a/toml/toml.go b/toml/toml.go index faa7176..dc7a1ae 100644 --- a/toml/toml.go +++ b/toml/toml.go @@ -1,7 +1,6 @@ /* -Package toml is driver use TOML format content as config source - -Usage please see example: +Package toml is driver use TOML format content as config source. +How to usage please see README and unit tests. */ package toml @@ -22,13 +21,8 @@ var Decoder config.Decoder = func(blob []byte, ptr any) (err error) { // Encoder the toml content encoder var Encoder config.Encoder = func(ptr any) (out []byte, err error) { buf := new(bytes.Buffer) - err = toml.NewEncoder(buf).Encode(ptr) - if err != nil { - return - } - - return buf.Bytes(), nil + return buf.Bytes(), err } // Driver for toml format