@@ -415,27 +415,31 @@ encodeSumCase encoding rawTag jsons =
415
415
416
416
type FlatEncoding (tag ∷ Symbol ) =
417
417
{ tag ∷ Proxy tag
418
+ , mapTag ∷ String → String
418
419
}
419
420
420
421
defaultFlatEncoding ∷ FlatEncoding " tag"
421
- defaultFlatEncoding = { tag: Proxy }
422
+ defaultFlatEncoding =
423
+ { tag: Proxy
424
+ , mapTag: identity
425
+ }
422
426
423
427
sumFlat ∷ ∀ r rep a . GFlatCases " tag" r rep ⇒ Generic a rep ⇒ String → Record r → JsonCodec a
424
428
sumFlat = sumFlatWith defaultFlatEncoding
425
429
426
430
sumFlatWith ∷ ∀ @tag r rep a . GFlatCases tag r rep ⇒ Generic a rep ⇒ FlatEncoding tag → String → Record r → JsonCodec a
427
- sumFlatWith _ name r =
431
+ sumFlatWith encoding name r =
428
432
dimap from to $ codec' dec enc
429
433
where
430
- dec = gFlatCasesDecode @tag r >>> (lmap $ Named name)
431
- enc = gFlatCasesEncode @tag r
434
+ dec = gFlatCasesDecode @tag encoding r >>> (lmap $ Named name)
435
+ enc = gFlatCasesEncode @tag encoding r
432
436
433
437
class GFlatCases ∷ Symbol → Row Type → Type → Constraint
434
438
class
435
439
GFlatCases tag r rep
436
440
where
437
- gFlatCasesEncode ∷ Record r → rep → Json
438
- gFlatCasesDecode ∷ Record r → Json → Either JsonDecodeError rep
441
+ gFlatCasesEncode ∷ FlatEncoding tag → Record r → rep → Json
442
+ gFlatCasesDecode ∷ FlatEncoding tag → Record r → Json → Either JsonDecodeError rep
439
443
440
444
instance gFlatCasesConstructorNoArg ∷
441
445
( Row.Cons name Unit () rc
@@ -444,22 +448,23 @@ instance gFlatCasesConstructorNoArg ∷
444
448
, IsSymbol tag
445
449
) ⇒
446
450
GFlatCases tag rc (Constructor name NoArguments ) where
447
- gFlatCasesEncode ∷ Record rc → Constructor name NoArguments → Json
448
- gFlatCasesEncode _ (Constructor NoArguments ) =
451
+ gFlatCasesEncode ∷ FlatEncoding tag → Record rc → Constructor name NoArguments → Json
452
+ gFlatCasesEncode { mapTag } _ (Constructor NoArguments ) =
449
453
let
450
- name = reflectSymbol (Proxy @name) ∷ String
454
+ nameRaw = reflectSymbol (Proxy @name) ∷ String
455
+ name = mapTag nameRaw ∷ String
451
456
propCodec = CAR .record {} ∷ JPropCodec { }
452
457
propCodecWithTag = CA .recordProp (Proxy @tag) CA .string propCodec ∷ JPropCodec (Record rf )
453
458
codecWithTag = CA .object (" case " <> name) propCodecWithTag ∷ JsonCodec (Record rf )
454
459
rcWithTag = Record .insert (Proxy @tag) name {} ∷ Record rf
455
460
in
456
461
CA .encode codecWithTag rcWithTag
457
462
458
- gFlatCasesDecode ∷ Record rc → Json → Either JsonDecodeError (Constructor name NoArguments )
459
- gFlatCasesDecode _ json = do
463
+ gFlatCasesDecode ∷ FlatEncoding tag → Record rc → Json → Either JsonDecodeError (Constructor name NoArguments )
464
+ gFlatCasesDecode { mapTag } _ json = do
460
465
let
461
- name = reflectSymbol (Proxy @name) ∷ String
462
-
466
+ nameRaw = reflectSymbol (Proxy @name) ∷ String
467
+ name = mapTag nameRaw ∷ String
463
468
propCodec = CAR .record {} ∷ JPropCodec { }
464
469
propCodecWithTag = CA .recordProp (Proxy @tag) CA .string propCodec ∷ JPropCodec (Record rf )
465
470
codecWithTag = CA .object (" case " <> name) propCodecWithTag ∷ JsonCodec (Record rf )
@@ -480,21 +485,23 @@ instance gFlatCasesConstructorSingleArg ∷
480
485
, IsSymbol tag
481
486
) ⇒
482
487
GFlatCases tag rc (Constructor name (Argument (Record rf ))) where
483
- gFlatCasesEncode ∷ Record rc → Constructor name (Argument (Record rf )) → Json
484
- gFlatCasesEncode rc (Constructor (Argument rf)) =
488
+ gFlatCasesEncode ∷ FlatEncoding tag → Record rc → Constructor name (Argument (Record rf )) → Json
489
+ gFlatCasesEncode { mapTag } rc (Constructor (Argument rf)) =
485
490
let
486
- name = reflectSymbol (Proxy @name) ∷ String
491
+ nameRaw = reflectSymbol (Proxy @name) ∷ String
492
+ name = mapTag nameRaw ∷ String
487
493
propCodec = Record .get (Proxy @name) rc ∷ JPropCodec (Record rf )
488
494
propCodecWithTag = CA .recordProp (Proxy @tag) CA .string propCodec ∷ JPropCodec (Record rf' )
489
495
codecWithTag = CA .object (" case " <> name) propCodecWithTag ∷ JsonCodec (Record rf' )
490
496
rcWithTag = Record .insert (Proxy @tag) name rf ∷ Record rf'
491
497
in
492
498
CA .encode codecWithTag rcWithTag
493
499
494
- gFlatCasesDecode ∷ Record rc → Json → Either JsonDecodeError (Constructor name (Argument (Record rf )))
495
- gFlatCasesDecode rc json = do
500
+ gFlatCasesDecode ∷ FlatEncoding tag → Record rc → Json → Either JsonDecodeError (Constructor name (Argument (Record rf )))
501
+ gFlatCasesDecode { mapTag } rc json = do
496
502
let
497
- name = reflectSymbol (Proxy @name) ∷ String
503
+ nameRaw = reflectSymbol (Proxy @name) ∷ String
504
+ name = mapTag nameRaw ∷ String
498
505
propCodec = Record .get (Proxy @name) rc ∷ JPropCodec (Record rf )
499
506
propCodecWithTag = CA .recordProp (Proxy @tag) CA .string propCodec ∷ JPropCodec (Record rf' )
500
507
codecWithTag = CA .object (" case " <> name) propCodecWithTag ∷ JsonCodec (Record rf' )
@@ -518,61 +525,30 @@ instance gFlatCasesSum ∷
518
525
, IsSymbol name
519
526
) ⇒
520
527
GFlatCases tag r (Sum (Constructor name lhs ) rhs ) where
521
- gFlatCasesEncode ∷ Record r → Sum (Constructor name lhs ) rhs → Json
522
- gFlatCasesEncode r =
528
+ gFlatCasesEncode ∷ FlatEncoding tag → Record r → Sum (Constructor name lhs ) rhs → Json
529
+ gFlatCasesEncode encoding r =
523
530
let
524
531
codec = Record .get (Proxy @name) r ∷ codec
525
532
r1 = Record .insert (Proxy @name) codec {} ∷ Record r1
526
533
r2 = unsafeDelete (Proxy @name) r ∷ Record r2
527
534
in
528
535
case _ of
529
- Inl lhs → gFlatCasesEncode @tag r1 lhs
530
- Inr rhs → gFlatCasesEncode @tag r2 rhs
536
+ Inl lhs → gFlatCasesEncode @tag encoding r1 lhs
537
+ Inr rhs → gFlatCasesEncode @tag encoding r2 rhs
531
538
532
- gFlatCasesDecode ∷ Record r → Json → Either JsonDecodeError (Sum (Constructor name lhs ) rhs )
533
- gFlatCasesDecode r tagged = do
539
+ gFlatCasesDecode ∷ FlatEncoding tag → Record r → Json → Either JsonDecodeError (Sum (Constructor name lhs ) rhs )
540
+ gFlatCasesDecode encoding r tagged = do
534
541
let
535
542
codec = Record .get (Proxy @name) r ∷ codec
536
543
r1 = Record .insert (Proxy @name) codec {} ∷ Record r1
537
544
r2 = Record .delete (Proxy @name) r ∷ Record r2
538
545
let
539
- lhs = gFlatCasesDecode @tag r1 tagged ∷ _ (Constructor name lhs )
540
- rhs = gFlatCasesDecode @tag r2 tagged ∷ _ rhs
546
+ lhs = gFlatCasesDecode @tag encoding r1 tagged ∷ _ (Constructor name lhs )
547
+ rhs = gFlatCasesDecode @tag encoding r2 tagged ∷ _ rhs
541
548
(Inl <$> lhs) <|> (Inr <$> rhs)
542
549
543
550
-- ------------------------------------------------------------------------------
544
551
545
- sumEnum ∷ ∀ r rep a . GEnumCases r rep ⇒ Generic a rep ⇒ String → Record r → JsonCodec a
546
- sumEnum = unsafeCoerce 1
547
-
548
- class GEnumCases ∷ Row Type → Type → Constraint
549
- class
550
- GEnumCases r rep
551
- where
552
- gEnumCasesEncode ∷ Record r → rep → Json
553
- gEnumCasesDecode ∷ Record r → Json → Either JsonDecodeError rep
554
-
555
- instance gEnumCasesConstructorNoArg ∷
556
- ( Row.Cons name Unit () rc
557
- , IsSymbol name
558
- ) ⇒
559
- GEnumCases rc (Constructor name NoArguments ) where
560
- gEnumCasesEncode ∷ Record rc → Constructor name NoArguments → Json
561
- gEnumCasesEncode _ _ =
562
- let
563
- name = reflectSymbol (Proxy @name) ∷ String
564
- in
565
- encodeSumCase defaultEncoding name []
566
-
567
- gEnumCasesDecode ∷ Record rc → Json → Either JsonDecodeError (Constructor name NoArguments )
568
- gEnumCasesDecode _ json = do
569
- let name = reflectSymbol (Proxy @name) ∷ String
570
-
571
- parseNoFields defaultEncoding json name
572
- pure $ Constructor NoArguments
573
-
574
- -- ------------------------------------------------------------------------------
575
-
576
552
-- | Same as `Record.delete` but deleting only happens at the type level
577
553
-- | and the value is left untouched.
578
554
unsafeDelete ∷ ∀ r1 r2 l a . IsSymbol l ⇒ Row.Lacks l r1 ⇒ Row.Cons l a r1 r2 ⇒ Proxy l → Record r2 → Record r1
0 commit comments