Fix key/value parsing returning erroneous duplicate entry errors

This commit is contained in:
Sasha Koshka 2024-04-28 19:27:07 -04:00
parent 3b1620b5ef
commit 8b4f77b223
2 changed files with 63 additions and 4 deletions

View File

@ -66,7 +66,7 @@ func Parse (reader io.Reader) (File, error) {
var specifiedValues map[string] struct { } var specifiedValues map[string] struct { }
for { for {
line, err := buffer.ReadString('\n') line, err := buffer.ReadString('\n')
if errors.Is(err, io.EOF) { return file, nil } if errors.Is(err, io.EOF) { break }
if err != nil { return nil, err } if err != nil { return nil, err }
line = strings.TrimSpace(line) line = strings.TrimSpace(line)
@ -109,15 +109,20 @@ func Parse (reader io.Reader) (File, error) {
group[key] = entry group[key] = entry
} else { } else {
// default value // default value
_, exists := group[key] _, specified := specifiedValues[key]
if exists { return nil, ErrDuplicateEntry } if specified { return nil, ErrDuplicateEntry }
entry := newEntry()
entry, exists := group[key]
if !exists { entry = newEntry() }
entry.Value = value entry.Value = value
group[key] = entry group[key] = entry
specifiedValues[key] = struct { } { } specifiedValues[key] = struct { } { }
} }
} }
} }
return file, nil
} }
func newEntry () Entry { func newEntry () Entry {

View File

@ -168,3 +168,57 @@ File {
"Icon": testEntry("fooview-new"), "Icon": testEntry("fooview-new"),
}, },
})} })}
func TestParseLocalized (test *testing.T) {
testParseString(test, `
[Group 0]
Name=something
Name[xx_XX]=other thing
Name[yy_YY]=another thing
Name[zz_ZZ]=yet another thing
[Group 1]
Name[xx_XX]=other thing
Name[yy_YY]=another thing
Name=something
Name[zz_ZZ]=yet another thing
`,
File {
"Group 0": Group {
"Name": Entry {
Value: "something",
Localized: map[locale.Locale] string {
locale.Locale {
Lang: "xx",
Country: "XX",
}: "other thing",
locale.Locale {
Lang: "yy",
Country: "YY",
}: "another thing",
locale.Locale {
Lang: "zz",
Country: "ZZ",
}: "yet another thing",
},
},
},
"Group 1": Group {
"Name": Entry {
Value: "something",
Localized: map[locale.Locale] string {
locale.Locale {
Lang: "xx",
Country: "XX",
}: "other thing",
locale.Locale {
Lang: "yy",
Country: "YY",
}: "another thing",
locale.Locale {
Lang: "zz",
Country: "ZZ",
}: "yet another thing",
},
},
},
})}