Key/value parsing always checks group integrity

This commit is contained in:
Sasha Koshka 2024-04-28 19:34:59 -04:00
parent 8b4f77b223
commit e9ff323f69
2 changed files with 35 additions and 7 deletions

View File

@ -64,6 +64,18 @@ func Parse (reader io.Reader) (File, error) {
var file = File { }
var group Group
var specifiedValues map[string] struct { }
checkGroupIntegrity := func () error {
if group != nil {
for key := range group {
if _, ok := specifiedValues[key]; !ok {
return ErrNoDefaultValue
}
}
}
return nil
}
for {
line, err := buffer.ReadString('\n')
if errors.Is(err, io.EOF) { break }
@ -78,13 +90,8 @@ func Parse (reader io.Reader) (File, error) {
// group header
case strings.HasPrefix(line, "["):
// check integrity of prev. group
if group != nil {
for key := range group {
if _, ok := specifiedValues[key]; !ok {
return nil, ErrNoDefaultValue
}
}
}
err := checkGroupIntegrity()
if err != nil { return File { }, err }
// create new group
name, err := parseGroupHeader(line)
@ -122,6 +129,9 @@ func Parse (reader io.Reader) (File, error) {
}
}
err := checkGroupIntegrity()
if err != nil { return File { }, err }
return file, nil
}

View File

@ -52,6 +52,15 @@ func testParseString (test *testing.T, input string, correct File) {
}
}
func testParseErr (test *testing.T, input string, correct error) {
_, err := Parse(strings.NewReader(input))
if err != correct {
test.Fatalf (
"errors are different\n---correct---\n%v\n---got---\n%v\n",
err, correct)
}
}
func testEntry (value string) Entry {
entry := newEntry()
entry.Value = value
@ -222,3 +231,12 @@ File {
},
},
})}
func TestParseErrNoDefaultValue (test *testing.T) {
testParseErr(test, `
[Group 0]
Name[xx_XX]=other thing
Name[yy_YY]=another thing
Name[zz_ZZ]=yet another thing
`, ErrNoDefaultValue)
}