@@ -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
3539import Data.Codec.Argonaut.Record as CAR
3640import Data.Either (Either (..), note )
3741import 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 )
3943import Data.Profunctor (dimap )
4044import Data.Symbol (class IsSymbol , reflectSymbol )
4145import Data.Tuple (Tuple (..))
@@ -150,6 +154,44 @@ finalizeError name err =
150154
151155data 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
155197class GCases ∷ Row Type → Type → Constraint
@@ -487,7 +529,6 @@ instance gFlatCasesConstructorNoArg ∷
487529
488530 pure (Constructor NoArguments )
489531
490-
491532instance 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.
571610unsafeDelete ∷ ∀ r1 r2 l a . IsSymbol l ⇒ Row.Lacks l r1 ⇒ Row.Cons l a r1 r2 ⇒ Proxy l → Record r2 → Record r1
572611unsafeDelete _ r = unsafeCoerce r
573-
0 commit comments