我正在写一些CRUD助手,为了好玩和利润,我发现自己需要一条空的或无路可走的路线。:>的mempty,如果您愿意的话。
这是我想写的:
type Index model =
Reassoc (QueryParams model :> Get '[JSON] (Collection model))
type family Reassoc xs where
Reassoc ((x :> y) :> z) = Reassoc (x :> Reassoc (y :> z))
Reassoc (x :> y) = x :> y
type family QueryParams model
type instance QueryParams User =
MakeQueryParams '[ '("organizationId", Int) ]当然,这一切都建立在这个人身上:
type family MakeQueryParams xs where
MakeQueryParams ( '(sym, ty) ': xs )
= QueryParam sym ty :> MakeQueryParams xs
MakeQueryParams '[]
= ... :(是否有空的路由组合器?
到目前为止,我已经通过在这些家庭中使用next参数解决了这个问题,但对于Servant来说,这还不是很常见。
type family MakeQueryParams xs next where
MakeQueryParams '[] next =
next
MakeQueryParams ('(sym, ty) ': xs) next =
QueryParam sym ty :> MakeQueryParams xs next
type Index model = QueryParams model (Get '[JSON] (Collection model))
type family QueryParams model next
type instance QueryParams User next =
MakeQueryParams '[ '("organizationId", Int) ] next发布于 2019-02-27 01:40:37
如果你真的坚持要写
type API = QueryParams '[ '("id", Int) ] :> Get '[JSON] Bool你将你的类似foldr的想法/解决方案(这是完全好的)与一个新的组合器结合在一起:
data QueryParams (ps :: [(Symbol, *)])
instance HasServer api ctx
=> HasServer (QueryParams '[] :> api) ctx where
type ServerT (QueryParams '[] api) m = ServerT api m
route = ...
instance HasServer (QueryParam sym ty :> MakeQueryParams ('(sym, ty) ': ps) api) ctx
=> HasServer (QueryParams ('(sym, ty) ': ps) api) ctx where
type ServerT (QueryParams ('(sym, ty) ': ps) api) m = ...
route = ...为nil和cons情况编写单独的实例将使实例的实现更直接。
可以认为我们必须引入新的combinator,否则我们无法在正确的位置插入类型族。有点像我们有时需要编写newtype来编写不同的实例。
https://stackoverflow.com/questions/43010941
复制相似问题