Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit c62288a

Browse files
authored
Merge pull request #10 from garyb/prisms
Add Prisms for EJson types
2 parents 5250cf0 + 3fe4528 commit c62288a

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

bower.json

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"purescript-newtype": "^1.2.0",
2525
"purescript-parsing": "^3.0.0",
2626
"purescript-precise": "^1.0.0",
27+
"purescript-profunctor-lenses": "^2.4.0",
2728
"purescript-strongcheck": "^2.0.0"
2829
}
2930
}

src/Data/Json/Extended.purs

+82
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ module Data.Json.Extended
2828
, arbitraryJsonEncodableEJsonOfSize
2929

3030
, getType
31+
32+
, _Null
33+
, _String
34+
, _Boolean
35+
, _Integer
36+
, _Decimal
37+
, _Timestamp
38+
, _Date
39+
, _Time
40+
, _Interval
41+
, _ObjectId
42+
, _Array
43+
, _Map
44+
, _Map'
3145
) where
3246

3347
import Prelude hiding (map)
@@ -39,16 +53,19 @@ import Control.Lazy as Lazy
3953
import Data.Argonaut.Decode (class DecodeJson, decodeJson)
4054
import Data.Argonaut.Encode (class EncodeJson, encodeJson)
4155
import Data.Array as A
56+
import Data.Bitraversable (bitraverse)
4257
import Data.Eq1 (eq1)
4358
import Data.Functor.Mu as Mu
4459
import Data.HugeNum as HN
4560
import Data.Json.Extended.Signature as Sig
4661
import Data.Json.Extended.Type (EJsonType)
62+
import Data.Lens (Prism', preview, prism')
4763
import Data.Map as Map
4864
import Data.Maybe as M
4965
import Data.Newtype as N
5066
import Data.Ord1 (compare1)
5167
import Data.StrMap as SM
68+
import Data.Traversable (for)
5269
import Data.Tuple as T
5370

5471
import Matryoshka (class Corecursive, class Recursive, embed, project)
@@ -217,3 +234,68 @@ map' = roll <<< Sig.Map <<< F.map go <<< A.fromFoldable <<< SM.toList
217234

218235
getType EJson EJsonType
219236
getType = Sig.getType <<< head
237+
238+
_Null Prism' EJson Unit
239+
_Null = prism' (const null) $ head >>> case _ of
240+
Sig.NullM.Just unit
241+
_ → M.Nothing
242+
243+
_String Prism' EJson String
244+
_String = prism' string $ head >>> case _ of
245+
Sig.String s → M.Just s
246+
_ → M.Nothing
247+
248+
_Boolean Prism' EJson Boolean
249+
_Boolean = prism' boolean $ head >>> case _ of
250+
Sig.Boolean b → M.Just b
251+
_ → M.Nothing
252+
253+
_Integer Prism' EJson Int
254+
_Integer = prism' integer $ head >>> case _ of
255+
Sig.Integer i → M.Just i
256+
_ → M.Nothing
257+
258+
_Decimal Prism' EJson HN.HugeNum
259+
_Decimal = prism' decimal $ head >>> case _ of
260+
Sig.Decimal d → M.Just d
261+
_ → M.Nothing
262+
263+
_Timestamp Prism' EJson String
264+
_Timestamp = prism' timestamp $ head >>> case _ of
265+
Sig.Timestamp t → M.Just t
266+
_ → M.Nothing
267+
268+
_Date Prism' EJson String
269+
_Date = prism' date $ head >>> case _ of
270+
Sig.Date d → M.Just d
271+
_ → M.Nothing
272+
273+
_Time Prism' EJson String
274+
_Time = prism' time $ head >>> case _ of
275+
Sig.Time t → M.Just t
276+
_ → M.Nothing
277+
278+
_Interval Prism' EJson String
279+
_Interval = prism' interval $ head >>> case _ of
280+
Sig.Interval i → M.Just i
281+
_ → M.Nothing
282+
283+
_ObjectId Prism' EJson String
284+
_ObjectId = prism' objectId $ head >>> case _ of
285+
Sig.ObjectId id → M.Just id
286+
_ → M.Nothing
287+
288+
_Array Prism' EJson (Array EJson)
289+
_Array = prism' array $ unroll >>> case _ of
290+
Sig.Array xs → M.Just xs
291+
_ → M.Nothing
292+
293+
_Map Prism' EJson (Map.Map EJson EJson)
294+
_Map = prism' map $ unroll >>> case _ of
295+
Sig.Map kvs → M.Just $ Map.fromFoldable kvs
296+
_ → M.Nothing
297+
298+
_Map' Prism' EJson (SM.StrMap EJson)
299+
_Map' = prism' map' $ unroll >>> case _ of
300+
Sig.Map kvs → SM.fromFoldable <$> for kvs (bitraverse (preview _String) pure)
301+
_ → M.Nothing

0 commit comments

Comments
 (0)