Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix value format when SpaceBeforeInlineComment:true #326

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion file.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"
"io/ioutil"
"os"
"regexp"
"strings"
"sync"
)
Expand All @@ -47,6 +48,11 @@ type File struct {
ValueMapper
}

var (
commentMarker = regexp.MustCompile(`[#;]`)
commentSpaceMarker = regexp.MustCompile(`\s+[#;]`)
)

// newFile initializes File object with given data sources.
func newFile(dataSources []dataSource, opts LoadOptions) *File {
if len(opts.KeyValueDelimiters) == 0 {
Expand Down Expand Up @@ -444,6 +450,15 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
kname = `"""` + kname + `"""`
}

hasInlineComment := func(val string) bool {
if f.options.IgnoreInlineComment {
return false
}
if f.options.SpaceBeforeInlineComment {
return commentSpaceMarker.MatchString(val)
}
return commentMarker.MatchString(val)
}
writeKeyValue := func(val string) (bool, error) {
if _, err := buf.WriteString(kname); err != nil {
return false, err
Expand All @@ -462,7 +477,7 @@ func (f *File) writeToBuffer(indent string) (*bytes.Buffer, error) {
// In case key value contains "\n", "`", "\"", "#" or ";"
if strings.ContainsAny(val, "\n`") {
val = `"""` + val + `"""`
} else if !f.options.IgnoreInlineComment && strings.ContainsAny(val, "#;") {
} else if hasInlineComment(val) {
val = "`" + val + "`"
} else if len(strings.TrimSpace(val)) != len(val) {
val = `"` + val + `"`
Expand Down
44 changes: 44 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,50 @@ test =

})

t.Run("support inline comments (no require space before)", func(t *testing.T) {
f, err := LoadSources(LoadOptions{SpaceBeforeInlineComment: false}, []byte{})
require.NoError(t, err)
var buf bytes.Buffer

f.Section("").Key("test").SetValue("a#b")
_, err = f.WriteTo(&buf)
require.NoError(t, err)
assert.Equal(t, "test = `a#b`\n", buf.String())
})

t.Run("support inline comments (no require space before)", func(t *testing.T) {
f, err := LoadSources(LoadOptions{SpaceBeforeInlineComment: false}, []byte{})
require.NoError(t, err)
var buf bytes.Buffer

f.Section("").Key("test").SetValue("a #b")
_, err = f.WriteTo(&buf)
require.NoError(t, err)
assert.Equal(t, "test = `a #b`\n", buf.String())
})

t.Run("support inline comments (require space before)", func(t *testing.T) {
f, err := LoadSources(LoadOptions{SpaceBeforeInlineComment: true}, []byte{})
require.NoError(t, err)
var buf bytes.Buffer

f.Section("").Key("test").SetValue("a#b")
_, err = f.WriteTo(&buf)
require.NoError(t, err)
assert.Equal(t, "test = a#b\n", buf.String())
})

t.Run("support inline comments (require space before)", func(t *testing.T) {
f, err := LoadSources(LoadOptions{SpaceBeforeInlineComment: true}, []byte{})
require.NoError(t, err)
var buf bytes.Buffer

f.Section("").Key("test").SetValue("a #b")
_, err = f.WriteTo(&buf)
require.NoError(t, err)
assert.Equal(t, "test = `a #b`\n", buf.String())
})

t.Run("keep leading and trailing spaces in value", func(t *testing.T) {
f, _ := Load([]byte(`[foo]
bar1 = ' val ue1 '
Expand Down