diff --git a/ini.go b/ini.go index 5eb9d5a..35a91ac 100644 --- a/ini.go +++ b/ini.go @@ -130,6 +130,11 @@ type LoadOptions struct { // empty assignment to reset prior values. Empty values are otherwise // omitted. PreserveEmptyShadowValues bool + // EscapeSliceDelimiters indicates whether ReflectFrom escapes backslashes + // and the delimiter in the elements of a string slice, so Key.Strings and + // MapTo read back the same elements. Other INI readers may not unescape + // them, so it is off by default. + EscapeSliceDelimiters bool } // DebugFunc is the type of function called to log parse events. diff --git a/key.go b/key.go index 72f0de4..46bf21e 100644 --- a/key.go +++ b/key.go @@ -516,6 +516,16 @@ func (k *Key) RangeTime(defaultVal, min, max time.Time) time.Time { return k.RangeTimeFormat(time.RFC3339, defaultVal, min, max) } +// escapeDelimited prepares a slice element for joining with delim. +// Backslashes are escaped first, then occurrences of delim, matching Strings(). +func escapeDelimited(val, delim string) string { + if delim == "" { + return val + } + val = strings.ReplaceAll(val, `\`, `\\`) + return strings.ReplaceAll(val, delim, `\`+delim) +} + // Strings returns list of string divided by given delimiter. func (k *Key) Strings(delim string) []string { str := k.String() diff --git a/struct.go b/struct.go index c8bff94..7c77e7b 100644 --- a/struct.go +++ b/struct.go @@ -533,7 +533,11 @@ func reflectSliceWithProperType(key *Key, field reflect.Value, delim string, all for i := 0; i < field.Len(); i++ { switch sliceOf { case reflect.String: - buf.WriteString(slice.Index(i).String()) + val := slice.Index(i).String() + if key.s.f.options.EscapeSliceDelimiters { + val = escapeDelimited(val, delim) + } + buf.WriteString(val) case reflect.Int, reflect.Int64: fmt.Fprint(&buf, slice.Index(i).Int()) case reflect.Uint, reflect.Uint64: diff --git a/struct_test.go b/struct_test.go index 3fe9491..057c4d7 100644 --- a/struct_test.go +++ b/struct_test.go @@ -929,6 +929,48 @@ func Test_Duration(t *testing.T) { }) } +// https://github.com/go-ini/ini/issues/348 +func Test_ReflectFromStringSliceWithComma(t *testing.T) { + type C struct { + Scale float32 + History []string + } + in := C{ + Scale: 1.5, + History: []string{ + "Test One", + "Test Two", + "Test with comma, Three", + `C:\dir\`, + "Test Four", + }, + } + + t.Run("escaped with EscapeSliceDelimiters", func(t *testing.T) { + f := Empty(LoadOptions{EscapeSliceDelimiters: true}) + require.NoError(t, f.ReflectFrom(&in)) + + var buf bytes.Buffer + _, err := f.WriteTo(&buf) + require.NoError(t, err) + assert.Contains(t, buf.String(), `Test with comma\, Three`) + + got := f.Section("").Key("History").Strings(",") + assert.Equal(t, in.History, got) + + var out C + require.NoError(t, MapTo(&out, buf.Bytes())) + assert.Equal(t, in.History, out.History) + assert.Equal(t, float32(1.5), out.Scale) + }) + + t.Run("written as is by default", func(t *testing.T) { + f := Empty() + require.NoError(t, f.ReflectFrom(&in)) + assert.Equal(t, `Test One,Test Two,Test with comma, Three,C:\dir\,Test Four`, f.Section("").Key("History").String()) + }) +} + type Employer struct { Name string Title string