Skip to content

Commit 842b9a9

Browse files
write: pretty print section only if it is not the last one (#321)
Co-authored-by: Joe Chen <[email protected]>
1 parent 386cf30 commit 842b9a9

6 files changed

+9
-34
lines changed

file.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
342342

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

375+
isLastSection := i == lastSectionIdx
374376
if sec.isRawSection {
375377
if _, err := buf.WriteString(sec.rawBody); err != nil {
376378
return nil, err
377379
}
378380

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

450452
if key.isBooleanType {
451-
if kname != sec.keyList[len(sec.keyList)-1] {
452-
buf.WriteString(LineBreak)
453-
}
453+
buf.WriteString(LineBreak)
454454
return true, nil
455455
}
456456

@@ -496,7 +496,7 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
496496
}
497497
}
498498

499-
if PrettySection {
499+
if PrettySection && !isLastSection {
500500
// Put a line between sections
501501
if _, err := buf.WriteString(LineBreak); err != nil {
502502
return nil, err

file_test.go

-6
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ AllowedIPs = 192.168.2.3/32
108108
[Peer]
109109
PublicKey = <client3's publickey>
110110
AllowedIPs = 192.168.2.4/32
111-
112111
`, str)
113112
})
114113

@@ -155,7 +154,6 @@ AllowedIPs = 192.168.2.2/32
155154
[Peer]
156155
PublicKey = <client3's publickey>
157156
AllowedIPs = 192.168.2.4/32
158-
159157
`, str)
160158
})
161159

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

374372
assert.Equal(t, `[section1]
375373
key1 = value1
376-
377374
`, buf.String())
378375
})
379376

