11module Data.Json.Extended.Signature.Core
22 ( EJsonF (..)
3+ , EJsonMap (..)
34 , getType
45 ) where
56
67import Prelude
78
89import Data.Bifunctor as BF
910import Data.DateTime as DT
10- import Data.Eq (class Eq1 , eq1 )
11+ import Data.Eq (class Eq1 )
1112import Data.Foldable as F
1213import Data.HugeNum as HN
13- import Data.Int as Int
1414import Data.Json.Extended.Type as JT
1515import Data.List as L
16+ import Data.Map as M
1617import Data.Monoid (mempty )
18+ import Data.Newtype (class Newtype )
1719import Data.Ord (class Ord1 )
20+ import Data.TacitString (TacitString )
1821import Data.Traversable as T
1922import Data.Tuple (Tuple (..))
20- import Data.TacitString (TacitString )
2123
2224-- | The signature endofunctor for the EJson theory.
2325data EJsonF a
@@ -32,42 +34,34 @@ data EJsonF a
3234 | Interval String
3335 | ObjectId String
3436 | 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
5246
5347instance foldableEJsonF ∷ F.Foldable EJsonF where
5448 foldMap f = case _ of
5549 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
5751 _ → mempty
5852 foldl f a = case _ of
5953 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
6155 _ → a
6256 foldr f a = case _ of
6357 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
6559 _ → a
6660
6761instance traversableEJsonF ∷ T.Traversable EJsonF where
6862 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
7165 Null → pure Null
7266 String str → pure $ String str
7367 Boolean b → pure $ Boolean b
@@ -80,57 +74,6 @@ instance traversableEJsonF ∷ T.Traversable EJsonF where
8074 ObjectId oid → pure $ ObjectId oid
8175 sequence = T .sequenceDefault
8276
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-
13477instance showEJsonF ∷ Show (EJsonF TacitString ) where
13578 show = case _ of
13679 Null → " Null"
@@ -160,3 +103,41 @@ getType = case _ of
160103 ObjectId _ → JT.ObjectId
161104 Array _ → JT.Array
162105 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