waargonaut 0.6.0.0 → 0.6.1.0
raw patch · 6 files changed
+92/−4 lines, 6 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Waargonaut.Decode: passKeysToValues :: (Snoc c c v v, Monad f) => c -> Decoder f k -> (k -> Decoder f v) -> Decoder f c
Files
- changelog.md +5/−0
- src/Waargonaut/Decode.hs +54/−0
- src/Waargonaut/Encode/Types.hs +1/−1
- test/Decoder.hs +21/−1
- test/json-data/keys-in-obj.json +8/−0
- waargonaut.cabal +3/−2
changelog.md view
@@ -1,5 +1,10 @@ # Revision history for waargonaut +## 0.6.1.0 -- 2019-02-27++* Add `passKeysToValues` decoder for decoding JSON objects where the key should+ be part of the value.+ ## 0.6.0.0 -- 2019-02-19 #### Fixes
src/Waargonaut/Decode.hs view
@@ -87,6 +87,7 @@ , maybeOrNull , either , oneOf+ , passKeysToValues ) where @@ -599,6 +600,59 @@ (flip runReaderT p . unDecodeResult . f) (DI.Decoder' $ runDecoder elemD p) curs++-- | A specialised decoder for moving over a JSON object where the keys are+-- values that you would like to have as part of the value at the different+-- keys.+--+-- An example of such an input is:+--+-- @+-- { "Collection" : {+-- "BobsInput_ce43dff22": {+-- "someValue": "Some data"+-- },+-- "FredsInput_a4b32def": {+-- "someValue": "Some different data"+-- }+-- }+-- @+--+-- Where those key values like "XInput_YYYY" are to be included in the object.+--+-- Given a type like this:+--+-- @+-- data ContainsItsKey = ContainsItsKey+-- { _containsItsKey_KeyValue :: Text+-- , _containsItsKey_SomeValue :: Text+-- }+-- @+-- +-- To decode the above you would use this function like so:+-- +-- @+-- takesKeyDecoder :: Monad f => Text -> Decoder f ContainsItsKey+-- takesKeyDecoder k = ContainsItsKey k <$> D.atKey "someValue" D.text+--+-- collectionDecoder :: Monad f => Decoder f [ContainsItsKey]+-- collectionDecoder = D.atKey "Collection" $ D.passKeysToValues D.text takesKeyDecoder+-- @+--+passKeysToValues+ :: ( Snoc c c v v+ , Monad f+ )+ => c + -> Decoder f k+ -> (k -> Decoder f v)+ -> Decoder f c+passKeysToValues empty dK kDV = withCursor $ down >=> foldCursor snoc+ (moveRightN (successor' (successor' zero'))) empty+ (withCursor $ \c' -> do+ k <- focus dK c'+ moveRight1 c' >>= focus (kDV k)+ ) -- | Helper function for "pattern matching" on a decoded value to some Haskell -- value. The 'Text' argument is used in the error message should this decoder
src/Waargonaut/Encode/Types.hs view
@@ -104,7 +104,7 @@ type ObjEncoder' a = EncoderFns (JObject WS Json) Identity a -- | Run any encoder to the 'Json' representation, allowing for some--- 'Applicative' context @f@.+-- 'Functor' context @f@. runEncoder :: Functor f => EncoderFns i f a -> a -> f Json runEncoder e = fmap (finaliseEncoding e) . initialEncoding e {-# INLINE runEncoder #-}
test/Decoder.hs view
@@ -1,4 +1,3 @@-{-# LANGUAGE LambdaCase #-} {-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE OverloadedStrings #-} module Decoder@@ -32,6 +31,7 @@ import Data.Tagged (untag) import Data.Text (Text) import qualified Data.Text as T+import qualified Data.Text.IO as TIO import qualified Natural as N @@ -67,7 +67,27 @@ , testCase "Absent Key Decoder" absentKeyDecoder , testCase "Either decoding order - Right first" decodeEitherRightFirst , testProperty "Unicode codepoint handling regression" unicodeHandlingRegression+ , testCase "Pass decoded key to the object value decoder" testKeysPassedToValueDecoder ]++data ContainsItsKey = ContainsItsKey+ { _containsItsKeyKey :: Text+ , _containsItsKeyOtherVal :: Text+ }+ deriving (Eq,Show)++testKeysPassedToValueDecoder :: Assertion+testKeysPassedToValueDecoder =+ TIO.readFile "test/json-data/keys-in-obj.json"+ >>= WA.decodeAttoparsecText dec+ >>= (@?= Either.Right expected)+ where+ expected = [+ ContainsItsKey "ID0" "Some data"+ , ContainsItsKey "ID1" "Some different data"+ ]+ dec = D.atKey "Collection" (D.passKeysToValues [] D.text takesKeyDecoder)+ takesKeyDecoder k = ContainsItsKey k <$> D.atKey "SomeCommonKeys" D.text unicodeHandlingRegression :: Property unicodeHandlingRegression = withTests 1 . property $ do
+ test/json-data/keys-in-obj.json view
@@ -0,0 +1,8 @@+{ "Collection" : {+ "ID0": {+ "SomeCommonKeys": "Some data"+ },+ "ID1": {+ "SomeCommonKeys": "Some different data"+ }+}
waargonaut.cabal view
@@ -10,7 +10,7 @@ -- PVP summary: +-+------- breaking API changes -- | | +----- non-breaking API additions -- | | | +--- code changes with no API change-version: 0.6.0.0+version: 0.6.1.0 -- A short (one-line) description of the package. synopsis: JSON wrangling@@ -48,6 +48,7 @@ , test/json-data/image_obj.json , test/json-data/twitter_with_hex_vals.json , test/json-data/unicode_2705.json+ , test/json-data/keys-in-obj.json -- Known bad json files , test/json-data/bad-json/no_comma_arr.json@@ -255,6 +256,6 @@ , waargonaut - ghc-options: -Wall -O2+ ghc-options: -Wall default-language: Haskell2010