Skip to content

Commit

Permalink
Merge pull request #729 from robbiemcmichael/format-path
Browse files Browse the repository at this point in the history
Add formatPath function
  • Loading branch information
bergmark authored Oct 16, 2019
2 parents b23d351 + 0ca4679 commit 98bbc5b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Data/Aeson/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ module Data.Aeson.Types
, (<?>)
, JSONPath
, JSONPathElement(..)
, formatPath
, formatRelativePath
) where

import Prelude.Compat
Expand Down
14 changes: 13 additions & 1 deletion Data/Aeson/Types/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ module Data.Aeson.Types.Internal
, parserThrowError
, parserCatchError
, formatError
, formatPath
, formatRelativePath
, (<?>)
-- * Constructors and accessors
, object
Expand Down Expand Up @@ -458,7 +460,17 @@ parseEither m v = runParser (m v) [] onError Right
-- | Annotate an error message with a
-- <http://goessner.net/articles/JsonPath/ JSONPath> error location.
formatError :: JSONPath -> String -> String
formatError path msg = "Error in " ++ format "$" path ++ ": " ++ msg
formatError path msg = "Error in " ++ formatPath path ++ ": " ++ msg

-- | Format a <http://goessner.net/articles/JsonPath/ JSONPath> as a 'String',
-- representing the root object as @$@.
formatPath :: JSONPath -> String
formatPath path = "$" ++ formatRelativePath path

-- | Format a <http://goessner.net/articles/JsonPath/ JSONPath> as a 'String'
-- which represents the path relative to some root object.
formatRelativePath :: JSONPath -> String
formatRelativePath path = format "" path
where
format :: String -> JSONPath -> String
format pfx [] = pfx
Expand Down
15 changes: 14 additions & 1 deletion tests/UnitTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ import Data.Aeson.Parser
, json', jsonLast', jsonAccum', jsonNoDup')
import Data.Aeson.Types
( Options(..), Result(Success), ToJSON(..), Value(Array, Bool, Null, Object)
, camelTo, camelTo2, defaultOptions, omitNothingFields, parse)
, camelTo, camelTo2, defaultOptions, formatPath, formatRelativePath
, omitNothingFields, parse)
import Data.Attoparsec.ByteString (Parser, parseOnly)
import Data.Char (toUpper)
import Data.Either.Compat (isLeft, isRight)
Expand Down Expand Up @@ -253,6 +254,18 @@ formatErrorExample =
lhs = "Error in $[0].foo.bar['a.b.c']['']['\\'\\\\'].end: error msg"
in assertEqual "formatError example" lhs rhs

formatPathExample :: Assertion
formatPathExample =
let rhs = formatPath [Key "x", Index 0]
lhs = "$.x[0]"
in assertEqual "formatPath example" lhs rhs

formatRelativePathExample :: Assertion
formatRelativePathExample =
let rhs = formatPath [Key "x", Index 0]
lhs = ".x[0]"
in assertEqual "formatRelativePath example" lhs rhs

------------------------------------------------------------------------------
-- Comparison (.:?) and (.:!)
------------------------------------------------------------------------------
Expand Down

0 comments on commit 98bbc5b

Please sign in to comment.