Version with megaparsec and attoparsec
This commit is contained in:
@@ -63,6 +63,7 @@ library
|
||||
, jose-jwt >=0.8
|
||||
, jsonpath >=0.1 && <0.4
|
||||
, kubernetes-client-core ==0.4.3.0
|
||||
, megaparsec ==9.*
|
||||
, microlens >=0.4
|
||||
, mtl >=2.2
|
||||
, oidc-client >=0.4
|
||||
@@ -110,6 +111,7 @@ test-suite example
|
||||
, jsonpath >=0.1 && <0.4
|
||||
, kubernetes-client
|
||||
, kubernetes-client-core ==0.4.3.0
|
||||
, megaparsec ==9.*
|
||||
, microlens >=0.4
|
||||
, mtl >=2.2
|
||||
, oidc-client >=0.4
|
||||
@@ -159,12 +161,14 @@ test-suite spec
|
||||
, hoauth2 >=1.11 && <=3
|
||||
, hspec
|
||||
, hspec-attoparsec
|
||||
, hspec-megaparsec
|
||||
, http-client >=0.5 && <0.8
|
||||
, http-client-tls >=0.3
|
||||
, jose-jwt >=0.8
|
||||
, jsonpath >=0.1 && <0.4
|
||||
, kubernetes-client
|
||||
, kubernetes-client-core ==0.4.3.0
|
||||
, megaparsec ==9.*
|
||||
, microlens >=0.4
|
||||
, mtl >=2.2
|
||||
, oidc-client >=0.4
|
||||
|
||||
@@ -25,6 +25,7 @@ tests:
|
||||
- kubernetes-client
|
||||
- hspec
|
||||
- hspec-attoparsec
|
||||
- hspec-megaparsec
|
||||
- yaml
|
||||
- file-embed
|
||||
example:
|
||||
@@ -52,6 +53,7 @@ dependencies:
|
||||
- http-client-tls >=0.3
|
||||
- jose-jwt >=0.8
|
||||
- kubernetes-client-core ==0.4.3.0
|
||||
- megaparsec >=9 && <10
|
||||
- microlens >=0.4
|
||||
- mtl >=2.2
|
||||
- oidc-client >=0.4
|
||||
|
||||
@@ -6,7 +6,6 @@ where
|
||||
|
||||
import Control.Concurrent.STM
|
||||
import Control.Exception.Safe (Exception, throwM)
|
||||
import Data.Attoparsec.Text
|
||||
import Data.Either.Combinators
|
||||
import Data.Function ((&))
|
||||
import Data.JSONPath
|
||||
@@ -126,7 +125,6 @@ parseGCPAuthInfo authInfo = do
|
||||
Just expiryText -> Just <$> parseExpiryTime expiryText
|
||||
lookupEither key = Map.lookup key authInfo
|
||||
& maybeToRight (GCPAuthMissingInformation $ Text.unpack key)
|
||||
parseK8sJSONPath = parseOnly (k8sJSONPath <* endOfInput)
|
||||
readJSONPath key defaultPath =
|
||||
maybe (Right defaultPath) parseK8sJSONPath $ Map.lookup key authInfo
|
||||
|
||||
|
||||
@@ -5,29 +5,53 @@ module Kubernetes.Data.K8sJSONPath where
|
||||
import Control.Applicative ((<|>))
|
||||
import Data.Aeson
|
||||
import Data.Aeson.Text
|
||||
import Data.Attoparsec.Text ( many1, char, takeWhile1, Parser )
|
||||
import Data.Bifunctor
|
||||
import Data.JSONPath
|
||||
import Data.Monoid ((<>))
|
||||
import Data.Text as Text
|
||||
import Data.Text.Lazy (toStrict)
|
||||
|
||||
#if MIN_VERSION_jsonpath(0,3,0)
|
||||
import Data.Void (Void)
|
||||
import Text.Megaparsec ( Parsec, eof, runParser, some, takeWhile1P )
|
||||
import Text.Megaparsec.Char ( char )
|
||||
type Parser a = Parsec Void Text a
|
||||
#else
|
||||
import Data.Attoparsec.Text ( many1, char, takeWhile1, Parser )
|
||||
#endif
|
||||
|
||||
|
||||
data K8sPathElement = PlainText Text
|
||||
| JSONPath [JSONPathElement]
|
||||
deriving (Show, Eq)
|
||||
|
||||
parseK8sJSONPath :: Text -> Either String [K8sPathElement]
|
||||
#if MIN_VERSION_jsonpath(0,3,0)
|
||||
parseK8sJSONPath = first show . runParser (k8sJSONPath <* eof) "nothing"
|
||||
#else
|
||||
parseK8sJSONPath = parseOnly (k8sJSONPath <* endOfInput)
|
||||
#endif
|
||||
|
||||
k8sJSONPath :: Parser [K8sPathElement]
|
||||
#if MIN_VERSION_jsonpath(0,3,0)
|
||||
k8sJSONPath = some pathElementParser
|
||||
#else
|
||||
k8sJSONPath = many1 pathElementParser
|
||||
#endif
|
||||
|
||||
pathElementParser :: Parser K8sPathElement
|
||||
pathElementParser = jsonpathParser <|> plainTextParser
|
||||
|
||||
plainTextParser :: Parser K8sPathElement
|
||||
#if MIN_VERSION_jsonpath(0,3,0)
|
||||
plainTextParser = PlainText <$> takeWhile1P (Just "non_open_brace") (/= '{')
|
||||
#else
|
||||
plainTextParser = PlainText <$> takeWhile1 (/= '{')
|
||||
#endif
|
||||
|
||||
jsonpathParser :: Parser K8sPathElement
|
||||
#if MIN_VERSION_jsonpath(0,3,0)
|
||||
jsonpathParser = JSONPath <$> (char '{' *> jsonPath undefined <* char '}')
|
||||
jsonpathParser = JSONPath <$> (char '{' *> jsonPath (char '}') <* char '}')
|
||||
#else
|
||||
jsonpathParser = JSONPath <$> (char '{' *> jsonPath <* char '}')
|
||||
#endif
|
||||
|
||||
@@ -1,14 +1,28 @@
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# LANGUAGE CPP #-}
|
||||
|
||||
module Kubernetes.Data.K8sJSONPathSpec where
|
||||
|
||||
import Test.Hspec
|
||||
import Test.Hspec.Attoparsec
|
||||
|
||||
import Kubernetes.Data.K8sJSONPath
|
||||
import Data.Text
|
||||
import Data.JSONPath
|
||||
import Data.Aeson
|
||||
|
||||
#if MIN_VERSION_jsonpath(0,3,0)
|
||||
import Data.Void (Void)
|
||||
import Test.Hspec.Megaparsec
|
||||
import Text.Megaparsec (runParser)
|
||||
import Text.Megaparsec.Error (ParseErrorBundle)
|
||||
|
||||
(~>) :: Text -> Parser [K8sPathElement] -> Either (ParseErrorBundle Text Void) [K8sPathElement]
|
||||
(~>) text parser = runParser parser "nothing" text
|
||||
#else
|
||||
import Test.Hspec.Attoparsec
|
||||
#endif
|
||||
|
||||
|
||||
spec :: Spec
|
||||
spec = do
|
||||
describe "K8sJSONPath" $ do
|
||||
@@ -30,4 +44,3 @@ spec = do
|
||||
let path = [PlainText "kind is ", JSONPath [KeyChild "kind"]]
|
||||
val = (object ["kind" .= ("Pod" :: Text)])
|
||||
runJSONPath path val `shouldBe` Right "kind is Pod"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user