1
1
module Data.Json.Extended.Signature.Core
2
2
( EJsonF (..)
3
+ , EJsonMap (..)
3
4
, getType
4
5
) where
5
6
6
7
import Prelude
7
8
8
9
import Data.Bifunctor as BF
9
10
import Data.DateTime as DT
10
- import Data.Eq (class Eq1 , eq1 )
11
+ import Data.Eq (class Eq1 )
11
12
import Data.Foldable as F
12
13
import Data.HugeNum as HN
13
- import Data.Int as Int
14
14
import Data.Json.Extended.Type as JT
15
15
import Data.List as L
16
+ import Data.Map as M
16
17
import Data.Monoid (mempty )
18
+ import Data.Newtype (class Newtype )
17
19
import Data.Ord (class Ord1 )
20
+ import Data.TacitString (TacitString )
18
21
import Data.Traversable as T
19
22
import Data.Tuple (Tuple (..))
20
- import Data.TacitString (TacitString )
21
23
22
24
-- | The signature endofunctor for the EJson theory.
23
25
data EJsonF a
@@ -32,42 +34,34 @@ data EJsonF a
32
34
| Interval String
33
35
| ObjectId String
34
36
| Array (Array a )
35
- | Map (Array (Tuple a a ))
36
-
37
- instance functorEJsonF ∷ Functor EJsonF where
38
- map f x =
39
- case x of
40
- Null → Null
41
- String str → String str
42
- Boolean b → Boolean b
43
- Integer i → Integer i
44
- Decimal a → Decimal a
45
- Timestamp ts → Timestamp ts
46
- Date d → Date d
47
- Time t → Time t
48
- Interval i → Interval i
49
- ObjectId oid → ObjectId oid
50
- Array xs → Array $ f <$> xs
51
- Map xs → Map $ BF .bimap f f <$> xs
37
+ | Map (EJsonMap a )
38
+
39
+ derive instance functorEJsonF ∷ Functor EJsonF
40
+
41
+ derive instance eqEJsonF ∷ Eq a ⇒ Eq (EJsonF a )
42
+ instance eq1EJsonF ∷ Eq1 EJsonF where eq1 = eq
43
+
44
+ derive instance ordEJsonF ∷ Ord a ⇒ Ord (EJsonF a )
45
+ instance ord1EJsonF ∷ Ord1 EJsonF where compare1 = compare
52
46
53
47
instance foldableEJsonF ∷ F.Foldable EJsonF where
54
48
foldMap f = case _ of
55
49
Array xs → F .foldMap f xs
56
- Map xs → F .foldMap (\( Tuple k v) → f k <> f v) xs
50
+ Map xs → F .foldMap f xs
57
51
_ → mempty
58
52
foldl f a = case _ of
59
53
Array xs → F .foldl f a xs
60
- Map xs → F .foldl (\acc ( Tuple k v) → f (f acc k) v) a xs
54
+ Map xs → F .foldl f a xs
61
55
_ → a
62
56
foldr f a = case _ of
63
57
Array xs → F .foldr f a xs
64
- Map xs → F .foldr (\( Tuple k v) acc → f k $ f v acc) a xs
58
+ Map xs → F .foldr f a xs
65
59
_ → a
66
60
67
61
instance traversableEJsonF ∷ T.Traversable EJsonF where
68
62
traverse f = case _ of
69
- Array xs → map Array $ T .traverse f xs
70
- Map xs → map Map $ T .traverse (\( Tuple k v) → Tuple <$> f k <*> f v) xs
63
+ Array xs → Array <$> T .traverse f xs
64
+ Map xs → Map <$> T .traverse f xs
71
65
Null → pure Null
72
66
String str → pure $ String str
73
67
Boolean b → pure $ Boolean b
@@ -80,57 +74,6 @@ instance traversableEJsonF ∷ T.Traversable EJsonF where
80
74
ObjectId oid → pure $ ObjectId oid
81
75
sequence = T .sequenceDefault
82
76
83
- instance eq1EJsonF ∷ Eq1 EJsonF where
84
- eq1 Null Null = true
85
- eq1 (Boolean b1) (Boolean b2) = b1 == b2
86
- eq1 (Integer i) (Integer j) = i == j
87
- eq1 (Decimal a) (Decimal b) = a == b
88
- eq1 (Integer i) (Decimal b) = intToHugeNum i == b
89
- eq1 (Decimal a) (Integer j) = a == intToHugeNum j
90
- eq1 (String a) (String b) = a == b
91
- eq1 (Timestamp a) (Timestamp b) = a == b
92
- eq1 (Date a) (Date b) = a == b
93
- eq1 (Time a) (Time b) = a == b
94
- eq1 (Interval a) (Interval b) = a == b
95
- eq1 (ObjectId a) (ObjectId b) = a == b
96
- eq1 (Array xs) (Array ys) = xs == ys
97
- eq1 (Map xs) (Map ys) =
98
- let
99
- xs' = L .fromFoldable xs
100
- ys' = L .fromFoldable ys
101
- in
102
- isSubobject xs' ys'
103
- && isSubobject ys' xs'
104
- eq1 _ _ = false
105
-
106
- instance eqEJsonF ∷ Eq a ⇒ Eq (EJsonF a ) where
107
- eq = eq1
108
-
109
- -- | Very badly performing, but we don't have access to Ord here,
110
- -- | so the performant version is not implementable.
111
- isSubobject
112
- ∷ ∀ a b
113
- . (Eq a , Eq b )
114
- ⇒ L.List (Tuple a b )
115
- → L.List (Tuple a b )
116
- → Boolean
117
- isSubobject xs ys =
118
- F .foldl
119
- (\acc x → acc && F .elem x ys)
120
- true
121
- xs
122
-
123
- intToHugeNum
124
- ∷ Int
125
- → HN.HugeNum
126
- intToHugeNum =
127
- HN .fromNumber
128
- <<< Int .toNumber
129
-
130
- derive instance ordEJsonF ∷ Ord a ⇒ Ord (EJsonF a )
131
- instance ord1EJsonF ∷ Ord1 EJsonF where
132
- compare1 = compare
133
-
134
77
instance showEJsonF ∷ Show (EJsonF TacitString ) where
135
78
show = case _ of
136
79
Null → " Null"
@@ -160,3 +103,41 @@ getType = case _ of
160
103
ObjectId _ → JT.ObjectId
161
104
Array _ → JT.Array
162
105
Map _ → JT.Map
106
+
107
+ newtype EJsonMap a = EJsonMap (Array (Tuple a a ))
108
+
109
+ derive instance newtypeEJsonMap ∷ Newtype (EJsonMap a ) _
110
+
111
+ instance functorEJsonMap ∷ Functor EJsonMap where
112
+ map f (EJsonMap xs) = EJsonMap (BF .bimap f f <$> xs)
113
+
114
+ instance eqEJsonMap ∷ Eq a ⇒ Eq (EJsonMap a ) where
115
+ eq (EJsonMap xs) (EJsonMap ys) =
116
+ let
117
+ xs' = L .fromFoldable xs
118
+ ys' = L .fromFoldable ys
119
+ in
120
+ isSubobject xs' ys'
121
+ && isSubobject ys' xs'
122
+
123
+ -- | Very badly performing, but we don't have access to Ord here,
124
+ -- | so the performant version is not implementable.
125
+ isSubobject ∷ ∀ a . Eq a ⇒ L.List (Tuple a a ) → L.List (Tuple a a ) → Boolean
126
+ isSubobject xs ys = F .foldl (\acc x → acc && F .elem x ys) true xs
127
+
128
+ instance ordEJsonMap ∷ Ord a ⇒ Ord (EJsonMap a ) where
129
+ compare (EJsonMap xs) (EJsonMap ys) =
130
+ compare (M .fromFoldable xs) (M .fromFoldable ys)
131
+
132
+ instance showEJsonMap ∷ Show (EJsonMap TacitString ) where
133
+ show (EJsonMap xs) = " (EJsonMap " <> show xs <> " )"
134
+
135
+ instance foldableEJsonMap ∷ F.Foldable EJsonMap where
136
+ foldMap f (EJsonMap xs) = F .foldMap (\(Tuple k v) → f k <> f v) xs
137
+ foldl f a (EJsonMap xs) = F .foldl (\acc (Tuple k v) → f (f acc k) v) a xs
138
+ foldr f a (EJsonMap xs) = F .foldr (\(Tuple k v) acc → f k $ f v acc) a xs
139
+
140
+ instance traversableEJsonMap ∷ T.Traversable EJsonMap where
141
+ traverse f (EJsonMap xs) =
142
+ EJsonMap <$> T .traverse (\(Tuple k v) → Tuple <$> f k <*> f v) xs
143
+ sequence = T .sequenceDefault
0 commit comments