mirror of
https://github.com/zhigang1992/graphql-engine.git
synced 2026-06-15 01:59:08 +08:00
* allow underscore prefix and special characters in json path * server: Rewrite/refactor JSONPath parser The JSONPath parser is also rewritten, the previous implementation was written in a very explicitly “recursive descent” style, but the whole point of using attoparsec is to be able to backtrack! Taking advantage of the combinators makes for a much simpler parser. Co-authored-by: Vamshi Surabhi <0x777@users.noreply.github.com> Co-authored-by: Alexis King <lexi.lambda@gmail.com> Co-authored-by: Aleksandra Sikora <ola.zxcvbnm@gmail.com> Co-authored-by: Shahidh K Muhammed <shahidh@hasura.io>
44 lines
1.5 KiB
Haskell
44 lines
1.5 KiB
Haskell
module Data.Parser.JSONPathSpec (spec) where
|
|
|
|
import Data.Aeson.Internal (JSONPath, JSONPathElement (..))
|
|
import Hasura.Prelude
|
|
import Hasura.RQL.Types (encodeJSONPath)
|
|
|
|
import Data.Parser.JSONPath
|
|
import Test.Hspec
|
|
import Test.QuickCheck
|
|
|
|
import qualified Data.Text as T
|
|
|
|
spec :: Spec
|
|
spec = describe "encode and parse JSONPath" $ do
|
|
it "JSONPath encoder" $
|
|
forM_ generateTestEncodeJSONPath $ \(jsonPath, result) ->
|
|
encodeJSONPath jsonPath `shouldBe` result
|
|
|
|
it "JSONPath parser" $
|
|
withMaxSuccess 1000 $
|
|
forAll(resize 20 generateJSONPath) $ \jsonPath ->
|
|
let encPath = encodeJSONPath jsonPath
|
|
parsedJSONPathE = parseJSONPath $ T.pack encPath
|
|
in case parsedJSONPathE of
|
|
Left err -> counterexample (err <> ": " <> encPath) False
|
|
Right parsedJSONPath -> property $ parsedJSONPath == jsonPath
|
|
|
|
|
|
generateTestEncodeJSONPath :: [(JSONPath, String)]
|
|
generateTestEncodeJSONPath =
|
|
[ ([Key "7seven", Index 0, Key "@!^@*#(!("], "$['7seven'][0]['@!^@*#(!(']")
|
|
, ([Key "ABCD"], "$.ABCD")
|
|
]
|
|
|
|
generateJSONPath :: Gen JSONPath
|
|
generateJSONPath = map (either id id) <$> listOf1 genPathElementEither
|
|
where
|
|
genPathElementEither = do
|
|
indexLeft <- Left <$> genIndex
|
|
keyRight <- Right <$> genKey
|
|
elements [indexLeft, keyRight]
|
|
genIndex = Index <$> choose (0, 100)
|
|
genKey = (Key . T.pack) <$> listOf1 (elements $ alphaNumerics ++ ".,!@#$%^&*_-?:;|/\"")
|