1 | module Pact.API.Component
3 | import Pact.API.Operator
4 | import Pact.API.HttpApiData
8 | import Data.Vect.Quantifiers
11 | data Component : Type -> Vect n Type -> (reqBody : Type) -> Type where
13 | StaticPath : String -> Component () [()] Void
15 | Capture : String -> (a : Type) -> Component a [a] Void
17 | ReqBody : (a : Type) -> Component () [()] a
19 | (:/) : Component tl [tl] Void -> Component tr tsr reqBody -> Component tl (tl :: tsr) reqBody
22 | isReqBody : Component t ts reqBody -> Bool
23 | isReqBody (ReqBody _) = True
27 | PathReqBody : Component t ts reqBody -> Type
28 | PathReqBody (StaticPath _) = Void
29 | PathReqBody (Capture _ _) = Void
30 | PathReqBody (ReqBody reqBody) = reqBody
32 | PathReqBody (path :/ restPath) = PathReqBody restPath
35 | hasReqBody : Component t ts reqBody -> Bool
36 | hasReqBody (StaticPath _) = False
37 | hasReqBody (Capture _ _) = False
38 | hasReqBody (ReqBody _) = True
39 | hasReqBody (_ :/ restPath) = hasReqBody restPath
46 | getCaptureName : Component t _ _ -> String
47 | getCaptureName (Capture s _) = s
48 | getCaptureName _ = "unknown"
58 | matchPath : Component t ts reqBody' -> Vect m String -> { auto allprf : All FromHttpApiData ts } -> Either String (HVect ts)
59 | matchPath (StaticPath s) [seg] = if s == seg then Right [()] else Left "Path mismatch"
60 | matchPath (ReqBody s) [] = Right [()]
61 | matchPath (Capture s t) [seg] { allprf = prf :: restPrf } = case parseUrlPiece seg of
62 | Right val => Right [val]
63 | Left err => Left ("Failed to parse path parameter:" ++ s ++ " " ++ err)
64 | matchPath (StaticPath s :/ restPath) (seg :: segs) { allprf = prf :: restPrf } = if s == seg then map (\xs => () :: xs) (matchPath restPath segs) else Left "Path mismatch"
65 | matchPath (Capture s t :/ restPath) (seg :: segs) { allprf = prf :: restPrf } = case parseUrlPiece seg of
66 | Right val => case matchPath restPath segs of
67 | Right vals => Right (val :: vals)
68 | Left err => Left err
69 | Left err => Left ("Failed to parse path parameter:" ++ s ++ " " ++ err)
70 | matchPath _ segs = Left ("unknown path" ++ show segs)
79 | GetPathType : Component _ ts reqBody' -> (epType : Type) -> Type
80 | GetPathType (StaticPath _) epType = epType
81 | GetPathType (Capture _ t) epType = t -> epType
82 | GetPathType (ReqBody reqBody) epType = reqBody -> epType
83 | GetPathType { ts = t :: ts'} (path :/ restPath) epType = let epType' = GetPathType restPath epType in case path of
84 | StaticPath _ => epType'
85 | Capture name t => t -> epType'