diff --git a/console/command_executer.go b/console/command_executer.go index a828d388b..ca2e5ded3 100644 --- a/console/command_executer.go +++ b/console/command_executer.go @@ -24,6 +24,7 @@ import ( "fmt" "reflect" "regexp" + "sort" "strings" "time" @@ -162,20 +163,19 @@ func getSliceAsString(mp []interface{}, defaultDurationFields map[string]struct{ } func getMapAsString(mp map[string]interface{}, defaultDurationFields map[string]struct{}) (out string) { - // defaultDurationFields := map[string]struct{}{"b": struct{}{}, "c": struct{}{}, "d": struct{}{}} - out = "{" + // in order to find the data faster + keylist := []string{} // add key value pairs to list so at the end we can sort them for k, v := range mp { if _, has := defaultDurationFields[k]; has { if t, err := utils.IfaceAsDuration(v); err == nil { - out += fmt.Sprintf(`"%s":"%s",`, k, t.String()) + keylist = append(keylist, fmt.Sprintf(`"%s":"%s"`, k, t.String())) continue - } else { - fmt.Println(err) } } - out += fmt.Sprintf(`"%s":%s,`, k, getStringValue(v, defaultDurationFields)) + keylist = append(keylist, fmt.Sprintf(`"%s":%s`, k, getStringValue(v, defaultDurationFields))) } - return strings.TrimSuffix(out, ",") + "}" + sort.Strings(keylist) + return fmt.Sprintf(`{%s}`, strings.Join(keylist, ",")) } func GetFormatedResult(result interface{}, defaultDurationFields map[string]struct{}) string { diff --git a/console/command_executer_test.go b/console/command_executer_test.go index 765e2d768..1dc2e61b1 100644 --- a/console/command_executer_test.go +++ b/console/command_executer_test.go @@ -20,6 +20,7 @@ package console import ( "encoding/json" "testing" + "time" ) func TestToJSON(t *testing.T) { @@ -105,3 +106,134 @@ func TestFromJSONArraySpace(t *testing.T) { t.Errorf("Expected: %s got: '%s'", expected, line) } } + +func TestGetStringValue(t *testing.T) { + dflt := map[string]struct{}{} + expected := "10" + if rply := getStringValue(int64(10), dflt); rply != expected { + t.Errorf("Expecting: %s , received: %s", expected, rply) + } + + expected = "true" + if rply := getStringValue(true, dflt); rply != expected { + t.Errorf("Expecting: %s , received: %s", expected, rply) + } + + expected = "null" + if rply := getStringValue(nil, dflt); rply != expected { + t.Errorf("Expecting: %s , received: %s", expected, rply) + } + + expected = "10.5" + if rply := getStringValue(10.5, dflt); rply != expected { + t.Errorf("Expecting: %s , received: %s", expected, rply) + } + + expected = "[10,5]" + if rply := getStringValue([]float32{10, 5}, dflt); rply != expected { + t.Errorf("Expecting: %s , received: %s", expected, rply) + } + + expected = `{"ID":"id1","TimeValue":10000}` + if rply := getStringValue(struct { + ID string + TimeValue int64 + }{ID: "id1", TimeValue: 10000}, dflt); rply != expected { + t.Errorf("Expecting: %s , received: %s", expected, rply) + } + + if rply := getStringValue(map[string]interface{}{ + "ID": "id1", + "TimeValue": 10000}, dflt); rply != expected { + t.Errorf("Expecting: %s , received: %s", expected, rply) + } + + expected = `{"ID":"id1","TimeValue":"1s"}` + if rply := getStringValue(map[string]interface{}{ + "ID": "id1", + "TimeValue": int64(time.Second)}, map[string]struct{}{"TimeValue": struct{}{}}); rply != expected { + t.Errorf("Expecting: %s , received: %s", expected, rply) + } + + expected = "[10,20,30]" + if rply := getSliceAsString([]interface{}{10, 20, 30}, dflt); rply != expected { + t.Errorf("Expecting: %s , received: %s", expected, rply) + } +} + +func TestGetSliceAsString(t *testing.T) { + dflt := map[string]struct{}{} + expected := "[10,20,30]" + if rply := getSliceAsString([]interface{}{10, 20, 30}, dflt); rply != expected { + t.Errorf("Expecting: %s , received: %s", expected, rply) + } + + expected = `["test1","test2","test3"]` + if rply := getSliceAsString([]interface{}{"test1", "test2", "test3"}, dflt); rply != expected { + t.Errorf("Expecting: %s , received: %s", expected, rply) + } +} + +func TestGetMapAsString(t *testing.T) { + dflt := map[string]struct{}{} + expected := `{"ID":"id1","TimeValue":10000}` + if rply := getStringValue(map[string]interface{}{ + "ID": "id1", + "TimeValue": 10000}, dflt); rply != expected { + t.Errorf("Expecting: %s , received: %s", expected, rply) + } + + expected = `{"ID":"id1","TimeValue":"1s"}` + if rply := getStringValue(map[string]interface{}{ + "ID": "id1", + "TimeValue": int64(time.Second)}, map[string]struct{}{"TimeValue": struct{}{}}); rply != expected { + t.Errorf("Expecting: %s , received: %s", expected, rply) + } +} + +func TestGetFormatedResult(t *testing.T) { + dflt := map[string]struct{}{} + expected := `{ + "ID": "id1", + "TimeValue": 10000 +}` + if rply := GetFormatedResult(map[string]interface{}{ + "ID": "id1", + "TimeValue": 10000}, dflt); rply != expected { + t.Errorf("Expecting: %q , received: %q", expected, rply) + } + + expected = `{ + "ID": "id1", + "TimeValue": "1s" +}` + if rply := GetFormatedResult(map[string]interface{}{ + "ID": "id1", + "TimeValue": int64(time.Second)}, map[string]struct{}{"TimeValue": struct{}{}}); rply != expected { + t.Errorf("Expecting: %s , received: %s", expected, rply) + } + + expected = `{ + "ID": "id1", + "TimeValue": 10000 +}` + if rply := GetFormatedResult(struct { + ID string + TimeValue int64 + }{ID: "id1", TimeValue: 10000}, dflt); rply != expected { + t.Errorf("Expecting: %s , received: %s", expected, rply) + } +} + +func TestGetFormatedSliceResult(t *testing.T) { + dflt := map[string]struct{}{} + expected := "[10,20,30]" + if rply := getSliceAsString([]interface{}{10, 20, 30}, dflt); rply != expected { + t.Errorf("Expecting: %s , received: %s", expected, rply) + } + + expected = `["test1","test2","test3"]` + if rply := getSliceAsString([]interface{}{"test1", "test2", "test3"}, dflt); rply != expected { + t.Errorf("Expecting: %s , received: %s", expected, rply) + } +}