From 3cb3b737b1fbecf6ba3638d2b57e0c6ecf61cb0f Mon Sep 17 00:00:00 2001
From: yuangongji
Date: Sat, 29 Aug 2020 17:18:08 +0800
Subject: [PATCH 1/2] Support Short-circuit load
---
file.go | 3 +++
file_test.go | 2 +-
ini.go | 2 ++
ini_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 55 insertions(+), 1 deletion(-)
diff --git a/file.go b/file.go
index 7eecaca..549c00a 100644
--- a/file.go
+++ b/file.go
@@ -302,6 +302,9 @@ func (f *File) Reload() (err error) {
}
return err
}
+ if f.options.ShortCircuit {
+ return nil
+ }
}
return nil
}
diff --git a/file_test.go b/file_test.go
index 02fdfed..9a70d5e 100644
--- a/file_test.go
+++ b/file_test.go
@@ -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 "
diff --git a/ini.go b/ini.go
index 80ebf3a..96322b3 100644
--- a/ini.go
+++ b/ini.go
@@ -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
diff --git a/ini_test.go b/ini_test.go
index 94012a9..091ff4b 100644
--- a/ini_test.go
+++ b/ini_test.go
@@ -1367,6 +1367,55 @@ GITHUB = U;n;k;n;w;o;n
}
})
})
+
+ Convey("with `ShortCircuit` 'ture'", 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 = [email protected]
+
+`)
+ })
+
+ 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 = [email protected]
+
+`)
+ })
+
+ Convey("Inverse case", 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 = [email protected]
+
+`)
+ })
+ })
})
}
From 93f60be489352e8d0ab78ea4493f7101e4aa8a56 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E1=B4=9C=C9=B4=E1=B4=8B=C9=B4=E1=B4=A1=E1=B4=8F=C9=B4?=
Date: Sat, 5 Sep 2020 16:13:48 +0800
Subject: [PATCH 2/2] Apply suggestions from code review
---
ini_test.go | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/ini_test.go b/ini_test.go
index 091ff4b..fb9a681 100644
--- a/ini_test.go
+++ b/ini_test.go
@@ -1368,7 +1368,7 @@ GITHUB = U;n;k;n;w;o;n
})
})
- Convey("with `ShortCircuit` 'ture'", func() {
+ 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)
@@ -1401,7 +1401,7 @@ E-MAIL = [email protected]
`)
})
- Convey("Inverse case", func() {
+ 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)