"strings"
"time"
- "git.curoverse.com/arvados.git/sdk/go/arvados"
- "git.curoverse.com/arvados.git/sdk/go/httpserver"
+ "git.arvados.org/arvados.git/sdk/go/arvados"
+ "git.arvados.org/arvados.git/sdk/go/httpserver"
)
const rfc3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
selected[attr] = v
}
}
- // Preserve "kind" even if not requested
- if v, ok := orig["kind"]; ok {
- selected["kind"] = v
+ // Some keys are always preserved, even if not requested
+ for _, k := range []string{"etag", "kind", "writable_by"} {
+ if v, ok := orig[k]; ok {
+ selected[k] = v
+ }
}
return selected
}
-func (rtr *router) sendResponse(w http.ResponseWriter, resp interface{}, opts responseOptions) {
- respKind := kind(resp)
+func (rtr *router) sendResponse(w http.ResponseWriter, req *http.Request, resp interface{}, opts responseOptions) {
var tmp map[string]interface{}
+
+ if resp, ok := resp.(http.Handler); ok {
+ // resp knows how to write its own http response
+ // header and body.
+ resp.ServeHTTP(w, req)
+ return
+ }
+
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)
}
tmp[k] = t.Format(rfc3339NanoFixed)
}
}
- json.NewEncoder(w).Encode(tmp)
+ w.Header().Set("Content-Type", "application/json")
+ enc := json.NewEncoder(w)
+ enc.SetEscapeHTML(false)
+ enc.Encode(tmp)
}
func (rtr *router) sendError(w http.ResponseWriter, err error) {
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:])
})