Skip to content

Commit

Permalink
write: pretty print section only if it is not the last one (#321)
Browse files Browse the repository at this point in the history
Co-authored-by: Joe Chen <[email protected]>
  • Loading branch information
danilo-gemoli and unknwon authored May 29, 2022
1 parent 386cf30 commit 842b9a9
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 34 deletions.
10 changes: 5 additions & 5 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {

// Use buffer to make sure target is safe until finish encoding.
buf := bytes.NewBuffer(nil)
lastSectionIdx := len(f.sectionList) - 1
for i, sname := range f.sectionList {
sec := f.SectionWithIndex(sname, f.sectionIndexes[i])
if len(sec.Comment) > 0 {
Expand Down Expand Up @@ -371,12 +372,13 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
}
}

isLastSection := i == lastSectionIdx
if sec.isRawSection {
if _, err := buf.WriteString(sec.rawBody); err != nil {
return nil, err
}

if PrettySection {
if PrettySection && !isLastSection {
// Put a line between sections
if _, err := buf.WriteString(LineBreak); err != nil {
return nil, err
Expand Down Expand Up @@ -448,9 +450,7 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
}

if key.isBooleanType {
if kname != sec.keyList[len(sec.keyList)-1] {
buf.WriteString(LineBreak)
}
buf.WriteString(LineBreak)
return true, nil
}

Expand Down Expand Up @@ -496,7 +496,7 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
}
}

if PrettySection {
if PrettySection && !isLastSection {
// Put a line between sections
if _, err := buf.WriteString(LineBreak); err != nil {
return nil, err
Expand Down
6 changes: 0 additions & 6 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ AllowedIPs = 192.168.2.3/32
[Peer]
PublicKey = <client3's publickey>
AllowedIPs = 192.168.2.4/32
`, str)
})

Expand Down Expand Up @@ -155,7 +154,6 @@ AllowedIPs = 192.168.2.2/32
[Peer]
PublicKey = <client3's publickey>
AllowedIPs = 192.168.2.4/32
`, str)
})

Expand Down Expand Up @@ -373,7 +371,6 @@ func TestFile_DeleteSection(t *testing.T) {

assert.Equal(t, `[section1]
key1 = value1
`, buf.String())
})

Expand Down Expand Up @@ -456,7 +453,6 @@ domain = mydomain.com
; Multiline
; Comment
test =
`, buf.String())

})
Expand All @@ -476,7 +472,6 @@ bar3 = " val ue3 "
bar1 = " val ue1 "
bar2 = " val ue2 "
bar3 = " val ue3 "
`, buf.String())
})
}
Expand Down Expand Up @@ -517,7 +512,6 @@ Populations -> 12345678,98765432
Coordinates -> 192.168,10.11
Flags -> true,false
Note -> Hello world!
`)
_, err = f.WriteTo(&actual)
require.NoError(t, err)
Expand Down
16 changes: 4 additions & 12 deletions ini_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ func TestInsensitiveLoad(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, `[author]
e-mail = [email protected]
`,
buf.String(),
)
Expand Down Expand Up @@ -305,7 +304,6 @@ func TestLoadSources(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, `[author]
e-mail = [email protected]
`,
buf.String(),
)
Expand Down Expand Up @@ -333,7 +331,6 @@ e-mail = [email protected]
require.NoError(t, err)
assert.Equal(t, `[author]
E-MAIL = [email protected]
`,
buf.String(),
)
Expand Down Expand Up @@ -361,7 +358,6 @@ E-MAIL = [email protected]
require.NoError(t, err)
assert.Equal(t, `[author]
e-mail = [email protected]
`,
buf.String(),
)
Expand Down Expand Up @@ -517,7 +513,6 @@ fetch = +refs/heads/*:refs/remotes/origin/*`))
url = https://github.com/Antergone/test1.git
url = https://github.com/Antergone/test2.git
fetch = +refs/heads/*:refs/remotes/origin/*
`,
buf.String(),
)
Expand Down Expand Up @@ -822,7 +817,8 @@ my lesson state data – 1111111111111111111000000000000000001110000
111111111111111111100000000000111000000000 – end my lesson state data
[COMMENTS]
<1><L.Slide#2> This slide has the fuel listed in the wrong units <e.1>`))
<1><L.Slide#2> This slide has the fuel listed in the wrong units <e.1>
`))
require.NoError(t, err)
require.NotNil(t, f)

Expand Down Expand Up @@ -935,7 +931,6 @@ GITHUB = U;n;k;n;w;o;n
require.NoError(t, err)
assert.Equal(t, `[author]
e-mail = [email protected]
`,
buf.String(),
)
Expand Down Expand Up @@ -1070,7 +1065,6 @@ fetch = +refs/heads/*:refs/remotes/origin/*`))
url = https://github.com/Antergone/test1.git
url = https://github.com/Antergone/test2.git
fetch = +refs/heads/*:refs/remotes/origin/*
`,
buf.String(),
)
Expand Down Expand Up @@ -1261,7 +1255,8 @@ my lesson state data – 1111111111111111111000000000000000001110000
111111111111111111100000000000111000000000 – end my lesson state data
[COMMENTS]
<1><L.Slide#2> This slide has the fuel listed in the wrong units <e.1>`))
<1><L.Slide#2> This slide has the fuel listed in the wrong units <e.1>
`))
require.NoError(t, err)
require.NotNil(t, f)

Expand Down Expand Up @@ -1413,7 +1408,6 @@ GITHUB = U;n;k;n;w;o;n
require.NoError(t, err)
assert.Equal(t, `[author]
E-MAIL = [email protected]
`,
buf.String(),
)
Expand All @@ -1434,7 +1428,6 @@ E-MAIL = [email protected]
require.NoError(t, err)
assert.Equal(t, `[author]
E-MAIL = [email protected]
`,
buf.String(),
)
Expand All @@ -1451,7 +1444,6 @@ E-MAIL = [email protected]
[author]
E-MAIL = [email protected]
`,
buf.String(),
)
Expand Down
1 change: 0 additions & 1 deletion key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,6 @@ region = us-west-2
s3 =
max_concurrent_requests=10
max_queue_size=1000
`,
buf.String(),
)
Expand Down
9 changes: 0 additions & 9 deletions struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ AllowedIPs = 10.2.0.2/32,fd00:2::2/128
PublicKey = pubClientKey2
PresharedKey = psKey2
AllowedIPs = 10.2.0.3/32,fd00:2::3/128
`

type unsupport struct {
Expand Down Expand Up @@ -550,7 +549,6 @@ Populations = 12345678,98765432
Coordinates = 192.168,10.11
Flags = true,false
None =
`,
buf.String(),
)
Expand Down Expand Up @@ -588,7 +586,6 @@ None =
assert.Equal(t, `first_name = John
last_name = Doe
omitempty = 9
`,
buf.String(),
)
Expand Down Expand Up @@ -623,7 +620,6 @@ enable = true
type = type
addr = address
name = name
`,
buf.String(),
)
Expand Down Expand Up @@ -697,7 +693,6 @@ AllowedIPs = 10.2.0.4/32,fd00:2::4/128
PublicKey = pubClientKey4
PresharedKey = psKey4
AllowedIPs = 10.2.0.5/32,fd00:2::5/128
`,
buf.String(),
)
Expand All @@ -723,7 +718,6 @@ PrivateKey = privServerKey
PublicKey = pubClientKey5
PresharedKey = psKey5
AllowedIPs = 10.2.0.6/32,fd00:2::6/128
`,
buf.String(),
)
Expand Down Expand Up @@ -759,7 +753,6 @@ path = /tmp/gpm-profiles/test1.profile`))
assert.Equal(t, `[include]
path = /tmp/gpm-profiles/test5.profile
path = /tmp/gpm-profiles/test1.profile
`,
buf.String(),
)
Expand Down Expand Up @@ -823,7 +816,6 @@ Coordinates = 10.11
Flags = true
Flags = false
None =
`,
buf.String(),
)
Expand Down Expand Up @@ -913,7 +905,6 @@ Title = Staff II Engineer
[Employer "EMC"]
Title = Consultant Engineer
`,
buf.String(),
)
Expand Down
1 change: 0 additions & 1 deletion testdata/TestFile_WriteTo.golden
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,3 @@ ADDRESS = """404 road,
NotFound, State, 50000"""
two_lines = how about continuation lines?
lots_of_lines = "1 2 3 4 "

0 comments on commit 842b9a9

Please sign in to comment.