From 7de206c760f0e01093828f9d5f1afa18838a1e3a Mon Sep 17 00:00:00 2001
From: Danilo Gemoli
Date: Sat, 21 May 2022 17:35:13 +0200
Subject: [PATCH 1/2] Pretty print section only if it is not the last one
---
file.go | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/file.go b/file.go
index 9d91c31..0b826bc 100644
--- a/file.go
+++ b/file.go
@@ -342,7 +342,9 @@ 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 {
+ isLastSection := i == lastSectionIdx
sec := f.SectionWithIndex(sname, f.sectionIndexes[i])
if len(sec.Comment) > 0 {
// Support multiline comments
@@ -376,7 +378,7 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
return nil, err
}
- if PrettySection {
+ if PrettySection && !isLastSection {
// Put a line between sections
if _, err := buf.WriteString(LineBreak); err != nil {
return nil, err
@@ -496,7 +498,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
From c45b39d4240f0e10651465ca0d79fdd358a9ac62 Mon Sep 17 00:00:00 2001
From: Joe Chen
Date: Sun, 29 May 2022 16:45:11 +0800
Subject: [PATCH 2/2] fix tests
---
file.go | 6 ++----
file_test.go | 6 ------
ini_test.go | 16 ++++------------
key_test.go | 1 -
struct_test.go | 9 ---------
testdata/TestFile_WriteTo.golden | 1 -
6 files changed, 6 insertions(+), 33 deletions(-)
diff --git a/file.go b/file.go
index 0b826bc..f8b2240 100644
--- a/file.go
+++ b/file.go
@@ -344,7 +344,6 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
buf := bytes.NewBuffer(nil)
lastSectionIdx := len(f.sectionList) - 1
for i, sname := range f.sectionList {
- isLastSection := i == lastSectionIdx
sec := f.SectionWithIndex(sname, f.sectionIndexes[i])
if len(sec.Comment) > 0 {
// Support multiline comments
@@ -373,6 +372,7 @@ 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
@@ -450,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
}
diff --git a/file_test.go b/file_test.go
index 6ab6739..c9914b5 100644
--- a/file_test.go
+++ b/file_test.go
@@ -108,7 +108,6 @@ AllowedIPs = 192.168.2.3/32
[Peer]
PublicKey =
AllowedIPs = 192.168.2.4/32
-
`, str)
})
@@ -155,7 +154,6 @@ AllowedIPs = 192.168.2.2/32
[Peer]
PublicKey =
AllowedIPs = 192.168.2.4/32
-
`, str)
})
@@ -373,7 +371,6 @@ func TestFile_DeleteSection(t *testing.T) {
assert.Equal(t, `[section1]
key1 = value1
-
`, buf.String())
})
@@ -456,7 +453,6 @@ domain = mydomain.com
; Multiline
; Comment
test =
-
`, buf.String())
})
@@ -476,7 +472,6 @@ bar3 = " val ue3 "
bar1 = " val ue1 "
bar2 = " val ue2 "
bar3 = " val ue3 "
-
`, buf.String())
})
}
@@ -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)
diff --git a/ini_test.go b/ini_test.go
index ade2a2c..12672e2 100644
--- a/ini_test.go
+++ b/ini_test.go
@@ -250,7 +250,6 @@ func TestInsensitiveLoad(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, `[author]
e-mail = [email protected]
-
`,
buf.String(),
)
@@ -305,7 +304,6 @@ func TestLoadSources(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, `[author]
e-mail = [email protected]
-
`,
buf.String(),
)
@@ -333,7 +331,6 @@ e-mail = [email protected]
require.NoError(t, err)
assert.Equal(t, `[author]
E-MAIL = [email protected]
-
`,
buf.String(),
)
@@ -361,7 +358,6 @@ E-MAIL = [email protected]
require.NoError(t, err)
assert.Equal(t, `[author]
e-mail = [email protected]
-
`,
buf.String(),
)
@@ -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(),
)
@@ -822,7 +817,8 @@ my lesson state data – 1111111111111111111000000000000000001110000
111111111111111111100000000000111000000000 – end my lesson state data
[COMMENTS]
-<1> This slide has the fuel listed in the wrong units `))
+<1> This slide has the fuel listed in the wrong units
+`))
require.NoError(t, err)
require.NotNil(t, f)
@@ -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(),
)
@@ -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(),
)
@@ -1261,7 +1255,8 @@ my lesson state data – 1111111111111111111000000000000000001110000
111111111111111111100000000000111000000000 – end my lesson state data
[COMMENTS]
-<1> This slide has the fuel listed in the wrong units `))
+<1> This slide has the fuel listed in the wrong units
+`))
require.NoError(t, err)
require.NotNil(t, f)
@@ -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(),
)
@@ -1434,7 +1428,6 @@ E-MAIL = [email protected]
require.NoError(t, err)
assert.Equal(t, `[author]
E-MAIL = [email protected]
-
`,
buf.String(),
)
@@ -1451,7 +1444,6 @@ E-MAIL = [email protected]
[author]
E-MAIL = [email protected]
-
`,
buf.String(),
)
diff --git a/key_test.go b/key_test.go
index 83095d7..cbd9bed 100644
--- a/key_test.go
+++ b/key_test.go
@@ -604,7 +604,6 @@ region = us-west-2
s3 =
max_concurrent_requests=10
max_queue_size=1000
-
`,
buf.String(),
)
diff --git a/struct_test.go b/struct_test.go
index 6bacaea..b51243a 100644
--- a/struct_test.go
+++ b/struct_test.go
@@ -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 {
@@ -550,7 +549,6 @@ Populations = 12345678,98765432
Coordinates = 192.168,10.11
Flags = true,false
None =
-
`,
buf.String(),
)
@@ -588,7 +586,6 @@ None =
assert.Equal(t, `first_name = John
last_name = Doe
omitempty = 9
-
`,
buf.String(),
)
@@ -623,7 +620,6 @@ enable = true
type = type
addr = address
name = name
-
`,
buf.String(),
)
@@ -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(),
)
@@ -723,7 +718,6 @@ PrivateKey = privServerKey
PublicKey = pubClientKey5
PresharedKey = psKey5
AllowedIPs = 10.2.0.6/32,fd00:2::6/128
-
`,
buf.String(),
)
@@ -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(),
)
@@ -823,7 +816,6 @@ Coordinates = 10.11
Flags = true
Flags = false
None =
-
`,
buf.String(),
)
@@ -913,7 +905,6 @@ Title = Staff II Engineer
[Employer "EMC"]
Title = Consultant Engineer
-
`,
buf.String(),
)
diff --git a/testdata/TestFile_WriteTo.golden b/testdata/TestFile_WriteTo.golden
index 885924f..ea13d2a 100644
--- a/testdata/TestFile_WriteTo.golden
+++ b/testdata/TestFile_WriteTo.golden
@@ -85,4 +85,3 @@ ADDRESS = """404 road,
NotFound, State, 50000"""
two_lines = how about continuation lines?
lots_of_lines = "1 2 3 4 "
-