Skip to content

Commit

Permalink
Adds a pretty decoder that removes Aeson portion of decoding error
Browse files Browse the repository at this point in the history
This splits the error message produced by `fromValue` into two:
the Aeson and Fleece error message combined (the original behavior)
and just the Fleece error message (that produced by the schema and
the `Decoder` functions). `decodePretty`, as a result, produces a
slightly easier to understand error message that is more suitable
for displaying to users. Ideally, we would be able to remove the
type from the error messages produced by the `Decoder` functions as
well, but for now, this isn't changing that since it would alter
the current tests' expected results.
  • Loading branch information
AugmenTab committed Jan 2, 2024
1 parent cb820e9 commit 97d0097
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
3 changes: 2 additions & 1 deletion json-fleece-aeson/json-fleece-aeson.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.35.2.
-- This file has been generated from package.yaml by hpack version 0.36.0.
--
-- see: https://github.com/sol/hpack

Expand Down Expand Up @@ -41,6 +41,7 @@ library
, base >=4.7 && <5
, bytestring ==0.11.*
, containers ==0.6.*
, either ==5.0.2
, json-fleece-core ==0.5.*
, shrubbery >=0.1.2 && <0.2
, text >=1.2 && <2.1
Expand Down
1 change: 1 addition & 0 deletions json-fleece-aeson/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ library:
- Fleece.Aeson.Decoder
- Fleece.Aeson.Encoder
dependencies:
- either == 5.0.2
- shrubbery >= 0.1.2 && < 0.2

tests:
Expand Down
17 changes: 12 additions & 5 deletions json-fleece-aeson/src/Fleece/Aeson/Decoder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Fleece.Aeson.Decoder
( Decoder (..)
, decode
, decodePretty
, decodeStrict
, fromValue
, toParser
Expand All @@ -16,6 +17,7 @@ import qualified Data.Aeson.KeyMap as AesonKeyMap
import qualified Data.Aeson.Types as AesonTypes
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LBS
import Data.Either.Combinators (mapLeft)
import qualified Data.Map.Strict as Map
import qualified Data.Text.Encoding as Enc
import qualified Shrubbery
Expand All @@ -25,21 +27,26 @@ import qualified Fleece.Core as FC
data Decoder a
= Decoder FC.Name (Aeson.Value -> AesonTypes.Parser a)

fromValue :: Decoder a -> Aeson.Value -> Either String a
fromValue =
AesonTypes.parseEither . toParser
fromValue :: Decoder a -> Aeson.Value -> Either (String, String) a
fromValue decoder value =
mapLeft (\(path, err) -> (AesonTypes.formatError path err, err)) $
AesonTypes.iparseEither (toParser decoder) value

toParser :: Decoder a -> Aeson.Value -> AesonTypes.Parser a
toParser (Decoder _name f) =
f

decode :: Decoder a -> LBS.ByteString -> Either String a
decode decoder =
fromValue decoder <=< Aeson.eitherDecode
mapLeft fst . fromValue decoder <=< Aeson.eitherDecode

decodePretty :: Decoder a -> LBS.ByteString -> Either String a
decodePretty decoder =
mapLeft snd . fromValue decoder <=< Aeson.eitherDecode

decodeStrict :: Decoder a -> BS.ByteString -> Either String a
decodeStrict decoder =
fromValue decoder <=< Aeson.eitherDecodeStrict
mapLeft fst . fromValue decoder <=< Aeson.eitherDecodeStrict

instance FC.Fleece Decoder where
data Object Decoder _object a = Object
Expand Down

0 comments on commit 97d0097

Please sign in to comment.