packages feed

toml-reader 0.1.0.0 → 0.2.0.0

raw patch · 11 files changed

+83/−16 lines, 11 filesdep −deepseqdep ~megaparsecPVP ok

version bump matches the API change (PVP)

Dependencies removed: deepseq

Dependency ranges changed: megaparsec

API changes (from Hackage documentation)

- TOML.Decode: instance TOML.Decode.DecodeTOML GHC.Integer.Type.Integer
- TOML.Decode: instance TOML.Decode.DecodeTOML GHC.Natural.Natural
- TOML.Value: instance Control.DeepSeq.NFData TOML.Value.Value
- TOML.Value: instance GHC.Generics.Generic TOML.Value.Value
+ TOML: getFieldOr :: DecodeTOML a => a -> Text -> Decoder a
+ TOML.Decode: getFieldOr :: DecodeTOML a => a -> Text -> Decoder a
+ TOML.Decode: instance TOML.Decode.DecodeTOML GHC.Num.Integer.Integer
+ TOML.Decode: instance TOML.Decode.DecodeTOML GHC.Num.Natural.Natural
+ TOML.Error: instance GHC.Exception.Type.Exception TOML.Error.TOMLError

Files

CHANGELOG.md view
@@ -1,3 +1,9 @@+## v0.2.0.0++* Add getFieldOr [#10](https://github.com/brandonchinn178/toml-reader/issues/10)+* Handle extremely large float values [#8](https://github.com/brandonchinn178/toml-reader/issues/8)+* Add Exception instance for TOMLError+ ## v0.1.0.0  Initial release
README.md view
@@ -9,10 +9,14 @@ ## Usage  ```hs+{-# LANGUAGE OverloadedStrings #-}++import TOML (DecodeTOML, tomlDecoder, getField, decodeFile)+ data MyConfig = MyConfig   { field1 :: Int   , field2 :: Bool-  }+  } deriving (Show)  instance DecodeTOML MyConfig where   tomlDecoder =
src/TOML.hs view
@@ -8,6 +8,7 @@    -- ** Decoding getters   getField,+  getFieldOr,   getFields,   getFieldOpt,   getFieldsOpt,
src/TOML/Decode.hs view
@@ -20,6 +20,7 @@    -- ** Decoder getters   getField,+  getFieldOr,   getFields,   getFieldOpt,   getFieldsOpt,@@ -57,6 +58,7 @@ import qualified Data.List.NonEmpty as NonEmpty import Data.Map (Map) import qualified Data.Map.Strict as Map+import Data.Maybe (fromMaybe) import qualified Data.Monoid as Monoid import Data.Proxy (Proxy (..)) import Data.Ratio (Ratio)@@ -278,6 +280,22 @@ -} getField :: DecodeTOML a => Text -> Decoder a getField = getFieldWith tomlDecoder++{- |+Decode a field in a TOML Value or succeed with a default value when the field is missing.++@+a = 1+# b is missing+@++@+-- MyConfig 1 "asdf"+MyConfig \<$> getFieldOr 42 "a" \<*> getFieldOr "asdf" "b"+@+-}+getFieldOr :: DecodeTOML a => a -> Text -> Decoder a+getFieldOr def key = fromMaybe def <$> getFieldOpt key  -- | Same as 'getField', except with the given 'Decoder'. getFieldWith :: Decoder a -> Text -> Decoder a
src/TOML/Error.hs view
@@ -11,6 +11,7 @@   renderTOMLError, ) where +import Control.Exception (Exception (..)) import Data.List.NonEmpty (NonEmpty) import qualified Data.List.NonEmpty as NonEmpty import Data.Text (Text)@@ -23,6 +24,9 @@   | NormalizeError NormalizeError   | DecodeError DecodeContext DecodeError   deriving (Show, Eq)++instance Exception TOMLError where+  displayException = Text.unpack . renderTOMLError  data NormalizeError   = -- | When a key is defined twice, e.g.
src/TOML/Parser.hs view
@@ -382,7 +382,13 @@           [ try $ (,) <$> pure "" <*> parseExp           , (,) <$> parseFrac <*> optionalOr "" parseExp           ]-      pure $ readFloat $ intPart <> fracPart <> expPart++      -- guess if the exponent is too big to fit in a double precision float anyway.+      -- https://github.com/brandonchinn178/toml-reader/issues/8+      pure $+        if Text.length expPart > 7+          then inf+          else readFloat $ intPart <> fracPart <> expPart      parseExp =       fmap Text.concat . sequence $
src/TOML/Value.hs view
@@ -1,5 +1,3 @@-{-# LANGUAGE DeriveAnyClass #-}-{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE OverloadedStrings #-} @@ -9,13 +7,11 @@   Table, ) where -import Control.DeepSeq (NFData) import Data.Map.Strict (Map) import qualified Data.Map.Strict as Map import Data.Text (Text) import qualified Data.Text as Text import Data.Time (Day, LocalTime, TimeOfDay, TimeZone)-import GHC.Generics (Generic)  type Table = Map Text Value @@ -24,13 +20,13 @@   | Array [Value]   | String Text   | Integer Integer-  | Float Double+  | Float Double -- TOML spec specifies this must be a double precision float: https://github.com/toml-lang/toml/issues/538   | Boolean Bool   | OffsetDateTime (LocalTime, TimeZone)   | LocalDateTime LocalTime   | LocalDate Day   | LocalTime TimeOfDay-  deriving (Show, Eq, Generic, NFData)+  deriving (Show, Eq)  -- | Render a Value in pseudo-JSON format. renderValue :: Value -> Text
test/tasty/Main.hs view
@@ -2,6 +2,7 @@  import qualified TOML.DecodeTest import qualified TOML.ErrorTest+import qualified TOML.ParserTest import qualified TOML.Utils.MapTest import qualified TOML.Utils.NonEmptyTest @@ -12,5 +13,6 @@       [ TOML.Utils.MapTest.test       , TOML.Utils.NonEmptyTest.test       , TOML.ErrorTest.test+      , TOML.ParserTest.test       , TOML.DecodeTest.test       ]
test/tasty/TOML/DecodeTest.hs view
@@ -19,6 +19,7 @@   getField,   getFieldOpt,   getFieldOptWith,+  getFieldOr,   getFieldWith,   getFields,   getFieldsOpt,@@ -50,6 +51,15 @@           decodeWith (getField @Int "a") "" @?= Left (DecodeError [Key "a"] MissingField)       , testCase "errors if field is the wrong type" $           decodeWith (getField @Int "a") "a = true" @?= Left (DecodeError [Key "a"] (TypeMismatch (Boolean True)))+      ]+  , testGroup+      "getFieldOr"+      [ testCase "decodes field" $+          decodeWith (getFieldOr @Int 42 "a") "a = 1" @?= Right 1+      , testCase "returns default if field does not exist" $+          decodeWith (getFieldOr @Int 42 "a") "" @?= Right 42+      , testCase "errors if field is the wrong type" $+          decodeWith (getFieldOr @Int 42 "a") "a = true" @?= Left (DecodeError [Key "a"] (TypeMismatch (Boolean True)))       ]   , testGroup       "getFieldOpt"
+ test/tasty/TOML/ParserTest.hs view
@@ -0,0 +1,20 @@+{-# LANGUAGE OverloadedStrings #-}++module TOML.ParserTest (test) where++import qualified Data.Map.Strict as Map+import Test.Tasty (TestTree, testGroup)+import Test.Tasty.HUnit++import TOML.Parser (parseTOML)+import TOML.Value (Value (..))++test :: TestTree+test =+  testGroup+    "TOML.Parser"+    [ testCase "Large floats are parsed efficiently" $+        case parseTOML "" "a = 1e1000000000000000000000000000000000000000000000000000000000000000" of+          Right (Table t) -> Map.lookup "a" t @?= Just (Float (read "Infinity"))+          result -> assertFailure $ "Got unexpected result: " ++ show result+    ]
toml-reader.cabal view
@@ -1,11 +1,11 @@ cabal-version: 1.12 --- This file has been generated from package.yaml by hpack version 0.34.4.+-- This file has been generated from package.yaml by hpack version 0.35.0. -- -- see: https://github.com/sol/hpack  name:           toml-reader-version:        0.1.0.0+version:        0.2.0.0 synopsis:       TOML format parser compliant with v1.0.0. description:    TOML format parser compliant with v1.0.0. See README.md for more details. category:       TOML, Text, Configuration@@ -53,14 +53,13 @@   build-depends:       base >=4.9 && <5     , containers >=0.6.0.1 && <0.7-    , deepseq >=1.4.4.0 && <1.5     , megaparsec >=7.0.5 && <9.3     , parser-combinators >=1.1.0 && <1.4-    , text >=1.2.3.1 && <1.3-    , time >=1.8.0.2 && <1.12+    , text >=1.2.3.1 && <2.1+    , time >=1.8.0.2 && <1.13+  default-language: Haskell2010   if impl(ghc >= 8.0)     ghc-options: -Wcompat -Wincomplete-record-updates -Wincomplete-uni-patterns -Wnoncanonical-monad-instances-  default-language: Haskell2010  test-suite parser-validator   type: exitcode-stdio-1.0@@ -82,9 +81,9 @@     , toml-reader     , unordered-containers     , vector+  default-language: Haskell2010   if impl(ghc >= 8.0)     ghc-options: -Wcompat -Wincomplete-record-updates -Wincomplete-uni-patterns -Wnoncanonical-monad-instances-  default-language: Haskell2010  test-suite toml-reader-tests   type: exitcode-stdio-1.0@@ -92,6 +91,7 @@   other-modules:       TOML.DecodeTest       TOML.ErrorTest+      TOML.ParserTest       TOML.Utils.MapTest       TOML.Utils.NonEmptyTest       Paths_toml_reader@@ -107,6 +107,6 @@     , text     , time     , toml-reader+  default-language: Haskell2010   if impl(ghc >= 8.0)     ghc-options: -Wcompat -Wincomplete-record-updates -Wincomplete-uni-patterns -Wnoncanonical-monad-instances-  default-language: Haskell2010