Skip to content

Commit 6098d42

Browse files
authored
parser: fix line skipping when key is empty (#323)
1 parent 7c569cc commit 6098d42

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

error.go

+15
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,18 @@ func IsErrDelimiterNotFound(err error) bool {
3232
func (err ErrDelimiterNotFound) Error() string {
3333
return fmt.Sprintf("key-value delimiter not found: %s", err.Line)
3434
}
35+
36+
// ErrEmptyKeyName indicates the error type of no key name is found which there should be one.
37+
type ErrEmptyKeyName struct {
38+
Line string
39+
}
40+
41+
// IsErrEmptyKeyName returns true if the given error is an instance of ErrEmptyKeyName.
42+
func IsErrEmptyKeyName(err error) bool {
43+
_, ok := err.(ErrEmptyKeyName)
44+
return ok
45+
}
46+
47+
func (err ErrEmptyKeyName) Error() string {
48+
return fmt.Sprintf("empty key name: %s", err.Line)
49+
}

ini_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,8 @@ BiomeRarityScale: 100
441441
442442
BiomeGroup(NormalBiomes, 3, 99, RoofedForestEnchanted, ForestSakura, FloatingJungle
443443
BiomeGroup(IceBiomes, 4, 85, Ice Plains)
444+
445+
= RainForest
444446
`))
445447
require.NoError(t, err)
446448
require.NotNil(t, f)

parser.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ func readKeyName(delimiters string, in []byte) (string, int, error) {
164164
if endIdx < 0 {
165165
return "", -1, ErrDelimiterNotFound{line}
166166
}
167+
if endIdx == 0 {
168+
return "", -1, ErrEmptyKeyName{line}
169+
}
170+
167171
return strings.TrimSpace(line[0:endIdx]), endIdx + 1, nil
168172
}
169173

@@ -463,8 +467,9 @@ func (f *File) parse(reader io.Reader) (err error) {
463467

464468
kname, offset, err := readKeyName(f.options.KeyValueDelimiters, line)
465469
if err != nil {
470+
switch {
466471
// Treat as boolean key when desired, and whole line is key name.
467-
if IsErrDelimiterNotFound(err) {
472+
case IsErrDelimiterNotFound(err):
468473
switch {
469474
case f.options.AllowBooleanKeys:
470475
kname, err := p.readValue(line, parserBufferSize)
@@ -482,6 +487,8 @@ func (f *File) parse(reader io.Reader) (err error) {
482487
case f.options.SkipUnrecognizableLines:
483488
continue
484489
}
490+
case IsErrEmptyKeyName(err) && f.options.SkipUnrecognizableLines:
491+
continue
485492
}
486493
return err
487494
}

0 commit comments

Comments
 (0)