mirror of
https://github.com/go-ini/ini.git
synced 2026-06-19 21:46:45 +02:00
parser: support SkipUnrecognizableLines (#150)
This commit is contained in:
@@ -34,7 +34,7 @@ const (
|
|||||||
|
|
||||||
// Maximum allowed depth when recursively substituing variable names.
|
// Maximum allowed depth when recursively substituing variable names.
|
||||||
_DEPTH_VALUES = 99
|
_DEPTH_VALUES = 99
|
||||||
_VERSION = "1.37.0"
|
_VERSION = "1.38.0"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Version returns current package version literal.
|
// Version returns current package version literal.
|
||||||
@@ -134,6 +134,8 @@ type LoadOptions struct {
|
|||||||
IgnoreContinuation bool
|
IgnoreContinuation bool
|
||||||
// IgnoreInlineComment indicates whether to ignore comments at the end of value and treat it as part of value.
|
// IgnoreInlineComment indicates whether to ignore comments at the end of value and treat it as part of value.
|
||||||
IgnoreInlineComment bool
|
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.
|
// AllowBooleanKeys indicates whether to allow boolean type keys or treat as value is missing.
|
||||||
// This type of keys are mostly used in my.cnf.
|
// This type of keys are mostly used in my.cnf.
|
||||||
AllowBooleanKeys bool
|
AllowBooleanKeys bool
|
||||||
@@ -159,7 +161,7 @@ type LoadOptions struct {
|
|||||||
// when value is NOT surrounded by any quotes.
|
// 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.
|
// Note: UNSTABLE, behavior might change to only unescape inside double quotes but may noy necessary at all.
|
||||||
UnescapeValueCommentSymbols bool
|
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.
|
// conform to key/value pairs. Specify the names of those blocks here.
|
||||||
UnparseableSections []string
|
UnparseableSections []string
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-1
@@ -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() {
|
Convey("Allow boolean type keys", func() {
|
||||||
f, err := ini.LoadSources(ini.LoadOptions{
|
f, err := ini.LoadSources(ini.LoadOptions{
|
||||||
AllowPythonMultilineValues: true,
|
AllowPythonMultilineValues: true,
|
||||||
@@ -764,7 +787,7 @@ GITHUB = U;n;k;n;w;o;n
|
|||||||
`))
|
`))
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(f, ShouldNotBeNil)
|
So(f, ShouldNotBeNil)
|
||||||
sec := f.Section("author")
|
sec := f.Section("author")
|
||||||
nameValue := sec.Key("NAME").String()
|
nameValue := sec.Key("NAME").String()
|
||||||
githubValue := sec.Key("GITHUB").String()
|
githubValue := sec.Key("GITHUB").String()
|
||||||
So(nameValue, ShouldEqual, "U")
|
So(nameValue, ShouldEqual, "U")
|
||||||
|
|||||||
@@ -339,8 +339,7 @@ func (f *File) parse(reader io.Reader) (err error) {
|
|||||||
|
|
||||||
// NOTE: Iterate and increase `currentPeekSize` until
|
// NOTE: Iterate and increase `currentPeekSize` until
|
||||||
// the size of the parser buffer is found.
|
// the size of the parser buffer is found.
|
||||||
// TODO: When Golang 1.10 is the lowest version supported,
|
// TODO(unknwon): When Golang 1.10 is the lowest version supported, replace with `parserBufferSize := p.buf.Size()`.
|
||||||
// replace with `parserBufferSize := p.buf.Size()`.
|
|
||||||
parserBufferSize := 0
|
parserBufferSize := 0
|
||||||
// NOTE: Peek 1kb at a time.
|
// NOTE: Peek 1kb at a time.
|
||||||
currentPeekSize := 1024
|
currentPeekSize := 1024
|
||||||
@@ -432,25 +431,31 @@ func (f *File) parse(reader io.Reader) (err error) {
|
|||||||
kname, offset, err := readKeyName(line)
|
kname, offset, err := readKeyName(line)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Treat as boolean key when desired, and whole line is key name.
|
// Treat as boolean key when desired, and whole line is key name.
|
||||||
if IsErrDelimiterNotFound(err) && f.options.AllowBooleanKeys {
|
if IsErrDelimiterNotFound(err) {
|
||||||
kname, err := p.readValue(line,
|
switch {
|
||||||
parserBufferSize,
|
case f.options.AllowBooleanKeys:
|
||||||
f.options.IgnoreContinuation,
|
kname, err := p.readValue(line,
|
||||||
f.options.IgnoreInlineComment,
|
parserBufferSize,
|
||||||
f.options.UnescapeValueDoubleQuotes,
|
f.options.IgnoreContinuation,
|
||||||
f.options.UnescapeValueCommentSymbols,
|
f.options.IgnoreInlineComment,
|
||||||
f.options.AllowPythonMultilineValues,
|
f.options.UnescapeValueDoubleQuotes,
|
||||||
f.options.SpaceBeforeInlineComment)
|
f.options.UnescapeValueCommentSymbols,
|
||||||
if err != nil {
|
f.options.AllowPythonMultilineValues,
|
||||||
return err
|
f.options.SpaceBeforeInlineComment)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
key, err := section.NewBooleanKey(kname)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
key.Comment = strings.TrimSpace(p.comment.String())
|
||||||
|
p.comment.Reset()
|
||||||
|
continue
|
||||||
|
|
||||||
|
case f.options.SkipUnrecognizableLines:
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
key, err := section.NewBooleanKey(kname)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
key.Comment = strings.TrimSpace(p.comment.String())
|
|
||||||
p.comment.Reset()
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user