喷涂路由访问url路径

时间:2014-01-23 08:15:23

标签: scala routing spray

我的路线有这样的部分:

...
(pathEnd | path("summary")) {
    parameters(...).as(Query) { query =>
        onSuccess(model ? query) {
            case MyResponse(list) =>
                // at this point I would like to know if I hit pathEnd or
                // path(summary) so I can complete with summary or full response.
                if (???)
                    complete(OK, list)
                else
                    complete(OK, list map (_.toSummary))
        }
    }
}
...

基本上有很多参数争用和查询模型是相同的,但是我正在对响应进行额外的转换,以便在汇总端点被击中时丢弃一些数据。是否有可能以某种方式做到这一点?

我尝试在ctx =>之后添加(pathEnd | path("summary")) { ctx =>,但根本不起作用。 (路线不匹配,从未返回任何东西。)

1 个答案:

答案 0 :(得分:1)

我给这个自定义指令一个快速单元测试,它似乎工作:

  def pathEndOr(p: String) =
    pathEnd.hmap(true :: _) | path(p).hmap(false :: _)

你可以像你这样使用它:

...
pathEndOr("summary") { isPathEnd =>
    parameters(...).as(Query) { query =>
        onSuccess(model ? query) {
            case MyResponse(list) =>
                // at this point I would like to know if I hit pathEnd or
                // path(summary) so I can complete with summary or full response.
                if (isPathEnd)
                    complete(OK, list)
                else
                    complete(OK, list map (_.toSummary))
        }
    }
}
...