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

Commit 5250cf0

Browse files
authored
Merge pull request #8 from garyb/types
Add sum for EJson types
2 parents 956a733 + 22d9efc commit 5250cf0

File tree

7 files changed

+83
-29
lines changed

7 files changed

+83
-29
lines changed

src/Data/Json/Extended.purs

+20-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Data.Json.Extended
2-
( module Sig
2+
( module Exports
33

44
, EJson(..)
55
, getEJson
@@ -17,18 +17,22 @@ module Data.Json.Extended
1717
, time
1818
, interval
1919
, objectId
20-
, object
21-
, object'
20+
, map
21+
, map'
2222
, array
2323

2424
, renderEJson
2525
, parseEJson
2626

2727
, arbitraryEJsonOfSize
2828
, arbitraryJsonEncodableEJsonOfSize
29+
30+
, getType
2931
) where
3032

31-
import Prelude
33+
import Prelude hiding (map)
34+
35+
import Data.Functor as F
3236

3337
import Control.Lazy as Lazy
3438

@@ -39,6 +43,7 @@ import Data.Eq1 (eq1)
3943
import Data.Functor.Mu as Mu
4044
import Data.HugeNum as HN
4145
import Data.Json.Extended.Signature as Sig
46+
import Data.Json.Extended.Type (EJsonType)
4247
import Data.Map as Map
4348
import Data.Maybe as M
4449
import Data.Newtype as N
@@ -52,6 +57,8 @@ import Test.StrongCheck.Arbitrary as SC
5257
import Test.StrongCheck.Gen as Gen
5358
import Text.Parsing.Parser as P
5459

60+
import Data.Json.Extended.Signature hiding (getType) as Exports
61+
5562
newtype EJson = EJson (Mu.Mu Sig.EJsonF)
5663

5764
derive instance newtypeEJson :: N.Newtype EJson _
@@ -74,15 +81,15 @@ roll
7481
roll =
7582
EJson
7683
<<< Mu.roll
77-
<<< map getEJson
84+
<<< F.map getEJson
7885

7986
unroll
8087
EJson
8188
Sig.EJsonF EJson
8289
unroll =
8390
getEJson
8491
>>> Mu.unroll
85-
>>> map EJson
92+
>>> F.map EJson
8693

8794
head EJson Sig.EJsonF (Mu.Mu Sig.EJsonF)
8895
head = Mu.unroll <<< getEJson
@@ -200,10 +207,13 @@ objectId = roll <<< Sig.ObjectId
200207
array Array EJson EJson
201208
array = roll <<< Sig.Array
202209

203-
object Map.Map EJson EJson EJson
204-
object = roll <<< Sig.Object <<< A.fromFoldable <<< Map.toList
210+
map Map.Map EJson EJson EJson
211+
map = roll <<< Sig.Map <<< A.fromFoldable <<< Map.toList
205212

206-
object' SM.StrMap EJson EJson
207-
object' = roll <<< Sig.Object <<< map go <<< A.fromFoldable <<< SM.toList
213+
map' SM.StrMap EJson EJson
214+
map' = roll <<< Sig.Map <<< F.map go <<< A.fromFoldable <<< SM.toList
208215
where
209216
go (T.Tuple a b) = T.Tuple (string a) b
217+
218+
getType EJson EJsonType
219+
getType = Sig.getType <<< head

src/Data/Json/Extended/Signature/Core.purs

+24-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module Data.Json.Extended.Signature.Core
22
( EJsonF(..)
3+
, getType
34
) where
45

56
import Prelude
@@ -9,10 +10,11 @@ import Data.Eq1 (class Eq1)
910
import Data.Foldable as F
1011
import Data.HugeNum as HN
1112
import Data.Int as Int
13+
import Data.Json.Extended.Type as T
1214
import Data.List as L
1315
import Data.Map as Map
1416
import Data.Ord1 (class Ord1)
15-
import Data.Tuple as T
17+
import Data.Tuple (Tuple)
1618

1719
-- | The signature endofunctor for the EJson theory.
1820
data EJsonF a
@@ -27,7 +29,7 @@ data EJsonF a
2729
| Interval String
2830
| ObjectId String
2931
| Array (Array a)
30-
| Object (Array (T.Tuple a a))
32+
| Map (Array (Tuple a a))
3133

3234
instance functorEJsonFFunctor EJsonF where
3335
map f x =
@@ -43,7 +45,7 @@ instance functorEJsonF ∷ Functor EJsonF where
4345
Interval i → Interval i
4446
ObjectId oid → ObjectId oid
4547
Array xs → Array $ f <$> xs
46-
Object xs → Object $ BF.bimap f f <$> xs
48+
Map xs → Map $ BF.bimap f f <$> xs
4749

4850
instance eq1EJsonFEq1 EJsonF where
4951
eq1 Null Null = true
@@ -59,7 +61,7 @@ instance eq1EJsonF ∷ Eq1 EJsonF where
5961
eq1 (Interval a) (Interval b) = a == b
6062
eq1 (ObjectId a) (ObjectId b) = a == b
6163
eq1 (Array xs) (Array ys) = xs == ys
62-
eq1 (Object xs) (Object ys) =
64+
eq1 (Map xs) (Map ys) =
6365
let
6466
xs' = L.fromFoldable xs
6567
ys' = L.fromFoldable ys
@@ -73,8 +75,8 @@ instance eq1EJsonF ∷ Eq1 EJsonF where
7375
isSubobject
7476
a b
7577
. (Eq a, Eq b)
76-
L.List (T.Tuple a b)
77-
L.List (T.Tuple a b)
78+
L.List (Tuple a b)
79+
L.List (Tuple a b)
7880
Boolean
7981
isSubobject xs ys =
8082
F.foldl
@@ -136,11 +138,19 @@ instance ord1EJsonF ∷ Ord1 EJsonF where
136138
compare1 _ (Array _) = GT
137139
compare1 (Array _) _ = LT
138140

139-
compare1 (Object a) (Object b) = compare (pairsToObject a) (pairsToObject b)
140-
141-
pairsToObject
142-
a b
143-
. (Ord a)
144-
Array (T.Tuple a b)
145-
Map.Map a b
146-
pairsToObject = Map.fromFoldable
141+
compare1 (Map a) (Map b) = compare (Map.fromFoldable a) (Map.fromFoldable b)
142+
143+
getType a. EJsonF a T.EJsonType
144+
getType = case _ of
145+
NullT.Null
146+
String _ → T.String
147+
Boolean _ → T.Boolean
148+
Integer _ → T.Integer
149+
Decimal _ → T.Decimal
150+
Timestamp _ → T.Timestamp
151+
Date _ → T.Date
152+
Time _ → T.Time
153+
Interval _ → T.Interval
154+
ObjectId _ → T.ObjectId
155+
Array _ → T.Array
156+
Map _ → T.Map

src/Data/Json/Extended/Signature/Gen.purs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ arbitraryEJsonFWithKeyGen keyGen rec =
3939
Gen.oneOf (pure Null)
4040
[ arbitraryBaseEJsonF
4141
, Array <$> Gen.arrayOf rec
42-
, Object <$> do
42+
, Map <$> do
4343
keys ← distinctArrayOf keyGen
4444
vals ← Gen.vectorOf (A.length keys) rec
4545
pure $ A.zip keys vals

src/Data/Json/Extended/Signature/Json.purs

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ encodeJsonEJsonF rec asKey x =
3636
Interval str → JS.jsonSingletonObject "$interval" $ encodeJson str
3737
ObjectId str → JS.jsonSingletonObject "$oid" $ encodeJson str
3838
Array xs → encodeJson $ rec <$> xs
39-
Object xs → JS.jsonSingletonObject "$obj" $ encodeJson $ asStrMap xs
39+
Map xs → JS.jsonSingletonObject "$obj" $ encodeJson $ asStrMap xs
4040
where
4141
tuple
4242
T.Tuple a a
@@ -137,7 +137,7 @@ decodeJsonEJsonF rec makeKey =
137137
SM.StrMap a
138138
EJsonF a
139139
strMapObject =
140-
Object
140+
Map
141141
<<< A.fromFoldable
142142
<<< map (\(T.Tuple k v) → T.Tuple (makeKey k) v)
143143
<<< SM.toList

src/Data/Json/Extended/Signature/Parse.purs

+1-1
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ parseEJsonF rec =
258258
, Interval <$> taggedLiteral "INTERVAL"
259259
, ObjectId <$> taggedLiteral "OID"
260260
, Array <<< A.fromFoldable <$> squares (commaSep rec)
261-
, Object <<< A.fromFoldable <$> braces (commaSep parseAssignment)
261+
, Map <<< A.fromFoldable <$> braces (commaSep parseAssignment)
262262
]
263263

264264
where

src/Data/Json/Extended/Signature/Render.purs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ renderEJsonF rec d =
3131
Interval str → tagged "INTERVAL" str
3232
ObjectId str → tagged "OID" str
3333
Array ds → squares $ commaSep ds
34-
Object ds → braces $ renderPairs ds
34+
Map ds → braces $ renderPairs ds
3535
where
3636
tagged
3737
String

src/Data/Json/Extended/Type.purs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module Data.Json.Extended.Type where
2+
3+
import Prelude
4+
5+
data EJsonType
6+
= Null
7+
| String
8+
| Boolean
9+
| Integer
10+
| Decimal
11+
| Timestamp
12+
| Date
13+
| Time
14+
| Interval
15+
| ObjectId
16+
| Array
17+
| Map
18+
19+
derive instance eqEJsonTypeEq EJsonType
20+
derive instance ordEJsonTypeOrd EJsonType
21+
22+
instance showEJsonTypeShow EJsonType where
23+
show Null = "Null"
24+
show String = "String"
25+
show Boolean = "Boolean"
26+
show Integer = "Integer"
27+
show Decimal = "Decimal"
28+
show Timestamp = "Timestamp"
29+
show Date = "Date"
30+
show Time = "Time"
31+
show Interval = "Interval"
32+
show ObjectId = "ObjectId"
33+
show Array = "Array"
34+
show Map = "Map"

0 commit comments

Comments
 (0)