1 | module Pact.API.HttpApiData
9 | interface FromHttpApiData a where
11 | parseUrlPiece : String -> Either String a
14 | parseHeader : String -> Either String a
15 | parseHeader = parseUrlPiece
18 | parseQueryParam : String -> Either String a
19 | parseQueryParam = parseUrlPiece
22 | implementation FromHttpApiData () where
23 | parseUrlPiece _ = Right ()
26 | implementation FromHttpApiData Bool where
27 | parseUrlPiece s = case s of
28 | "true" => Right True
29 | "false" => Right False
30 | _ => Left "Invalid Bool"
33 | implementation FromHttpApiData Char where
34 | parseUrlPiece s = case asList s of
36 | _ => Left "Invalid Char"
39 | implementation FromHttpApiData String where
40 | parseUrlPiece = Right
44 | implementation FromHttpApiData Nat where
45 | parseUrlPiece s = case parsePositive s of
47 | Nothing => Left "Invalid Int"
50 | implementation FromHttpApiData Int where
51 | parseUrlPiece s = case parseInteger s of
53 | Nothing => Left "Invalid Int"
56 | implementation FromHttpApiData Integer where
57 | parseUrlPiece s = case parseInteger s of
59 | Nothing => Left "Invalid Integer"
62 | implementation FromHttpApiData Int8 where
63 | parseUrlPiece s = case parseInteger { a = Int8 } s of
64 | Just n => if n > 127 || n < -
128 then Left "Invalid Int8" else Right n
65 | Nothing => Left "Invalid Int8"
68 | implementation FromHttpApiData Int16 where
69 | parseUrlPiece s = case parseInteger { a = Int16 } s of
70 | Just n => if n > 32767 || n < -
32768 then Left "Invalid Int16" else Right n
71 | Nothing => Left "Invalid Int16"
74 | implementation FromHttpApiData Int32 where
75 | parseUrlPiece s = case parseInteger { a = Int32 } s of
76 | Just n => if n > 2147483647 || n < -
2147483648 then Left "Invalid Int32" else Right n
77 | Nothing => Left "Invalid Int32"
80 | implementation FromHttpApiData Int64 where
81 | parseUrlPiece s = case parseInteger { a = Int64 } s of
82 | Just n => if n > 9223372036854775807 || n < -
9223372036854775808 then Left "Invalid Int64" else Right n
83 | Nothing => Left "Invalid Int64"
86 | implementation FromHttpApiData Bits8 where
87 | parseUrlPiece s = case parsePositive { a = Bits8 } s of
88 | Just n => if n > 255 then Left "Invalid Bits8" else Right n
89 | Nothing => Left "Invalid Bits8"
92 | implementation FromHttpApiData Bits16 where
93 | parseUrlPiece s = case parsePositive { a = Bits16 } s of
94 | Just n => if n > 65535 then Left "Invalid Bits16" else Right n
95 | Nothing => Left "Invalid Bits16"
98 | implementation FromHttpApiData Bits32 where
99 | parseUrlPiece s = case parsePositive { a = Bits32 } s of
100 | Just n => if n > 4294967295 then Left "Invalid Bits32" else Right n
101 | Nothing => Left "Invalid Bits32"
104 | implementation FromHttpApiData Bits64 where
105 | parseUrlPiece s = case parsePositive { a = Bits64 } s of
106 | Just n => if n > 18446744073709551615 then Left "Invalid Bits64" else Right n
107 | Nothing => Left "Invalid Bits64"
110 | implementation FromHttpApiData Double where
111 | parseUrlPiece s = case parseDouble s of
113 | Nothing => Left "Invalid Double"
116 | implementation FromHttpApiData a => FromHttpApiData (Maybe a) where
117 | parseUrlPiece s = case s of
118 | "" => Right Nothing
119 | _ => map Just $
parseUrlPiece s
122 | implementation Monoid a => FromHttpApiData b => FromHttpApiData (Either a b) where
123 | parseUrlPiece s = case s of
124 | "" => Right $
Left neutral
125 | _ => map Right $
parseUrlPiece s
129 | implementation FromHttpApiData a => FromHttpApiData (List1 a) where
130 | parseUrlPiece s = case s of
131 | "" => Left "Invalid List1"
132 | _ => Data.String.split (== ',') s |> map parseUrlPiece |> sequence
135 | implementation FromHttpApiData a => FromHttpApiData (List a) where
136 | parseUrlPiece s = case s of
138 | _ => map forget $
parseUrlPiece s
141 | implementation FromHttpApiData a => FromHttpApiData (
n ** Vect n a)
where
142 | parseUrlPiece s = case s of
143 | "" => Right (
0 ** [])
144 | _ => parseUrlPiece {a = List a} s |> map (\list => (
length list ** fromList list)
)
149 | interface ToHttpApiData a where
151 | toUrlPiece : a -> String
154 | toHeader : a -> String
155 | toHeader = toUrlPiece
158 | toQueryParam : a -> String
159 | toQueryParam = toUrlPiece
162 | implementation ToHttpApiData String where
166 | implementation ToHttpApiData () where
170 | implementation ToHttpApiData Bool where
171 | toUrlPiece b = if b then "true" else "false"
174 | implementation ToHttpApiData Char where
175 | toUrlPiece c = singleton c
178 | implementation ToHttpApiData Nat where
182 | implementation ToHttpApiData Int where
186 | implementation ToHttpApiData Integer where
190 | implementation ToHttpApiData Int8 where
194 | implementation ToHttpApiData Int16 where
198 | implementation ToHttpApiData Int32 where
202 | implementation ToHttpApiData Int64 where
206 | implementation ToHttpApiData Bits8 where
210 | implementation ToHttpApiData Bits16 where
214 | implementation ToHttpApiData Bits32 where
218 | implementation ToHttpApiData Bits64 where
222 | implementation ToHttpApiData Double where
226 | implementation ToHttpApiData a => ToHttpApiData (Maybe a) where
227 | toUrlPiece = maybe "" toUrlPiece
230 | implementation ToHttpApiData a => ToHttpApiData (Either a b) where
231 | toUrlPiece = either toUrlPiece $
const ""
234 | implementation ToHttpApiData a => ToHttpApiData (List a) where
235 | toUrlPiece = joinBy "," . map toUrlPiece
238 | implementation ToHttpApiData a => ToHttpApiData (List1 a) where
239 | toUrlPiece = joinBy "," . toList . map toUrlPiece
242 | implementation ToHttpApiData a => ToHttpApiData (
n ** Vect n a)
where
243 | toUrlPiece (
n ** v)
= joinBy "," . toList $
map toUrlPiece v