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

Commit a5b61df

Browse files
committed
Add sum for EJson types
1 parent 956a733 commit a5b61df

File tree

3 files changed

+65
-13
lines changed

3 files changed

+65
-13
lines changed

src/Data/Json/Extended.purs

Lines changed: 9 additions & 1 deletion
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
@@ -26,6 +26,8 @@ module Data.Json.Extended
2626

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

3133
import Prelude
@@ -39,6 +41,7 @@ import Data.Eq1 (eq1)
3941
import Data.Functor.Mu as Mu
4042
import Data.HugeNum as HN
4143
import Data.Json.Extended.Signature as Sig
44+
import Data.Json.Extended.Type (EJsonType)
4245
import Data.Map as Map
4346
import Data.Maybe as M
4447
import Data.Newtype as N
@@ -52,6 +55,8 @@ import Test.StrongCheck.Arbitrary as SC
5255
import Test.StrongCheck.Gen as Gen
5356
import Text.Parsing.Parser as P
5457

58+
import Data.Json.Extended.Signature hiding (getType) as Exports
59+
5560
newtype EJson = EJson (Mu.Mu Sig.EJsonF)
5661

5762
derive instance newtypeEJson :: N.Newtype EJson _
@@ -207,3 +212,6 @@ object' ∷ SM.StrMap EJson → EJson
207212
object' = roll <<< Sig.Object <<< map go <<< A.fromFoldable <<< SM.toList
208213
where
209214
go (T.Tuple a b) = T.Tuple (string a) b
215+
216+
getType EJson EJsonType
217+
getType = Sig.getType <<< head

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

Lines changed: 22 additions & 12 deletions
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+
| Object (Array (Tuple a a))
3133

3234
instance functorEJsonFFunctor EJsonF where
3335
map f x =
@@ -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 (Object a) (Object 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+
Object _ → T.Object

src/Data/Json/Extended/Type.purs

Lines changed: 34 additions & 0 deletions
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+
| Object
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 Object = "Object"

0 commit comments

Comments
 (0)