akka/akka-http

Add query parameter name to ParamsSpec

Open

#3,566 创建于 2020年10月23日

在 GitHub 查看
 (3 评论) (0 反应) (0 负责人)Scala (598 fork)batch import
1 - triagedhelp wantednice-to-have (low-prio)t:routing

仓库指标

Star
 (1,311 star)
PR 合并指标
 (平均合并 1天 10小时) (30 天内合并 2 个 PR)

描述

The trait ParamsSpec could also contain the following:

def name: String

being name the requested query parameter field name. This would be very useful for us because we want to override the parameter directive adding some extra action (adding an attribute to the HttpRequest to keep track of consumed query parameters):

  private val atKey: AttributeKey[Set[String]] = AttributeKey("allowed-query-params")

  private def trackQueryParam(name: String): Directive0 =
    optionalAttribute(atKey).flatMap { paramsOpt =>
      mapRequest(addAttribute(atKey, paramsOpt.fold(Set(name))(_ + name)))
    }

def param(pdef1: ParamSpec): Directive[Tuple1[pdef1.Out]] =
    Directives.parameter(pdef1.name) & trackQueryParam(pdef1.name)

Since the field name is not currently accessible from ParamSpec we have to do some ugly work around to make that work currently.

贡献者指南