lens-aeson 1.0.2 → 1.2.3
raw patch · 13 files changed
Files
- .ghci +0/−1
- .gitignore +18/−0
- .hlint.yaml +1/−0
- .travis.yml +0/−106
- CHANGELOG.markdown +83/−1
- README.markdown +1/−1
- Setup.lhs +5/−32
- Warning.hs +0/−5
- lens-aeson.cabal +25/−47
- src/Data/Aeson/Lens.hs +221/−131
- tests/doctests.hs +0/−25
- travis/cabal-apt-install +0/−27
- travis/config +0/−16
− .ghci
@@ -1,1 +0,0 @@-:set -isrc -idist/build/autogen -optP-include -optPdist/build/autogen/cabal_macros.h
.gitignore view
@@ -12,3 +12,21 @@ *.hi *~ *#+.stack-work/+cabal-dev+*.chi+*.chs.h+*.dyn_o+*.dyn_hi+.hpc+.hsenv+.cabal-sandbox/+cabal.sandbox.config+*.prof+*.aux+*.hp+*.eventlog+cabal.project.local+cabal.project.local~+.HTF/+.ghc.environment.*
+ .hlint.yaml view
@@ -0,0 +1,1 @@+- arguments: [--cpp-define=HLINT, --cpp-ansi]
− .travis.yml
@@ -1,106 +0,0 @@-# This file has been generated -- see https://github.com/hvr/multi-ghc-travis-language: c-sudo: false--cache:- directories:- - $HOME/.cabsnap- - $HOME/.cabal/packages--before_cache:- - rm -fv $HOME/.cabal/packages/hackage.haskell.org/build-reports.log- - rm -fv $HOME/.cabal/packages/hackage.haskell.org/00-index.tar--matrix:- include:- - env: CABALVER=1.24 GHCVER=7.4.2- compiler: ": #GHC 7.4.2"- addons: {apt: {packages: [cabal-install-1.24,ghc-7.4.2,hlint], sources: [hvr-ghc]}}- - env: CABALVER=1.24 GHCVER=7.6.3- compiler: ": #GHC 7.6.3"- addons: {apt: {packages: [cabal-install-1.24,ghc-7.6.3,hlint], sources: [hvr-ghc]}}- - env: CABALVER=1.24 GHCVER=7.8.4- compiler: ": #GHC 7.8.4"- addons: {apt: {packages: [cabal-install-1.24,ghc-7.8.4,hlint], sources: [hvr-ghc]}}- - env: CABALVER=1.24 GHCVER=7.10.3- compiler: ": #GHC 7.10.3"- addons: {apt: {packages: [cabal-install-1.24,ghc-7.10.3,hlint], sources: [hvr-ghc]}}- - env: CABALVER=1.24 GHCVER=8.0.2- compiler: ": #GHC 8.0.2"- addons: {apt: {packages: [cabal-install-1.24,ghc-8.0.2,hlint], sources: [hvr-ghc]}}- - env: CABALVER=2.0 GHCVER=8.2.1- compiler: ": #GHC 8.2.1"- addons: {apt: {packages: [cabal-install-2.0,ghc-8.2.1,hlint], sources: [hvr-ghc]}}- - env: CABALVER=head GHCVER=head- compiler: ": #GHC head"- addons: {apt: {packages: [cabal-install-head,ghc-head,hlint], sources: [hvr-ghc]}}-- allow_failures:- - env: CABALVER=head GHCVER=head--before_install:- - unset CC- - export PATH=/opt/ghc/$GHCVER/bin:/opt/cabal/$CABALVER/bin:$PATH--install:- - cabal --version- - echo "$(ghc --version) [$(ghc --print-project-git-commit-id 2> /dev/null || echo '?')]"- - if [ -f $HOME/.cabal/packages/hackage.haskell.org/00-index.tar.gz ];- then- zcat $HOME/.cabal/packages/hackage.haskell.org/00-index.tar.gz >- $HOME/.cabal/packages/hackage.haskell.org/00-index.tar;- fi- - travis_retry cabal update -v- - sed -i 's/^jobs:/-- jobs:/' ${HOME}/.cabal/config- - cabal install --only-dependencies --enable-tests --enable-benchmarks --dry -v > installplan.txt- - sed -i -e '1,/^Resolving /d' installplan.txt; cat installplan.txt--# check whether current requested install-plan matches cached package-db snapshot- - if diff -u installplan.txt $HOME/.cabsnap/installplan.txt;- then- echo "cabal build-cache HIT";- rm -rfv .ghc;- cp -a $HOME/.cabsnap/ghc $HOME/.ghc;- cp -a $HOME/.cabsnap/lib $HOME/.cabsnap/share $HOME/.cabsnap/bin $HOME/.cabal/;- else- echo "cabal build-cache MISS";- rm -rf $HOME/.cabsnap;- mkdir -p $HOME/.ghc $HOME/.cabal/lib $HOME/.cabal/share $HOME/.cabal/bin;- cabal install -j --only-dependencies --enable-tests --enable-benchmarks;- fi--# snapshot package-db on cache miss- - if [ ! -d $HOME/.cabsnap ];- then- echo "snapshotting package-db to build-cache";- mkdir $HOME/.cabsnap;- cp -a $HOME/.ghc $HOME/.cabsnap/ghc;- cp -a $HOME/.cabal/lib $HOME/.cabal/share $HOME/.cabal/bin installplan.txt $HOME/.cabsnap/;- fi--# Here starts the actual work to be performed for the package under test;-# any command which exits with a non-zero exit code causes the build to fail.-script:- - cabal configure -v2 --enable-tests --enable-benchmarks # -v2 provides useful information for debugging- - cabal build # this builds all libraries and executables (including tests/benchmarks)- - cabal test- - cabal sdist # tests that a source-distribution can be generated- - hlint src --cpp-define HLINT- - export SRC_TGZ=$(cabal info . | awk '{print $2 ".tar.gz";exit}') ;- cd dist/;- if [ -f "$SRC_TGZ" ]; then- cabal install "$SRC_TGZ";- else- echo "expected '$SRC_TGZ' not found";- exit 1;- fi--notifications:- irc:- channels:- - "irc.freenode.org#haskell-lens"- skip_join: true- template:- - "\x0313lens-aeson\x0f/\x0306%{branch}\x0f \x0314%{commit}\x0f %{message} \x0302\x1f%{build_url}\x0f"--# EOF
CHANGELOG.markdown view
@@ -1,3 +1,86 @@+1.2.3 [2023.06.27]+------------------+* Use `aeson`'s `decode` function in the `AsJSON` instance for `ByteString`s.+ This allows removing a direct dependency on `attoparsec`.+* Allow building with `aeson-2.2`.++1.2.2 [2022.08.11]+------------------+* Add `atKey :: AsValue t => Key -> Traversal' t (Maybe Value)`, a variant of+ `key` that uses `at` instead of `ix`.++1.2.1 [2022.05.07]+------------------+* Change the `IsKey` instances for `ByteString`s to use+ `decodeUtf8With lenientDecode` instead of `decodeUtf8`. While these `IsKey`+ instances are meant to used for interoperability with `aeson` values that+ are UTF-8–encoded, using `decodeUtf8With lenientDecode` at least ensures+ that converting a non–UTF-8–encoded `ByteString` will not crash.++1.2 [2022.03.19]+----------------+* Require `aeson-2.0.2.*` and `lens-5.0.*` or greater.+* Change the types of `_Object`, `key`, and `members`:++ ```diff+ -_Object :: Prism' t (HashMap Text Value)+ +_Object :: Prism' t (KeyMap Value)++ -key :: AsValue t => Text -> Traversal' t Value+ +key :: AsValue t => Key -> Traversal' t Value++ -members :: AsValue t => IndexedTraversal' Text t Value+ +members :: AsValue t => IndexedTraversal' Key t Value+ ```++ This mirrors similar changes made in `aeson-2.0.*`, where the type of+ `Object`'s field was changed from `HashMap Text Value` to `KeyMap Value`.++ The `Ixed Value` instance changes similarly:++ ```diff+ -type instance Index Value = Text+ +type instance Index Value = Key+ ```+* Remove `Primitive` and `AsPrimitive`, since https://tools.ietf.org/html/rfc7159+ de-emphasized the notion of primitive versus composite JSON values.+ * The `AsPrimitive` methods (`_Value`, `_String`, and `_Bool`) are now+ `AsValue` methods.+ * `_Number`'s default signature, `Bool_`, `String_`, and `Null_` now have an+ `AsValue` constraint.+* Add `Wrapped` and `Rewrapped` instances for `KeyMap`. These treat `KeyMap v`+ as a wrapper around `[(Key, v)]`. The order in which the key-value pairs+ appear in this list is not stable.+* Add an `IsKey` class, whose method `_Key` is an `Iso` for converting values+ to and from a `Key`.++1.1.3 [2021.11.16]+------------------+* Drop support for pre-8.0 versions of GHC.++1.1.2 [2021.10.09]+------------------+* Allow building with `aeson-2.0.0.0`.+* Add `Index`, `IxValue`, `Ixed`, `At`, and `Each` instances for `KeyMap` if+ building with `aeson-2.0.0.0` or later.++1.1.1 [2021.02.17]+------------------+* Allow building with `lens-5.*`.+* The build-type has been changed from `Custom` to `Simple`.+ To achieve this, the `doctests` test suite has been removed in favor of using+ [`cabal-docspec`](https://github.com/phadej/cabal-extras/tree/master/cabal-docspec)+ to run the doctests.++1.1 [2019.09.26]+----------------+* Generalize the type of `_JSON` from `Prism' t a` to `Prism t t a b`. If you+ wish to continue to use the less general type, use the newly added `_JSON'`+ prism.+* Add pattern synonyms corresponding to the `Prism`s that `lens-aeson`+ provides.+* Fix the test suite on 32-bit architectures.+ 1.0.2 ----- * Support `doctest-0.12`@@ -33,4 +116,3 @@ 0.1 --- * Repository initialized-
README.markdown view
@@ -1,7 +1,7 @@ lens-aeson ========== -[](https://hackage.haskell.org/package/lens-aeson) [](http://travis-ci.org/lens/lens-aeson)+[](https://hackage.haskell.org/package/lens-aeson) [](https://github.com/lens/lens-aeson/actions?query=workflow%3AHaskell-CI) The goal of `lens-aeson` is to provide traversals and prisms for the [Aeson](http://hackage.haskell.org/package/aeson) library's
Setup.lhs view
@@ -1,34 +1,7 @@-\begin{code}-{-# LANGUAGE CPP #-}-{-# OPTIONS_GHC -Wall #-}-module Main (main) where--#ifndef MIN_VERSION_cabal_doctest-#define MIN_VERSION_cabal_doctest(x,y,z) 0-#endif--#if MIN_VERSION_cabal_doctest(1,0,0)--import Distribution.Extra.Doctest ( defaultMainWithDoctests )-main :: IO ()-main = defaultMainWithDoctests "doctests"--#else--#ifdef MIN_VERSION_Cabal--- If the macro is defined, we have new cabal-install,--- but for some reason we don't have cabal-doctest in package-db------ Probably we are running cabal sdist, when otherwise using new-build--- workflow-import Warning ()-#endif--import Distribution.Simple--main :: IO ()-main = defaultMain+#!/usr/bin/runhaskell+> module Main (main) where -#endif+> import Distribution.Simple -\end{code}+> main :: IO ()+> main = defaultMain
− Warning.hs
@@ -1,5 +0,0 @@-module Warning- {-# WARNING ["You are configuring this package without cabal-doctest installed.",- "The doctests test-suite will not work as a result.",- "To fix this, install cabal-doctest before configuring."] #-}- () where
lens-aeson.cabal view
@@ -1,8 +1,8 @@ name: lens-aeson-category: Numeric-version: 1.0.2+category: Data, JSON, Lenses+version: 1.2.3 license: MIT-cabal-version: >= 1.8+cabal-version: >= 1.10 license-file: LICENSE author: Edward A. Kmett maintainer: Edward A. Kmett <ekmett@gmail.com>@@ -12,69 +12,47 @@ copyright: Copyright (C) 2012 Paul Wilson Copyright (C) 2013 Edward A. Kmett-build-type: Custom-tested-with: GHC == 7.4.2, GHC == 7.6.3, GHC == 7.8.4, GHC == 7.10.3, GHC==8.0.2, GHC==8.2.1+build-type: Simple+tested-with: GHC == 8.0.2+ , GHC == 8.2.2+ , GHC == 8.4.4+ , GHC == 8.6.5+ , GHC == 8.8.4+ , GHC == 8.10.7+ , GHC == 9.0.2+ , GHC == 9.2.7+ , GHC == 9.4.5+ , GHC == 9.6.2 synopsis: Law-abiding lenses for aeson-description: Law-abiding lenses for aeson+description: Law-abiding lenses for aeson. extra-source-files:- .travis.yml- .ghci .gitignore+ .hlint.yaml .vim.custom- travis/cabal-apt-install- travis/config AUTHORS.markdown README.markdown CHANGELOG.markdown- Warning.hs source-repository head type: git location: https://github.com/lens/lens-aeson -custom-setup- setup-depends:- base >= 4 && < 5,- Cabal,- cabal-doctest >= 1 && < 1.1---- You can disable the doctests test suite with -f-test-doctests-flag test-doctests- default: True- manual: True- library build-depends:- base >= 4.5 && < 5,- lens >= 4.4 && < 5,- text >= 0.11.1.10 && < 1.3,- vector >= 0.9 && < 0.13,+ base >= 4.9 && < 5,+ lens >= 5.0 && < 6,+ text >= 0.11.1.10 && < 2.1,+ text-short >= 0.1.4 && < 0.2,+ vector >= 0.9 && < 0.14, unordered-containers >= 0.2.3 && < 0.3,- attoparsec >= 0.10 && < 0.14,- bytestring >= 0.9 && < 0.11,- aeson >= 0.7.0.5 && < 1.3,+ bytestring >= 0.9 && < 0.12,+ aeson >= 2.0.2 && < 2.3, scientific >= 0.3.2 && < 0.4 exposed-modules: Data.Aeson.Lens - ghc-options: -Wall -fwarn-tabs -O2+ ghc-options: -Wall -Wtabs -O2 hs-source-dirs: src--test-suite doctests- type: exitcode-stdio-1.0- main-is: doctests.hs- ghc-options: -Wall -threaded- hs-source-dirs: tests-- if !flag(test-doctests)- buildable: False- else- build-depends:- base,- doctest >= 0.11.1 && < 0.13,- generic-deriving,- lens-aeson,- semigroups >= 0.9,- simple-reflect >= 0.3.1+ default-language: Haskell2010
src/Data/Aeson/Lens.hs view
@@ -1,16 +1,17 @@-{-# LANGUAGE CPP #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE Trustworthy #-} {-# LANGUAGE TypeFamilies #-}+{-# LANGUAGE TypeOperators #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-}-{-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE DefaultSignatures #-}-{-# OPTIONS_GHC -fno-warn-orphans #-}+{-# LANGUAGE PatternSynonyms #-}+{-# LANGUAGE ViewPatterns #-}+{-# OPTIONS_GHC -Wno-orphans #-} -------------------------------------------------------------------- -- |--- Copyright : (c) Edward Kmett 2013-2014, (c) Paul Wilson 2012+-- Copyright : (c) Edward Kmett 2013-2019, (c) Paul Wilson 2012 -- License : BSD3 -- Maintainer: Edward Kmett <ekmett@gmail.com> -- Stability : experimental@@ -25,30 +26,41 @@ AsNumber(..) , _Integral , nonNull- -- * Primitive- , Primitive(..)- , AsPrimitive(..) -- * Objects and Arrays , AsValue(..)- , key, members+ , key, atKey, members , nth, values+ , IsKey(..) -- * Decoding , AsJSON(..)+ , _JSON'+ -- * Pattern Synonyms+ , pattern JSON+ , pattern Value_+ , pattern Number_+ , pattern Double+ , pattern Integer+ , pattern Integral+ , pattern Bool_+ , pattern String_+ , pattern Null_+ , pattern Key_ ) where import Control.Applicative import Control.Lens import Data.Aeson-import Data.Aeson.Parser (value)-import Data.Attoparsec.ByteString.Lazy (maybeResult, parse)+import qualified Data.Aeson.Key as Key+import qualified Data.Aeson.KeyMap as KM+import Data.Aeson.KeyMap (KeyMap) import Data.Scientific (Scientific) import qualified Data.Scientific as Scientific import qualified Data.ByteString as Strict import Data.ByteString.Lazy.Char8 as Lazy hiding (putStrLn)-import Data.Data-import Data.HashMap.Strict (HashMap) import Data.Text as Text+import qualified Data.Text.Encoding.Error as Encoding import qualified Data.Text.Lazy as LazyText+import Data.Text.Short (ShortText) import Data.Text.Lens (packed) import qualified Data.Text.Encoding as StrictText import qualified Data.Text.Lazy.Encoding as LazyText@@ -56,7 +68,12 @@ import Prelude hiding (null) -- $setup+-- >>> import Control.Lens+-- >>> import Data.Aeson+-- >>> import Data.Text (Text)+-- >>> import qualified Data.ByteString as Strict (ByteString) -- >>> import Data.ByteString.Char8 as Strict.Char8+-- >>> import qualified Data.ByteString.Lazy as Lazy (ByteString) -- >>> import qualified Data.Vector as Vector -- >>> :set -XOverloadedStrings @@ -72,14 +89,12 @@ -- >>> "[1, \"x\"]" ^? nth 1 . _Number -- Nothing _Number :: Prism' t Scientific-#ifndef HLINT- default _Number :: AsPrimitive t => Prism' t Scientific- _Number = _Primitive._Number+ default _Number :: AsValue t => Prism' t Scientific+ _Number = _Value._Number {-# INLINE _Number #-}-#endif -- |- -- Prism into an 'Double' over a 'Value', 'Primitive' or 'Scientific'+ -- Prism into an 'Double' over a 'Value' or 'Scientific' -- -- >>> "[10.2]" ^? nth 0 . _Double -- Just 10.2@@ -88,7 +103,7 @@ {-# INLINE _Double #-} -- |- -- Prism into an 'Integer' over a 'Value', 'Primitive' or 'Scientific'+ -- Prism into an 'Integer' over a 'Value' or 'Scientific' -- -- >>> "[10]" ^? nth 0 . _Integer -- Just 10@@ -132,43 +147,32 @@ {-# INLINE _Integral #-} --------------------------------------------------------------------------------- Null values and primitives+-- Null values ------------------------------------------------------------------------------ --- | Primitives of 'Value'-data Primitive- = StringPrim !Text- | NumberPrim !Scientific- | BoolPrim !Bool- | NullPrim- deriving (Eq,Ord,Show,Data,Typeable)+-- | Prism into non-'Null' values+--+-- >>> "{\"a\": \"xyz\", \"b\": null}" ^? key "a" . nonNull+-- Just (String "xyz")+--+-- >>> "{\"a\": {}, \"b\": null}" ^? key "a" . nonNull+-- Just (Object (fromList []))+--+-- >>> "{\"a\": \"xyz\", \"b\": null}" ^? key "b" . nonNull+-- Nothing+nonNull :: Prism' Value Value+nonNull = prism id (\v -> if isn't _Null v then Right v else Left v)+{-# INLINE nonNull #-} -instance AsNumber Primitive where- _Number = prism NumberPrim $ \v -> case v of NumberPrim s -> Right s; _ -> Left v- {-# INLINE _Number #-}+------------------------------------------------------------------------------+-- Non-number traversals+------------------------------------------------------------------------------ -class AsNumber t => AsPrimitive t where+class AsNumber t => AsValue t where -- |- -- >>> "[1, \"x\", null, true, false]" ^? nth 0 . _Primitive- -- Just (NumberPrim 1.0)- --- -- >>> "[1, \"x\", null, true, false]" ^? nth 1 . _Primitive- -- Just (StringPrim "x")- --- -- >>> "[1, \"x\", null, true, false]" ^? nth 2 . _Primitive- -- Just NullPrim- --- -- >>> "[1, \"x\", null, true, false]" ^? nth 3 . _Primitive- -- Just (BoolPrim True)- --- -- >>> "[1, \"x\", null, true, false]" ^? nth 4 . _Primitive- -- Just (BoolPrim False)- _Primitive :: Prism' t Primitive-#ifndef HLINT- default _Primitive :: AsValue t => Prism' t Primitive- _Primitive = _Value._Primitive- {-# INLINE _Primitive #-}-#endif+ -- >>> preview _Value "[1,2,3]" == Just (Array (Vector.fromList [Number 1.0,Number 2.0,Number 3.0]))+ -- True+ _Value :: Prism' t Value -- | -- >>> "{\"a\": \"xyz\", \"b\": true}" ^? key "a" . _String@@ -177,10 +181,10 @@ -- >>> "{\"a\": \"xyz\", \"b\": true}" ^? key "b" . _String -- Nothing --- -- >>> _Object._Wrapped # [("key" :: Text, _String # "value")] :: String+ -- >>> _Object._Wrapped # [("key" :: Key, _String # "value")] :: String -- "{\"key\":\"value\"}" _String :: Prism' t Text- _String = _Primitive.prism StringPrim (\v -> case v of StringPrim s -> Right s; _ -> Left v)+ _String = _Value.prism String (\v -> case v of String s -> Right s; _ -> Left v) {-# INLINE _String #-} -- |@@ -196,7 +200,7 @@ -- >>> _Bool # False :: String -- "false" _Bool :: Prism' t Bool- _Bool = _Primitive.prism BoolPrim (\v -> case v of BoolPrim b -> Right b; _ -> Left v)+ _Bool = _Value.prism Bool (\v -> case v of Bool b -> Right b; _ -> Left v) {-# INLINE _Bool #-} -- |@@ -209,76 +213,19 @@ -- >>> _Null # () :: String -- "null" _Null :: Prism' t ()- _Null = _Primitive.prism (const NullPrim) (\v -> case v of NullPrim -> Right (); _ -> Left v)- {-# INLINE _Null #-}---instance AsPrimitive Value where- _Primitive = prism fromPrim toPrim- where- toPrim (String s) = Right $ StringPrim s- toPrim (Number n) = Right $ NumberPrim n- toPrim (Bool b) = Right $ BoolPrim b- toPrim Null = Right NullPrim- toPrim v = Left v- {-# INLINE toPrim #-}- fromPrim (StringPrim s) = String s- fromPrim (NumberPrim n) = Number n- fromPrim (BoolPrim b) = Bool b- fromPrim NullPrim = Null- {-# INLINE fromPrim #-}- {-# INLINE _Primitive #-}- _String = prism String $ \v -> case v of String s -> Right s; _ -> Left v- {-# INLINE _String #-}- _Bool = prism Bool (\v -> case v of Bool b -> Right b; _ -> Left v)- {-# INLINE _Bool #-}- _Null = prism (const Null) (\v -> case v of Null -> Right (); _ -> Left v)+ _Null = _Value.prism (const Null) (\v -> case v of Null -> Right (); _ -> Left v) {-# INLINE _Null #-} -instance AsPrimitive Strict.ByteString-instance AsPrimitive Lazy.ByteString-instance AsPrimitive Text.Text-instance AsPrimitive LazyText.Text-instance AsPrimitive String--instance AsPrimitive Primitive where- _Primitive = id- {-# INLINE _Primitive #-}---- | Prism into non-'Null' values------ >>> "{\"a\": \"xyz\", \"b\": null}" ^? key "a" . nonNull--- Just (String "xyz")------ >>> "{\"a\": {}, \"b\": null}" ^? key "a" . nonNull--- Just (Object (fromList []))------ >>> "{\"a\": \"xyz\", \"b\": null}" ^? key "b" . nonNull--- Nothing-nonNull :: Prism' Value Value-nonNull = prism id (\v -> if isn't _Null v then Right v else Left v)-{-# INLINE nonNull #-}----------------------------------------------------------------------------------- Non-primitive traversals---------------------------------------------------------------------------------class AsPrimitive t => AsValue t where -- |- -- >>> preview _Value "[1,2,3]" == Just (Array (Vector.fromList [Number 1.0,Number 2.0,Number 3.0]))- -- True- _Value :: Prism' t Value-- -- | -- >>> "{\"a\": {}, \"b\": null}" ^? key "a" . _Object -- Just (fromList []) -- -- >>> "{\"a\": {}, \"b\": null}" ^? key "b" . _Object -- Nothing --- -- >>> _Object._Wrapped # [("key" :: Text, _String # "value")] :: String+ -- >>> _Object._Wrapped # [("key" :: Key, _String # "value")] :: String -- "{\"key\":\"value\"}"- _Object :: Prism' t (HashMap Text Value)+ _Object :: Prism' t (KeyMap Value) _Object = _Value.prism Object (\v -> case v of Object o -> Right o; _ -> Left v) {-# INLINE _Object #-} @@ -314,7 +261,7 @@ {-# INLINE _Value #-} -- |--- Like 'ix', but for 'Object' with Text indices. This often has better+-- Like 'ix', but for 'Object' with 'Key' indices. This often has better -- inference than 'ix' when used with OverloadedStrings. -- -- >>> "{\"a\": 100, \"b\": 200}" ^? key "a"@@ -322,18 +269,31 @@ -- -- >>> "[1,2,3]" ^? key "a" -- Nothing-key :: AsValue t => Text -> Traversal' t Value+key :: AsValue t => Key -> Traversal' t Value key i = _Object . ix i {-# INLINE key #-} +-- |+-- Like 'key', but uses 'at' instead of 'ix'. This is handy when+-- adding and removing object keys:+--+-- >>> "{\"a\": 100, \"b\": 200}" & atKey "a" .~ Nothing+-- "{\"b\":200}"+--+-- >>> "{\"a\": 100, \"b\": 200}" & atKey "c" ?~ String "300"+-- "{\"a\":100,\"b\":200,\"c\":\"300\"}"+atKey :: AsValue t => Key -> Traversal' t (Maybe Value)+atKey i = _Object . at i+{-# INLINE atKey #-}+ -- | An indexed Traversal into Object properties ----- >>> "{\"a\": 4, \"b\": 7}" ^@.. members--- [("a",Number 4.0),("b",Number 7.0)]+-- >>> Data.List.sort ("{\"a\": 4, \"b\": 7}" ^@.. members . _Number)+-- [("a",4.0),("b",7.0)] ----- >>> "{\"a\": 4, \"b\": 7}" & members . _Number *~ 10--- "{\"a\":40,\"b\":70}"-members :: AsValue t => IndexedTraversal' Text t Value+-- >>> "{\"a\": 4}" & members . _Number *~ 10+-- "{\"a\":40}"+members :: AsValue t => IndexedTraversal' Key t Value members = _Object . itraversed {-# INLINE members #-} @@ -342,7 +302,7 @@ -- >>> "[1,2,3]" ^? nth 1 -- Just (Number 2.0) ----- >>> "\"a\": 100, \"b\": 200}" ^? nth 1+-- >>> "{\"a\": 100, \"b\": 200}" ^? nth 1 -- Nothing -- -- >>> "[1,2,3]" & nth 1 .~ Number 20@@ -366,26 +326,87 @@ strictUtf8 = packed . strictTextUtf8 strictTextUtf8 :: Iso' Text.Text Strict.ByteString-strictTextUtf8 = iso StrictText.encodeUtf8 StrictText.decodeUtf8+strictTextUtf8 = iso StrictText.encodeUtf8 (StrictText.decodeUtf8With Encoding.lenientDecode) lazyTextUtf8 :: Iso' LazyText.Text Lazy.ByteString-lazyTextUtf8 = iso LazyText.encodeUtf8 LazyText.decodeUtf8+lazyTextUtf8 = iso LazyText.encodeUtf8 (LazyText.decodeUtf8With Encoding.lenientDecode) +_JSON' :: (AsJSON t, FromJSON a, ToJSON a) => Prism' t a+_JSON' = _JSON+++class IsKey t where+ -- | '_Key' is an 'Iso' from something to a 'Key'. This is primarily intended+ -- for situations where one wishes to use object keys that are not string+ -- literals and therefore must be converted:+ --+ -- >>> let k = "a" :: Text+ -- >>> "{\"a\": 100, \"b\": 200}" ^? key (k ^. _Key)+ -- Just (Number 100.0)+ --+ -- Note that applying '_Key' directly to a string literal+ -- (e.g., @\"a\" ^. '_Key'@) will likely not typecheck when+ -- @OverloadedStrings@ is enabled.+ _Key :: Iso' t Key++instance IsKey Key where+ _Key = id+ {-# INLINE _Key #-}++instance IsKey String where+ _Key = iso Key.fromString Key.toString+ {-# INLINE _Key #-}++instance IsKey Text.Text where+ _Key = iso Key.fromText Key.toText+ {-# INLINE _Key #-}++instance IsKey LazyText.Text where+ _Key = strict._Key+ {-# INLINE _Key #-}++instance IsKey ShortText where+ _Key = iso Key.fromShortText Key.toShortText+ {-# INLINE _Key #-}++-- | This instance assumes that you are dealing with UTF-8–encoded+-- 'Strict.ByteString's, as this is the encoding that RFC 8259 requires JSON+-- values to use. As such, this is not a full 'Iso', since non–UTF-8–encoded+-- 'Strict.ByteString's will not roundtrip:+--+-- >>> let str = view _Key ("\255" :: Strict.ByteString)+-- >>> str+-- "\65533"+-- >>> view (from _Key) str :: Strict.ByteString+-- "\239\191\189"+instance IsKey Strict.ByteString where+ _Key = from strictTextUtf8._Key+ {-# INLINE _Key #-}++-- | This instance assumes that you are dealing with UTF-8–encoded+-- 'Lazy.ByteString's, as this is the encoding that RFC 8259 requires JSON+-- values to use. As such, this is not a full 'Iso', since non–UTF-8–encoded+-- 'Lazy.ByteString's will not roundtrip:+--+-- >>> let str = view _Key ("\255" :: Lazy.ByteString)+-- >>> str+-- "\65533"+-- >>> view (from _Key) str :: Lazy.ByteString+-- "\239\191\189"+instance IsKey Lazy.ByteString where+ _Key = from lazyTextUtf8._Key+ {-# INLINE _Key #-}+ class AsJSON t where -- | '_JSON' is a 'Prism' from something containing JSON to something encoded in that structure- _JSON :: (FromJSON a, ToJSON a) => Prism' t a+ _JSON :: (FromJSON a, ToJSON b) => Prism t t a b instance AsJSON Strict.ByteString where _JSON = lazy._JSON {-# INLINE _JSON #-} instance AsJSON Lazy.ByteString where- _JSON = prism' encode decodeValue- where- decodeValue :: (FromJSON a) => Lazy.ByteString -> Maybe a- decodeValue s = maybeResult (parse value s) >>= \x -> case fromJSON x of- Success v -> Just v- _ -> Nothing+ _JSON = prism' encode decode {-# INLINE _JSON #-} instance AsJSON String where@@ -411,6 +432,8 @@ ------------------------------------------------------------------------------ -- $LazyByteStringTests+-- >>> import qualified Data.ByteString.Lazy.Char8 as Lazy+-- -- >>> "42" ^? (_JSON :: Prism' Lazy.ByteString Value) -- Just (Number 42.0) --@@ -421,6 +444,8 @@ -- "42" -- $StrictByteStringTests+-- >>> import qualified Data.ByteString.Char8 as Strict+-- -- >>> "42" ^? (_JSON :: Prism' Strict.ByteString Value) -- Just (Number 42.0) --@@ -444,7 +469,7 @@ -- Orphan instances for lens library interop ------------------------------------------------------------------------------ -type instance Index Value = Text+type instance Index Value = Key type instance IxValue Value = Value instance Ixed Value where@@ -457,3 +482,68 @@ plate f (Array a) = Array <$> traverse f a plate _ xs = pure xs {-# INLINE plate #-}++type instance Index (KM.KeyMap v) = Key.Key+type instance IxValue (KM.KeyMap v) = v++instance Ixed (KM.KeyMap v)++instance At (KM.KeyMap v) where+ at k f = KM.alterF f k+ {-# INLINE at #-}++instance Each (KM.KeyMap a) (KM.KeyMap b) a b where+ each = traversed+ {-# INLINE each #-}++-- | Use @'iso' 'KM.toList' 'KM.fromList'@.+-- Unwrapping returns some permutation of the list.+instance (t ~ KeyMap v') => Rewrapped (KeyMap v) t+instance Wrapped (KeyMap v) where+ type Unwrapped (KeyMap v) = [(Key, v)]+ _Wrapped' = iso KM.toList KM.fromList+ {-# INLINE _Wrapped' #-}++------------------------------------------------------------------------------+-- Pattern Synonyms+------------------------------------------------------------------------------++pattern JSON :: (FromJSON a, ToJSON a, AsJSON t) => () => a -> t+pattern JSON a <- (preview _JSON -> Just a) where+ JSON a = _JSON # a++pattern Value_ :: (FromJSON a, ToJSON a) => () => a -> Value+pattern Value_ a <- (fromJSON -> Success a) where+ Value_ a = toJSON a++pattern Number_ :: AsNumber t => Scientific -> t+pattern Number_ n <- (preview _Number -> Just n) where+ Number_ n = _Number # n++pattern Double :: AsNumber t => Double -> t+pattern Double d <- (preview _Double -> Just d) where+ Double d = _Double # d++pattern Integer :: AsNumber t => Integer -> t+pattern Integer i <- (preview _Integer -> Just i) where+ Integer i = _Integer # i++pattern Integral :: (AsNumber t, Integral a) => a -> t+pattern Integral d <- (preview _Integral -> Just d) where+ Integral d = _Integral # d++pattern Bool_ :: AsValue t => Bool -> t+pattern Bool_ b <- (preview _Bool -> Just b) where+ Bool_ b = _Bool # b++pattern String_ :: AsValue t => Text -> t+pattern String_ p <- (preview _String -> Just p) where+ String_ p = _String # p++pattern Null_ :: AsValue t => t+pattern Null_ <- (preview _Null -> Just ()) where+ Null_ = _Null # ()++pattern Key_ :: IsKey t => Key -> t+pattern Key_ k <- (preview _Key -> Just k) where+ Key_ k = _Key # k
− tests/doctests.hs
@@ -1,25 +0,0 @@--------------------------------------------------------------------------------- |--- Module : Main (doctests)--- Copyright : (C) 2012-14 Edward Kmett--- License : BSD-style (see the file LICENSE)--- Maintainer : Edward Kmett <ekmett@gmail.com>--- Stability : provisional--- Portability : portable------ This module provides doctests for a project based on the actual versions--- of the packages it was built with. It requires a corresponding Setup.lhs--- to be added to the project-------------------------------------------------------------------------------module Main where--import Build_doctests (flags, pkgs, module_sources)-import Data.Foldable (traverse_)-import Test.DocTest--main :: IO ()-main = do- traverse_ putStrLn args- doctest args- where- args = flags ++ pkgs ++ module_sources
− travis/cabal-apt-install
@@ -1,27 +0,0 @@-#! /bin/bash-set -eu--APT="sudo apt-get -q -y"-CABAL_INSTALL_DEPS="cabal install --only-dependencies --force-reinstall"--$APT update-$APT install dctrl-tools--# Find potential system packages to satisfy cabal dependencies-deps()-{- local M='^\([^ ]\+\)-[0-9.]\+ (.*$'- local G=' -o ( -FPackage -X libghc-\L\1\E-dev )'- local E="$($CABAL_INSTALL_DEPS "$@" --dry-run -v 2> /dev/null \- | sed -ne "s/$M/$G/p" | sort -u)"- grep-aptavail -n -sPackage \( -FNone -X None \) $E | sort -u-}--$APT install $(deps "$@") libghc-quickcheck2-dev # QuickCheck is special-$CABAL_INSTALL_DEPS "$@" # Install the rest via Hackage--if ! $APT install hlint ; then- $APT install $(deps hlint)- cabal install hlint-fi-
− travis/config
@@ -1,16 +0,0 @@--- This provides a custom ~/.cabal/config file for use when hackage is down that should work on unix------ This is particularly useful for travis-ci to get it to stop complaining--- about a broken build when everything is still correct on our end.------ This uses Luite Stegeman's mirror of hackage provided by his 'hdiff' site instead------ To enable this, uncomment the before_script in .travis.yml--remote-repo: hdiff.luite.com:http://hdiff.luite.com/packages/archive-remote-repo-cache: ~/.cabal/packages-world-file: ~/.cabal/world-build-summary: ~/.cabal/logs/build.log-remote-build-reporting: anonymous-install-dirs user-install-dirs global