Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unescape literal new-line #38

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions cshared.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"reflect"
"runtime"
"strconv"
"strings"
"time"
"unsafe"

Expand Down Expand Up @@ -331,10 +332,21 @@ type flbInputConfigLoader struct {
}

func (f *flbInputConfigLoader) String(key string) string {
s := input.FLBPluginConfigKey(f.ptr, key)
return unquote(input.FLBPluginConfigKey(f.ptr, key))
}

func unquote(s string) string {
if tmp, err := strconv.Unquote(s); err == nil {
return tmp
}

// unescape literal newlines
if strings.Contains(s, `\n`) {
if tmp2, err := strconv.Unquote(`"` + s + `"`); err == nil {
return tmp2
}
}

return s
}

Expand All @@ -343,11 +355,7 @@ type flbOutputConfigLoader struct {
}

func (f *flbOutputConfigLoader) String(key string) string {
s := output.FLBPluginConfigKey(f.ptr, key)
if tmp, err := strconv.Unquote(s); err == nil {
return tmp
}
return s
return unquote(output.FLBPluginConfigKey(f.ptr, key))
}

type flbInputLogger struct {
Expand Down
10 changes: 6 additions & 4 deletions plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,18 @@ func testPlugin(t *testing.T, pool *dockertest.Pool) {
}

var got struct {
Foo string `json:"foo"`
Message string `json:"message"`
Tmpl string `json:"tmpl"`
Foo string `json:"foo"`
Message string `json:"message"`
TmplOut string `json:"tmpl_out"`
MultilineSplit []string `json:"multiline_split"`
}

err := json.Unmarshal([]byte(line), &got)
assert.NoError(t, err)
assert.Equal(t, "bar", got.Foo)
assert.Equal(t, "hello from go-test-input-plugin", got.Message)
assert.Equal(t, "inside double quotes\nnew line", got.Tmpl)
assert.Equal(t, "inside double quotes\nnew line", got.TmplOut)
assert.Equal(t, []string{"foo", "bar"}, got.MultilineSplit)

t.Logf("took %s", time.Since(start))

Expand Down
1 change: 1 addition & 0 deletions testdata/fluent-bit.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Tag test-input
foo bar
tmpl "{{print \"inside double quotes\"}}\nnew line"
multiline foo\nbar

# [OUTPUT]
# Name go-test-output-plugin
Expand Down
11 changes: 7 additions & 4 deletions testdata/input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func init() {
type inputPlugin struct {
foo string
tmpl *template.Template
multilineSplit []string
collectCounter metric.Counter
log plugin.Logger
}
Expand All @@ -30,6 +31,7 @@ func (plug *inputPlugin) Init(ctx context.Context, fbit *plugin.Fluentbit) error
}

plug.foo = fbit.Conf.String("foo")
plug.multilineSplit = strings.Split(fbit.Conf.String("multiline"), "\n")
plug.collectCounter = fbit.Metrics.NewCounter("collect_total", "Total number of collects", "go-test-input-plugin")
plug.log = fbit.Logger
return nil
Expand Down Expand Up @@ -61,10 +63,11 @@ func (plug inputPlugin) Collect(ctx context.Context, ch chan<- plugin.Message) e

ch <- plugin.Message{
Time: time.Now(),
Record: map[string]string{
"message": "hello from go-test-input-plugin",
"foo": plug.foo,
"tmpl": buff.String(),
Record: map[string]any{
"message": "hello from go-test-input-plugin",
"foo": plug.foo,
"tmpl_out": buff.String(),
"multiline_split": plug.multilineSplit,
},
}
}
Expand Down