This is an automated email from the ASF dual-hosted git repository.

rohit pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudstack-cloudmonkey.git


The following commit(s) were added to refs/heads/main by this push:
     new f4a900a  output: fix map response showing as list (#179)
f4a900a is described below

commit f4a900ae689701fbb9d8a34495ea60ec60049c49
Author: Abhishek Kumar <abhishek.mr...@gmail.com>
AuthorDate: Mon Aug 11 16:21:59 2025 +0530

    output: fix map response showing as list (#179)
    
    Signed-off-by: Abhishek Kumar <abhishek.mr...@gmail.com>
---
 cmd/output.go | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)

diff --git a/cmd/output.go b/cmd/output.go
index d817d04..79f3900 100644
--- a/cmd/output.go
+++ b/cmd/output.go
@@ -62,22 +62,22 @@ func printJSON(response map[string]interface{}) {
        enc.Encode(response)
 }
 
-func getItemsFromValue(v interface{}) ([]interface{}, bool) {
-       valueType := reflect.TypeOf(v)
-       if valueType.Kind() == reflect.Slice {
+func getItemsFromValue(v interface{}) ([]interface{}, reflect.Kind, bool) {
+       valueKind := reflect.TypeOf(v).Kind()
+       if valueKind == reflect.Slice {
                sliceItems, ok := v.([]interface{})
                if !ok {
-                       return nil, false
+                       return nil, valueKind, false
                }
-               return sliceItems, true
-       } else if valueType.Kind() == reflect.Map {
+               return sliceItems, valueKind, true
+       } else if valueKind == reflect.Map {
                mapItem, ok := v.(map[string]interface{})
                if !ok {
-                       return nil, false
+                       return nil, valueKind, false
                }
-               return []interface{}{mapItem}, true
+               return []interface{}{mapItem}, valueKind, true
        }
-       return nil, false
+       return nil, valueKind, false
 }
 
 func printText(response map[string]interface{}) {
@@ -85,7 +85,7 @@ func printText(response map[string]interface{}) {
        for k, v := range response {
                valueType := reflect.TypeOf(v)
                if valueType.Kind() == reflect.Slice || valueType.Kind() == 
reflect.Map {
-                       items, ok := getItemsFromValue(v)
+                       items, _, ok := getItemsFromValue(v)
                        if ok {
                                fmt.Printf("%v:\n", k)
                                for idx, item := range items {
@@ -114,7 +114,7 @@ func printTable(response map[string]interface{}, filter 
[]string) {
        for k, v := range response {
                valueType := reflect.TypeOf(v)
                if valueType.Kind() == reflect.Slice || valueType.Kind() == 
reflect.Map {
-                       items, ok := getItemsFromValue(v)
+                       items, _, ok := getItemsFromValue(v)
                        if !ok {
                                continue
                        }
@@ -155,7 +155,7 @@ func printColumn(response map[string]interface{}, filter 
[]string) {
        for _, v := range response {
                valueType := reflect.TypeOf(v)
                if valueType.Kind() == reflect.Slice || valueType.Kind() == 
reflect.Map {
-                       items, ok := getItemsFromValue(v)
+                       items, _, ok := getItemsFromValue(v)
                        if !ok {
                                continue
                        }
@@ -194,7 +194,7 @@ func printCsv(response map[string]interface{}, filter 
[]string) {
        for _, v := range response {
                valueType := reflect.TypeOf(v)
                if valueType.Kind() == reflect.Slice || valueType.Kind() == 
reflect.Map {
-                       items, ok := getItemsFromValue(v)
+                       items, _, ok := getItemsFromValue(v)
                        if !ok {
                                continue
                        }
@@ -247,7 +247,7 @@ func filterResponse(response map[string]interface{}, filter 
[]string, excludeFil
        for key, value := range response {
                valueType := reflect.TypeOf(value)
                if valueType.Kind() == reflect.Slice || valueType.Kind() == 
reflect.Map {
-                       items, ok := getItemsFromValue(value)
+                       items, originalKind, ok := getItemsFromValue(value)
                        if !ok {
                                continue
                        }
@@ -280,7 +280,11 @@ func filterResponse(response map[string]interface{}, 
filter []string, excludeFil
 
                                filteredRows = append(filteredRows, filteredRow)
                        }
-                       filteredResponse[key] = filteredRows
+                       if originalKind == reflect.Map && len(filteredRows) > 0 
{
+                               filteredResponse[key] = filteredRows[0]
+                       } else {
+                               filteredResponse[key] = filteredRows
+                       }
                } else {
                        filteredResponse[key] = value
                }

Reply via email to