mirror of
https://github.com/go-ini/ini.git
synced 2026-06-19 21:46:45 +02:00
@@ -36,7 +36,7 @@ const (
|
||||
|
||||
// Maximum allowed depth when recursively substituing variable names.
|
||||
_DEPTH_VALUES = 99
|
||||
_VERSION = "1.9.1"
|
||||
_VERSION = "1.10.0"
|
||||
)
|
||||
|
||||
// Version returns current package version literal.
|
||||
@@ -123,7 +123,7 @@ type File struct {
|
||||
// To keep data in order.
|
||||
sectionList []string
|
||||
|
||||
// whether the parser should check for files existence first
|
||||
// Whether the parser should ignore nonexistent files or return error.
|
||||
looseMode bool
|
||||
|
||||
NameMapper
|
||||
@@ -151,19 +151,6 @@ 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)
|
||||
@@ -183,6 +170,19 @@ func loadSources(looseMode bool, source interface{}, others ...interface{}) (_ *
|
||||
return f, nil
|
||||
}
|
||||
|
||||
// Load loads and parses from INI data sources.
|
||||
// Arguments can be mixed of file name with string type, or raw data in []byte.
|
||||
// It will return error if list contains nonexistent files.
|
||||
func Load(source interface{}, others ...interface{}) (*File, error) {
|
||||
return loadSources(false, source, others...)
|
||||
}
|
||||
|
||||
// LooseLoad has exactly same functionality as Load function
|
||||
// except it ignores nonexistent files instead of returning error.
|
||||
func LooseLoad(source interface{}, others ...interface{}) (*File, error) {
|
||||
return loadSources(true, source, others...)
|
||||
}
|
||||
|
||||
// Empty returns an empty file object.
|
||||
func Empty() *File {
|
||||
// Ignore error here, we sure our data is good.
|
||||
@@ -299,7 +299,10 @@ 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 && !f.looseMode {
|
||||
if err = f.reload(s); err != nil {
|
||||
if os.IsNotExist(err) && f.looseMode {
|
||||
continue
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
+7
-19
@@ -173,27 +173,15 @@ 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")
|
||||
Convey("Loose load from data sources", t, func() {
|
||||
Convey("Loose load mixed with nonexistent file", func() {
|
||||
cfg, err := LooseLoad("testdata/404.ini")
|
||||
So(err, ShouldBeNil)
|
||||
So(cfg, ShouldNotBeNil)
|
||||
|
||||
cfg, err = LooseLoad([]byte("name=Unknwon"), "testdata/404.ini")
|
||||
So(err, ShouldBeNil)
|
||||
So(cfg.Section("").Key("name").String(), ShouldEqual, "Unknwon")
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user