Skip to content

Commit

Permalink
Support Short-circuit load (#261)
Browse files Browse the repository at this point in the history
Co-authored-by: ᴜɴᴋɴᴡᴏɴ <u@gogs.io>
  • Loading branch information
ygj6 and unknwon committed Sep 5, 2020
1 parent 01f073e commit fcd0515
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
3 changes: 3 additions & 0 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@ func (f *File) Reload() (err error) {
}
return err
}
if f.options.ShortCircuit {
return nil
}
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ bar3 = " val ue3 "
var buf bytes.Buffer
_, err := f.WriteTo(&buf)
So(err, ShouldBeNil)
So(buf.String(),ShouldEqual,`[foo]
So(buf.String(), ShouldEqual, `[foo]
bar1 = " val ue1 "
bar2 = " val ue2 "
bar3 = " val ue3 "
Expand Down
2 changes: 2 additions & 0 deletions ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ type LoadOptions struct {
IgnoreInlineComment bool
// SkipUnrecognizableLines indicates whether to skip unrecognizable lines that do not conform to key/value pairs.
SkipUnrecognizableLines bool
// ShortCircuit indicates whether to ignore other configuration sources after loaded the first available configuration source.
ShortCircuit bool
// AllowBooleanKeys indicates whether to allow boolean type keys or treat as value is missing.
// This type of keys are mostly used in my.cnf.
AllowBooleanKeys bool
Expand Down
49 changes: 49 additions & 0 deletions ini_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1367,6 +1367,55 @@ GITHUB = U;n;k;n;w;o;n
}
})
})

Convey("ShortCircuit", func() {
Convey("Load the first available configuration, ignore other configuration", func() {
f, err := ini.LoadSources(ini.LoadOptions{ShortCircuit: true}, minimalConf, []byte(`key1 = value1`))
So(f, ShouldNotBeNil)
So(err, ShouldBeNil)
var buf bytes.Buffer
_, err = f.WriteTo(&buf)
So(err, ShouldBeNil)
So(buf.String(), ShouldEqual, `[author]
E-MAIL = u@gogs.io
`)
})

Convey("Return an error when fail to load", func() {
f, err := ini.LoadSources(ini.LoadOptions{ShortCircuit: true}, notFoundConf, minimalConf)
So(f, ShouldBeNil)
So(err, ShouldNotBeNil)
})

Convey("Used with Loose to ignore errors that the file does not exist", func() {
f, err := ini.LoadSources(ini.LoadOptions{ShortCircuit: true, Loose: true}, notFoundConf, minimalConf)
So(f, ShouldNotBeNil)
So(err, ShouldBeNil)
var buf bytes.Buffer
_, err = f.WriteTo(&buf)
So(err, ShouldBeNil)
So(buf.String(), ShouldEqual, `[author]
E-MAIL = u@gogs.io
`)
})

Convey("Ensure all sources are loaded without ShortCircuit", func() {
f, err := ini.LoadSources(ini.LoadOptions{ShortCircuit: false}, minimalConf, []byte(`key1 = value1`))
So(f, ShouldNotBeNil)
So(err, ShouldBeNil)
var buf bytes.Buffer
_, err = f.WriteTo(&buf)
So(err, ShouldBeNil)
So(buf.String(), ShouldEqual, `key1 = value1
[author]
E-MAIL = u@gogs.io
`)
})
})
})
}

Expand Down

0 comments on commit fcd0515

Please sign in to comment.