Skip to content

Commit fcd6cc3

Browse files
NDagestadunknwon
andauthored
Return an empty array from ValueWithShadows if there is none (#313)
Co-authored-by: Joe Chen <jc@unknwon.io>
1 parent d26f092 commit fcd6cc3

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

.editorconfig

+3
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ charset = utf-8
77
end_of_line = lf
88
insert_final_newline = true
99
trim_trailing_whitespace = true
10+
11+
[*_test.go]
12+
trim_trailing_whitespace = false

file.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -442,16 +442,16 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
442442
kname = `"""` + kname + `"""`
443443
}
444444

445-
for _, val := range key.ValueWithShadows() {
445+
writeKeyValue := func(val string) (bool, error) {
446446
if _, err := buf.WriteString(kname); err != nil {
447-
return nil, err
447+
return false, err
448448
}
449449

450450
if key.isBooleanType {
451451
if kname != sec.keyList[len(sec.keyList)-1] {
452452
buf.WriteString(LineBreak)
453453
}
454-
continue KeyList
454+
return true, nil
455455
}
456456

457457
// Write out alignment spaces before "=" sign
@@ -468,10 +468,27 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
468468
val = `"` + val + `"`
469469
}
470470
if _, err := buf.WriteString(equalSign + val + LineBreak); err != nil {
471+
return false, err
472+
}
473+
return false, nil
474+
}
475+
476+
shadows := key.ValueWithShadows()
477+
if len(shadows) == 0 {
478+
if _, err := writeKeyValue(""); err != nil {
471479
return nil, err
472480
}
473481
}
474482

483+
for _, val := range shadows {
484+
exitLoop, err := writeKeyValue(val)
485+
if err != nil {
486+
return nil, err
487+
} else if exitLoop {
488+
continue KeyList
489+
}
490+
}
491+
475492
for _, val := range key.nestedValues {
476493
if _, err := buf.WriteString(indent + " " + val + LineBreak); err != nil {
477494
return nil, err

key.go

+3
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ func (k *Key) Value() string {
113113
// ValueWithShadows returns raw values of key and its shadows if any.
114114
func (k *Key) ValueWithShadows() []string {
115115
if len(k.shadows) == 0 {
116+
if k.value == "" {
117+
return []string{}
118+
}
116119
return []string{k.value}
117120
}
118121
vals := make([]string, len(k.shadows)+1)

key_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,25 @@ func TestKey_Helpers(t *testing.T) {
521521
})
522522
}
523523

524+
func TestKey_ValueWithShadows(t *testing.T) {
525+
t.Run("", func(t *testing.T) {
526+
f, err := ShadowLoad([]byte(`
527+
keyName = value1
528+
keyName = value2
529+
`))
530+
require.NoError(t, err)
531+
require.NotNil(t, f)
532+
533+
k := f.Section("").Key("FakeKey")
534+
require.NotNil(t, k)
535+
assert.Equal(t, []string{}, k.ValueWithShadows())
536+
537+
k = f.Section("").Key("keyName")
538+
require.NotNil(t, k)
539+
assert.Equal(t, []string{"value1", "value2"}, k.ValueWithShadows())
540+
})
541+
}
542+
524543
func TestKey_StringsWithShadows(t *testing.T) {
525544
t.Run("get strings of shadows of a key", func(t *testing.T) {
526545
f, err := ShadowLoad([]byte(""))

0 commit comments

Comments
 (0)