Skip to content

Commit 6bf1de2

Browse files
committed
solved 1 more task when source code processing
1 parent 53498c9 commit 6bf1de2

2 files changed

Lines changed: 33 additions & 6 deletions

File tree

formatter.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,14 @@ func Format(template string, args ...any) string {
117117
strVal := getItemAsStr(&args[argNumber], &argFormatOptions)
118118
formattedStr.WriteString(strVal)
119119
} else {
120-
formattedStr.WriteByte('{')
121-
formattedStr.WriteString(argNumberStr)
122-
formattedStr.WriteByte('}')
120+
if argNumberStr != "" {
121+
formattedStr.WriteByte('{')
122+
formattedStr.WriteString(argNumberStr)
123+
formattedStr.WriteByte('}')
124+
} else {
125+
// complicated case when we have brackets in line and open line at the end
126+
formattedStr.WriteByte('{')
127+
}
123128
}
124129
i = j
125130
}
@@ -210,9 +215,14 @@ func FormatComplex(template string, args map[string]any) string {
210215
strVal := getItemAsStr(&arg, &argFormatOptions)
211216
formattedStr.WriteString(strVal)
212217
} else {
213-
formattedStr.WriteByte('{')
214-
formattedStr.WriteString(argNumberStr)
215-
formattedStr.WriteByte('}')
218+
if argNumberStr != "" {
219+
formattedStr.WriteByte('{')
220+
formattedStr.WriteString(argNumberStr)
221+
formattedStr.WriteByte('}')
222+
} else {
223+
// complicated case when we have brackets in line and open line at the end
224+
formattedStr.WriteByte('{')
225+
}
216226
}
217227
i = j
218228
}

formatter_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ func TestFormat(t *testing.T) {
123123
args: []any{},
124124
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) {",
125125
},
126+
127+
"close bracket at the end of line of go line with {} inside": {
128+
template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) }",
129+
args: []any{},
130+
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) }",
131+
},
126132
} {
127133
t.Run(name, func(t *testing.T) {
128134
assert.Equal(t, test.expected, stringFormatter.Format(test.template, test.args...))
@@ -207,6 +213,17 @@ func TestFormatComplex(t *testing.T) {
207213
args: map[string]any{},
208214
expected: " \"server\": {",
209215
},
216+
"open bracket at the end of line of go line with {} inside": {
217+
template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) {",
218+
args: map[string]any{},
219+
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) {",
220+
},
221+
222+
"close bracket at the end of line of go line with {} inside": {
223+
template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) }",
224+
args: map[string]any{},
225+
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) }",
226+
},
210227
} {
211228
t.Run(name, func(t *testing.T) {
212229
assert.Equal(t, test.expected, stringFormatter.FormatComplex(test.template, test.args))

0 commit comments

Comments
 (0)