Skip to content

Commit cbf4500

Browse files
committed
functionally it works correct
1 parent b4faa43 commit cbf4500

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

formatter.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
)
88

99
const argumentFormatSeparator = ":"
10+
const bytesPerArgDefault = 20
1011

1112
// Format
1213
/* Func that makes string formatting from template
@@ -35,7 +36,7 @@ func Format(template string, args ...any) string {
3536

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

@@ -160,7 +161,8 @@ func FormatComplex(template string, args map[string]any) string {
160161

161162
templateLen := len(template)
162163
formattedStr := &strings.Builder{}
163-
formattedStr.Grow(templateLen + 22*len(args))
164+
argsLen := bytesPerArgDefault * len(args)
165+
formattedStr.Grow(templateLen + argsLen + 1)
164166
j := -1 //nolint:ineffassign
165167
nestedBrackets := false
166168
formattedStr.WriteString(template[:start])
@@ -224,7 +226,15 @@ func FormatComplex(template string, args map[string]any) string {
224226
}
225227
if ok || (argFormatOptions != "" && !nestedBrackets) {
226228
// get number from placeholder
227-
strVal := getItemAsStr(&arg, &argFormatOptions)
229+
strVal := ""
230+
if arg != nil {
231+
strVal = getItemAsStr(&arg, &argFormatOptions)
232+
} else {
233+
formattedStr.WriteString(template[i:j])
234+
if j < templateLen-1 {
235+
formattedStr.WriteByte(template[j])
236+
}
237+
}
228238
formattedStr.WriteString(strVal)
229239
} else {
230240
formattedStr.WriteString(template[i:j])

formatter_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,11 @@ func TestFormatComplex(t *testing.T) {
251251
args: map[string]any{},
252252
expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) }",
253253
},
254-
/*"commentaries after bracket": {
254+
"commentaries after bracket": {
255255
template: "switch app.appConfig.ServerCfg.Schema { //nolint:exhaustive",
256256
args: map[string]any{},
257257
expected: "switch app.appConfig.ServerCfg.Schema { //nolint:exhaustive",
258-
},*/
258+
},
259259
} {
260260
t.Run(name, func(t *testing.T) {
261261
assert.Equal(t, test.expected, stringFormatter.FormatComplex(test.template, test.args))

0 commit comments

Comments
 (0)