#33 create default section for nonexistent files in loose mode

This commit is contained in:
Unknwon
2016-02-22 18:24:41 -05:00
parent c83bc2f5e9
commit 776aa739ce
2 changed files with 9 additions and 1 deletions
+3 -1
View File
@@ -36,7 +36,7 @@ const (
// Maximum allowed depth when recursively substituing variable names.
_DEPTH_VALUES = 99
_VERSION = "1.10.0"
_VERSION = "1.10.1"
)
// Version returns current package version literal.
@@ -300,7 +300,9 @@ func (f *File) reload(s dataSource) error {
func (f *File) Reload() (err error) {
for _, s := range f.dataSources {
if err = f.reload(s); err != nil {
// In loose mode, we create an empty default section for nonexistent files.
if os.IsNotExist(err) && f.looseMode {
f.parse(bytes.NewBuffer(nil))
continue
}
return err
+6
View File
@@ -178,10 +178,16 @@ func Test_LooseLoad(t *testing.T) {
cfg, err := LooseLoad("testdata/404.ini")
So(err, ShouldBeNil)
So(cfg, ShouldNotBeNil)
var fake struct {
Name string `ini:"name"`
}
So(cfg.MapTo(&fake), ShouldBeNil)
cfg, err = LooseLoad([]byte("name=Unknwon"), "testdata/404.ini")
So(err, ShouldBeNil)
So(cfg.Section("").Key("name").String(), ShouldEqual, "Unknwon")
So(cfg.MapTo(&fake), ShouldBeNil)
So(fake.Name, ShouldEqual, "Unknwon")
})
})