Skip to content

Commit

Permalink
code(utils/formatter) - introduce newline separator for prevent escap…
Browse files Browse the repository at this point in the history
…ing in json
  • Loading branch information
PxyUp committed Oct 28, 2024
1 parent c12d5ca commit e36eb05
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ type CalculatedConfig struct {

**fResRaw** - result in bytes format

**reverse([]T)** - return reverse array
**FNewLine** - new line separator

```json
{
Expand Down
15 changes: 9 additions & 6 deletions pkg/utils/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ import (
)

const (
fitterResultJsonRef = "fResJson"
fitterResultRef = "fRes"
fitterIndexRef = "fIndex"
fitterResultRaw = "fResRaw"
fitterResultJsonRef = "fResJson"
fitterResultRef = "fRes"
fitterIndexRef = "fIndex"
fitterResultRaw = "fResRaw"
fitterNewLinePlaceholderKey = "FNewLine"
fitterNewLinePlaceholderValue = "$__FLINE__$"
)

var (
defEnv = map[string]interface{}{
"FNull": builder.NullValue,
"FNil": nil,
fitterNewLinePlaceholderKey: fitterNewLinePlaceholderValue,
"FNull": builder.NullValue,
"FNil": nil,
"isNull": func(value interface{}) bool {
return builder.NullValue == value
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Format(str string, value builder.Interfacable, index *uint32, input builder
str = strings.ReplaceAll(str, humanIndexPlaceHolder, fmt.Sprintf("%d", *index+1))
}

return formatJsonPathString(str, value, index, input)
return strings.ReplaceAll(formatJsonPathString(str, value, index, input), fitterNewLinePlaceholderValue, "\n")
}

func processPrefix(prefix string, value builder.Interfacable, index *uint32, input builder.Interfacable) string {
Expand Down
4 changes: 4 additions & 0 deletions pkg/utils/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func (s *TestFormatterSuite) TestFile() {
assert.Equal(s.T(), "hi 14 test end", utils.Format("{{{FromExp='hi ' + '{{{FromFile=./test_formatted_file.log}}}' + ' end'}}}", nil, nil, nil))
}

func (s *TestFormatterSuite) TestNewLineSeparator() {
assert.Equal(s.T(), "\n", utils.Format("$__FLINE__$", nil, nil, nil))
}

func (s *TestFormatterSuite) TestExpr() {
index := uint32(1)
assert.Equal(s.T(), "8", utils.Format("{{{FromExp=fRes + 5 + fIndex}}}", builder.Number(2), &index, nil))
Expand Down

0 comments on commit e36eb05

Please sign in to comment.