Skip to content

Commit 2dd4256

Browse files
committed
Fixes & tests
1 parent 88f6c32 commit 2dd4256

File tree

2 files changed

+89
-25
lines changed

2 files changed

+89
-25
lines changed

src/Data/Codec/Argonaut/Sum.purs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ module Data.Codec.Argonaut.Sum
1919
, sumFlatWith
2020
, sumWith
2121
, taggedSum
22-
)
23-
where
22+
) where
2423

2524
import Prelude
2625

@@ -310,8 +309,7 @@ checkTag tagKey obj expectedTag = do
310309
) _ Json
311310
tag ← CA.decode CA.string val # lmap JErr _ String
312311
when (tag /= expectedTag)
313-
$ Left
314-
$ NoCase
312+
(Left NoCase)
315313

316314
parseNoFields Encoding Json String Either Err Unit
317315
parseNoFields encoding json expectedTag =
@@ -377,8 +375,7 @@ parseManyFields encoding json expectedTag =
377375
EncodeNested {} → do
378376
obj ← lmap JErr $ CA.decode jobject json
379377
val ←
380-
( Obj.lookup expectedTag obj
381-
# note (JErr $ TypeMismatch ("Expecting a property `" <> expectedTag <> "`"))
378+
( Obj.lookup expectedTag obj # note NoCase
382379
) _ Json
383380
lmap JErr $ CA.decode CA.jarray val
384381

@@ -464,15 +461,11 @@ instance gFlatCasesConstructorNoArg ∷
464461
gFlatCasesDecode _ json = do
465462
let
466463
name = reflectSymbol (Proxy @name) String
464+
tag = reflectSymbol (Proxy @tag) String
467465

468-
propCodec = CAR.record {} JPropCodec {}
469-
propCodecWithTag = CA.recordProp (Proxy @tag) CA.string propCodec JPropCodec (Record rf)
470-
codecWithTag = CA.object ("case " <> name) propCodecWithTag JsonCodec (Record rf)
471-
r ← lmap JErr $ CA.decode codecWithTag json _ (Record rf)
472-
let actualTag = Record.get (Proxy @tag) r String
466+
obj ← lmap JErr $ CA.decode jobject json
473467

474-
when (actualTag /= name)
475-
$ Left NoCase
468+
checkTag tag obj name
476469

477470
pure (Constructor NoArguments)
478471

@@ -499,17 +492,19 @@ instance gFlatCasesConstructorSingleArg ∷
499492
gFlatCasesDecode rc json = do
500493
let
501494
name = reflectSymbol (Proxy @name) String
495+
tag = reflectSymbol (Proxy @tag) String
496+
497+
obj ← lmap JErr $ CA.decode jobject json
498+
499+
checkTag tag obj name
500+
501+
let
502502
propCodec = Record.get (Proxy @name) rc JPropCodec (Record rf)
503-
propCodecWithTag = CA.recordProp (Proxy @tag) CA.string propCodec JPropCodec (Record rf')
504-
codecWithTag = CA.object ("case " <> name) propCodecWithTag JsonCodec (Record rf')
505-
r ← lmap JErr $ CA.decode codecWithTag json _ (Record rf')
503+
codec = CA.object ("case " <> name) propCodec JsonCodec (Record rf)
506504

507-
let actualTag = Record.get (Proxy @tag) r String
508-
when (actualTag /= name)
509-
$ Left NoCase
505+
r ← lmap JErr $ CA.decode codec json _ (Record rf)
510506

511-
let r' = Record.delete (Proxy @tag) r Record rf
512-
pure (Constructor (Argument r'))
507+
pure (Constructor (Argument r))
513508

514509
instance gFlatCasesSum
515510
( GFlatCases tag r1 (Constructor name lhs)

test/Test/Sum.purs

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import Data.Argonaut.Core (stringify, stringifyWithIndent)
77
import Data.Argonaut.Decode (parseJson)
88
import Data.Bifunctor (lmap)
99
import Data.Codec (decode, encode)
10-
import Data.Codec.Argonaut (JsonCodec)
10+
import Data.Codec.Argonaut (JsonCodec, JsonDecodeError(..))
1111
import Data.Codec.Argonaut as C
1212
import Data.Codec.Argonaut.Record as CR
13-
import Data.Codec.Argonaut.Sum (Encoding(..), defaultEncoding, sumFlat, sumFlatWith, sumWith)
13+
import Data.Codec.Argonaut.Sum (Encoding(..), defaultEncoding, sumFlatWith, sumWith)
14+
import Data.Either (Either(..))
1415
import Data.Generic.Rep (class Generic)
1516
import Data.Show.Generic (genericShow)
1617
import Data.String as Str
@@ -22,7 +23,6 @@ import Test.QuickCheck (class Arbitrary, arbitrary, quickCheck)
2223
import Test.QuickCheck.Arbitrary (genericArbitrary)
2324
import Test.Util (propCodec)
2425
import Type.Prelude (Proxy(..))
25-
import Type.Proxy (Proxy)
2626

2727
--------------------------------------------------------------------------------
2828

@@ -91,11 +91,20 @@ check codec val expectEncoded = do
9191

9292
json ← liftEither $ lmap (show >>> error) $ parseJson encodedStr
9393

94-
decoded ← liftEither $ lmap (\err -> error ("decode failed: " <> show err <> " JSON was: " <> stringify json)) $ decode codec json
94+
decoded ← liftEither $ lmap (\err error ("decode failed: " <> show err <> " JSON was: " <> stringify json)) $ decode codec json
9595

9696
when (decoded /= val) $
9797
throw ("decode check failed, expected: " <> show val <> ", got: " <> show decoded)
9898

99+
checkFailure a. Show a Eq a JsonCodec a JsonDecodeError String Effect Unit
100+
checkFailure codec err encodedStr = do
101+
json ← liftEither $ lmap (show >>> error) $ parseJson encodedStr
102+
103+
let result = decode codec json
104+
105+
when (result /= Left err) $
106+
throw ("decode check failed, expected: " <> show err <> ", got: " <> show result)
107+
99108
main Effect Unit
100109
main = do
101110
log "Check sum"
@@ -184,6 +193,27 @@ main = do
184193
, "}"
185194
]
186195

196+
checkFailure
197+
(codecSample opts)
198+
(Named "Sample" (TypeMismatch "Expecting at least one element"))
199+
$ Str.joinWith "\n"
200+
[ "{"
201+
, " \"customTag\": \"Baz\","
202+
, " \"customValues\": ["
203+
, " true"
204+
, " ]"
205+
, "}"
206+
]
207+
208+
checkFailure
209+
(codecSample opts)
210+
(Named "Sample" (TypeMismatch "No case matched"))
211+
$ Str.joinWith "\n"
212+
[ "{"
213+
, " \"customTag\": \"Qux\""
214+
, "}"
215+
]
216+
187217
log " - Option: Omit empty arguments"
188218
do
189219
let
@@ -315,6 +345,26 @@ main = do
315345
, "}"
316346
]
317347

348+
checkFailure
349+
(codecSample opts)
350+
(Named "Sample" (TypeMismatch "Expecting at least one element"))
351+
$ Str.joinWith "\n"
352+
[ "{"
353+
, " \"Baz\": ["
354+
, " true"
355+
, " ]"
356+
, "}"
357+
]
358+
359+
checkFailure
360+
(codecSample opts)
361+
(Named "Sample" (TypeMismatch "No case matched"))
362+
$ Str.joinWith "\n"
363+
[ "{"
364+
, " \"Qux\": []"
365+
, "}"
366+
]
367+
318368
log " - Option: Unwrap single arguments"
319369
do
320370
let
@@ -385,5 +435,24 @@ main = do
385435
, "}"
386436
]
387437

438+
checkFailure
439+
codecSampleFlat
440+
(Named "Sample" (Named "case FlatBaz" (AtKey "pos" MissingValue)))
441+
$ Str.joinWith "\n"
442+
[ "{"
443+
, " \"tag\": \"FlatBaz\","
444+
, " \"active\": true"
445+
, "}"
446+
]
447+
448+
checkFailure
449+
codecSampleFlat
450+
(Named "Sample" (TypeMismatch "No case matched"))
451+
$ Str.joinWith "\n"
452+
[ "{"
453+
, " \"tag\": \"FlatQux\""
454+
, "}"
455+
]
456+
388457
quickCheck (propCodec arbitrary codecSampleFlat)
389458

0 commit comments

Comments
 (0)