Skip to content

Commit b04c1a4

Browse files
committed
not elegant but solves all issues
1 parent 74fe596 commit b04c1a4

2 files changed

Lines changed: 42 additions & 17 deletions

File tree

formatter.go

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ func Format(template string, args ...any) string {
5555
continue
5656
}
5757
// find end of placeholder
58+
// process empty pair - {}
59+
if template[i+1] == '}' {
60+
i++
61+
formattedStr.WriteString("{}")
62+
continue
63+
}
64+
// process non-empty placeholder
5865
j = i + 2
5966
for {
6067
if j >= templateLen {
@@ -117,14 +124,9 @@ func Format(template string, args ...any) string {
117124
strVal := getItemAsStr(&args[argNumber], &argFormatOptions)
118125
formattedStr.WriteString(strVal)
119126
} else {
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-
}
127+
formattedStr.WriteByte('{')
128+
formattedStr.WriteString(argNumberStr)
129+
formattedStr.WriteByte('}')
128130
}
129131
i = j
130132
}
@@ -170,10 +172,18 @@ func FormatComplex(template string, args map[string]any) string {
170172
break
171173
}
172174

173-
if template[i+1] == '{' { // todo: umv: this not considering {{0}}
175+
if template[i+1] == '{' {
174176
formattedStr.WriteByte('{')
175177
continue
176178
}
179+
// find end of placeholder
180+
// process empty pair - {}
181+
if template[i+1] == '}' {
182+
i++
183+
formattedStr.WriteString("{}")
184+
continue
185+
}
186+
// process non-empty placeholder
177187

178188
// find end of placeholder
179189
j = i + 2
@@ -215,14 +225,12 @@ func FormatComplex(template string, args map[string]any) string {
215225
strVal := getItemAsStr(&arg, &argFormatOptions)
216226
formattedStr.WriteString(strVal)
217227
} else {
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-
}
228+
formattedStr.WriteByte('{')
229+
230+
//if argNumberStr != "" {
231+
formattedStr.WriteString(argNumberStr)
232+
formattedStr.WriteByte('}')
233+
//}
226234
}
227235
i = j
228236
}

formatter_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@ func TestFormat(t *testing.T) {
129129
args: []any{},
130130
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) }",
131131
},
132+
133+
"no bracket at the end of line with {} inside": {
134+
template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) ",
135+
args: []any{},
136+
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) ",
137+
},
138+
"open bracket at the end of line of go line with multiple {} inside": {
139+
template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}, additionalData interface{}) {",
140+
args: []any{},
141+
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}, additionalData interface{}) {",
142+
},
132143
} {
133144
t.Run(name, func(t *testing.T) {
134145
assert.Equal(t, test.expected, stringFormatter.Format(test.template, test.args...))
@@ -219,6 +230,12 @@ func TestFormatComplex(t *testing.T) {
219230
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) {",
220231
},
221232

233+
"open bracket at the end of line of go line with multiple {} inside": {
234+
template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}, additionalData interface{}) {",
235+
args: map[string]any{},
236+
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}, additionalData interface{}) {",
237+
},
238+
222239
"close bracket at the end of line of go line with {} inside": {
223240
template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) }",
224241
args: map[string]any{},

0 commit comments

Comments
 (0)