toml-parser 1.3.1.2 → 1.3.1.3
raw patch · 5 files changed
+42/−14 lines, 5 files
Files
- ChangeLog.md +4/−0
- src/Toml/Semantics.hs +7/−7
- src/Toml/ToValue.hs +19/−5
- test/TomlSpec.hs +10/−0
- toml-parser.cabal +2/−2
ChangeLog.md view
@@ -1,5 +1,9 @@ # Revision history for toml-parser +## 1.3.1.3++* Bugfix: Previous fix admitted some invalid inline tables - these are now rejected+ ## 1.3.1.2 * Bugfix: In some cases overlapping keys in inline tables could throw an exception
src/Toml/Semantics.hs view
@@ -122,20 +122,20 @@ -- | Build a 'Table' value out of a list of key-value pairs. These keys are -- checked to not overlap. In the case of overlap a 'SemanticError' is returned.-constructTable :: [(Key, Value)] -> M Table-constructTable = foldM (uncurry . addEntry) Map.empty+constructInlineTable :: [(Key, Value)] -> M Table+constructInlineTable kvs = framesToTable <$> foldM (uncurry . addEntry) Map.empty kvs where -- turns x.y.z = v into a nested table of one leaf value- singleCase = foldr (\k v -> Table (Map.singleton (locThing k) v))+ singleCase = foldr (\k v -> FrameTable Open (Map.singleton (locThing k) v)) addEntry tab (key :| subkey) val = Map.alterF f (locThing key) tab where -- no existing assignment at this parent key - no more validation needed- f Nothing = pure (Just (singleCase val subkey))+ f Nothing = pure (Just (singleCase (FrameValue val) subkey)) -- there's already a table at this parent key, attempt to extend it- f (Just (Table subtab)) | Just subkey' <- NonEmpty.nonEmpty subkey =- Just . Table <$> addEntry subtab subkey' val+ f (Just (FrameTable _ subtab)) | Just subkey' <- NonEmpty.nonEmpty subkey =+ Just . FrameTable Open <$> addEntry subtab subkey' val -- attempted to overwrite an existing assignment, abort f _ = invalidKey key AlreadyAssigned@@ -231,7 +231,7 @@ ValDay x -> Right (Day x) ValArray xs -> Array <$> traverse valToValue xs ValTable kvs -> do entries <- (traverse . traverse) valToValue kvs- Table <$> constructTable entries+ Table <$> constructInlineTable entries -- | Abort validation by reporting an error about the given key. invalidKey ::
src/Toml/ToValue.hs view
@@ -1,4 +1,4 @@-{-# LANGUAGE TypeFamilies #-}+{-# LANGUAGE TypeFamilies #-} -- needed for type equality on old GHC {-| Module : Toml.ToValue Description : Automation for converting application values to TOML.@@ -11,7 +11,7 @@ Because the top-level TOML document is always a table, the 'ToTable' class is for types that specifically support-conversion from a 'Table'.+conversion to a 'Table'. "Toml.ToValue.Generic" can be used to derive instances of 'ToTable' automatically for record types.@@ -73,8 +73,21 @@ -- | Class for things that can be embedded into a TOML table. ----- Implement this for things that embed into a 'Table' and then+-- Implement this for things that always embed into a 'Table' and then -- the 'ToValue' instance can be derived with 'defaultTableToValue'.+--+-- @+-- instance ToValue Example where+-- toValue = defaultTableToValue+--+-- -- Option 1: Manual instance+-- instance ToTable Example where+-- toTable x = 'table' ["field1" '.=' field1 x, "field2" '.=' field2 x]+--+-- -- Option 2: GHC.Generics derived instance using Toml.ToValue.Generic+-- instance ToTable Example where+-- toTable = genericToTable+-- @ class ToValue a => ToTable a where -- | Convert a single value into a table@@ -88,7 +101,8 @@ instance (ToKey k, ToValue v) => ToValue (Map k v) where toValue = defaultTableToValue --- | Convert to a table key+-- | Convert to a table key. This class enables various string types to be+-- used as the keys of a 'Map' when converting into TOML tables. -- -- @since 1.3.0.0 class ToKey a where@@ -104,7 +118,7 @@ -- -- @since 1.3.0.0 instance ToKey Data.Text.Text where- toKey =Data.Text.unpack+ toKey = Data.Text.unpack -- | toKey = unpack --
test/TomlSpec.hs view
@@ -566,6 +566,16 @@ parse [quoteStr| x = {a.b = 1, a = 2}|] `shouldBe` Left "1:15: key error: a is already assigned"+ + it "checks for overwrites from other inline tables" $+ parse [quoteStr|+ tab = { inner = { dog = "best" }, inner.cat = "worst" }|]+ `shouldBe` Left "1:35: key error: inner is already assigned"+ + it "checks for overlaps of other inline tables" $+ parse [quoteStr|+ tbl = { fruit = { apple.color = "red" }, fruit.apple.texture = { smooth = true } }|]+ `shouldBe` Left "1:42: key error: fruit is already assigned" describe "array of tables" do it "supports array of tables syntax" $
toml-parser.cabal view
@@ -1,6 +1,6 @@ cabal-version: 3.0 name: toml-parser-version: 1.3.1.2+version: 1.3.1.3 synopsis: TOML 1.0.0 parser description: TOML parser using generated lexers and parsers with@@ -13,7 +13,7 @@ copyright: 2023 Eric Mertens category: Text build-type: Simple-tested-with: GHC == {8.10.7, 9.0.2, 9.2.8, 9.4.8, 9.6.3, 9.8.1}+tested-with: GHC == {8.10.7, 9.0.2, 9.2.8, 9.4.8, 9.6.4, 9.8.1} extra-doc-files: ChangeLog.md