parser: support SkipUnrecognizableLines (#150)

This commit is contained in:
Unknwon
2018-07-11 21:03:03 +08:00
parent fa6ee696e4
commit af26abd521
3 changed files with 53 additions and 23 deletions
+4 -2
View File
@@ -34,7 +34,7 @@ const (
// Maximum allowed depth when recursively substituing variable names.
_DEPTH_VALUES = 99
_VERSION = "1.37.0"
_VERSION = "1.38.0"
)
// Version returns current package version literal.
@@ -134,6 +134,8 @@ type LoadOptions struct {
IgnoreContinuation bool
// IgnoreInlineComment indicates whether to ignore comments at the end of value and treat it as part of value.
IgnoreInlineComment bool
// SkipUnrecognizableLines indicates whether to skip unrecognizable lines that do not conform to key/value pairs.
SkipUnrecognizableLines 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
@@ -159,7 +161,7 @@ type LoadOptions struct {
// when value is NOT surrounded by any quotes.
// Note: UNSTABLE, behavior might change to only unescape inside double quotes but may noy necessary at all.
UnescapeValueCommentSymbols bool
// Some INI formats allow group blocks that store a block of raw content that doesn't otherwise
// UnparseableSections stores a list of blocks that are allowed with raw content which do not otherwise
// conform to key/value pairs. Specify the names of those blocks here.
UnparseableSections []string
}
+23
View File
@@ -363,6 +363,29 @@ key2=value2 #comment2`))
})
})
Convey("Skip unrecognizable lines", func() {
f, err := ini.LoadSources(ini.LoadOptions{
SkipUnrecognizableLines: true,
}, []byte(`
GenerationDepth: 13
BiomeRarityScale: 100
################
# Biome Groups #
################
BiomeGroup(NormalBiomes, 3, 99, RoofedForestEnchanted, ForestSakura, FloatingJungle
BiomeGroup(IceBiomes, 4, 85, Ice Plains)
`))
So(err, ShouldBeNil)
So(f, ShouldNotBeNil)
So(f.Section("").Key("GenerationDepth").String(), ShouldEqual, "13")
So(f.Section("").Key("BiomeRarityScale").String(), ShouldEqual, "100")
So(f.Section("").HasKey("BiomeGroup"), ShouldBeFalse)
})
Convey("Allow boolean type keys", func() {
f, err := ini.LoadSources(ini.LoadOptions{
AllowPythonMultilineValues: true,
+8 -3
View File
@@ -339,8 +339,7 @@ func (f *File) parse(reader io.Reader) (err error) {
// NOTE: Iterate and increase `currentPeekSize` until
// the size of the parser buffer is found.
// TODO: When Golang 1.10 is the lowest version supported,
// replace with `parserBufferSize := p.buf.Size()`.
// TODO(unknwon): When Golang 1.10 is the lowest version supported, replace with `parserBufferSize := p.buf.Size()`.
parserBufferSize := 0
// NOTE: Peek 1kb at a time.
currentPeekSize := 1024
@@ -432,7 +431,9 @@ func (f *File) parse(reader io.Reader) (err error) {
kname, offset, err := readKeyName(line)
if err != nil {
// Treat as boolean key when desired, and whole line is key name.
if IsErrDelimiterNotFound(err) && f.options.AllowBooleanKeys {
if IsErrDelimiterNotFound(err) {
switch {
case f.options.AllowBooleanKeys:
kname, err := p.readValue(line,
parserBufferSize,
f.options.IgnoreContinuation,
@@ -451,6 +452,10 @@ func (f *File) parse(reader io.Reader) (err error) {
key.Comment = strings.TrimSpace(p.comment.String())
p.comment.Reset()
continue
case f.options.SkipUnrecognizableLines:
continue
}
}
return err
}