我正在编写一些 CRUD 助手以获得乐趣和利润,并且我发现自己需要一条空路径或 noop 路径。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