2018-07-02 16:43:32 +00:00
|
|
|
package gjson
|
2018-06-28 21:33:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2018-07-02 18:20:16 +00:00
|
|
|
"reflect"
|
2018-06-28 21:33:38 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseJsonPath(t *testing.T) {
|
|
|
|
testString := `{
|
|
|
|
"total_devices": 5,
|
|
|
|
"total_threads": 10,
|
|
|
|
"shares": {
|
|
|
|
"total": 5,
|
|
|
|
"accepted": 5,
|
|
|
|
"rejected": 0,
|
|
|
|
"avg_find_time": 4,
|
|
|
|
"tester": "work",
|
2018-07-02 16:43:32 +00:00
|
|
|
"tester2": true,
|
2018-06-28 21:33:38 +00:00
|
|
|
"tester3": {
|
|
|
|
"hello":"sup",
|
|
|
|
"fun":"money",
|
|
|
|
"break":9
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
jsonParser := JSONPath{
|
|
|
|
MetricName: "jsonpather",
|
2018-07-02 16:43:32 +00:00
|
|
|
TagPath: map[string]string{"hello": "shares.tester3.hello"},
|
|
|
|
BoolPath: map[string]string{"bool": "shares.tester2"},
|
2018-06-28 21:33:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
metrics, err := jsonParser.Parse([]byte(testString))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
log.Printf("m[0] name: %v, tags: %v, fields: %v", metrics[0].Name(), metrics[0].Tags(), metrics[0].Fields())
|
|
|
|
t.Error()
|
|
|
|
|
|
|
|
}
|
2018-07-02 18:20:16 +00:00
|
|
|
|
|
|
|
func TestTagTypes(t *testing.T) {
|
|
|
|
testString := `{
|
|
|
|
"total_devices": 5,
|
|
|
|
"total_threads": 10,
|
|
|
|
"shares": {
|
|
|
|
"total": 5,
|
|
|
|
"accepted": 5,
|
|
|
|
"rejected": 0,
|
|
|
|
"my_bool": true,
|
|
|
|
"tester": "work",
|
|
|
|
"tester2": {
|
|
|
|
"hello":"sup",
|
|
|
|
"fun":true,
|
|
|
|
"break":9.97
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
r := JSONPath{
|
|
|
|
TagPath: map[string]string{"int1": "total_devices", "my_bool": "shares.my_bool"},
|
|
|
|
FloatPath: map[string]string{"total": "shares.total"},
|
|
|
|
BoolPath: map[string]string{"fun": "shares.tester2.fun"},
|
|
|
|
StrPath: map[string]string{"hello": "shares.tester2.hello"},
|
|
|
|
IntPath: map[string]string{"accepted": "shares.accepted"},
|
|
|
|
}
|
|
|
|
|
|
|
|
metrics, err := r.Parse([]byte(testString))
|
|
|
|
log.Printf("m[0] name: %v, tags: %v, fields: %v", metrics[0].Name(), metrics[0].Tags(), metrics[0].Fields())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, true, reflect.DeepEqual(map[string]interface{}{"total": 5.0, "fun": true, "hello": "sup", "accepted": int64(5)}, metrics[0].Fields()))
|
|
|
|
}
|