git.iko.soy/typeable/hjsonpointer
on 950a712 help sign in
.travis.yml+59−79
since splitsnapshots
unifiedsplit
@@ -1,104 +1,84 @@
1−# This Travis job script has been generated by a script via
1+# This file has been generated -- see https://github.com/hvr/multi-ghc-travis
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−
7−# NOTE: After generating comment out `cabal check`, it requires a description field.
3+# Manually modified to remove cabal check. I don't want a description field.
8
4
9language: c
5language: c
10sudo: false
6sudo: false
11
7
12−git:
13− submodules: false # whether to recursively clone submodules
14−
15cache:
8cache:
16 directories:
9 directories:
10+ - $HOME/.cabsnap
17 - $HOME/.cabal/packages
11 - $HOME/.cabal/packages
18− - $HOME/.cabal/store
19
12
20before_cache:
13before_cache:
21 - rm -fv $HOME/.cabal/packages/hackage.haskell.org/build-reports.log
14 - rm -fv $HOME/.cabal/packages/hackage.haskell.org/build-reports.log
22− # remove files that are regenerated by 'cabal update'
23− - rm -fv $HOME/.cabal/packages/hackage.haskell.org/00-index.*
15+ - rm -fv $HOME/.cabal/packages/hackage.haskell.org/00-index.tar
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
30
16
31matrix:
17matrix:
32 include:
18 include:
33− - compiler: "ghc-8.0.2"
19+ - env: CABALVER=1.18 GHCVER=7.8.4
34− # env: TEST=--disable-tests BENCH=--disable-benchmarks
20+ compiler: ": #GHC 7.8.4"
35− addons: {apt: {packages: [ghc-ppa-tools,cabal-install-head,ghc-8.0.2], sources: [hvr-ghc]}}
21+ addons: {apt: {packages: [cabal-install-1.18,ghc-7.8.4], sources: [hvr-ghc]}}
36− - compiler: "ghc-8.2.2"
22+ - env: CABALVER=1.22 GHCVER=7.10.2
37− # env: TEST=--disable-tests BENCH=--disable-benchmarks
23+ compiler: ": #GHC 7.10.2"
38− addons: {apt: {packages: [ghc-ppa-tools,cabal-install-head,ghc-8.2.2], sources: [hvr-ghc]}}
24+ addons: {apt: {packages: [cabal-install-1.22,ghc-7.10.2], sources: [hvr-ghc]}}
39− - compiler: "ghc-8.4.1"
25+ - env: CABALVER=1.24 GHCVER=8.0.1
40− # env: TEST=--disable-tests BENCH=--disable-benchmarks
26+ compiler: ": #GHC 8.0.1"
41− addons: {apt: {packages: [ghc-ppa-tools,cabal-install-head,ghc-8.4.1], sources: [hvr-ghc]}}
27+ addons: {apt: {packages: [cabal-install-1.24,ghc-8.0.1], sources: [hvr-ghc]}}
42
28
43before_install:
29before_install:
44− - HC=${CC}
45− - HCPKG=${HC/ghc/ghc-pkg}
46− - unset CC
30+ - unset CC
47− - ROOTDIR=$(pwd)
48− - mkdir -p $HOME/.local/bin
49− - "PATH=/opt/ghc/bin:/opt/ghc-ppa-tools/bin:$HOME/local/bin:$PATH"
31+ - export PATH=/opt/ghc/$GHCVER/bin:/opt/cabal/$CABALVER/bin:$PATH
50− - HCNUMVER=$(( $(${HC} --numeric-version|sed -E 's/([0-9]+)\.([0-9]+)\.([0-9]+).*/\1 * 10000 + \2 * 100 + \3/') ))
51− - echo $HCNUMVER
52
32
53install:
33install:
54− - cabal --version
34+ - cabal --version
55− - echo "$(${HC} --version) [$(${HC} --print-project-git-commit-id 2> /dev/null || echo '?')]"
35+ - echo "$(ghc --version) [$(ghc --print-project-git-commit-id 2> /dev/null || echo '?')]"
56− - BENCH=${BENCH---enable-benchmarks}
36+ - if [ -f $HOME/.cabal/packages/hackage.haskell.org/00-index.tar.gz ];
57− - TEST=${TEST---enable-tests}
37+ then
58− - HADDOCK=${HADDOCK-true}
38+ zcat $HOME/.cabal/packages/hackage.haskell.org/00-index.tar.gz >
59− - INSTALLED=${INSTALLED-true}
39+ $HOME/.cabal/packages/hackage.haskell.org/00-index.tar;
60− - GHCHEAD=${GHCHEAD-false}
40+ fi
61− - travis_retry cabal update -v
41+ - travis_retry cabal update -v
62− - "sed -i.bak 's/^jobs:/-- jobs:/' ${HOME}/.cabal/config"
42+ - sed -i 's/^jobs:/-- jobs:/' ${HOME}/.cabal/config
63− - rm -fv cabal.project cabal.project.local
43+ - cabal install --only-dependencies --enable-tests --enable-benchmarks --dry -v > installplan.txt
64− - grep -Ev -- '^\s*--' ${HOME}/.cabal/config | grep -Ev '^\s*$'
44+ - sed -i -e '1,/^Resolving /d' installplan.txt; cat installplan.txt
65− - "printf 'packages: \".\"\\n' > cabal.project"
45+
66− - cat cabal.project
46+# check whether current requested install-plan matches cached package-db snapshot
67− - if [ -f "./configure.ac" ]; then
47+ - if diff -u installplan.txt $HOME/.cabsnap/installplan.txt;
68− (cd "." && autoreconf -i);
48+ then
69− fi
49+ echo "cabal build-cache HIT";
70− - rm -f cabal.project.freeze
50+ rm -rfv .ghc;
71− - cabal new-build -w ${HC} ${TEST} ${BENCH} --project-file="cabal.project" --dep -j2 all
51+ cp -a $HOME/.cabsnap/ghc $HOME/.ghc;
72− - cabal new-build -w ${HC} --disable-tests --disable-benchmarks --project-file="cabal.project" --dep -j2 all
52+ cp -a $HOME/.cabsnap/lib $HOME/.cabsnap/share $HOME/.cabsnap/bin $HOME/.cabal/;
73− - rm -rf .ghc.environment.* "."/dist
53+ else
74− - DISTDIR=$(mktemp -d /tmp/dist-test.XXXX)
54+ echo "cabal build-cache MISS";
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
75
68
76# Here starts the actual work to be performed for the package under test;
69# Here starts the actual work to be performed for the package under test;
77# any command which exits with a non-zero exit code causes the build to fail.
70# any command which exits with a non-zero exit code causes the build to fail.
78script:
71script:
79− # test that source-distributions can be generated
72+ - if [ -f configure.ac ]; then autoreconf -i; fi
80− - (cd "." && cabal sdist)
73+ - cabal configure --enable-tests --enable-benchmarks -v2 # -v2 provides useful information for debugging
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
86− # this builds all libraries and executables (without tests/benchmarks)
74+ - cabal build # this builds all libraries and executables (including tests/benchmarks)
87− - cabal new-build -w ${HC} --disable-tests --disable-benchmarks all
75+ - cabal test
88−
76+ - cabal sdist # tests that a source-distribution can be generated
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)
98
77
99− # haddock
78+# Check that the resulting source distribution can be built & installed.
100− - rm -rf ./dist-newstyle
79+# If there are no other `.tar.gz` files in `dist`, this can be even simpler:
101− - if $HADDOCK; then cabal new-haddock -w ${HC} ${TEST} ${BENCH} all; else echo "Skipping haddock generation";fi
80+# `cabal install --force-reinstalls dist/*-*.tar.gz`
81+ - SRC_TGZ=$(cabal info . | awk '{print $2;exit}').tar.gz &&
82+ (cd dist && cabal install --force-reinstalls "$SRC_TGZ")
102
83
103−# REGENDATA ["hjsonpointer.cabal"]
104# EOF
84# EOF
Example.hs+8−7
@@ -1,26 +1,27 @@
1+{-# LANGUAGE OverloadedStrings #-}
1
2
2module Example where
3module Example where
3
4
4−import Control.Monad (unless)
5+import Control.Monad (unless)
5import Data.Aeson
6import Data.Aeson
6−import qualified JSONPointer as JP
7+import qualified Data.Aeson.Pointer as P
7
8
8main :: IO ()
9main :: IO ()
9main = do
10main = do
10 -- JSON Pointers must either be empty or start with a /.
11 -- JSON Pointers must either be empty or start with a /.
11− pntr1 <- case JP.unescape "/foo/0" of
12+ pntr1 <- case P.unescape "/foo/0" of
12 Left _ -> error "Failed to construct JSON Pointer."
13 Left _ -> error "Failed to construct JSON Pointer."
13 Right pntr -> return pntr
14 Right pntr -> return pntr
14
15
15 -- We can also write JSON Pointers in Haskell.
16 -- We can also write JSON Pointers in Haskell.
16− let pntr2 = JP.Pointer [JP.Token "/"]
17+ let pntr2 = P.Pointer [P.Token "/"]
17 -- When we do this we don't have to escape / or ~ characters
18 -- When we do this we don't have to escape / or ~ characters
18 -- (as ~1 and ~0 respectively) like we do in an escaped JSON
19 -- (as ~1 and ~0 respectively) like we do in an escaped JSON
19 -- Pointer string.
20 -- Pointer string.
20− unless (JP.unescape "/~1" == Right pntr2) (error "ohno!")
21+ unless (P.unescape "/~1" == Right pntr2) (error "ohno!")
21
22
22− print (JP.resolve pntr1 document)
23+ print (P.resolve pntr1 document)
23− print (JP.resolve pntr2 document)
24+ print (P.resolve pntr2 document)
24
25
25 where
26 where
26 document :: Value
27 document :: Value
MIT-LICENSE.txt+1−1
@@ -1,5 +1,5 @@
1The MIT License (MIT)
1The MIT License (MIT)
2−Copyright (c) 2016 Ian Grant Jeffries
2+Copyright (c) 2015 Ian Grant Jeffries
3
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
5of this software and associated documentation files (the "Software"), to deal
README.md+9−7
@@ -7,28 +7,30 @@
7# Example
7# Example
8
8
9```haskell
9```haskell
10+{-# LANGUAGE OverloadedStrings #-}
11+
10module Example where
12module Example where
11
13
12−import Control.Monad (unless)
14+import Control.Monad (unless)
13import Data.Aeson
15import Data.Aeson
14−import qualified JSONPointer as JP
16+import qualified Data.Aeson.Pointer as P
15
17
16main :: IO ()
18main :: IO ()
17main = do
19main = do
18 -- JSON Pointers must either be empty or start with a /.
20 -- JSON Pointers must either be empty or start with a /.
19− pntr1 <- case JP.unescape "/foo/0" of
21+ pntr1 <- case P.unescape "/foo/0" of
20 Left _ -> error "Failed to construct JSON Pointer."
22 Left _ -> error "Failed to construct JSON Pointer."
21 Right pntr -> return pntr
23 Right pntr -> return pntr
22
24
23 -- We can also write JSON Pointers in Haskell.
25 -- We can also write JSON Pointers in Haskell.
24− let pntr2 = JP.Pointer [JP.Token "/"]
26+ let pntr2 = P.Pointer [P.Token "/"]
25 -- When we do this we don't have to escape / or ~ characters
27 -- When we do this we don't have to escape / or ~ characters
26 -- (as ~1 and ~0 respectively) like we do in an escaped JSON
28 -- (as ~1 and ~0 respectively) like we do in an escaped JSON
27 -- Pointer string.
29 -- Pointer string.
28− unless (JP.unescape "/~1" == Right pntr2) (error "ohno!")
30+ unless (P.unescape "/~1" == Right pntr2) (error "ohno!")
29
31
30− print (JP.resolve pntr1 document)
32+ print (P.resolve pntr1 document)
31− print (JP.resolve pntr2 document)
33+ print (P.resolve pntr2 document)
32
34
33 where
35 where
34 document :: Value
36 document :: Value
changelog.md−44
@@ -1,47 +1,3 @@
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−
41−# 1.0.0.1
42−
43−+ Remove HUnit dep.
44−
45# 1.0.0.0
1# 1.0.0.0
46
2
47+ Bump aeson.
3+ Bump aeson.
hjsonpointer.cabal+22−15
@@ -1,5 +1,5 @@
1name: hjsonpointer
1name: hjsonpointer
2−version: 1.5.0
2+version: 1.0.0.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,8 +9,7 @@ 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:
12+tested-with: GHC == 7.8.4, GHC == 7.10.3, GHC == 8.0.1
13−Tested-With: GHC == 8.0.2, GHC == 8.2.2, GHC == 8.4.1
14extra-source-files:
13extra-source-files:
15 changelog.md
14 changelog.md
16 README.md
15 README.md
@@ -19,38 +18,46 @@ library
19 hs-source-dirs:
18 hs-source-dirs:
20 src
19 src
21 default-language: Haskell2010
20 default-language: Haskell2010
21+ default-extensions:
22+ OverloadedStrings
23+ other-extensions:
24+ GeneralizedNewtypeDeriving
22 ghc-options:
25 ghc-options:
23 -Wall
26 -Wall
24 exposed-modules:
27 exposed-modules:
25− JSONPointer
28+ Data.Aeson.Pointer
26 build-depends:
29 build-depends:
27− base >= 4.9 && < 5
28− , aeson >= 0.7
30+ aeson >= 0.7 && < 1.1
29− , hashable >= 1.2
31+ , base >= 4.6 && < 4.10
32+ , QuickCheck >= 2.8 && < 2.10
30− , unordered-containers >= 0.2
33+ , unordered-containers >= 0.2 && < 0.3
31− , text >= 1.2
34+ , text >= 1.2 && < 1.3
32− , vector >= 0.10
35+ , vector >= 0.10 && < 0.12
33
36
34test-suite unit
37test-suite unit
35 hs-source-dirs:
38 hs-source-dirs:
36 test
39 test
37 ./
40 ./
38− main-is: Unit.hs
39 default-language: Haskell2010
41 default-language: Haskell2010
40− type: exitcode-stdio-1.0
41 default-extensions:
42 default-extensions:
42 OverloadedStrings
43 OverloadedStrings
43 ghc-options:
44 ghc-options:
44 -Wall
45 -Wall
45− -fno-warn-orphans
46+ type: exitcode-stdio-1.0
47+ main-is: Unit.hs
46 other-modules: Example
48 other-modules: Example
47 build-depends:
49 build-depends:
48 aeson
50 aeson
49 , base
51 , base
50 , hjsonpointer
52 , hjsonpointer
51− , hspec >= 2.2
52− , http-types >= 0.8
53 , QuickCheck
53 , QuickCheck
54 , unordered-containers
54 , unordered-containers
55 , text
55 , text
56 , vector
56 , vector
57+ , hspec >= 2.2 && < 2.3
58+ , http-types >= 0.8 && < 0.10
59+ , HUnit >= 1.2 && < 1.4
60+
61+source-repository head
62+ type: git
63+ location: git://github.com/seagreen/hjsonpointer.git
shell.nixdeleted−9
@@ -1,9 +0,0 @@
1−{ghc}:
2−with (import <nixpkgs> {});
3−
4−haskell.lib.buildStackProject {
5− name = "none";
6− inherit ghc;
7− buildInputs = [
8− ];
9−}
src/JSONPointer.hs → src/Data/Aeson/Pointer.hsrenamed+31−76
@@ -1,44 +1,24 @@
1−{-# LANGUAGE DeriveGeneric #-}
2{-# LANGUAGE GeneralizedNewtypeDeriving #-}
1{-# LANGUAGE GeneralizedNewtypeDeriving #-}
3−{-# LANGUAGE OverloadedStrings #-}
4
2
5−module JSONPointer where
3+module Data.Aeson.Pointer where
6
4
7import Control.Monad (when)
5import Control.Monad (when)
8import Data.Aeson
6import Data.Aeson
9−import qualified Data.Hashable as HA
7+import qualified Data.HashMap.Strict as H
10−import qualified Data.HashMap.Strict as HM
11−import Data.Semigroup (Semigroup)
12import Data.Text (Text)
8import Data.Text (Text)
13import qualified Data.Text as T
9import qualified Data.Text as T
14import qualified Data.Vector as V
10import qualified Data.Vector as V
15−import GHC.Generics (Generic)
11+import Test.QuickCheck
16import Text.Read (readMaybe)
12import Text.Read (readMaybe)
17
13
18−--------------------------------------------------
14+-- For GHCs before 7.10:
19−-- * Resolution
15+import Control.Applicative
20−--------------------------------------------------
16+import Data.Monoid
21−
17+import Data.Traversable
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−--------------------------------------------------
36
18
37newtype Pointer
19newtype Pointer
38 = Pointer { _unPointer :: [Token] }
20 = Pointer { _unPointer :: [Token] }
39− deriving (Eq, Show, Semigroup, Monoid, Generic)
21+ deriving (Eq, Show, Monoid, Arbitrary)
40−
41−instance HA.Hashable Pointer
42
22
43instance FromJSON Pointer where
23instance FromJSON Pointer where
44 parseJSON = withText "JSON Pointer" $ \t ->
24 parseJSON = withText "JSON Pointer" $ \t ->
@@ -52,19 +32,13 @@ instance ToJSON Pointer where
52-- | We don't try to distinguish between integer tokens and string
32-- | We don't try to distinguish between integer tokens and string
53-- tokens since all tokens start as strings, and all tokens can
33-- tokens since all tokens start as strings, and all tokens can
54-- be used to resolve JSON objects.
34-- 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.
59newtype Token
35newtype Token
60 = Token { _unToken :: Text }
36 = Token { _unToken :: Text }
61− deriving (Eq, Show, Generic)
37+ deriving (Eq, Show)
62
38
63−instance HA.Hashable Token
39+instance Arbitrary Token where
40+ arbitrary = Token . T.pack <$> arbitrary
64
41
65−-- | This escapes @"/"@ (because it's the token separator character).
66−--
67−-- It also escapes @"~"@ (because it's the escape character).
68escape :: Pointer -> Text
42escape :: Pointer -> Text
69escape (Pointer []) = ""
43escape (Pointer []) = ""
70escape (Pointer ts) =
44escape (Pointer ts) =
@@ -73,12 +47,15 @@ escape (Pointer ts) =
73 . fmap (T.replace "/" "~1" . T.replace "~" "~0" . _unToken)
47 . fmap (T.replace "/" "~1" . T.replace "~" "~0" . _unToken)
74 $ ts
48 $ ts
75
49
50+-- * Unescaping
51+
76data FormatError
52data FormatError
77 = InvalidFirstChar
53 = InvalidFirstChar
78 -- ^ JSON Pointers must either be empty or start with a @/@.
54 -- ^ JSON Pointers must either be empty or start with a @/@.
79 | UnescapedTilde
55 | UnescapedTilde
80 deriving (Eq, Show)
56 deriving (Eq, Show)
81
57
58+
82-- | JSON Pointers must either be empty or start with a @/@. This means
59-- | JSON Pointers must either be empty or start with a @/@. This means
83-- that if you're turning a URI Fragment into a JSON Pointer you must
60-- that if you're turning a URI Fragment into a JSON Pointer you must
84-- drop the initial @#@.
61-- drop the initial @#@.
@@ -100,40 +77,7 @@ unescape txt =
100 Nothing -> Left UnescapedTilde
77 Nothing -> Left UnescapedTilde
101 Just tok -> Right tok
78 Just tok -> Right tok
102
79
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−
136−-- | For internal use (by 'unescape').
80+-- | For internal use by 'unescape'.
137unescapeToken :: Text -> Maybe Token
81unescapeToken :: Text -> Maybe Token
138unescapeToken t
82unescapeToken t
139 | not (isValid t) = Nothing
83 | not (isValid t) = Nothing
@@ -149,10 +93,21 @@ unescapeToken t
149 replace :: Text -> Text
93 replace :: Text -> Text
150 replace = T.replace "~0" "~" . T.replace "~1" "/"
94 replace = T.replace "~0" "~" . T.replace "~1" "/"
151
95
152−-- | For internal use (by 'resolve').
96+-- * Resolution
153−--
97+
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+
154−-- Might also be useful for specialized applications that don't
109+-- | For internal use (or specialized applications that don't want to
155−-- want to resolve an entire pointer at once.
110+-- resolve an entire pointer at once).
156resolveToken :: Token -> Value -> Either ResolutionError Value
111resolveToken :: Token -> Value -> Either ResolutionError Value
157resolveToken tok (Array vs) =
112resolveToken tok (Array vs) =
158 case readMaybe . T.unpack . _unToken $ tok of
113 case readMaybe . T.unpack . _unToken $ tok of
@@ -163,7 +118,7 @@ resolveToken tok (Array vs) =
163 Nothing -> Left ArrayElemNotFound
118 Nothing -> Left ArrayElemNotFound
164 Just res -> Right res
119 Just res -> Right res
165resolveToken tok (Object h) =
120resolveToken tok (Object h) =
166− case HM.lookup (_unToken tok) h of
121+ case H.lookup (_unToken tok) h of
167 Nothing -> Left ObjectLookupFailed
122 Nothing -> Left ObjectLookupFailed
168 Just res -> Right res
123 Just res -> Right res
169resolveToken _ _ = Left ExpectedObjectOrArray
124resolveToken _ _ = Left ExpectedObjectOrArray
stack.yamldeleted−2
@@ -1,2 +0,0 @@
1−resolver: lts-12.10
2−pvp-bounds: upper-revision
test/Unit.hs+24−23
@@ -1,28 +1,26 @@
1−{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2−{-# LANGUAGE StandaloneDeriving #-}
3
1
4module Main where
2module Main where
5
3
4+import Control.Arrow
6import Data.Aeson
5import Data.Aeson
7−import Data.Bifunctor (first)
6+import qualified Data.Aeson.Pointer as P
8−import Data.Foldable
9−import Data.Monoid
10−import Data.Text (Text)
7+import Data.Text (Text)
11−import qualified Data.Text as T
8+import qualified Data.Text as T
12−import Data.Text.Encoding (decodeUtf8, encodeUtf8)
9+import Data.Text.Encoding
13−import qualified Data.Vector as V
10+import qualified Data.Vector as V
14−import qualified JSONPointer as JP
15−import Network.HTTP.Types.URI (urlDecode)
11+import Network.HTTP.Types.URI (urlDecode)
16
12
17import Test.Hspec
13import Test.Hspec
18−import Test.QuickCheck (Arbitrary(..), property)
14+import Test.QuickCheck (property)
19
15
20−import qualified Example
16+import Test.HUnit hiding (Test)
21
17
22−deriving instance Arbitrary JP.Pointer
18+-- For GHCs before 7.10:
19+import Control.Applicative
20+import Data.Foldable
21+import Data.Monoid
23
22
24−instance Arbitrary JP.Token where
23+import qualified Example
25− arbitrary = JP.Token . T.pack <$> arbitrary
26
24
27main :: IO ()
25main :: IO ()
28main = hspec $ do
26main = hspec $ do
@@ -33,10 +31,10 @@ main = hspec $ do
33 it "can be represented in a JSON string value" jsonString
31 it "can be represented in a JSON string value" jsonString
34 it "can be represented in a URI fragment identifier" uriFragment
32 it "can be represented in a URI fragment identifier" uriFragment
35
33
36−roundtrip :: JP.Pointer -> Bool
34+roundtrip :: P.Pointer -> Bool
37roundtrip a = Just a == decode (encode a)
35roundtrip a = Just a == decode (encode a)
38
36
39−jsonString :: Expectation
37+jsonString :: Assertion
40jsonString = traverse_ resolvesTo
38jsonString = traverse_ resolvesTo
41 [ ("" , specExample)
39 [ ("" , specExample)
42 , ("/foo" , Array $ V.fromList ["bar", "baz"])
40 , ("/foo" , Array $ V.fromList ["bar", "baz"])
@@ -52,7 +50,7 @@ jsonString = traverse_ resolvesTo
52 , ("/m~0n" , Number 8)
50 , ("/m~0n" , Number 8)
53 ]
51 ]
54
52
55−uriFragment :: Expectation
53+uriFragment :: Assertion
56uriFragment = traverse_ resolvesTo . fmap (first decodeFragment) $
54uriFragment = traverse_ resolvesTo . fmap (first decodeFragment) $
57 [ ("#" , specExample)
55 [ ("#" , specExample)
58 , ("#/foo" , Array $ V.fromList ["bar", "baz"])
56 , ("#/foo" , Array $ V.fromList ["bar", "baz"])
@@ -71,11 +69,14 @@ uriFragment = traverse_ resolvesTo . fmap (first decodeFragment) $
71 decodeFragment :: Text -> Text
69 decodeFragment :: Text -> Text
72 decodeFragment = T.drop 1 . decodeUtf8 . urlDecode True . encodeUtf8
70 decodeFragment = T.drop 1 . decodeUtf8 . urlDecode True . encodeUtf8
73
71
74−resolvesTo :: (Text, Value) -> Expectation
72+resolvesTo :: (Text, Value) -> Assertion
75resolvesTo (t, expected) =
73resolvesTo (t, expected) =
76− case JP.unescape t of
74+ case P.unescape t of
77− Left e -> expectationFailure (show e <> " error for pointer: " <> show t)
75+ Left e -> assertFailure (show e <> " error for pointer: " <> show t)
76+ Right p -> assertEqual
77+ ("Resolved value for pointer: " <> show t)
78+ (Right expected)
78− Right p -> JP.resolve p specExample `shouldBe` Right expected
79+ (P.resolve p specExample)
79
80
80specExample :: Value
81specExample :: Value
81specExample = object
82specExample = object