Skip to content

Commit 9999507

Browse files
committed
output: allow filtering for non-list apis
Currently, filtering is not working when the response object is not returning an array. This PR makes changes to allow filtering when response object is an array or a map. For async apis, filter arg is passed to the query request allowing filtering to work with the final response. Signed-off-by: Abhishek Kumar <[email protected]>
1 parent a6552ee commit 9999507

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

cmd/network.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,16 @@ func pollAsyncJob(r *Request, jobID string) (map[string]interface{}, error) {
157157
return nil, errors.New("async API job query timed out")
158158

159159
case <-ticker.C:
160-
queryResult, queryError := NewAPIRequest(r, "queryAsyncJobResult", []string{"jobid=" + jobID}, false)
160+
args := []string{"jobid=" + jobID}
161+
if r.Args != nil {
162+
for _, arg := range r.Args {
163+
if strings.HasPrefix(strings.ToLower(arg), "filter=") {
164+
args = append(args, arg)
165+
break
166+
}
167+
}
168+
}
169+
queryResult, queryError := NewAPIRequest(r, "queryAsyncJobResult", args, false)
161170
if queryError != nil {
162171
return queryResult, queryError
163172
}

cmd/output.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,22 +206,43 @@ func printCsv(response map[string]interface{}, filter []string) {
206206
enc.Flush()
207207
}
208208

209+
func getItemsFromValue(v interface{}) ([]interface{}, bool) {
210+
valueType := reflect.TypeOf(v)
211+
if valueType.Kind() == reflect.Slice {
212+
sliceItems, ok := v.([]interface{})
213+
if !ok {
214+
return nil, false
215+
}
216+
return sliceItems, true
217+
} else if valueType.Kind() == reflect.Map {
218+
mapItem, ok := v.(map[string]interface{})
219+
if !ok {
220+
return nil, false
221+
}
222+
return []interface{}{mapItem}, true
223+
}
224+
return nil, false
225+
}
226+
209227
func filterResponse(response map[string]interface{}, filter []string, outputType string) map[string]interface{} {
210-
if filter == nil || len(filter) == 0 {
228+
config.Debug("Filtering response with filter:", filter, "and output type:", outputType)
229+
if len(filter) == 0 {
211230
return response
212231
}
213232
filteredResponse := make(map[string]interface{})
214233
for k, v := range response {
215234
valueType := reflect.TypeOf(v)
216235
if valueType.Kind() == reflect.Slice || valueType.Kind() == reflect.Map {
217-
items, ok := v.([]interface{})
236+
items, ok := getItemsFromValue(v)
218237
if !ok {
238+
config.Debug("Skipping non-slice/map value for key:", k)
219239
continue
220240
}
221241
var filteredRows []interface{}
222242
for _, item := range items {
223243
row, ok := item.(map[string]interface{})
224244
if !ok || len(row) < 1 {
245+
config.Debug("Skipping non-map item for key:", k)
225246
continue
226247
}
227248
filteredRow := make(map[string]interface{})

0 commit comments

Comments
 (0)