Skip to content

Commit e646590

Browse files
committed
Add GTagsMap class, sum', sumWith'
1 parent 1637fa9 commit e646590

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

src/Data/Codec/Argonaut/Sum.purs

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Data.Codec.Argonaut.Sum
55
, class GCases
66
, class GFields
77
, class GFlatCases
8+
, class GTagsMap
89
, defaultEncoding
910
, defaultFlatEncoding
1011
, enumSum
@@ -14,10 +15,13 @@ module Data.Codec.Argonaut.Sum
1415
, gFieldsEncode
1516
, gFlatCasesDecode
1617
, gFlatCasesEncode
18+
, gTagsMap
1719
, sum
20+
, sum'
1821
, sumFlat
1922
, sumFlatWith
2023
, sumWith
24+
, sumWith'
2125
, taggedSum
2226
) where
2327

@@ -35,7 +39,7 @@ import Data.Codec.Argonaut as CA
3539
import Data.Codec.Argonaut.Record as CAR
3640
import Data.Either (Either(..), note)
3741
import Data.Generic.Rep (class Generic, Argument(..), Constructor(..), NoArguments(..), Product(..), Sum(..), from, to)
38-
import Data.Maybe (Maybe(..), maybe)
42+
import Data.Maybe (Maybe(..), fromMaybe, maybe)
3943
import Data.Profunctor (dimap)
4044
import Data.Symbol (class IsSymbol, reflectSymbol)
4145
import Data.Tuple (Tuple(..))
@@ -150,6 +154,44 @@ finalizeError name err =
150154

151155
data Err = UnmatchedCase | JErr JsonDecodeError
152156

157+
sum' r' r rep a. GTagsMap r' rep GCases r rep Generic a rep String Record r' Record r JsonCodec a
158+
sum' = sumWith' defaultEncoding
159+
160+
sumWith' r' r rep a. GTagsMap r' rep GCases r rep Generic a rep Encoding String Record r' Record r JsonCodec a
161+
sumWith' encoding name tags r =
162+
sumWith encoding' name r
163+
where
164+
mapObj = gTagsMap (Proxy @rep) tags
165+
useTagsMap s = fromMaybe s (Obj.lookup s mapObj)
166+
encoding' = case encoding of
167+
EncodeNested enc →
168+
EncodeNested $ enc { mapTag = useTagsMap >>> enc.mapTag }
169+
170+
EncodeTagged enc →
171+
EncodeTagged $ enc { mapTag = useTagsMap >>> enc.mapTag }
172+
173+
--------------------------------------------------------------------------------
174+
175+
class GTagsMap k. Row Type k Constraint
176+
class GTagsMap r rep where
177+
gTagsMap Proxy rep Record r Object String
178+
179+
instance gTagsMapConstructor
180+
( Row.Cons name String () r
181+
, IsSymbol name
182+
)
183+
GTagsMap r (Constructor name a) where
184+
gTagsMap _ = unsafeCoerce
185+
186+
instance gTagsMapSum
187+
( GTagsMap r1 rhs
188+
, Row.Cons name String r1 r
189+
, Row.Lacks name r1
190+
, IsSymbol name
191+
)
192+
GTagsMap r (Sum (Constructor name lhs) rhs) where
193+
gTagsMap _ = unsafeCoerce
194+
153195
--------------------------------------------------------------------------------
154196

155197
class GCasesRow Type Type Constraint
@@ -487,7 +529,6 @@ instance gFlatCasesConstructorNoArg ∷
487529

488530
pure (Constructor NoArguments)
489531

490-
491532
instance gFlatCasesConstructorSingleArg
492533
( Row.Cons name (JPropCodec (Record rf)) () rc
493534
, Row.Lacks tag rf
@@ -508,19 +549,17 @@ instance gFlatCasesConstructorSingleArg ∷
508549
in
509550
CA.encode codecWithTag rcWithTag
510551

511-
512552
gFlatCasesDecode FlatEncoding tag Record rc Json Either Err (Constructor name (Argument (Record rf)))
513553
gFlatCasesDecode { mapTag } rc json = do
514554
let
515555
nameRaw = reflectSymbol (Proxy @name) String
516556
name = mapTag nameRaw String
517557
tag = reflectSymbol (Proxy @tag) String
518-
519558

520559
obj ← lmap JErr $ CA.decode jobject json
521560

522561
checkTag tag obj name
523-
562+
524563
let
525564
propCodec = Record.get (Proxy @name) rc JPropCodec (Record rf)
526565
codec = CA.object ("case " <> name) propCodec JsonCodec (Record rf)
@@ -550,7 +589,7 @@ instance gFlatCasesSum ∷
550589
Inl lhs → gFlatCasesEncode @tag encoding r1 lhs
551590
Inr rhs → gFlatCasesEncode @tag encoding r2 rhs
552591

553-
gFlatCasesDecode FlatEncoding tag -> Record r Json Either Err (Sum (Constructor name lhs) rhs)
592+
gFlatCasesDecode FlatEncoding tag Record r Json Either Err (Sum (Constructor name lhs) rhs)
554593
gFlatCasesDecode encoding r tagged = do
555594
let
556595
codec = Record.get (Proxy @name) r codec
@@ -570,4 +609,3 @@ instance gFlatCasesSum ∷
570609
-- | and the value is left untouched.
571610
unsafeDelete r1 r2 l a. IsSymbol l Row.Lacks l r1 Row.Cons l a r1 r2 Proxy l Record r2 Record r1
572611
unsafeDelete _ r = unsafeCoerce r
573-

0 commit comments

Comments
 (0)