@@ -456,7 +453,6 @@ domain = mydomain.com
456453
; Multiline
457454
; Comment
458455
test =
459-
460456
`, buf.String())
461457

462458
})
@@ -476,7 +472,6 @@ bar3 = " val ue3 "
476472
bar1 = " val ue1 "
477473
bar2 = " val ue2 "
478474
bar3 = " val ue3 "
479-
480475
`, buf.String())
481476
})
482477
}
@@ -517,7 +512,6 @@ Populations -> 12345678,98765432
517512
Coordinates -> 192.168,10.11
518513
Flags -> true,false
519514
Note -> Hello world!
520-
521515
`)
522516
_, err = f.WriteTo(&actual)
523517
require.NoError(t, err)

ini_test.go

+4-12
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,6 @@ func TestInsensitiveLoad(t *testing.T) {
250250
require.NoError(t, err)
251251
assert.Equal(t, `[author]
252252
253-
254253
`,
255254
buf.String(),
256255
)
@@ -305,7 +304,6 @@ func TestLoadSources(t *testing.T) {
305304
require.NoError(t, err)
306305
assert.Equal(t, `[author]
307306
308-
309307
`,
310308
buf.String(),
311309
)
@@ -333,7 +331,6 @@ e-mail = [email protected]
333331
require.NoError(t, err)
334332
assert.Equal(t, `[author]
335333
336-
337334
`,
338335
buf.String(),
339336
)
@@ -361,7 +358,6 @@ E-MAIL = [email protected]
361358
require.NoError(t, err)
362359
assert.Equal(t, `[author]
363360
364-
365361
`,
366362
buf.String(),
367363
)
@@ -517,7 +513,6 @@ fetch = +refs/heads/*:refs/remotes/origin/*`))
517513
url = https://github.com/Antergone/test1.git
518514
url = https://github.com/Antergone/test2.git
519515
fetch = +refs/heads/*:refs/remotes/origin/*
520-
521516
`,
522517
buf.String(),
523518
)
@@ -822,7 +817,8 @@ my lesson state data – 1111111111111111111000000000000000001110000
822817
111111111111111111100000000000111000000000 – end my lesson state data
823818
824819
[COMMENTS]
825-
<1><L.Slide#2> This slide has the fuel listed in the wrong units <e.1>`))
820+
<1><L.Slide#2> This slide has the fuel listed in the wrong units <e.1>
821+
`))
826822
require.NoError(t, err)
827823
require.NotNil(t, f)
828824

@@ -935,7 +931,6 @@ GITHUB = U;n;k;n;w;o;n
935931
require.NoError(t, err)
936932
assert.Equal(t, `[author]
937933
938-
939934
`,
940935
buf.String(),
941936
)
@@ -1070,7 +1065,6 @@ fetch = +refs/heads/*:refs/remotes/origin/*`))
10701065
url = https://github.com/Antergone/test1.git
10711066
url = https://github.com/Antergone/test2.git
10721067
fetch = +refs/heads/*:refs/remotes/origin/*
1073-
10741068
`,
10751069
buf.String(),
10761070
)
@@ -1261,7 +1255,8 @@ my lesson state data – 1111111111111111111000000000000000001110000
12611255
111111111111111111100000000000111000000000 – end my lesson state data
12621256
12631257
[COMMENTS]
1264-
<1><L.Slide#2> This slide has the fuel listed in the wrong units <e.1>`))
1258+
<1><L.Slide#2> This slide has the fuel listed in the wrong units <e.1>
1259+
`))
12651260
require.NoError(t, err)
12661261
require.NotNil(t, f)
12671262

@@ -1413,7 +1408,6 @@ GITHUB = U;n;k;n;w;o;n
14131408
require.NoError(t, err)
14141409
assert.Equal(t, `[author]
14151410
1416-
14171411
`,
14181412
buf.String(),
14191413
)
@@ -1434,7 +1428,6 @@ E-MAIL = [email protected]
14341428
require.NoError(t, err)
14351429
assert.Equal(t, `[author]
14361430
1437-
14381431
`,
14391432
buf.String(),
14401433
)
@@ -1451,7 +1444,6 @@ E-MAIL = [email protected]
14511444
14521445
[author]
14531446
1454-
14551447
`,
14561448
buf.String(),
14571449
)

key_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,6 @@ region = us-west-2
604604
s3 =
605605
max_concurrent_requests=10
606606
max_queue_size=1000
607-
608607
`,
609608
buf.String(),
610609
)

struct_test.go

-9
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ AllowedIPs = 10.2.0.2/32,fd00:2::2/128
169169
PublicKey = pubClientKey2
170170
PresharedKey = psKey2
171171
AllowedIPs = 10.2.0.3/32,fd00:2::3/128
172-
173172
`
174173

175174
type unsupport struct {
@@ -550,7 +549,6 @@ Populations = 12345678,98765432
550549
Coordinates = 192.168,10.11
551550
Flags = true,false
552551
None =
553-
554552
`,
555553
buf.String(),
556554
)
@@ -588,7 +586,6 @@ None =
588586
assert.Equal(t, `first_name = John
589587
last_name = Doe
590588
omitempty = 9
591-
592589
`,
593590
buf.String(),
594591
)
@@ -623,7 +620,6 @@ enable = true
623620
type = type
624621
addr = address
625622
name = name
626-
627623
`,
628624
buf.String(),
629625
)
@@ -697,7 +693,6 @@ AllowedIPs = 10.2.0.4/32,fd00:2::4/128
697693
PublicKey = pubClientKey4
698694
PresharedKey = psKey4
699695
AllowedIPs = 10.2.0.5/32,fd00:2::5/128
700-
701696
`,
702697
buf.String(),
703698
)
@@ -723,7 +718,6 @@ PrivateKey = privServerKey
723718
PublicKey = pubClientKey5
724719
PresharedKey = psKey5
725720
AllowedIPs = 10.2.0.6/32,fd00:2::6/128
726-
727721
`,
728722
buf.String(),
729723
)
@@ -759,7 +753,6 @@ path = /tmp/gpm-profiles/test1.profile`))
759753
assert.Equal(t, `[include]
760754
path = /tmp/gpm-profiles/test5.profile
761755
path = /tmp/gpm-profiles/test1.profile
762-
763756
`,
764757
buf.String(),
765758
)
@@ -823,7 +816,6 @@ Coordinates = 10.11
823816
Flags = true
824817
Flags = false
825818
None =
826-
827819
`,
828820
buf.String(),
829821
)
@@ -913,7 +905,6 @@ Title = Staff II Engineer
913905
914906
[Employer "EMC"]
915907
Title = Consultant Engineer
916-
917908
`,
918909
buf.String(),
919910
)

testdata/TestFile_WriteTo.golden

-1
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,3 @@ ADDRESS = """404 road,
8585
NotFound, State, 50000"""
8686
two_lines = how about continuation lines?
8787
lots_of_lines = "1 2 3 4 "
88-

0 commit comments

Comments
 (0)