From 53498c9daab587a29a0e3cdc3b1874fc4351bdb4 Mon Sep 17 00:00:00 2001 From: Ushakov Michale Date: Wed, 13 Nov 2024 15:43:30 +0500 Subject: [PATCH 1/3] a test was added --- formatter_test.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/formatter_test.go b/formatter_test.go index 4d4b2c5..c2a3230 100644 --- a/formatter_test.go +++ b/formatter_test.go @@ -113,11 +113,16 @@ func TestFormat(t *testing.T) { }, expected: "Example is: {123 This is a test str, nothing more special -1.098743 main question error, is 42}", }, - "open bracket at the end of line of go file": { + "open bracket at the end of line of go line": { template: "type serviceHealth struct {", args: []any{}, expected: "type serviceHealth struct {", }, + "open bracket at the end of line of go line with {} inside": { + template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) {", + args: []any{}, + expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) {", + }, } { t.Run(name, func(t *testing.T) { assert.Equal(t, test.expected, stringFormatter.Format(test.template, test.args...)) From 6bf1de25940cb1ba3bed85210eecdb988c17148c Mon Sep 17 00:00:00 2001 From: Ushakov Michale Date: Wed, 13 Nov 2024 16:40:36 +0500 Subject: [PATCH 2/3] solved 1 more task when source code processing --- formatter.go | 22 ++++++++++++++++------ formatter_test.go | 17 +++++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/formatter.go b/formatter.go index c0b1e1a..f1ddb4d 100644 --- a/formatter.go +++ b/formatter.go @@ -117,9 +117,14 @@ func Format(template string, args ...any) string { strVal := getItemAsStr(&args[argNumber], &argFormatOptions) formattedStr.WriteString(strVal) } else { - formattedStr.WriteByte('{') - formattedStr.WriteString(argNumberStr) - formattedStr.WriteByte('}') + if argNumberStr != "" { + formattedStr.WriteByte('{') + formattedStr.WriteString(argNumberStr) + formattedStr.WriteByte('}') + } else { + // complicated case when we have brackets in line and open line at the end + formattedStr.WriteByte('{') + } } i = j } @@ -210,9 +215,14 @@ func FormatComplex(template string, args map[string]any) string { strVal := getItemAsStr(&arg, &argFormatOptions) formattedStr.WriteString(strVal) } else { - formattedStr.WriteByte('{') - formattedStr.WriteString(argNumberStr) - formattedStr.WriteByte('}') + if argNumberStr != "" { + formattedStr.WriteByte('{') + formattedStr.WriteString(argNumberStr) + formattedStr.WriteByte('}') + } else { + // complicated case when we have brackets in line and open line at the end + formattedStr.WriteByte('{') + } } i = j } diff --git a/formatter_test.go b/formatter_test.go index c2a3230..15d7d9e 100644 --- a/formatter_test.go +++ b/formatter_test.go @@ -123,6 +123,12 @@ func TestFormat(t *testing.T) { args: []any{}, expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) {", }, + + "close bracket at the end of line of go line with {} inside": { + template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) }", + args: []any{}, + expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) }", + }, } { t.Run(name, func(t *testing.T) { assert.Equal(t, test.expected, stringFormatter.Format(test.template, test.args...)) @@ -207,6 +213,17 @@ func TestFormatComplex(t *testing.T) { args: map[string]any{}, expected: " \"server\": {", }, + "open bracket at the end of line of go line with {} inside": { + template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) {", + args: map[string]any{}, + expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) {", + }, + + "close bracket at the end of line of go line with {} inside": { + template: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) }", + args: map[string]any{}, + expected: "func afterHandle(respWriter *http.ResponseWriter, statusCode int, data interface{}) }", + }, } { t.Run(name, func(t *testing.T) { assert.Equal(t, test.expected, stringFormatter.FormatComplex(test.template, test.args)) From 74fe596515bb56e5017e015a42ab8abf1a9d3ee6 Mon Sep 17 00:00:00 2001 From: Ushakov Michale Date: Wed, 13 Nov 2024 16:47:29 +0500 Subject: [PATCH 3/3] version update in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4699189..a6f2468 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ A set of a ***high performance string tools*** that helps to build strings from ![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/wissance/stringFormatter?style=plastic) ![GitHub issues](https://img.shields.io/github/issues/wissance/stringFormatter?style=plastic) ![GitHub Release Date](https://img.shields.io/github/release-date/wissance/stringFormatter) -![GitHub release (latest by date)](https://img.shields.io/github/downloads/wissance/stringFormatter/v1.2.1/total?style=plastic) +![GitHub release (latest by date)](https://img.shields.io/github/downloads/wissance/stringFormatter/v1.2.2/total?style=plastic) ![String Formatter: a convenient string formatting tool](/img/sf_cover.png)