mirror of
https://github.com/go-ini/ini.git
synced 2026-06-19 21:46:45 +02:00
@@ -123,16 +123,20 @@ type File struct {
|
||||
// To keep data in order.
|
||||
sectionList []string
|
||||
|
||||
// whether the parser should check for files existence first
|
||||
looseMode bool
|
||||
|
||||
NameMapper
|
||||
}
|
||||
|
||||
// newFile initializes File object with given data sources.
|
||||
func newFile(dataSources []dataSource) *File {
|
||||
func newFile(dataSources []dataSource, looseMode bool) *File {
|
||||
return &File{
|
||||
BlockMode: true,
|
||||
dataSources: dataSources,
|
||||
sections: make(map[string]*Section),
|
||||
sectionList: make([]string, 0, 10),
|
||||
looseMode: looseMode,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,6 +154,17 @@ func parseDataSource(source interface{}) (dataSource, error) {
|
||||
// Load loads and parses from INI data sources.
|
||||
// Arguments can be mixed of file name with string type, or raw data in []byte.
|
||||
func Load(source interface{}, others ...interface{}) (_ *File, err error) {
|
||||
return loadSources(false, source, others...)
|
||||
}
|
||||
|
||||
// Load loads and parses from INI data sources and ignores errors.
|
||||
// Arguments can be mixed of file name with string type, or raw data in []byte.
|
||||
// If a file listed in data sources cannot be opened, that file will be ignored.
|
||||
func LooseLoad(source interface{}, others ...interface{}) (_ *File, err error) {
|
||||
return loadSources(true, source, others...)
|
||||
}
|
||||
|
||||
func loadSources(looseMode bool, source interface{}, others ...interface{}) (_ *File, err error) {
|
||||
sources := make([]dataSource, len(others)+1)
|
||||
sources[0], err = parseDataSource(source)
|
||||
if err != nil {
|
||||
@@ -161,7 +176,7 @@ func Load(source interface{}, others ...interface{}) (_ *File, err error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
f := newFile(sources)
|
||||
f := newFile(sources, looseMode)
|
||||
if err = f.Reload(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -284,7 +299,7 @@ func (f *File) reload(s dataSource) error {
|
||||
// Reload reloads and parses all data sources.
|
||||
func (f *File) Reload() (err error) {
|
||||
for _, s := range f.dataSources {
|
||||
if err = f.reload(s); err != nil {
|
||||
if err = f.reload(s); err != nil && !f.looseMode {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
+31
@@ -116,6 +116,10 @@ func Test_Load(t *testing.T) {
|
||||
cfg, err := Load([]byte(_CONF_DATA), "testdata/conf.ini")
|
||||
So(err, ShouldBeNil)
|
||||
So(cfg, ShouldNotBeNil)
|
||||
|
||||
f, err := Load([]byte(_CONF_DATA), "testdata/404.ini")
|
||||
So(err, ShouldNotBeNil)
|
||||
So(f, ShouldBeNil)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -168,6 +172,33 @@ func Test_Load(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_LooseLoad(t *testing.T) {
|
||||
Convey("LooseLoad from data sources", t, func() {
|
||||
|
||||
Convey("LooseLoad from invalid data sources", func() {
|
||||
_, err := LooseLoad(_CONF_DATA)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
f, err := LooseLoad("testdata/404.ini")
|
||||
So(err, ShouldBeNil)
|
||||
So(f, ShouldNotBeNil)
|
||||
|
||||
_, err = LooseLoad(1)
|
||||
So(err, ShouldNotBeNil)
|
||||
|
||||
_, err = LooseLoad([]byte(""), 1)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
|
||||
Convey("LooseLoad with multiple data sources", func() {
|
||||
cfg, err := LooseLoad([]byte(_CONF_DATA), "testdata/404.ini")
|
||||
So(err, ShouldBeNil)
|
||||
So(cfg, ShouldNotBeNil)
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func Test_File_Append(t *testing.T) {
|
||||
Convey("Append data sources", t, func() {
|
||||
cfg, err := Load([]byte(""))
|
||||
|
||||
Reference in New Issue
Block a user