Skip to content

Commit

Permalink
go-ini#33 create default section for nonexistent files in loose mode
Browse files Browse the repository at this point in the history
  • Loading branch information
unknwon committed Feb 22, 2016
1 parent c83bc2f commit 776aa73
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions ini_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
})

Expand Down

0 comments on commit 776aa73

Please sign in to comment.