Skip to content

Commit

Permalink
Improved the behaviour of parseFile wrt. when sections are defined.
Browse files Browse the repository at this point in the history
* Only create the default section if it is not empty.
* Always create user defined sections, even when empty.
* Added a test for this: TestDefinedSectionBehaviour
  • Loading branch information
vaughan0 committed Sep 18, 2013
1 parent bf71521 commit e2d15f8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,18 @@ if !ok {
Iterate through values in a section:

```go
for key, value := range file.Section("mysection") {
for key, value := range file["mysection"] {
fmt.Printf("%s => %s\n", key, value)
}
```

Iterate through sections in a file:

```go
for sectionName, section := range file {
fmt.Printf("Section name: %s", sectionName)
for name, section := range file {
fmt.Printf("Section name: %s\n", name)
}
```
Note that the current implementation always includes the empty section
when iterating.

File Format
-----------
Expand Down
8 changes: 5 additions & 3 deletions ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (f File) LoadFile(file string) (err error) {
}

func parseFile(in *bufio.Reader, file File) (err error) {
section := file.Section("")
section := ""
lineNum := 0
for done := false; !done; {
var line string
Expand All @@ -93,10 +93,12 @@ func parseFile(in *bufio.Reader, file File) (err error) {
if groups := assignRegex.FindStringSubmatch(line); groups != nil {
key, val := groups[1], groups[2]
key, val = strings.TrimSpace(key), strings.TrimSpace(val)
section[key] = val
file.Section(section)[key] = val
} else if groups := sectionRegex.FindStringSubmatch(line); groups != nil {
name := strings.TrimSpace(groups[1])
section = file.Section(name)
section = name
// Create the section if it does not exist
file.Section(section)
} else {
return ErrSyntax{lineNum, line}
}
Expand Down
26 changes: 26 additions & 0 deletions ini_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ini

import (
"reflect"
"strings"
"testing"
)
Expand Down Expand Up @@ -61,3 +62,28 @@ func TestSyntaxError(t *testing.T) {
t.Fatal("incorrect source")
}
}

func TestDefinedSectionBehaviour(t *testing.T) {
check := func(src string, expect File) {
file, err := Load(strings.NewReader(src))
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(file, expect) {
t.Errorf("expected %v, got %v", expect, file)
}
}
// No sections for an empty file
check("", File{})
// Default section only if there are actually values for it
check("foo=bar", File{"": {"foo": "bar"}})
// User-defined sections should always be present, even if empty
check("[a]\n[b]\nfoo=bar", File{
"a": {},
"b": {"foo": "bar"},
})
check("foo=bar\n[a]\nthis=that", File{
"": {"foo": "bar"},
"a": {"this": "that"},
})
}

0 comments on commit e2d15f8

Please sign in to comment.