mirror of
https://github.com/go-ini/ini.git
synced 2026-06-19 21:46:45 +02:00
Return an empty array from ValueWithShadows if there is none (#313)
Co-authored-by: Joe Chen <jc@unknwon.io>
This commit is contained in:
co-authored by
Joe Chen
parent
d26f092124
commit
fcd6cc399e
@@ -7,3 +7,6 @@ charset = utf-8
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
[*_test.go]
|
||||
trim_trailing_whitespace = false
|
||||
|
||||
@@ -442,16 +442,16 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
|
||||
kname = `"""` + kname + `"""`
|
||||
}
|
||||
|
||||
for _, val := range key.ValueWithShadows() {
|
||||
writeKeyValue := func(val string) (bool, error) {
|
||||
if _, err := buf.WriteString(kname); err != nil {
|
||||
return nil, err
|
||||
return false, err
|
||||
}
|
||||
|
||||
if key.isBooleanType {
|
||||
if kname != sec.keyList[len(sec.keyList)-1] {
|
||||
buf.WriteString(LineBreak)
|
||||
}
|
||||
continue KeyList
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// Write out alignment spaces before "=" sign
|
||||
@@ -468,10 +468,27 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
|
||||
val = `"` + val + `"`
|
||||
}
|
||||
if _, err := buf.WriteString(equalSign + val + LineBreak); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
shadows := key.ValueWithShadows()
|
||||
if len(shadows) == 0 {
|
||||
if _, err := writeKeyValue(""); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
for _, val := range shadows {
|
||||
exitLoop, err := writeKeyValue(val)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if exitLoop {
|
||||
continue KeyList
|
||||
}
|
||||
}
|
||||
|
||||
for _, val := range key.nestedValues {
|
||||
if _, err := buf.WriteString(indent + " " + val + LineBreak); err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -113,6 +113,9 @@ func (k *Key) Value() string {
|
||||
// ValueWithShadows returns raw values of key and its shadows if any.
|
||||
func (k *Key) ValueWithShadows() []string {
|
||||
if len(k.shadows) == 0 {
|
||||
if k.value == "" {
|
||||
return []string{}
|
||||
}
|
||||
return []string{k.value}
|
||||
}
|
||||
vals := make([]string, len(k.shadows)+1)
|
||||
|
||||
+19
@@ -521,6 +521,25 @@ func TestKey_Helpers(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestKey_ValueWithShadows(t *testing.T) {
|
||||
t.Run("", func(t *testing.T) {
|
||||
f, err := ShadowLoad([]byte(`
|
||||
keyName = value1
|
||||
keyName = value2
|
||||
`))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, f)
|
||||
|
||||
k := f.Section("").Key("FakeKey")
|
||||
require.NotNil(t, k)
|
||||
assert.Equal(t, []string{}, k.ValueWithShadows())
|
||||
|
||||
k = f.Section("").Key("keyName")
|
||||
require.NotNil(t, k)
|
||||
assert.Equal(t, []string{"value1", "value2"}, k.ValueWithShadows())
|
||||
})
|
||||
}
|
||||
|
||||
func TestKey_StringsWithShadows(t *testing.T) {
|
||||
t.Run("get strings of shadows of a key", func(t *testing.T) {
|
||||
f, err := ShadowLoad([]byte(""))
|
||||
|
||||
Reference in New Issue
Block a user