You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2018/09/27 06:59:15 UTC

[GitHub] mkiiskila closed pull request #99: res - add pretty JSON format to CoAP response

mkiiskila closed pull request #99: res - add pretty JSON format to CoAP response
URL: https://github.com/apache/mynewt-newtmgr/pull/99
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/newtmgr/cli/res.go b/newtmgr/cli/res.go
index bca108a..e18e5a2 100644
--- a/newtmgr/cli/res.go
+++ b/newtmgr/cli/res.go
@@ -24,6 +24,7 @@ import (
 	"fmt"
 	"strconv"
 	"strings"
+	"encoding/json"
 
 	"github.com/runtimeco/go-coap"
 	"github.com/spf13/cobra"
@@ -92,6 +93,39 @@ func extractResKv(params []string) (map[string]interface{}, error) {
 	return m, nil
 }
 
+/* Helper functions to convert JSON object into pretty format
+   Adapted from elastic/beats/libbeat/common/mapstr.go
+*/
+
+func cleanUpInterfaceArray(in []interface{}) []interface{} {
+	result := make([]interface{}, len(in))
+	for i, v := range in {
+		result[i] = cleanUpMapValue(v)
+	}
+	return result
+}
+
+func cleanUpInterfaceMap(in map[interface{}]interface{}) map[string]interface{} {
+	result := make(map[string]interface{})
+	for k, v := range in {
+		result[fmt.Sprintf("%v", k)] = cleanUpMapValue(v)
+	}
+	return result
+}
+
+func cleanUpMapValue(v interface{}) interface{} {
+	switch v := v.(type) {
+	case []interface{}:
+		return cleanUpInterfaceArray(v)
+	case map[interface{}]interface{}:
+		return cleanUpInterfaceMap(v)
+	case string:
+		return v
+	default:
+		return fmt.Sprintf("%v", v)
+	}
+}
+
 func resResponseStr(path string, cbor []byte) string {
 	s := path
 
@@ -101,7 +135,11 @@ func resResponseStr(path string, cbor []byte) string {
 			s += fmt.Sprintf("\n    invalid incoming cbor:%v\n%s",
 				err, hex.Dump(cbor))
 		}
-		s += fmt.Sprintf("\n%v", m)
+		j, err := json.MarshalIndent(cleanUpMapValue(m), "", "    ")
+		if err != nil {
+			s += fmt.Sprintf("\nerror: ", err)
+		}
+		s += fmt.Sprintf("\n%v", string(j))
 	} else {
 		s += "\n    <empty>"
 	}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services