Skip to content

Commit fcd0515

Browse files
ygj6unknwon
andauthored
Support Short-circuit load (#261)
Co-authored-by: ᴜɴᴋɴᴡᴏɴ <[email protected]>
1 parent 01f073e commit fcd0515

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

file.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@ func (f *File) Reload() (err error) {
302302
}
303303
return err
304304
}
305+
if f.options.ShortCircuit {
306+
return nil
307+
}
305308
}
306309
return nil
307310
}

file_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ bar3 = " val ue3 "
449449
var buf bytes.Buffer
450450
_, err := f.WriteTo(&buf)
451451
So(err, ShouldBeNil)
452-
So(buf.String(),ShouldEqual,`[foo]
452+
So(buf.String(), ShouldEqual, `[foo]
453453
bar1 = " val ue1 "
454454
bar2 = " val ue2 "
455455
bar3 = " val ue3 "

ini.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ type LoadOptions struct {
8181
IgnoreInlineComment bool
8282
// SkipUnrecognizableLines indicates whether to skip unrecognizable lines that do not conform to key/value pairs.
8383
SkipUnrecognizableLines bool
84+
// ShortCircuit indicates whether to ignore other configuration sources after loaded the first available configuration source.
85+
ShortCircuit bool
8486
// AllowBooleanKeys indicates whether to allow boolean type keys or treat as value is missing.
8587
// This type of keys are mostly used in my.cnf.
8688
AllowBooleanKeys bool

ini_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,6 +1367,55 @@ GITHUB = U;n;k;n;w;o;n
13671367
}
13681368
})
13691369
})
1370+
1371+
Convey("ShortCircuit", func() {
1372+
Convey("Load the first available configuration, ignore other configuration", func() {
1373+
f, err := ini.LoadSources(ini.LoadOptions{ShortCircuit: true}, minimalConf, []byte(`key1 = value1`))
1374+
So(f, ShouldNotBeNil)
1375+
So(err, ShouldBeNil)
1376+
var buf bytes.Buffer
1377+
_, err = f.WriteTo(&buf)
1378+
So(err, ShouldBeNil)
1379+
So(buf.String(), ShouldEqual, `[author]
1380+
1381+
1382+
`)
1383+
})
1384+
1385+
Convey("Return an error when fail to load", func() {
1386+
f, err := ini.LoadSources(ini.LoadOptions{ShortCircuit: true}, notFoundConf, minimalConf)
1387+
So(f, ShouldBeNil)
1388+
So(err, ShouldNotBeNil)
1389+
})
1390+
1391+
Convey("Used with Loose to ignore errors that the file does not exist", func() {
1392+
f, err := ini.LoadSources(ini.LoadOptions{ShortCircuit: true, Loose: true}, notFoundConf, minimalConf)
1393+
So(f, ShouldNotBeNil)
1394+
So(err, ShouldBeNil)
1395+
var buf bytes.Buffer
1396+
_, err = f.WriteTo(&buf)
1397+
So(err, ShouldBeNil)
1398+
So(buf.String(), ShouldEqual, `[author]
1399+
1400+
1401+
`)
1402+
})
1403+
1404+
Convey("Ensure all sources are loaded without ShortCircuit", func() {
1405+
f, err := ini.LoadSources(ini.LoadOptions{ShortCircuit: false}, minimalConf, []byte(`key1 = value1`))
1406+
So(f, ShouldNotBeNil)
1407+
So(err, ShouldBeNil)
1408+
var buf bytes.Buffer
1409+
_, err = f.WriteTo(&buf)
1410+
So(err, ShouldBeNil)
1411+
So(buf.String(), ShouldEqual, `key1 = value1
1412+
1413+
[author]
1414+
1415+
1416+
`)
1417+
})
1418+
})
13701419
})
13711420
}
13721421

0 commit comments

Comments
 (0)