git.iko.soy/typeable/hjsonpointer
on master help sign in
.travis.yml+79−59
since splitsnapshots
unifiedsplit
@@ -1,84 +1,104 @@
1−# This file has been generated -- see https://github.com/hvr/multi-ghc-travis
1+# This Travis job script has been generated by a script via
2#
2#
3+# runghc make_travis_yml_2.hs 'hjsonpointer.cabal'
4+#
5+# For more information, see https://github.com/hvr/multi-ghc-travis
6+
3−# Manually modified to remove cabal check. I don't want a description field.
7+# NOTE: After generating comment out `cabal check`, it requires a description field.
4
8
5language: c
9language: c
6sudo: false
10sudo: false
7
11
12+git:
13+ submodules: false # whether to recursively clone submodules
14+
8cache:
15cache:
9 directories:
16 directories:
10− - $HOME/.cabsnap
11 - $HOME/.cabal/packages
17 - $HOME/.cabal/packages
18+ - $HOME/.cabal/store
12
19
13before_cache:
20before_cache:
14 - rm -fv $HOME/.cabal/packages/hackage.haskell.org/build-reports.log
21 - rm -fv $HOME/.cabal/packages/hackage.haskell.org/build-reports.log
22+ # remove files that are regenerated by 'cabal update'
15− - rm -fv $HOME/.cabal/packages/hackage.haskell.org/00-index.tar
23+ - rm -fv $HOME/.cabal/packages/hackage.haskell.org/00-index.*
24+ - rm -fv $HOME/.cabal/packages/hackage.haskell.org/*.json
25+ - rm -fv $HOME/.cabal/packages/hackage.haskell.org/01-index.cache
26+ - rm -fv $HOME/.cabal/packages/hackage.haskell.org/01-index.tar
27+ - rm -fv $HOME/.cabal/packages/hackage.haskell.org/01-index.tar.idx
28+
29+ - rm -rfv $HOME/.cabal/packages/head.hackage
16
30
17matrix:
31matrix:
18 include:
32 include:
19− - env: CABALVER=1.18 GHCVER=7.8.4
33+ - compiler: "ghc-8.0.2"
20− compiler: ": #GHC 7.8.4"
34+ # env: TEST=--disable-tests BENCH=--disable-benchmarks
21− addons: {apt: {packages: [cabal-install-1.18,ghc-7.8.4], sources: [hvr-ghc]}}
35+ addons: {apt: {packages: [ghc-ppa-tools,cabal-install-head,ghc-8.0.2], sources: [hvr-ghc]}}
22− - env: CABALVER=1.22 GHCVER=7.10.2
36+ - compiler: "ghc-8.2.2"
23− compiler: ": #GHC 7.10.2"
37+ # env: TEST=--disable-tests BENCH=--disable-benchmarks
24− addons: {apt: {packages: [cabal-install-1.22,ghc-7.10.2], sources: [hvr-ghc]}}
38+ addons: {apt: {packages: [ghc-ppa-tools,cabal-install-head,ghc-8.2.2], sources: [hvr-ghc]}}
25− - env: CABALVER=1.24 GHCVER=8.0.1
39+ - compiler: "ghc-8.4.1"
26− compiler: ": #GHC 8.0.1"
40+ # env: TEST=--disable-tests BENCH=--disable-benchmarks
27− addons: {apt: {packages: [cabal-install-1.24,ghc-8.0.1], sources: [hvr-ghc]}}
41+ addons: {apt: {packages: [ghc-ppa-tools,cabal-install-head,ghc-8.4.1], sources: [hvr-ghc]}}
28
42
29before_install:
43before_install:
44+ - HC=${CC}
45+ - HCPKG=${HC/ghc/ghc-pkg}
30− - unset CC
46+ - unset CC
47+ - ROOTDIR=$(pwd)
48+ - mkdir -p $HOME/.local/bin
31− - export PATH=/opt/ghc/$GHCVER/bin:/opt/cabal/$CABALVER/bin:$PATH
49+ - "PATH=/opt/ghc/bin:/opt/ghc-ppa-tools/bin:$HOME/local/bin:$PATH"
50+ - HCNUMVER=$(( $(${HC} --numeric-version|sed -E 's/([0-9]+)\.([0-9]+)\.([0-9]+).*/\1 * 10000 + \2 * 100 + \3/') ))
51+ - echo $HCNUMVER
32
52
33install:
53install:
34− - cabal --version
54+ - cabal --version
35− - echo "$(ghc --version) [$(ghc --print-project-git-commit-id 2> /dev/null || echo '?')]"
55+ - echo "$(${HC} --version) [$(${HC} --print-project-git-commit-id 2> /dev/null || echo '?')]"
36− - if [ -f $HOME/.cabal/packages/hackage.haskell.org/00-index.tar.gz ];
56+ - BENCH=${BENCH---enable-benchmarks}
37− then
57+ - TEST=${TEST---enable-tests}
38− zcat $HOME/.cabal/packages/hackage.haskell.org/00-index.tar.gz >
58+ - HADDOCK=${HADDOCK-true}
39− $HOME/.cabal/packages/hackage.haskell.org/00-index.tar;
59+ - INSTALLED=${INSTALLED-true}
40− fi
60+ - GHCHEAD=${GHCHEAD-false}
41− - travis_retry cabal update -v
61+ - travis_retry cabal update -v
42− - sed -i 's/^jobs:/-- jobs:/' ${HOME}/.cabal/config
62+ - "sed -i.bak 's/^jobs:/-- jobs:/' ${HOME}/.cabal/config"
43− - cabal install --only-dependencies --enable-tests --enable-benchmarks --dry -v > installplan.txt
63+ - rm -fv cabal.project cabal.project.local
44− - sed -i -e '1,/^Resolving /d' installplan.txt; cat installplan.txt
64+ - grep -Ev -- '^\s*--' ${HOME}/.cabal/config | grep -Ev '^\s*$'
45−
65+ - "printf 'packages: \".\"\\n' > cabal.project"
46−# check whether current requested install-plan matches cached package-db snapshot
66+ - cat cabal.project
47− - if diff -u installplan.txt $HOME/.cabsnap/installplan.txt;
67+ - if [ -f "./configure.ac" ]; then
48− then
68+ (cd "." && autoreconf -i);
49− echo "cabal build-cache HIT";
69+ fi
50− rm -rfv .ghc;
70+ - rm -f cabal.project.freeze
51− cp -a $HOME/.cabsnap/ghc $HOME/.ghc;
71+ - cabal new-build -w ${HC} ${TEST} ${BENCH} --project-file="cabal.project" --dep -j2 all
52− cp -a $HOME/.cabsnap/lib $HOME/.cabsnap/share $HOME/.cabsnap/bin $HOME/.cabal/;
72+ - cabal new-build -w ${HC} --disable-tests --disable-benchmarks --project-file="cabal.project" --dep -j2 all
53− else
73+ - rm -rf .ghc.environment.* "."/dist
54− echo "cabal build-cache MISS";
74+ - DISTDIR=$(mktemp -d /tmp/dist-test.XXXX)
55− rm -rf $HOME/.cabsnap;
56− mkdir -p $HOME/.ghc $HOME/.cabal/lib $HOME/.cabal/share $HOME/.cabal/bin;
57− cabal install --only-dependencies --enable-tests --enable-benchmarks;
58− fi
59−
60−# snapshot package-db on cache miss
61− - if [ ! -d $HOME/.cabsnap ];
62− then
63− echo "snapshotting package-db to build-cache";
64− mkdir $HOME/.cabsnap;
65− cp -a $HOME/.ghc $HOME/.cabsnap/ghc;
66− cp -a $HOME/.cabal/lib $HOME/.cabal/share $HOME/.cabal/bin installplan.txt $HOME/.cabsnap/;
67− fi
68
75
69# Here starts the actual work to be performed for the package under test;
76# Here starts the actual work to be performed for the package under test;
70# any command which exits with a non-zero exit code causes the build to fail.
77# any command which exits with a non-zero exit code causes the build to fail.
71script:
78script:
72− - if [ -f configure.ac ]; then autoreconf -i; fi
79+ # test that source-distributions can be generated
73− - cabal configure --enable-tests --enable-benchmarks -v2 # -v2 provides useful information for debugging
80+ - (cd "." && cabal sdist)
81+ - mv "."/dist/hjsonpointer-*.tar.gz ${DISTDIR}/
82+ - cd ${DISTDIR} || false
83+ - find . -maxdepth 1 -name '*.tar.gz' -exec tar -xvf '{}' \;
84+ - "printf 'packages: hjsonpointer-*/*.cabal\\n' > cabal.project"
85+ - cat cabal.project
74− - cabal build # this builds all libraries and executables (including tests/benchmarks)
86+ # this builds all libraries and executables (without tests/benchmarks)
75− - cabal test
87+ - cabal new-build -w ${HC} --disable-tests --disable-benchmarks all
76− - cabal sdist # tests that a source-distribution can be generated
88+
89+ # Build with installed constraints for packages in global-db
90+ - if $INSTALLED; then echo cabal new-build -w ${HC} --disable-tests --disable-benchmarks $(${HCPKG} list --global --simple-output --names-only | sed 's/\([a-zA-Z0-9-]\{1,\}\) */--constraint="\1 installed" /g') all | sh; else echo "Not building with installed constraints"; fi
91+
92+ # build & run tests, build benchmarks
93+ - cabal new-build -w ${HC} ${TEST} ${BENCH} all
94+ - if [ "x$TEST" = "x--enable-tests" ]; then cabal new-test -w ${HC} ${TEST} ${BENCH} all; fi
95+
96+ # cabal check
97+ # - (cd hjsonpointer-* && cabal check)
77
98
78−# Check that the resulting source distribution can be built & installed.
99+ # haddock
79−# If there are no other `.tar.gz` files in `dist`, this can be even simpler:
100+ - rm -rf ./dist-newstyle
80−# `cabal install --force-reinstalls dist/*-*.tar.gz`
101+ - if $HADDOCK; then cabal new-haddock -w ${HC} ${TEST} ${BENCH} all; else echo "Skipping haddock generation";fi
81− - SRC_TGZ=$(cabal info . | awk '{print $2;exit}').tar.gz &&
82− (cd dist && cabal install --force-reinstalls "$SRC_TGZ")
83
102
103+# REGENDATA ["hjsonpointer.cabal"]
84# EOF
104# EOF
Example.hs+7−8
@@ -1,27 +1,26 @@
1−{-# LANGUAGE OverloadedStrings #-}
2
1
3module Example where
2module Example where
4
3
5−import Control.Monad (unless)
4+import Control.Monad (unless)
6import Data.Aeson
5import Data.Aeson
7−import qualified Data.Aeson.Pointer as P
6+import qualified JSONPointer as JP
8
7
9main :: IO ()
8main :: IO ()
10main = do
9main = do
11 -- JSON Pointers must either be empty or start with a /.
10 -- JSON Pointers must either be empty or start with a /.
12− pntr1 <- case P.unescape "/foo/0" of
11+ pntr1 <- case JP.unescape "/foo/0" of
13 Left _ -> error "Failed to construct JSON Pointer."
12 Left _ -> error "Failed to construct JSON Pointer."
14 Right pntr -> return pntr
13 Right pntr -> return pntr
15
14
16 -- We can also write JSON Pointers in Haskell.
15 -- We can also write JSON Pointers in Haskell.
17− let pntr2 = P.Pointer [P.Token "/"]
16+ let pntr2 = JP.Pointer [JP.Token "/"]
18 -- When we do this we don't have to escape / or ~ characters
17 -- When we do this we don't have to escape / or ~ characters
19 -- (as ~1 and ~0 respectively) like we do in an escaped JSON
18 -- (as ~1 and ~0 respectively) like we do in an escaped JSON
20 -- Pointer string.
19 -- Pointer string.
21− unless (P.unescape "/~1" == Right pntr2) (error "ohno!")
20+ unless (JP.unescape "/~1" == Right pntr2) (error "ohno!")
22
21
23− print (P.resolve pntr1 document)
22+ print (JP.resolve pntr1 document)
24− print (P.resolve pntr2 document)
23+ print (JP.resolve pntr2 document)
25
24
26 where
25 where
27 document :: Value
26 document :: Value
README.md+7−9
@@ -7,30 +7,28 @@
7# Example
7# Example
8
8
9```haskell
9```haskell
10−{-# LANGUAGE OverloadedStrings #-}
11−
12module Example where
10module Example where
13
11
14−import Control.Monad (unless)
12+import Control.Monad (unless)
15import Data.Aeson
13import Data.Aeson
16−import qualified Data.Aeson.Pointer as P
14+import qualified JSONPointer as JP
17
15
18main :: IO ()
16main :: IO ()
19main = do
17main = do
20 -- JSON Pointers must either be empty or start with a /.
18 -- JSON Pointers must either be empty or start with a /.
21− pntr1 <- case P.unescape "/foo/0" of
19+ pntr1 <- case JP.unescape "/foo/0" of
22 Left _ -> error "Failed to construct JSON Pointer."
20 Left _ -> error "Failed to construct JSON Pointer."
23 Right pntr -> return pntr
21 Right pntr -> return pntr
24
22
25 -- We can also write JSON Pointers in Haskell.
23 -- We can also write JSON Pointers in Haskell.
26− let pntr2 = P.Pointer [P.Token "/"]
24+ let pntr2 = JP.Pointer [JP.Token "/"]
27 -- When we do this we don't have to escape / or ~ characters
25 -- When we do this we don't have to escape / or ~ characters
28 -- (as ~1 and ~0 respectively) like we do in an escaped JSON
26 -- (as ~1 and ~0 respectively) like we do in an escaped JSON
29 -- Pointer string.
27 -- Pointer string.
30− unless (P.unescape "/~1" == Right pntr2) (error "ohno!")
28+ unless (JP.unescape "/~1" == Right pntr2) (error "ohno!")
31
29
32− print (P.resolve pntr1 document)
30+ print (JP.resolve pntr1 document)
33− print (P.resolve pntr2 document)
31+ print (JP.resolve pntr2 document)
34
32
35 where
33 where
36 document :: Value
34 document :: Value
changelog.md+40
@@ -1,3 +1,43 @@
1+# 1.5.0
2+
3++ Drop QuickCheck dependency (thanks @mmhat !).
4+
5+# 1.4.0
6+
7++ Enable GHC 8.4 (thanks @4e6 !).
8++ Drop GHC 7.10.
9+
10+# 1.3.0
11+
12++ Test with GHC 8.2. Drop GHC 7.8.
13+
14+# 1.2.0
15+
16++ Track upper bounds in a Hackage revision instead of source code.
17+
18+# 1.1.1
19+
20++ Bump aeson.
21+
22+# 1.1.0.2
23+
24++ Raise test dep upper bounds.
25+
26+# 1.1.0.1
27+
28++ Bump `vector`.
29+
30+# 1.1.0.0
31+
32++ Rename the module to `JSONPointer`.
33++ Bump aeson.
34++ Derive `Semigroup` for `Pointer`.
35++ Reorganize code.
36+
37+# 1.0.0.2
38+
39++ Bump hspec.
40+
1# 1.0.0.1
41# 1.0.0.1
2
42
3+ Remove HUnit dep.
43+ Remove HUnit dep.
hjsonpointer.cabal+13−18
@@ -1,5 +1,5 @@
1name: hjsonpointer
1name: hjsonpointer
2−version: 1.0.0.1
2+version: 1.5.0
3synopsis: JSON Pointer library
3synopsis: JSON Pointer library
4homepage: https://github.com/seagreen/hjsonpointer
4homepage: https://github.com/seagreen/hjsonpointer
5license: MIT
5license: MIT
@@ -9,6 +9,8 @@ maintainer: ian@housejeffries.com
9category: Data
9category: Data
10build-type: Simple
10build-type: Simple
11cabal-version: >=1.10
11cabal-version: >=1.10
12+-- Rerun multi-ghc-travis (executable make-travis-yml-2) after changing:
13+Tested-With: GHC == 8.0.2, GHC == 8.2.2, GHC == 8.4.1
12extra-source-files:
14extra-source-files:
13 changelog.md
15 changelog.md
14 README.md
16 README.md
@@ -17,21 +19,17 @@ library
17 hs-source-dirs:
19 hs-source-dirs:
18 src
20 src
19 default-language: Haskell2010
21 default-language: Haskell2010
20− default-extensions:
21− OverloadedStrings
22− other-extensions:
23− GeneralizedNewtypeDeriving
24 ghc-options:
22 ghc-options:
25 -Wall
23 -Wall
26 exposed-modules:
24 exposed-modules:
27− Data.Aeson.Pointer
25+ JSONPointer
28 build-depends:
26 build-depends:
27+ base >= 4.9 && < 5
29− aeson >= 0.7 && < 1.1
28+ , aeson >= 0.7
30− , base >= 4.6 && < 4.10
29+ , hashable >= 1.2
31− , QuickCheck >= 2.8 && < 2.10
32− , unordered-containers >= 0.2 && < 0.3
30+ , unordered-containers >= 0.2
33− , text >= 1.2 && < 1.3
31+ , text >= 1.2
34− , vector >= 0.10 && < 0.12
32+ , vector >= 0.10
35
33
36test-suite unit
34test-suite unit
37 hs-source-dirs:
35 hs-source-dirs:
@@ -44,18 +42,15 @@ test-suite unit
44 OverloadedStrings
42 OverloadedStrings
45 ghc-options:
43 ghc-options:
46 -Wall
44 -Wall
45+ -fno-warn-orphans
47 other-modules: Example
46 other-modules: Example
48 build-depends:
47 build-depends:
49 aeson
48 aeson
50 , base
49 , base
51 , hjsonpointer
50 , hjsonpointer
51+ , hspec >= 2.2
52+ , http-types >= 0.8
52 , QuickCheck
53 , QuickCheck
53 , unordered-containers
54 , unordered-containers
54 , text
55 , text
55 , vector
56 , vector
56− , hspec >= 2.2 && < 2.3
57− , http-types >= 0.8 && < 0.10
58−
59−source-repository head
60− type: git
61− location: git://github.com/seagreen/hjsonpointer.git
shell.nixnew file+9
@@ -0,0 +1,9 @@
1+{ghc}:
2+with (import <nixpkgs> {});
3+
4+haskell.lib.buildStackProject {
5+ name = "none";
6+ inherit ghc;
7+ buildInputs = [
8+ ];
9+}
src/Data/Aeson/Pointer.hs → src/JSONPointer.hsrenamed+76−31
@@ -1,24 +1,44 @@
1+{-# LANGUAGE DeriveGeneric #-}
1{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3+{-# LANGUAGE OverloadedStrings #-}
2
4
3−module Data.Aeson.Pointer where
5+module JSONPointer where
4
6
5import Control.Monad (when)
7import Control.Monad (when)
6import Data.Aeson
8import Data.Aeson
7−import qualified Data.HashMap.Strict as H
9+import qualified Data.Hashable as HA
10+import qualified Data.HashMap.Strict as HM
11+import Data.Semigroup (Semigroup)
8import Data.Text (Text)
12import Data.Text (Text)
9import qualified Data.Text as T
13import qualified Data.Text as T
10import qualified Data.Vector as V
14import qualified Data.Vector as V
11−import Test.QuickCheck
15+import GHC.Generics (Generic)
12import Text.Read (readMaybe)
16import Text.Read (readMaybe)
13
17
14−-- For GHCs before 7.10:
18+--------------------------------------------------
15−import Control.Applicative
19+-- * Resolution
16−import Data.Monoid
20+--------------------------------------------------
17−import Data.Traversable
21+
22+data ResolutionError
23+ = ObjectLookupFailed
24+ | ArrayIndexInvalid
25+ | ArrayElemNotFound
26+ | ExpectedObjectOrArray
27+ deriving (Eq, Show)
28+
29+resolve :: Pointer -> Value -> Either ResolutionError Value
30+resolve (Pointer []) v = Right v
31+resolve (Pointer (t:ts)) v = resolveToken t v >>= resolve (Pointer ts)
32+
33+--------------------------------------------------
34+-- * Main types and escaping
35+--------------------------------------------------
18
36
19newtype Pointer
37newtype Pointer
20 = Pointer { _unPointer :: [Token] }
38 = Pointer { _unPointer :: [Token] }
21− deriving (Eq, Show, Monoid, Arbitrary)
39+ deriving (Eq, Show, Semigroup, Monoid, Generic)
40+
41+instance HA.Hashable Pointer
22
42
23instance FromJSON Pointer where
43instance FromJSON Pointer where
24 parseJSON = withText "JSON Pointer" $ \t ->
44 parseJSON = withText "JSON Pointer" $ \t ->
@@ -32,13 +52,19 @@ instance ToJSON Pointer where
32-- | We don't try to distinguish between integer tokens and string
52-- | We don't try to distinguish between integer tokens and string
33-- tokens since all tokens start as strings, and all tokens can
53-- tokens since all tokens start as strings, and all tokens can
34-- be used to resolve JSON objects.
54-- be used to resolve JSON objects.
55+--
56+-- Since these are unescaped you can write @"/"@ and @"~"@ normally.
57+-- (e.g. if you're referencing a key such as @"abc/123"@, go ahead
58+-- and write that exactly.
35newtype Token
59newtype Token
36 = Token { _unToken :: Text }
60 = Token { _unToken :: Text }
37− deriving (Eq, Show)
61+ deriving (Eq, Show, Generic)
38
62
39−instance Arbitrary Token where
63+instance HA.Hashable Token
40− arbitrary = Token . T.pack <$> arbitrary
41
64
65+-- | This escapes @"/"@ (because it's the token separator character).
66+--
67+-- It also escapes @"~"@ (because it's the escape character).
42escape :: Pointer -> Text
68escape :: Pointer -> Text
43escape (Pointer []) = ""
69escape (Pointer []) = ""
44escape (Pointer ts) =
70escape (Pointer ts) =
@@ -47,15 +73,12 @@ escape (Pointer ts) =
47 . fmap (T.replace "/" "~1" . T.replace "~" "~0" . _unToken)
73 . fmap (T.replace "/" "~1" . T.replace "~" "~0" . _unToken)
48 $ ts
74 $ ts
49
75
50−-- * Unescaping
51−
52data FormatError
76data FormatError
53 = InvalidFirstChar
77 = InvalidFirstChar
54 -- ^ JSON Pointers must either be empty or start with a @/@.
78 -- ^ JSON Pointers must either be empty or start with a @/@.
55 | UnescapedTilde
79 | UnescapedTilde
56 deriving (Eq, Show)
80 deriving (Eq, Show)
57
81
58−
59-- | JSON Pointers must either be empty or start with a @/@. This means
82-- | JSON Pointers must either be empty or start with a @/@. This means
60-- that if you're turning a URI Fragment into a JSON Pointer you must
83-- that if you're turning a URI Fragment into a JSON Pointer you must
61-- drop the initial @#@.
84-- drop the initial @#@.
@@ -77,7 +100,40 @@ unescape txt =
77 Nothing -> Left UnescapedTilde
100 Nothing -> Left UnescapedTilde
78 Just tok -> Right tok
101 Just tok -> Right tok
79
102
103+--------------------------------------------------
104+-- * Wrapper Types
105+--
106+-- $ These aren't used by the rest of the library
107+-- (as explained in the docs for 'Token').
108+--
109+-- However, they might be useful if you need to distinguish JSON Pointer
110+-- tokens from plain 'Text' or 'Int' without losing information by
111+-- converting to 'Token'.
112+--------------------------------------------------
113+
114+-- | A glorified @type@ alias. If you need to do JSON Pointer operations
115+-- you're looking for 'Token' instead.
116+--
117+-- NOTE: Unlike 'Token' this is escaped.
118+newtype Key
119+ = Key { _unKey :: Text }
120+ deriving (Eq, Show, Generic)
121+
122+instance HA.Hashable Key
123+
124+-- | A glorified @type@ alias. If you need to do JSON Pointer operations
125+-- you're looking for 'Token' instead.
126+newtype Index
127+ = Index { _unIndex :: Int }
128+ deriving (Eq, Show, Generic)
129+
130+instance HA.Hashable Index
131+
132+--------------------------------------------------
133+-- * Internals
134+--------------------------------------------------
135+
80−-- | For internal use by 'unescape'.
136+-- | For internal use (by 'unescape').
81unescapeToken :: Text -> Maybe Token
137unescapeToken :: Text -> Maybe Token
82unescapeToken t
138unescapeToken t
83 | not (isValid t) = Nothing
139 | not (isValid t) = Nothing
@@ -93,21 +149,10 @@ unescapeToken t
93 replace :: Text -> Text
149 replace :: Text -> Text
94 replace = T.replace "~0" "~" . T.replace "~1" "/"
150 replace = T.replace "~0" "~" . T.replace "~1" "/"
95
151
96−-- * Resolution
152+-- | For internal use (by 'resolve').
97−
153+--
98−data ResolutionError
99− = ObjectLookupFailed
100− | ArrayIndexInvalid
101− | ArrayElemNotFound
102− | ExpectedObjectOrArray
103− deriving (Eq, Show)
104−
105−resolve :: Pointer -> Value -> Either ResolutionError Value
106−resolve (Pointer []) v = Right v
107−resolve (Pointer (t:ts)) v = resolveToken t v >>= resolve (Pointer ts)
108−
109−-- | For internal use (or specialized applications that don't want to
154+-- Might also be useful for specialized applications that don't
110−-- resolve an entire pointer at once).
155+-- want to resolve an entire pointer at once.
111resolveToken :: Token -> Value -> Either ResolutionError Value
156resolveToken :: Token -> Value -> Either ResolutionError Value
112resolveToken tok (Array vs) =
157resolveToken tok (Array vs) =
113 case readMaybe . T.unpack . _unToken $ tok of
158 case readMaybe . T.unpack . _unToken $ tok of
@@ -118,7 +163,7 @@ resolveToken tok (Array vs) =
118 Nothing -> Left ArrayElemNotFound
163 Nothing -> Left ArrayElemNotFound
119 Just res -> Right res
164 Just res -> Right res
120resolveToken tok (Object h) =
165resolveToken tok (Object h) =
121− case H.lookup (_unToken tok) h of
166+ case HM.lookup (_unToken tok) h of
122 Nothing -> Left ObjectLookupFailed
167 Nothing -> Left ObjectLookupFailed
123 Just res -> Right res
168 Just res -> Right res
124resolveToken _ _ = Left ExpectedObjectOrArray
169resolveToken _ _ = Left ExpectedObjectOrArray
stack.yamlnew file+2
@@ -0,0 +1,2 @@
1+resolver: lts-12.10
2+pvp-bounds: upper-revision
test/Unit.hs+20−16
@@ -1,25 +1,29 @@
1+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2+{-# LANGUAGE StandaloneDeriving #-}
1
3
2module Main where
4module Main where
3
5
4−import Control.Arrow
5import Data.Aeson
6import Data.Aeson
6−import qualified Data.Aeson.Pointer as P
7+import Data.Bifunctor (first)
7−import Data.Text (Text)
8−import qualified Data.Text as T
9−import Data.Text.Encoding
10−import qualified Data.Vector as V
11−import Network.HTTP.Types.URI (urlDecode)
12−
13−import Test.Hspec
14−import Test.QuickCheck (property)
15−
16−-- For GHCs before 7.10:
17−import Control.Applicative
18import Data.Foldable
8import Data.Foldable
19import Data.Monoid
9import Data.Monoid
10+import Data.Text (Text)
11+import qualified Data.Text as T
12+import Data.Text.Encoding (decodeUtf8, encodeUtf8)
13+import qualified Data.Vector as V
14+import qualified JSONPointer as JP
15+import Network.HTTP.Types.URI (urlDecode)
16+
17+import Test.Hspec
18+import Test.QuickCheck (Arbitrary(..), property)
20
19
21import qualified Example
20import qualified Example
22
21
22+deriving instance Arbitrary JP.Pointer
23+
24+instance Arbitrary JP.Token where
25+ arbitrary = JP.Token . T.pack <$> arbitrary
26+
23main :: IO ()
27main :: IO ()
24main = hspec $ do
28main = hspec $ do
25 describe "example" $ do
29 describe "example" $ do
@@ -29,7 +33,7 @@ main = hspec $ do
29 it "can be represented in a JSON string value" jsonString
33 it "can be represented in a JSON string value" jsonString
30 it "can be represented in a URI fragment identifier" uriFragment
34 it "can be represented in a URI fragment identifier" uriFragment
31
35
32−roundtrip :: P.Pointer -> Bool
36+roundtrip :: JP.Pointer -> Bool
33roundtrip a = Just a == decode (encode a)
37roundtrip a = Just a == decode (encode a)
34
38
35jsonString :: Expectation
39jsonString :: Expectation
@@ -69,9 +73,9 @@ uriFragment = traverse_ resolvesTo . fmap (first decodeFragment) $
69
73
70resolvesTo :: (Text, Value) -> Expectation
74resolvesTo :: (Text, Value) -> Expectation
71resolvesTo (t, expected) =
75resolvesTo (t, expected) =
72− case P.unescape t of
76+ case JP.unescape t of
73 Left e -> expectationFailure (show e <> " error for pointer: " <> show t)
77 Left e -> expectationFailure (show e <> " error for pointer: " <> show t)
74− Right p -> P.resolve p specExample `shouldBe` Right expected
78+ Right p -> JP.resolve p specExample `shouldBe` Right expected
75
79
76specExample :: Value
80specExample :: Value
77specExample = object
81specExample = object