From 1bebeb5aa592da6bda97386d912c1e9f13943034 Mon Sep 17 00:00:00 2001 From: Nathan Leiby Date: Tue, 15 Aug 2017 11:46:59 -0700 Subject: [PATCH] decode: json decodes dimensions as array of interface Previously, this would result in a log line failing to extracting any dimensions because ```golang dimensions.([]string) ``` would fail because ```golang reflect.TypeOf(dimensions) ``` was `[]interface{}`. --- decode/decode.go | 11 ++++++++++- decode/decode_test.go | 12 ++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/decode/decode.go b/decode/decode.go index 47f08cc..dd8666d 100644 --- a/decode/decode.go +++ b/decode/decode.go @@ -154,11 +154,20 @@ func getStringArray(json map[string]interface{}, key string) []string { return []string{} } - strArray, ok := val.([]string) + iArray, ok := val.([]interface{}) if !ok { return []string{} } + strArray := []string{} + for _, item := range iArray { + s, ok := item.(string) + if !ok { + return []string{} + } + strArray = append(strArray, s) + } + return strArray } diff --git a/decode/decode_test.go b/decode/decode_test.go index 22877a5..59e9b11 100644 --- a/decode/decode_test.go +++ b/decode/decode_test.go @@ -562,13 +562,13 @@ func TestExtractKVMeta(t *testing.T) { "rule": "cool", "series": "1,1,2,3,5,8,13", "value_field": "value", - "dimensions": []string{"app", "district"}, + "dimensions": []interface{}{"app", "district"}, }, map[string]interface{}{ "type": "metrics", "rule": "cool2", "series": "1,1,2,6,24,120,720,5040", - "dimensions": []string{"app", "district"}, + "dimensions": []interface{}{"app", "district"}, }, }, }, @@ -688,7 +688,7 @@ func TestExtractKVMeta(t *testing.T) { "type": "alerts", "rule": "last-call", "series": "doing-it-til-we-fall", - "dimensions": []string{"who", "where"}, + "dimensions": []interface{}{"who", "where"}, "stat_type": "guage", "value_field": "status", }, @@ -696,7 +696,7 @@ func TestExtractKVMeta(t *testing.T) { "type": "alerts", "rule": "watch-out-now", "series": "dem-gators-gonna-bite-ya", - "dimensions": []string{"how-fresh", "how-clean"}, + "dimensions": []interface{}{"how-fresh", "how-clean"}, }, }, }, @@ -750,7 +750,7 @@ func TestExtractKVMeta(t *testing.T) { "type": "metrics", "rule": "all-combos", "series": "1,1,2,6,24,120,720,5040", - "dimensions": []string{"fact", "orial"}, + "dimensions": []interface{}{"fact", "orial"}, }, map[string]interface{}{ "type": "analytics", @@ -767,7 +767,7 @@ func TestExtractKVMeta(t *testing.T) { "type": "alerts", "rule": "last-call", "series": "doing-it-til-we-fall", - "dimensions": []string{"who", "where"}, + "dimensions": []interface{}{"who", "where"}, "stat_type": "guage", "value_field": "status", },