X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/928b996a2e1b1780eb760c8675edc315dd9d80df..bc14c62ad1528dbddc26781c5cea6a7968c93f2e:/lib/controller/router/response.go?ds=sidebyside diff --git a/lib/controller/router/response.go b/lib/controller/router/response.go index 82ca5ef5e6..aa3af1f64c 100644 --- a/lib/controller/router/response.go +++ b/lib/controller/router/response.go @@ -53,26 +53,40 @@ func applySelectParam(selectParam []string, orig map[string]interface{}) map[str } func (rtr *router) sendResponse(w http.ResponseWriter, resp interface{}, opts responseOptions) { - respKind := kind(resp) var tmp map[string]interface{} + err := rtr.transcode(resp, &tmp) if err != nil { rtr.sendError(w, err) return } - tmp["kind"] = respKind + respKind := kind(resp) + if respKind != "" { + tmp["kind"] = respKind + } + defaultItemKind := "" + if strings.HasSuffix(respKind, "List") { + defaultItemKind = strings.TrimSuffix(respKind, "List") + } + if items, ok := tmp["items"].([]interface{}); ok { for i, item := range items { - // Fill in "kind" by inspecting UUID + // Fill in "kind" by inspecting UUID/PDH if + // possible; fall back on assuming each + // Items[] entry in an "arvados#fooList" + // response should have kind="arvados#foo". item, _ := item.(map[string]interface{}) - uuid, _ := item["uuid"].(string) - if len(uuid) != 27 { - // unsure whether this happens - } else if t, ok := infixMap[uuid[6:11]]; !ok { - // infix not listed in infixMap - } else { - item["kind"] = kind(t) + infix := "" + if uuid, _ := item["uuid"].(string); len(uuid) == 27 { + infix = uuid[6:11] + } + if k := kind(infixMap[infix]); k != "" { + item["kind"] = k + } else if pdh, _ := item["portable_data_hash"].(string); pdh != "" { + item["kind"] = "arvados#collection" + } else if defaultItemKind != "" { + item["kind"] = defaultItemKind } items[i] = applySelectParam(opts.Select, item) } @@ -106,6 +120,7 @@ func (rtr *router) sendResponse(w http.ResponseWriter, resp interface{}, opts re tmp[k] = t.Format(rfc3339NanoFixed) } } + w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(tmp) } @@ -125,7 +140,11 @@ var infixMap = map[string]interface{}{ var mungeKind = regexp.MustCompile(`\..`) func kind(resp interface{}) string { - return mungeKind.ReplaceAllStringFunc(fmt.Sprintf("%T", resp), func(s string) string { + t := fmt.Sprintf("%T", resp) + if !strings.HasPrefix(t, "arvados.") { + return "" + } + return mungeKind.ReplaceAllStringFunc(t, func(s string) string { // "arvados.CollectionList" => "arvados#collectionList" return "#" + strings.ToLower(s[1:]) })