Skip to content

Commit 238da34

Browse files
committed
Fix formatting of multiline strings
1 parent 45f6cda commit 238da34

File tree

3 files changed

+77
-13
lines changed

3 files changed

+77
-13
lines changed

ast/ast_helper.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ package ast
33
import (
44
"bytes"
55
"fmt"
6+
"regexp"
67
"strings"
78
)
89

10+
// This regular expression splits the config into indentable chunks that are:
11+
// * empty line
12+
// * line of comment
13+
// * line without any quotes
14+
// * line(s) with quoted strings, which are kept together to form a single "line of configuration"
15+
var linesRe = regexp.MustCompile(`(\n|\s*//[^\n]*\n|[^'"\n]*\n|([^"'\n]*("(\\"|[^"])*"|'(\\'|[^'])*')[^"'\n]*)*\n)`)
16+
917
func prefix(in string, emptyNewline bool) string {
1018
if len(in) == 0 {
1119
if emptyNewline {
@@ -16,13 +24,13 @@ func prefix(in string, emptyNewline bool) string {
1624

1725
var s bytes.Buffer
1826
s.WriteString("\n")
19-
lines := strings.Split(strings.TrimRight(in, "\n"), "\n")
27+
lines := linesRe.FindAllString(strings.TrimRight(in, "\n")+"\n", -1)
2028
for _, l := range lines {
21-
if len(l) == 0 {
29+
if len(strings.TrimLeft(l, " \n")) == 0 {
2230
s.WriteString("\n")
2331
continue
2432
}
25-
s.WriteString(fmt.Sprintln(" " + l))
33+
s.WriteString(fmt.Sprint(" " + l))
2634
}
2735
return s.String()
2836
}

ast/ast_helper_test.go

+53-10
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,7 @@ func TestPrefix(t *testing.T) {
2020
want: "\n",
2121
},
2222
{
23-
name: "simple attribute",
24-
input: `value => 3.1415`,
25-
26-
want: `
27-
value => 3.1415
28-
`,
29-
},
30-
{
31-
name: "simple attribute with newline",
23+
name: "simple attribute",
3224
input: `value => 3.1415
3325
`,
3426

@@ -39,7 +31,8 @@ func TestPrefix(t *testing.T) {
3931
{
4032
name: "simple attribute with comment",
4133
input: `// comment
42-
value => 3.1415`,
34+
value => 3.1415
35+
`,
4336

4437
want: `
4538
// comment
@@ -63,6 +56,56 @@ add_tag => [ "foobar" ]
6356
}
6457
6558
add_tag => [ "foobar" ]
59+
`,
60+
},
61+
{
62+
name: "attribute with multiline string attribute",
63+
input: `value => "string
64+
with multiple
65+
lines"
66+
othervalue => 3.1415
67+
`,
68+
69+
want: `
70+
value => "string
71+
with multiple
72+
lines"
73+
othervalue => 3.1415
74+
`,
75+
},
76+
{
77+
name: "attribute with multiline string attribute (single quote)",
78+
input: `value => 'string
79+
with multiple
80+
lines'
81+
othervalue => 3.1415
82+
`,
83+
84+
want: `
85+
value => 'string
86+
with multiple
87+
lines'
88+
othervalue => 3.1415
89+
`,
90+
},
91+
{
92+
name: "comment with double and single quote",
93+
input: `// comment with " and '
94+
othervalue => 3.1415
95+
`,
96+
97+
want: `
98+
// comment with " and '
99+
othervalue => 3.1415
100+
`,
101+
},
102+
{
103+
name: "array with multiple string types",
104+
input: `value => [ "string", 'string', string ]
105+
`,
106+
107+
want: `
108+
value => [ "string", 'string', string ]
66109
`,
67110
},
68111
}

logstash_config_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,19 @@ output {}
368368
}
369369
}
370370
}
371+
`,
372+
},
373+
{
374+
name: "multiline string",
375+
input: `filter {
376+
mutate {
377+
add_field => {
378+
"largeint" => "a string
379+
with multiple
380+
lines"
381+
}
382+
}
383+
}
371384
`,
372385
},
373386
}

0 commit comments

Comments
 (0)