diff --git a/export_test.go b/export_test.go index 1f75463..09d58ed 100644 --- a/export_test.go +++ b/export_test.go @@ -57,3 +57,7 @@ func GetElementString(l *Line, i int, sep string, last bool) (string, error) { func GetLastMatch(s string, sep string) string { return getLastMatch(s, sep) } + +func SubstituteDots(s string) string { + return substituteDots(s) +} diff --git a/parser.go b/parser.go index 5f46496..57999c3 100644 --- a/parser.go +++ b/parser.go @@ -82,7 +82,7 @@ func makeKeyFromPaths(l *Line, m *metric) (string, error) { if err != nil { return "", err } - buffer.WriteString(m) + buffer.WriteString(substituteDots(m)) } else { buffer.WriteString(k.val) } @@ -99,7 +99,7 @@ func getElementString(l *Line, i int, sep string, last bool) (string, error) { return "", err } - if last { + if last && sep != "" { return getLastMatch(c, sep), nil } @@ -132,6 +132,13 @@ func getAmount(s string, sep string) (float32, error) { if s == "-" { return result, ErrEmptyString } + if sep == "" { + f, err := strconv.ParseFloat(s, 32) + if err != nil { + return result, err + } + return float32(f), nil + } columns := strings.Split(s, sep) for _, c := range columns { @@ -149,3 +156,8 @@ func getAmount(s string, sep string) (float32, error) { } return result, nil } + +// substituteDots replaces dots in string +func substituteDots(s string) string { + return strings.Replace(s, ".", "_", -1) +} diff --git a/parser_test.go b/parser_test.go index c083fad..21633d6 100644 --- a/parser_test.go +++ b/parser_test.go @@ -118,12 +118,6 @@ func Test_GetElementAmount(t *testing.T) { } func Test_HandleLine(t *testing.T) { - type testCase struct { - str string - err error - } - - a := assert.New(t) cfg := config.Source{ Delimiter: " ", Keys: []config.Key{ @@ -135,7 +129,31 @@ func Test_HandleLine(t *testing.T) { }, }, } + runHandleLineTestcases(cfg, t) +} +func Test_HandleLine2(t *testing.T) { + cfg := config.Source{ + Delimiter: " ", + Keys: []config.Key{ + { + Key: "$3.$6", + Count: true, + Timing: "$4", + }, + }, + } + + runHandleLineTestcases(cfg, t) +} + +func runHandleLineTestcases(cfg config.Source, t *testing.T) { + type testCase struct { + str string + err error + } + + a := assert.New(t) p, err := snitch.NewParser(snitch.NewNoopReader(nil), statsd.NoopClient{}, cfg) a.Nil(err) h, ok := p.(*snitch.Handler) @@ -160,9 +178,9 @@ func Test_HandleLine(t *testing.T) { a := assert.New(t) err := h.HandleLine(snitch.NewLine(tc.str, nil)) if tc.err != nil { - a.EqualError(tc.err, err.Error()) + a.EqualError(tc.err, err.Error(), "Should be equal") } else { - a.Nil(err) + a.Nil(err, "Should be nil error") } }) } @@ -210,3 +228,32 @@ func Test_HandleLineError(t *testing.T) { }) } } + +func TestSubstitute(t *testing.T) { + type testCase struct { + str string + expectation string + } + cases := []testCase{ + { + "10.1.12.13", + "10_1_12_13", + }, + { + "test", + "test", + }, + { + "1.2.3.4.5.6.7.8.9", + "1_2_3_4_5_6_7_8_9", + }, + } + + for i, tc := range cases { + t.Run(fmt.Sprint(i), func(t *testing.T) { + a := assert.New(t) + result := snitch.SubstituteDots(tc.str) + a.EqualValues(tc.expectation, result) + }) + } +}