From 115f6e32d218fac036d15f9c7f391297fefbd46d Mon Sep 17 00:00:00 2001 From: Christian Galsterer Date: Sat, 4 Feb 2017 13:39:12 +0100 Subject: [PATCH] Support for replacement character for json_dot_mode - Added implementation - Fixed unit tests --- beater/poller.go | 6 +++--- config/config_test.go | 4 ++-- tests/files/config.yml | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/beater/poller.go b/beater/poller.go index 901a089..f9587af 100644 --- a/beater/poller.go +++ b/beater/poller.go @@ -173,7 +173,7 @@ func (p *Poller) runOneTime() error { if p.config.JsonDotMode == "unflatten" { jsonBody = unflat(jsonBody).(map[string]interface{}) } else if p.config.JsonDotMode == "replace" { - jsonBody = replaceDots(jsonBody).(map[string]interface{}) + jsonBody = replaceDots(jsonBody, jsonDotModeCharacter).(map[string]interface{}) } } responseEvent.JsonBody = jsonBody @@ -193,12 +193,12 @@ func (p *Poller) runOneTime() error { return nil } -func replaceDots(data interface{}) interface{} { +func replaceDots(data interface{}, jsonDotModeCharacter string) interface{} { switch data.(type) { case map[string]interface{}: result := map[string]interface{}{} for key, value := range data.(map[string]interface{}) { - result[strings.Replace(key, ".", "_", -1)] = replaceDots(value) + result[strings.Replace(key, ".", jsonDotModeCharacter, -1)] = replaceDots(value, jsonDotModeCharacter) } return result default: diff --git a/config/config_test.go b/config/config_test.go index bc2f0c2..2436670 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -44,6 +44,7 @@ func TestReadConfig(t *testing.T) { assert.Equal(t, transport.TLSVersionSSL30, urls[0].SSL.Versions[0]) assert.Equal(t, "unflatten", urls[0].JsonDotMode) + assert.Equal(t, "http://example.org/2", urls[1].Url) assert.Equal(t, "post", urls[1].Method) assert.Equal(t, "@every 2m", urls[1].Cron) @@ -54,7 +55,7 @@ func TestReadConfig(t *testing.T) { assert.Equal(t, 0, len(urls[1].Fields)) assert.Equal(t, "", urls[1].DocumentType) assert.Equal(t, "replace", urls[1].JsonDotMode) - assert.Equal(t, "_", urls[1].JsonDotModeCharacter) + assert.Equal(t, "", urls[1].JsonDotModeCharacter) assert.Equal(t, "http://example.org/2", urls[2].Url) assert.Equal(t, "post", urls[2].Method) @@ -67,5 +68,4 @@ func TestReadConfig(t *testing.T) { assert.Equal(t, "", urls[2].DocumentType) assert.Equal(t, "replace", urls[2].JsonDotMode) assert.Equal(t, "-", urls[2].JsonDotModeCharacter) - } diff --git a/tests/files/config.yml b/tests/files/config.yml index d5842d0..0fb91b6 100644 --- a/tests/files/config.yml +++ b/tests/files/config.yml @@ -36,4 +36,4 @@ httpbeat: url: http://example.org/2 method: post json_dot_mode: replace - json_dot_mode_character: - + json_dot_mode_character: "-"