Skip to content

Commit

Permalink
Support for replacement character for json_dot_mode
Browse files Browse the repository at this point in the history
- Added implementation
- Fixed unit tests
  • Loading branch information
christiangalsterer committed Feb 4, 2017
1 parent 20a4028 commit 115f6e3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions beater/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)

}
2 changes: 1 addition & 1 deletion tests/files/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ httpbeat:
url: http://example.org/2
method: post
json_dot_mode: replace
json_dot_mode_character: -
json_dot_mode_character: "-"

0 comments on commit 115f6e3

Please sign in to comment.