Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read-msg recoverable under some key #1223

Merged
merged 4 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
4.7.0
---
* `read-*` functions are now recoverable if the key doesn't exist. That is, a call such as `(read-string 'key)` is recoverable
if `'key` does not exist in the data payload by surrounding it with `try`. As an example, `(try "string if key isn't present" (read-string "key"))`. This applies to
`read-integer`, `read-string`, `read-decimal`, `read-keyset` and `read-msg` (as long as `read-msg` has a key supplied as an argument).


4.6.0
---
* Add `DisablePact46` execution flag (#1138)
Expand Down
7 changes: 5 additions & 2 deletions src/Pact/Native/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ colsToList argFail _ = argFail
parseMsgKey :: (FromJSON t) => FunApp -> String -> Text -> Eval e t
parseMsgKey f s t = parseMsgKey' f s (Just t)

parseMsgKey' :: (FromJSON t) => FunApp -> String -> (Maybe Text) -> Eval e t
parseMsgKey' :: (FromJSON t) => FunApp -> String -> Maybe Text -> Eval e t
parseMsgKey' i msg key = do
b <- view eeMsgBody
let go v = case fromJSON v of
Expand All @@ -85,7 +85,10 @@ parseMsgKey' i msg key = do
case key of
Nothing -> go b
Just k -> case preview (A.key k) b of
Nothing -> evalError' i $ "No such key in message: " <> pretty k
Nothing ->
ifExecutionFlagSet FlagDisablePact47
(evalError' i $ "No such key in message: " <> pretty k)
(failTx' i $ "No such key in message: " <> pretty k)
Just v -> go v


Expand Down
25 changes: 25 additions & 0 deletions tests/pact/try.repl
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,28 @@
;; require-cap works in enforce-one post-fork
(expect "require-cap works sin enforce-one" true (enforce-cap))
(commit-tx)

; read-* functions made recoverable

(begin-tx)
(env-data {})
(env-exec-config ["DisablePact47"])
(expect-failure "read-integer fails on non-existent key" (try 1 (read-integer "somekey")))
(expect-failure "read-string fails on non-existent key" (try 1 (read-string "somekey")))
(expect-failure "read-keyset fails on non-existent key" (try 1 (read-keyset "somekey")))
(expect-failure "read-decimal fails on non-existent key" (try 1 (read-decimal "somekey")))
(expect-failure "read-msg fails on non-existent key" (try 1 (read-msg "somekey")))
(commit-tx)

; Post-fork, they are recoverable
(begin-tx)

(env-data {})
(env-exec-config [])
(expect "read-integer fails on non-existent key but is recoverable" 1 (try 1 (read-integer "somekey")))
(expect "read-string fails on non-existent key but is recoverable" 1 (try 1 (read-string "somekey")))
(expect "read-keyset fails on non-existent key but is recoverable" 1 (try 1 (read-keyset "somekey")))
(expect "read-decimal fails on non-existent key but is recoverable" 1 (try 1 (read-decimal "somekey")))
(expect "read-msg fails on non-existent key but is recoverable" 1 (try 1 (read-msg "somekey")))
(commit-tx)