Skip to content

Commit b4faa43

Browse files
committed
formatter seems to be works fine
1 parent 950e61f commit b4faa43

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

formatter.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ func Format(template string, args ...any) string {
3535

3636
templateLen := len(template)
3737
formattedStr := &strings.Builder{}
38-
formattedStr.Grow(templateLen + 22*len(args))
38+
argsLen := 16 * len(args)
39+
formattedStr.Grow(templateLen + argsLen + 1)
3940
j := -1 //nolint:ineffassign
4041

4142
nestedBrackets := false
@@ -124,9 +125,10 @@ func Format(template string, args ...any) string {
124125
strVal := getItemAsStr(&args[argNumber], &argFormatOptions)
125126
formattedStr.WriteString(strVal)
126127
} else {
127-
formattedStr.WriteByte('{')
128-
formattedStr.WriteString(argNumberStr)
129-
formattedStr.WriteByte('}')
128+
formattedStr.WriteString(template[i:j])
129+
if j < templateLen-1 {
130+
formattedStr.WriteByte(template[j])
131+
}
130132
}
131133
i = j
132134
}
@@ -225,9 +227,10 @@ func FormatComplex(template string, args map[string]any) string {
225227
strVal := getItemAsStr(&arg, &argFormatOptions)
226228
formattedStr.WriteString(strVal)
227229
} else {
228-
formattedStr.WriteByte('{')
229-
formattedStr.WriteString(argNumberStr)
230-
formattedStr.WriteByte('}')
230+
formattedStr.WriteString(template[i:j])
231+
if j < templateLen-1 {
232+
formattedStr.WriteByte(template[j])
233+
}
231234
}
232235
i = j
233236
}

formatter_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,16 @@ func TestFormat(t *testing.T) {
140140
args: []any{},
141141
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}, additionalData interface{}) {",
142142
},
143+
"commentaries after bracket": {
144+
template: "switch app.appConfig.ServerCfg.Schema { //nolint:exhaustive",
145+
args: []any{},
146+
expected: "switch app.appConfig.ServerCfg.Schema { //nolint:exhaustive",
147+
},
148+
"bracket in the middle": {
149+
template: "in the middle - { at the end - nothing",
150+
args: []any{},
151+
expected: "in the middle - { at the end - nothing",
152+
},
143153
} {
144154
t.Run(name, func(t *testing.T) {
145155
assert.Equal(t, test.expected, stringFormatter.Format(test.template, test.args...))
@@ -241,6 +251,11 @@ func TestFormatComplex(t *testing.T) {
241251
args: map[string]any{},
242252
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) }",
243253
},
254+
/*"commentaries after bracket": {
255+
template: "switch app.appConfig.ServerCfg.Schema { //nolint:exhaustive",
256+
args: map[string]any{},
257+
expected: "switch app.appConfig.ServerCfg.Schema { //nolint:exhaustive",
258+
},*/
244259
} {
245260
t.Run(name, func(t *testing.T) {
246261
assert.Equal(t, test.expected, stringFormatter.FormatComplex(test.template, test.args))

0 commit comments

Comments
 (0)