Skip to content

Commit e095518

Browse files
committed
Adds parseAttr combinator
Allow to parse nested records.
1 parent 9f2b58a commit e095518

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

Aws/DynamoDb/Core.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ module Aws.DynamoDb.Core
7575
, Parser (..)
7676
, getAttr
7777
, getAttr'
78+
, parseAttr
7879

7980
-- * Common types used by operations
8081
, Conditions (..)
@@ -1348,6 +1349,19 @@ getAttr' k m = do
13481349
Nothing -> return Nothing
13491350
Just dv -> return $ fromValue dv
13501351

1352+
-- | Combinator for parsing an attribute into a 'FromDynItem'.
1353+
parseAttr
1354+
:: FromDynItem a
1355+
=> T.Text
1356+
-- ^ Attribute name
1357+
-> Item
1358+
-- ^ Item from DynamoDb
1359+
-> Parser a
1360+
parseAttr k m =
1361+
case M.lookup k m of
1362+
Nothing -> fail ("Key " <> T.unpack k <> " not found")
1363+
Just (DMap dv) -> either (fail "...") return $ fromItem dv
1364+
_ -> fail ("Key " <> T.unpack k <> " is not a map!")
13511365

13521366
-------------------------------------------------------------------------------
13531367
-- | Parse an 'Item' into target type using the 'FromDynItem'

0 commit comments

Comments
 (0)