14287: Clean up context key usage.
[arvados.git] / lib / controller / router / response.go
index 4536380fd9cff22a368369440c3f27f63d7bdefa..aa3af1f64c45194c5d5b3cc2e1996ed212941e5e 100644 (file)
@@ -35,22 +35,68 @@ func (rtr *router) responseOptions(opts interface{}) (responseOptions, error) {
        return rOpts, nil
 }
 
+func applySelectParam(selectParam []string, orig map[string]interface{}) map[string]interface{} {
+       if len(selectParam) == 0 {
+               return orig
+       }
+       selected := map[string]interface{}{}
+       for _, attr := range selectParam {
+               if v, ok := orig[attr]; ok {
+                       selected[attr] = v
+               }
+       }
+       // Preserve "kind" even if not requested
+       if v, ok := orig["kind"]; ok {
+               selected["kind"] = v
+       }
+       return selected
+}
+
 func (rtr *router) sendResponse(w http.ResponseWriter, resp interface{}, opts responseOptions) {
        var tmp map[string]interface{}
+
        err := rtr.transcode(resp, &tmp)
        if err != nil {
                rtr.sendError(w, err)
                return
        }
-       if len(opts.Select) > 0 {
-               selected := map[string]interface{}{}
-               for _, attr := range opts.Select {
-                       if v, ok := tmp[attr]; ok {
-                               selected[attr] = v
+
+       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/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{})
+                       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)
+               }
+               if opts.Count == "none" {
+                       delete(tmp, "items_available")
                }
-               tmp = selected
+       } else {
+               tmp = applySelectParam(opts.Select, tmp)
        }
+
        // Format non-nil timestamps as rfc3339NanoFixed (by default
        // they will have been encoded to time.RFC3339Nano, which
        // omits trailing zeroes).
@@ -74,7 +120,7 @@ func (rtr *router) sendResponse(w http.ResponseWriter, resp interface{}, opts re
                        tmp[k] = t.Format(rfc3339NanoFixed)
                }
        }
-       tmp["kind"] = kind(resp)
+       w.Header().Set("Content-Type", "application/json")
        json.NewEncoder(w).Encode(tmp)
 }
 
@@ -86,10 +132,19 @@ func (rtr *router) sendError(w http.ResponseWriter, err error) {
        httpserver.Error(w, err.Error(), code)
 }
 
+var infixMap = map[string]interface{}{
+       "4zz18": arvados.Collection{},
+       "j7d0g": arvados.Group{},
+}
+
 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:])
        })