Skip to content

Commit c0e41ed

Browse files
committed
Version with megaparsec and attoparsec
1 parent 0911424 commit c0e41ed

File tree

5 files changed

+47
-6
lines changed

5 files changed

+47
-6
lines changed

Diff for: kubernetes-client/kubernetes-client.cabal

+4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ library
6363
, jose-jwt >=0.8
6464
, jsonpath >=0.1 && <0.4
6565
, kubernetes-client-core ==0.4.3.0
66+
, megaparsec ==9.*
6667
, microlens >=0.4
6768
, mtl >=2.2
6869
, oidc-client >=0.4
@@ -110,6 +111,7 @@ test-suite example
110111
, jsonpath >=0.1 && <0.4
111112
, kubernetes-client
112113
, kubernetes-client-core ==0.4.3.0
114+
, megaparsec ==9.*
113115
, microlens >=0.4
114116
, mtl >=2.2
115117
, oidc-client >=0.4
@@ -159,12 +161,14 @@ test-suite spec
159161
, hoauth2 >=1.11 && <=3
160162
, hspec
161163
, hspec-attoparsec
164+
, hspec-megaparsec
162165
, http-client >=0.5 && <0.8
163166
, http-client-tls >=0.3
164167
, jose-jwt >=0.8
165168
, jsonpath >=0.1 && <0.4
166169
, kubernetes-client
167170
, kubernetes-client-core ==0.4.3.0
171+
, megaparsec ==9.*
168172
, microlens >=0.4
169173
, mtl >=2.2
170174
, oidc-client >=0.4

Diff for: kubernetes-client/package.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ tests:
2525
- kubernetes-client
2626
- hspec
2727
- hspec-attoparsec
28+
- hspec-megaparsec
2829
- yaml
2930
- file-embed
3031
example:
@@ -52,6 +53,7 @@ dependencies:
5253
- http-client-tls >=0.3
5354
- jose-jwt >=0.8
5455
- kubernetes-client-core ==0.4.3.0
56+
- megaparsec >=9 && <10
5557
- microlens >=0.4
5658
- mtl >=2.2
5759
- oidc-client >=0.4

Diff for: kubernetes-client/src/Kubernetes/Client/Auth/GCP.hs

-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ where
66

77
import Control.Concurrent.STM
88
import Control.Exception.Safe (Exception, throwM)
9-
import Data.Attoparsec.Text
109
import Data.Either.Combinators
1110
import Data.Function ((&))
1211
import Data.JSONPath
@@ -126,7 +125,6 @@ parseGCPAuthInfo authInfo = do
126125
Just expiryText -> Just <$> parseExpiryTime expiryText
127126
lookupEither key = Map.lookup key authInfo
128127
& maybeToRight (GCPAuthMissingInformation $ Text.unpack key)
129-
parseK8sJSONPath = parseOnly (k8sJSONPath <* endOfInput)
130128
readJSONPath key defaultPath =
131129
maybe (Right defaultPath) parseK8sJSONPath $ Map.lookup key authInfo
132130

Diff for: kubernetes-client/src/Kubernetes/Data/K8sJSONPath.hs

+26-2
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,53 @@ module Kubernetes.Data.K8sJSONPath where
55
import Control.Applicative ((<|>))
66
import Data.Aeson
77
import Data.Aeson.Text
8-
import Data.Attoparsec.Text ( many1, char, takeWhile1, Parser )
8+
import Data.Bifunctor
99
import Data.JSONPath
1010
import Data.Monoid ((<>))
1111
import Data.Text as Text
1212
import Data.Text.Lazy (toStrict)
1313

14+
#if MIN_VERSION_jsonpath(0,3,0)
15+
import Data.Void (Void)
16+
import Text.Megaparsec ( Parsec, eof, runParser, some, takeWhile1P )
17+
import Text.Megaparsec.Char ( char )
18+
type Parser a = Parsec Void Text a
19+
#else
20+
import Data.Attoparsec.Text ( many1, char, takeWhile1, Parser )
21+
#endif
22+
1423

1524
data K8sPathElement = PlainText Text
1625
| JSONPath [JSONPathElement]
1726
deriving (Show, Eq)
1827

28+
parseK8sJSONPath :: Text -> Either String [K8sPathElement]
29+
#if MIN_VERSION_jsonpath(0,3,0)
30+
parseK8sJSONPath = first show . runParser (k8sJSONPath <* eof) "nothing"
31+
#else
32+
parseK8sJSONPath = parseOnly (k8sJSONPath <* endOfInput)
33+
#endif
34+
1935
k8sJSONPath :: Parser [K8sPathElement]
36+
#if MIN_VERSION_jsonpath(0,3,0)
37+
k8sJSONPath = some pathElementParser
38+
#else
2039
k8sJSONPath = many1 pathElementParser
40+
#endif
2141

2242
pathElementParser :: Parser K8sPathElement
2343
pathElementParser = jsonpathParser <|> plainTextParser
2444

2545
plainTextParser :: Parser K8sPathElement
46+
#if MIN_VERSION_jsonpath(0,3,0)
47+
plainTextParser = PlainText <$> takeWhile1P (Just "non_open_brace") (/= '{')
48+
#else
2649
plainTextParser = PlainText <$> takeWhile1 (/= '{')
50+
#endif
2751

2852
jsonpathParser :: Parser K8sPathElement
2953
#if MIN_VERSION_jsonpath(0,3,0)
30-
jsonpathParser = JSONPath <$> (char '{' *> jsonPath undefined <* char '}')
54+
jsonpathParser = JSONPath <$> (char '{' *> jsonPath (char '}') <* char '}')
3155
#else
3256
jsonpathParser = JSONPath <$> (char '{' *> jsonPath <* char '}')
3357
#endif

Diff for: kubernetes-client/test/Kubernetes/Data/K8sJSONPathSpec.hs

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
{-# LANGUAGE OverloadedStrings #-}
2+
{-# LANGUAGE CPP #-}
3+
24
module Kubernetes.Data.K8sJSONPathSpec where
35

46
import Test.Hspec
5-
import Test.Hspec.Attoparsec
67

78
import Kubernetes.Data.K8sJSONPath
89
import Data.Text
910
import Data.JSONPath
1011
import Data.Aeson
1112

13+
#if MIN_VERSION_jsonpath(0,3,0)
14+
import Data.Void (Void)
15+
import Test.Hspec.Megaparsec
16+
import Text.Megaparsec (runParser)
17+
import Text.Megaparsec.Error (ParseErrorBundle)
18+
19+
(~>) :: Text -> Parser [K8sPathElement] -> Either (ParseErrorBundle Text Void) [K8sPathElement]
20+
(~>) text parser = runParser parser "nothing" text
21+
#else
22+
import Test.Hspec.Attoparsec
23+
#endif
24+
25+
1226
spec :: Spec
1327
spec = do
1428
describe "K8sJSONPath" $ do
@@ -30,4 +44,3 @@ spec = do
3044
let path = [PlainText "kind is ", JSONPath [KeyChild "kind"]]
3145
val = (object ["kind" .= ("Pod" :: Text)])
3246
runJSONPath path val `shouldBe` Right "kind is Pod"
33-

0 commit comments

Comments
 (0)