yamlet 1.0.0.1 → 1.0.0.2
raw patch · 9 files changed
+489/−30 lines, 9 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Yamlet: [$sel:after:Comments] :: Comments -> ![Line]
- Yamlet: [$sel:before:Comments] :: Comments -> ![Line]
- Yamlet: [$sel:comments:Commented] :: Commented a -> !Comments
- Yamlet: [$sel:inline:Comments] :: Comments -> !Maybe Text
- Yamlet: [$sel:offset:Located] :: Located a -> !Offset
- Yamlet: [$sel:value:Commented] :: Commented a -> !a
- Yamlet: [$sel:value:Located] :: Located a -> !a
- Yamlet.Error: [$sel:column:Location] :: Location -> !Int
- Yamlet.Error: [$sel:line:Location] :: Location -> !Int
- Yamlet.Error: [$sel:location:Error] :: Error -> !Location
- Yamlet.Error: [$sel:message:Error] :: Error -> !String
- Yamlet.Error: [$sel:offset:Location] :: Location -> !Offset
- Yamlet.Error: [$sel:path:Error] :: Error -> !Path
- Yamlet.Error: [$sel:sourceIndex:Error] :: Error -> !Int
- Yamlet.Error: [$sel:sourceLine:Error] :: Error -> !Text
- Yamlet.Generic: [$sel:constructorTagModifier:YamlOptions] :: YamlOptions -> !String -> String
- Yamlet.Generic: [$sel:contentsKey:YamlOptions] :: YamlOptions -> !Text
- Yamlet.Generic: [$sel:fieldLabelModifier:YamlOptions] :: YamlOptions -> !String -> String
- Yamlet.Generic: [$sel:omitNullFields:YamlOptions] :: YamlOptions -> !Bool
- Yamlet.Generic: [$sel:rejectUnknownFields:YamlOptions] :: YamlOptions -> !Bool
- Yamlet.Generic: [$sel:tagKey:YamlOptions] :: YamlOptions -> !Text
- Yamlet.Generic: [$sel:tagSingleConstructors:YamlOptions] :: YamlOptions -> !Bool
- Yamlet.Internal.FromYaml: [$sel:duplicates:Object] :: Object -> !Bool
- Yamlet.Internal.FromYaml: [$sel:entries:Object] :: Object -> ![(Node, Node)]
- Yamlet.Internal.FromYaml: [$sel:index:Object] :: Object -> !Map Text (Node, Node)
- Yamlet.Internal.FromYaml: [$sel:node:Object] :: Object -> !Node
- Yamlet.Internal.FromYaml: [$sel:otherKeys:Object] :: Object -> ![(Node, Value)]
- Yamlet.Internal.Parser.Monad: [$sel:array:Env] :: Env -> !Array
- Yamlet.Internal.Parser.Monad: [$sel:base:Env] :: Env -> !Int
- Yamlet.Internal.Parser.Monad: [$sel:end:Env] :: Env -> !Int
- Yamlet.Internal.Parser.Monad: [$sel:handles:Env] :: Env -> !Map Text Text
- Yamlet.Internal.Parser.Monad: [$sel:streamEnd:Env] :: Env -> !Int
- Yamlet.Internal.Render: [$sel:forceBlock:RenderOptions] :: RenderOptions -> !Bool
- Yamlet.Internal.Syntax: [$sel:after:Comments] :: Comments -> ![Line]
- Yamlet.Internal.Syntax: [$sel:anchor:Props] :: Props -> !Maybe Text
- Yamlet.Internal.Syntax: [$sel:before:Comments] :: Comments -> ![Line]
- Yamlet.Internal.Syntax: [$sel:comments:Commented] :: Commented a -> !Comments
- Yamlet.Internal.Syntax: [$sel:comments:Node] :: Node -> !Comments
- Yamlet.Internal.Syntax: [$sel:content:Node] :: Node -> !Content
- Yamlet.Internal.Syntax: [$sel:docComments:Document] :: Document -> !Comments
- Yamlet.Internal.Syntax: [$sel:endOffset:Node] :: Node -> !Offset
- Yamlet.Internal.Syntax: [$sel:explicitEnd:Document] :: Document -> !Bool
- Yamlet.Internal.Syntax: [$sel:explicitStart:Document] :: Document -> !Bool
- Yamlet.Internal.Syntax: [$sel:inline:Comments] :: Comments -> !Maybe Text
- Yamlet.Internal.Syntax: [$sel:major:YamlVersion] :: YamlVersion -> !Int
- Yamlet.Internal.Syntax: [$sel:minor:YamlVersion] :: YamlVersion -> !Int
- Yamlet.Internal.Syntax: [$sel:offset:Located] :: Located a -> !Offset
- Yamlet.Internal.Syntax: [$sel:offset:Node] :: Node -> !Offset
- Yamlet.Internal.Syntax: [$sel:props:Node] :: Node -> !Props
- Yamlet.Internal.Syntax: [$sel:root:Document] :: Document -> !Node
- Yamlet.Internal.Syntax: [$sel:tag:Props] :: Props -> !Tag
- Yamlet.Internal.Syntax: [$sel:value:Commented] :: Commented a -> !a
- Yamlet.Internal.Syntax: [$sel:value:Located] :: Located a -> !a
- Yamlet.Internal.Syntax: [$sel:version:Document] :: Document -> !Maybe YamlVersion
- Yamlet.Syntax: [$sel:after:Comments] :: Comments -> ![Line]
- Yamlet.Syntax: [$sel:anchor:Props] :: Props -> !Maybe Text
- Yamlet.Syntax: [$sel:before:Comments] :: Comments -> ![Line]
- Yamlet.Syntax: [$sel:comments:Node] :: Node -> !Comments
- Yamlet.Syntax: [$sel:content:Node] :: Node -> !Content
- Yamlet.Syntax: [$sel:docComments:Document] :: Document -> !Comments
- Yamlet.Syntax: [$sel:endOffset:Node] :: Node -> !Offset
- Yamlet.Syntax: [$sel:explicitEnd:Document] :: Document -> !Bool
- Yamlet.Syntax: [$sel:explicitStart:Document] :: Document -> !Bool
- Yamlet.Syntax: [$sel:forceBlock:RenderOptions] :: RenderOptions -> !Bool
- Yamlet.Syntax: [$sel:inline:Comments] :: Comments -> !Maybe Text
- Yamlet.Syntax: [$sel:major:YamlVersion] :: YamlVersion -> !Int
- Yamlet.Syntax: [$sel:minor:YamlVersion] :: YamlVersion -> !Int
- Yamlet.Syntax: [$sel:offset:Node] :: Node -> !Offset
- Yamlet.Syntax: [$sel:props:Node] :: Node -> !Props
- Yamlet.Syntax: [$sel:root:Document] :: Document -> !Node
- Yamlet.Syntax: [$sel:tag:Props] :: Props -> !Tag
- Yamlet.Syntax: [$sel:version:Document] :: Document -> !Maybe YamlVersion
+ Yamlet: [after] :: Comments -> ![Line]
+ Yamlet: [before] :: Comments -> ![Line]
+ Yamlet: [comments] :: Commented a -> !Comments
+ Yamlet: [inline] :: Comments -> !Maybe Text
+ Yamlet: [offset] :: Located a -> !Offset
+ Yamlet: [value] :: Commented a -> !a
+ Yamlet.Error: [column] :: Location -> !Int
+ Yamlet.Error: [line] :: Location -> !Int
+ Yamlet.Error: [location] :: Error -> !Location
+ Yamlet.Error: [message] :: Error -> !String
+ Yamlet.Error: [offset] :: Location -> !Offset
+ Yamlet.Error: [path] :: Error -> !Path
+ Yamlet.Error: [sourceIndex] :: Error -> !Int
+ Yamlet.Error: [sourceLine] :: Error -> !Text
+ Yamlet.Generic: [constructorTagModifier] :: YamlOptions -> !String -> String
+ Yamlet.Generic: [contentsKey] :: YamlOptions -> !Text
+ Yamlet.Generic: [fieldLabelModifier] :: YamlOptions -> !String -> String
+ Yamlet.Generic: [omitNullFields] :: YamlOptions -> !Bool
+ Yamlet.Generic: [rejectUnknownFields] :: YamlOptions -> !Bool
+ Yamlet.Generic: [tagKey] :: YamlOptions -> !Text
+ Yamlet.Generic: [tagSingleConstructors] :: YamlOptions -> !Bool
+ Yamlet.Internal.FromYaml: [duplicates] :: Object -> !Bool
+ Yamlet.Internal.FromYaml: [entries] :: Object -> ![(Node, Node)]
+ Yamlet.Internal.FromYaml: [index] :: Object -> !Map Text (Node, Node)
+ Yamlet.Internal.FromYaml: [node] :: Object -> !Node
+ Yamlet.Internal.FromYaml: [otherKeys] :: Object -> ![(Node, Value)]
+ Yamlet.Internal.Parser.Monad: [array] :: Env -> !Array
+ Yamlet.Internal.Parser.Monad: [base] :: Env -> !Int
+ Yamlet.Internal.Parser.Monad: [end] :: Env -> !Int
+ Yamlet.Internal.Parser.Monad: [handles] :: Env -> !Map Text Text
+ Yamlet.Internal.Parser.Monad: [streamEnd] :: Env -> !Int
+ Yamlet.Internal.Render: [forceBlock] :: RenderOptions -> !Bool
+ Yamlet.Internal.Schema: isNull :: Text -> Bool
+ Yamlet.Internal.Syntax: [after] :: Comments -> ![Line]
+ Yamlet.Internal.Syntax: [anchor] :: Props -> !Maybe Text
+ Yamlet.Internal.Syntax: [before] :: Comments -> ![Line]
+ Yamlet.Internal.Syntax: [comments] :: Commented a -> !Comments
+ Yamlet.Internal.Syntax: [content] :: Node -> !Content
+ Yamlet.Internal.Syntax: [docComments] :: Document -> !Comments
+ Yamlet.Internal.Syntax: [endOffset] :: Node -> !Offset
+ Yamlet.Internal.Syntax: [explicitEnd] :: Document -> !Bool
+ Yamlet.Internal.Syntax: [explicitStart] :: Document -> !Bool
+ Yamlet.Internal.Syntax: [inline] :: Comments -> !Maybe Text
+ Yamlet.Internal.Syntax: [major] :: YamlVersion -> !Int
+ Yamlet.Internal.Syntax: [minor] :: YamlVersion -> !Int
+ Yamlet.Internal.Syntax: [offset] :: Located a -> !Offset
+ Yamlet.Internal.Syntax: [props] :: Node -> !Props
+ Yamlet.Internal.Syntax: [root] :: Document -> !Node
+ Yamlet.Internal.Syntax: [tag] :: Props -> !Tag
+ Yamlet.Internal.Syntax: [value] :: Located a -> !a
+ Yamlet.Internal.Syntax: [version] :: Document -> !Maybe YamlVersion
+ Yamlet.Syntax: [after] :: Comments -> ![Line]
+ Yamlet.Syntax: [anchor] :: Props -> !Maybe Text
+ Yamlet.Syntax: [before] :: Comments -> ![Line]
+ Yamlet.Syntax: [comments] :: Node -> !Comments
+ Yamlet.Syntax: [content] :: Node -> !Content
+ Yamlet.Syntax: [docComments] :: Document -> !Comments
+ Yamlet.Syntax: [endOffset] :: Node -> !Offset
+ Yamlet.Syntax: [explicitEnd] :: Document -> !Bool
+ Yamlet.Syntax: [explicitStart] :: Document -> !Bool
+ Yamlet.Syntax: [forceBlock] :: RenderOptions -> !Bool
+ Yamlet.Syntax: [inline] :: Comments -> !Maybe Text
+ Yamlet.Syntax: [major] :: YamlVersion -> !Int
+ Yamlet.Syntax: [minor] :: YamlVersion -> !Int
+ Yamlet.Syntax: [offset] :: Node -> !Offset
+ Yamlet.Syntax: [props] :: Node -> !Props
+ Yamlet.Syntax: [root] :: Document -> !Node
+ Yamlet.Syntax: [tag] :: Props -> !Tag
+ Yamlet.Syntax: [version] :: Document -> !Maybe YamlVersion
- Yamlet.Decode: parseItems :: forall a. (Node -> Parser a) -> [Node] -> Parser [a]
+ Yamlet.Decode: parseItems :: (Node -> Parser a) -> [Node] -> Parser [a]
- Yamlet.Generic: class GConstructors f
+ Yamlet.Generic: class GConstructors (f :: k)
- Yamlet.Generic: class GEncoding (e :: SumEncodingKind) f
+ Yamlet.Generic: class GEncoding (e :: SumEncodingKind) (f :: k)
- Yamlet.Generic: class GFields f
+ Yamlet.Generic: class GFields (f :: k)
- Yamlet.Generic: class GFromConstructor f
+ Yamlet.Generic: class GFromConstructor (f :: k -> Type)
- Yamlet.Generic: class GFromFields f
+ Yamlet.Generic: class GFromFields (f :: k -> Type)
- Yamlet.Generic: class GToConstructor f
+ Yamlet.Generic: class GToConstructor (f :: k -> Type)
- Yamlet.Generic: class GToFields f
+ Yamlet.Generic: class GToFields (f :: k -> Type)
- Yamlet.Generic: class () => Generic a
+ Yamlet.Generic: class Generic a
- Yamlet.Generic: genericParseYaml :: forall a f. (Generic a, GenericYamlOptions a, GDatatype (Rep a), Constructors (Rep a) ~ f, GConstructors f, GEncoding (SumEncoding a) f, GFromConstructor f) => Node -> Parser a
+ Yamlet.Generic: genericParseYaml :: forall a (f :: Type -> Type). (Generic a, GenericYamlOptions a, GDatatype (Rep a), Constructors (Rep a) ~ f, GConstructors f, GEncoding (SumEncoding a) f, GFromConstructor f) => Node -> Parser a
- Yamlet.Generic: genericToYaml :: forall a f. (Generic a, GenericYamlOptions a, GDatatype (Rep a), Constructors (Rep a) ~ f, GConstructors f, GEncoding (SumEncoding a) f, GToConstructor f) => a -> Node
+ Yamlet.Generic: genericToYaml :: forall a (f :: Type -> Type). (Generic a, GenericYamlOptions a, GDatatype (Rep a), Constructors (Rep a) ~ f, GConstructors f, GEncoding (SumEncoding a) f, GToConstructor f) => a -> Node
- Yamlet.Generic: type Constructors r :: Type -> Type;
+ Yamlet.Generic: type Constructors (r :: Type -> Type) :: Type -> Type;
- Yamlet.Generic: type SumEncoding a = TaggedObject;
+ Yamlet.Generic: type SumEncoding a = 'TaggedObject;
- Yamlet.Internal.FromYaml: parseItems :: forall a. (Node -> Parser a) -> [Node] -> Parser [a]
+ Yamlet.Internal.FromYaml: parseItems :: (Node -> Parser a) -> [Node] -> Parser [a]
- Yamlet.Internal.Utils: strictMap :: forall a b. (a -> b) -> [a] -> [b]
+ Yamlet.Internal.Utils: strictMap :: (a -> b) -> [a] -> [b]
Files
- CHANGELOG.md +7/−0
- bench/Yamlet/Bench/Derive.hs +122/−1
- bench/Yamlet/Bench/Derive/Generic.hs +127/−0
- bench/Yamlet/Bench/Derive/Manual.hs +146/−0
- src/Yamlet/Generic.hs +56/−12
- src/Yamlet/Internal/FromYaml.hs +20/−14
- src/Yamlet/Internal/Schema.hs +3/−0
- src/Yamlet/Internal/View.hs +7/−2
- yamlet.cabal +1/−1
CHANGELOG.md view
@@ -1,3 +1,10 @@+# yamlet-1.0.0.2 (2026-11-17)+* A `FromYaml` instance derived via `GenericYaml` for a type with named fields,+ and a `ToYaml` instance for a record, compile faster with optimization, more+ so for a type with more fields.+* `parseFieldMaybe` is faster, and so is a `ToYaml` instance derived via+ `GenericYaml` with `omitNullFields`.+ # yamlet-1.0.0.1 (2026-10-12) * Allow text-iso8601 0.1.1.2, so that yamlet builds together with packages that do not support text-iso8601 0.2 yet. With text-iso8601 0.1, a time of
bench/Yamlet/Bench/Derive.hs view
@@ -1,3 +1,5 @@+{-# LANGUAGE AllowAmbiguousTypes #-}+ -- | The benchmarks that compare generic instances with written ones. module Yamlet.Bench.Derive ( checkDerived@@ -7,23 +9,46 @@ import Control.DeepSeq import Control.Monad import Data.ByteString qualified as BS+import Data.Text qualified as T import Test.Tasty.Bench import Yamlet+import Yamlet.Bench.Derive.Fields import Yamlet.Bench.Derive.Generic qualified as G import Yamlet.Bench.Derive.Manual qualified as M --- | Fail if the two versions of a type give different YAML.+-- | Fail if the two versions of a type give different YAML, or decode the+-- YAML with missing keys to different values. checkDerived :: IO () checkDerived = do same "contents" G.mkX M.mkX same "flat" G.mkF M.mkF+ same "record" (fields G.A) (fields M.A)+ same "parameter" (fields (G.P @Int)) (fields (M.P @Int))+ same "omit null" (fields G.O) (fields M.O)+ same "omit default" (fields G.E) (fields M.E)+ sameMissing @G.A @M.A "null" nullInput+ sameMissing @G.D @M.D "default" defaultInput+ sameMissing @(G.P Int) @(M.P Int) "parameter" parameterInput where same :: (ToYaml g, ToYaml m) => String -> (Int -> g) -> (Int -> m) -> IO () same name mkG mkM = unless (encode (map mkG values) == encode (map mkM values)) $ fail ("the generic and the written instances differ: " ++ name) + sameMissing+ :: forall g m+ . (FromYaml g, ToYaml g, FromYaml m, ToYaml m)+ => String+ -> Node+ -> IO ()+ sameMissing name yaml =+ unless (encode (parsed @g) == encode (parsed @m)) $+ fail ("the generic and the written instances decode differently: " ++ name)+ where+ parsed :: FromYaml a => [a]+ parsed = either (error . show) id (runParser parseYaml yaml)+ -- | The benchmarks of each format, grouped by the operation, so that the -- times of the two versions are next to each other. deriveBenchmarks :: Benchmark@@ -32,6 +57,19 @@ "derive" [ format "contents" G.mkX M.mkX , format "flat" G.mkF M.mkF+ , format "record" (fields G.A) (fields M.A)+ , format "parameter" (fields (G.P @Int)) (fields (M.P @Int))+ , bgroup+ "missing"+ [ missing @G.A @M.A "null" nullInput+ , missing @G.D @M.D "default" defaultInput+ , missing @(G.P Int) @(M.P Int) "parameter" parameterInput+ ]+ , bgroup+ "omit"+ [ omit "null" (fields G.O) (fields M.O)+ , omit "default" (fields G.E) (fields M.E)+ ] ] where format@@ -79,5 +117,88 @@ bs :: BS.ByteString bs = encode gs + -- Only the decoders, because the encoders write each key.+ missing+ :: forall g m+ . (NFData g, FromYaml g, NFData m, FromYaml m)+ => String+ -> Node+ -> Benchmark+ missing name yaml =+ bgroup+ name+ [ bgroup+ "parseYaml"+ [ bench "generic" $ nf (runParser (parseYaml @[g])) yaml+ , bench "manual" $ nf (runParser (parseYaml @[m])) yaml+ ]+ , bgroup+ "decode"+ [ bench "generic" $ nf (either (error . show) id . decode @[g]) bs+ , bench "manual" $ nf (either (error . show) id . decode @[m]) bs+ ]+ ]+ where+ bs :: BS.ByteString+ bs = encode yaml++ -- Only the encoders, because leaving out null fields changes only them.+ omit+ :: forall g m+ . (ToYaml g, ToYaml m)+ => String+ -> (Int -> g)+ -> (Int -> m)+ -> Benchmark+ omit name mkG mkM =+ bgroup+ name+ [ bgroup+ "toYaml"+ [ bench "generic" $ nf toYaml gs+ , bench "manual" $ nf toYaml ms+ ]+ , bgroup+ "encode"+ [ bench "generic" $ nf encode gs+ , bench "manual" $ nf encode ms+ ]+ ]+ where+ gs :: [g]+ gs = map mkG values++ ms :: [m]+ ms = map mkM values+ values :: [Int] values = [1 .. 1000]++-- | The records without the keys of the fields of type 'Maybe', which decode+-- as null.+nullInput :: Node+nullInput = withoutKeys ["a02", "a05", "a08"] (toYaml (map (fields G.A) values))++-- | The records without the keys of the fields with a default.+defaultInput :: Node+defaultInput = withoutKeys (defaultKeys 'd') (toYaml (map (fields G.D) values))++-- | 'defaultInput' for the type with a parameter.+parameterInput :: Node+parameterInput =+ withoutKeys (defaultKeys 'p') (toYaml (map (fields (G.P @Int)) values))++-- | The keys of the fields with a default, for the prefix of the field names.+defaultKeys :: Char -> [T.Text]+defaultKeys prefix = [T.pack (prefix : n) | n <- ["02", "03", "05", "06", "08", "09"]]++-- | The sequence of mappings without the keys.+withoutKeys :: [T.Text] -> Node -> Node+withoutKeys keys n = case view n of+ SequenceView items -> toYaml (map dropKeys items)+ _ -> n+ where+ dropKeys :: Node -> Node+ dropKeys item = case view item of+ MappingView kvs -> mapping [kv | kv@(k, _) <- kvs, view k `notElem` map StringView keys]+ _ -> item
bench/Yamlet/Bench/Derive/Generic.hs view
@@ -6,6 +6,10 @@ , C (..) , X (..) , F (..)+ , D (..)+ , P (..)+ , O (..)+ , E (..) , mkX , mkF ) where@@ -63,6 +67,129 @@ deriving stock (Generic) deriving anyclass (NFData, GenericYamlOptions) deriving (FromYaml, ToYaml) via GenericYaml C++-- | A record with a default for the fields of type 'Maybe' and 'Int'.+data D = D+ { d01 :: T.Text+ , d02 :: Maybe Int+ , d03 :: Int+ , d04 :: T.Text+ , d05 :: Maybe Int+ , d06 :: Int+ , d07 :: T.Text+ , d08 :: Maybe Int+ , d09 :: Int+ , d10 :: T.Text+ }+ deriving stock (Generic)+ deriving anyclass (NFData)+ deriving (FromYaml, ToYaml) via GenericYaml D++instance GenericYamlOptions D where+ yamlDefault =+ Just+ D+ { d01 = requiredField+ , d02 = Nothing+ , d03 = 0+ , d04 = requiredField+ , d05 = Nothing+ , d06 = 0+ , d07 = requiredField+ , d08 = Nothing+ , d09 = 0+ , d10 = requiredField+ }++-- | 'D' with a type parameter. The default depends on the 'Num' instance of+-- the parameter.+data P a = P+ { p01 :: T.Text+ , p02 :: Maybe a+ , p03 :: a+ , p04 :: T.Text+ , p05 :: Maybe a+ , p06 :: a+ , p07 :: T.Text+ , p08 :: Maybe a+ , p09 :: a+ , p10 :: T.Text+ }+ deriving stock (Generic)+ deriving anyclass (NFData)++deriving via GenericYaml (P a) instance (FromYaml a, Num a) => FromYaml (P a)++deriving via GenericYaml (P a) instance (ToYaml a, Num a) => ToYaml (P a)++instance Num a => GenericYamlOptions (P a) where+ yamlDefault =+ Just+ P+ { p01 = requiredField+ , p02 = Nothing+ , p03 = 0+ , p04 = requiredField+ , p05 = Nothing+ , p06 = 0+ , p07 = requiredField+ , p08 = Nothing+ , p09 = 0+ , p10 = requiredField+ }++-- | A record that leaves out its null fields.+data O = O+ { o01 :: T.Text+ , o02 :: Maybe Int+ , o03 :: Int+ , o04 :: T.Text+ , o05 :: Maybe Int+ , o06 :: Int+ , o07 :: T.Text+ , o08 :: Maybe Int+ , o09 :: Int+ , o10 :: T.Text+ }+ deriving stock (Generic)+ deriving (ToYaml) via GenericYaml O++instance GenericYamlOptions O where+ yamlOptions = defaultYamlOptions {omitNullFields = True}++-- | 'O' with the default of 'D'. The encoder leaves out a null field only if+-- its default is null.+data E = E+ { e01 :: T.Text+ , e02 :: Maybe Int+ , e03 :: Int+ , e04 :: T.Text+ , e05 :: Maybe Int+ , e06 :: Int+ , e07 :: T.Text+ , e08 :: Maybe Int+ , e09 :: Int+ , e10 :: T.Text+ }+ deriving stock (Generic)+ deriving (ToYaml) via GenericYaml E++instance GenericYamlOptions E where+ yamlOptions = defaultYamlOptions {omitNullFields = True}+ yamlDefault =+ Just+ E+ { e01 = requiredField+ , e02 = Nothing+ , e03 = 0+ , e04 = requiredField+ , e05 = Nothing+ , e06 = 0+ , e07 = requiredField+ , e08 = Nothing+ , e09 = 0+ , e10 = requiredField+ } -- | A sum of records with the default encoding. data X = X1 A | X2 B | X3 C
bench/Yamlet/Bench/Derive/Manual.hs view
@@ -5,6 +5,10 @@ , C (..) , X (..) , F (..)+ , D (..)+ , P (..)+ , O (..)+ , E (..) , mkX , mkF ) where@@ -146,6 +150,148 @@ <*> parseFieldMaybe o "c08" <*> parseField o "c09" <*> parseField o "c10"++data D = D+ { d01 :: T.Text+ , d02 :: Maybe Int+ , d03 :: Int+ , d04 :: T.Text+ , d05 :: Maybe Int+ , d06 :: Int+ , d07 :: T.Text+ , d08 :: Maybe Int+ , d09 :: Int+ , d10 :: T.Text+ }+ deriving stock (Generic)+ deriving anyclass (NFData)++instance ToYaml D where+ toYaml x =+ mapping+ [ "d01" .= x.d01+ , "d02" .= x.d02+ , "d03" .= x.d03+ , "d04" .= x.d04+ , "d05" .= x.d05+ , "d06" .= x.d06+ , "d07" .= x.d07+ , "d08" .= x.d08+ , "d09" .= x.d09+ , "d10" .= x.d10+ ]++instance FromYaml D where+ parseYaml = withMapping $ \o ->+ D+ <$> parseField o "d01"+ <*> parseFieldMaybe o "d02"+ <*> parseFieldDefault o "d03" 0+ <*> parseField o "d04"+ <*> parseFieldMaybe o "d05"+ <*> parseFieldDefault o "d06" 0+ <*> parseField o "d07"+ <*> parseFieldMaybe o "d08"+ <*> parseFieldDefault o "d09" 0+ <*> parseField o "d10"++data P a = P+ { p01 :: T.Text+ , p02 :: Maybe a+ , p03 :: a+ , p04 :: T.Text+ , p05 :: Maybe a+ , p06 :: a+ , p07 :: T.Text+ , p08 :: Maybe a+ , p09 :: a+ , p10 :: T.Text+ }+ deriving stock (Generic)+ deriving anyclass (NFData)++instance ToYaml a => ToYaml (P a) where+ toYaml x =+ mapping+ [ "p01" .= x.p01+ , "p02" .= x.p02+ , "p03" .= x.p03+ , "p04" .= x.p04+ , "p05" .= x.p05+ , "p06" .= x.p06+ , "p07" .= x.p07+ , "p08" .= x.p08+ , "p09" .= x.p09+ , "p10" .= x.p10+ ]++instance (FromYaml a, Num a) => FromYaml (P a) where+ parseYaml = withMapping $ \o ->+ P+ <$> parseField o "p01"+ <*> parseFieldMaybe o "p02"+ <*> parseFieldDefault o "p03" 0+ <*> parseField o "p04"+ <*> parseFieldMaybe o "p05"+ <*> parseFieldDefault o "p06" 0+ <*> parseField o "p07"+ <*> parseFieldMaybe o "p08"+ <*> parseFieldDefault o "p09" 0+ <*> parseField o "p10"++-- | A record that leaves out its null fields.+data O = O+ { o01 :: T.Text+ , o02 :: Maybe Int+ , o03 :: Int+ , o04 :: T.Text+ , o05 :: Maybe Int+ , o06 :: Int+ , o07 :: T.Text+ , o08 :: Maybe Int+ , o09 :: Int+ , o10 :: T.Text+ }++instance ToYaml O where+ toYaml x =+ mapping $+ ["o01" .= x.o01]+ ++ present "o02" x.o02+ ++ ["o03" .= x.o03, "o04" .= x.o04]+ ++ present "o05" x.o05+ ++ ["o06" .= x.o06, "o07" .= x.o07]+ ++ present "o08" x.o08+ ++ ["o09" .= x.o09, "o10" .= x.o10]++-- | 'O' with a default. Only the generic encoder uses it.+data E = E+ { e01 :: T.Text+ , e02 :: Maybe Int+ , e03 :: Int+ , e04 :: T.Text+ , e05 :: Maybe Int+ , e06 :: Int+ , e07 :: T.Text+ , e08 :: Maybe Int+ , e09 :: Int+ , e10 :: T.Text+ }++instance ToYaml E where+ toYaml x =+ mapping $+ ["e01" .= x.e01]+ ++ present "e02" x.e02+ ++ ["e03" .= x.e03, "e04" .= x.e04]+ ++ present "e05" x.e05+ ++ ["e06" .= x.e06, "e07" .= x.e07]+ ++ present "e08" x.e08+ ++ ["e09" .= x.e09, "e10" .= x.e10]++-- | The entry of a field that is not 'Nothing'.+present :: ToYaml a => T.Text -> Maybe a -> [(Node, Node)]+present key = maybe [] (\v -> [key .= v]) -- | A sum of records with the default encoding. data X = X1 A | X2 B | X3 C
src/Yamlet/Generic.hs view
@@ -1,4 +1,5 @@ {-# LANGUAGE AllowAmbiguousTypes #-}+{-# LANGUAGE CPP #-} -- | Instances of t'Yamlet.Decode.FromYaml' and t'Yamlet.Encode.ToYaml' from -- the t'GHC.Generics.Generic' representation of a type.@@ -600,11 +601,13 @@ gDatatypeName = symbolVal (Proxy @name) --- | The names and the number of the constructors of a representation.+-- | The constructors of a representation: their names, if there are several,+-- and if none has fields. class GConstructors f where gConstructorNames :: [String] - gConstructorCount :: Int+ -- | The representation has more than one constructor.+ gSum :: Bool -- | No constructor has fields. gNullary :: Bool@@ -612,14 +615,14 @@ instance (GConstructors f, GConstructors g) => GConstructors (f :+: g) where gConstructorNames = gConstructorNames @f ++ gConstructorNames @g - gConstructorCount = gConstructorCount @f + gConstructorCount @g+ gSum = True gNullary = gNullary @f && gNullary @g instance GConstructors V1 where gConstructorNames = [] - gConstructorCount = 0+ gSum = False gNullary = True @@ -632,7 +635,7 @@ where gConstructorNames = [symbolVal (Proxy @name)] - gConstructorCount = 1+ gSum = False gNullary = gArity @f == 0 @@ -738,7 +741,10 @@ EitherShape (UnnamedField a) (UnnamedField _) = UnnamedField a isTagged :: forall f. GConstructors f => YamlOptions -> Bool-isTagged opts = opts.tagSingleConstructors || gConstructorCount @f > 1+-- GHC knows the value of 'gSum' at once. It would compare a number of+-- constructors only in a later pass. Until then, it would keep the fields of+-- a record once for each way to write the tag, which takes longer to compile.+isTagged opts = gSum @f || opts.tagSingleConstructors constructorTag :: YamlOptions -> String -> T.Text constructorTag opts = T.pack . opts.constructorTagModifier@@ -1413,19 +1419,57 @@ => GFromFields (S1 (MetaSel (Just name) u s d) (Rec0 a)) where gFromObject opts def o =- M1 . K1 <$> case M.lookup key o.index of+ M1 . K1 <$> case lookupIndex key o of Just entry -> parseEntry entry Nothing -> case def of- Just (M1 (K1 d))- | Just x <- defaultField d -> x <$ findKey o key- | otherwise -> missingKey o key+ -- For a type without parameters, GHC computes the value once, not at+ -- each missing key.+ Just (M1 (K1 d)) -> missingField (defaultField d) o key -- A missing field is null, if its type accepts null.- Nothing ->- maybe (missingKey o key) (<$ findKey o key) (succeeds parseYaml nullNode)+ Nothing -> missingField (nullValue @a) o key where key :: T.Text key = fieldKey @name opts {-# INLINE gFromObject #-}++-- The functions below are not in the instance above, because GHC copies the+-- instance into the decoder of each field. Taken out, they make the Core of a+-- derived decoder of a large record much smaller and faster to compile.+--+-- The comments on their pragmas come from two measurements. One compiles the+-- derived decoders of records with 10 to 100 fields with -O1. The other is+-- the derive benchmarks.++-- | The entry of a string key in a mapping.+lookupIndex :: T.Text -> Object -> Maybe (S.Node, S.Node)+lookupIndex key o = M.lookup key o.index++-- Without NOINLINE, the derived decoder of a record decodes slower, more so+-- with missing keys. GHC 9.12 needs INLINE instead. Without it, the derived+-- decoders of small records with a field of type 'Maybe' keep the generic+-- representation, and their inspection tests fail.+#if __GLASGOW_HASKELL__ == 912+{-# INLINE lookupIndex #-}+#else+{-# NOINLINE lookupIndex #-}+#endif++-- | The field of a missing key from its value, e.g. from the default or the+-- value of null, or an error without a value.+missingField :: Maybe a -> Object -> T.Text -> Parser a+missingField value o key = maybe (missingKey o key) (<$ findKey o key) value+-- Without the pragma, GHC copies the function into the decoder of each field,+-- which then takes longer to compile. With OPAQUE instead, the decoders compile+-- a little faster, but each missing key allocates more.+{-# NOINLINE missingField #-}++-- | The value of null for the type, if the type accepts null.+nullValue :: FromYaml a => Maybe a+nullValue = succeeds parseYaml nullNode+-- Without the pragma, GHC copies the decoder of the type into the decoder of+-- each field. The derived decoders of records without a default then take+-- longer to compile, and missing keys decode slower.+{-# NOINLINE nullValue #-} instance FromYaml a => GFromFields (S1 (MetaSel Nothing u s d) (Rec0 a)) where gFromValue n = M1 . K1 <$> parseNode parseYaml n
src/Yamlet/Internal/FromYaml.hs view
@@ -88,6 +88,7 @@ import Data.UUID.Types qualified as UUID import Data.Void import Data.Word+import GHC.Exts (oneShot) import GHC.Real import Numeric.Natural @@ -129,6 +130,11 @@ -- ends with 'pure' into '<*>'. Then they collect errors. newtype Parser a = Parser (S.Offset -> Result a) +-- | The parser of the function. GHC treats the function as called at most+-- once, so it can move work into the function instead of sharing it.+mkParser :: (S.Offset -> Result a) -> Parser a+mkParser f = Parser (oneShot f)+ -- | The errors of a parser and its value. The value of a parser with errors -- is 'failed', so the field of the value is lazy. --@@ -167,14 +173,14 @@ failure off msg = Result (OneError off msg []) failed instance Functor Parser where- fmap f (Parser g) = Parser $ \off -> case g off of+ fmap f (Parser g) = mkParser $ \off -> case g off of Result e a -> Result e (f a) -- '<*>' differs from 'ap', and '>>' differs from '*>', in the errors, but not -- in the results. instance Applicative Parser where- pure a = Parser $ \_ -> Result NoErrors a- Parser f <*> Parser g = Parser $ \off -> case f off of+ pure a = mkParser $ \_ -> Result NoErrors a+ Parser f <*> Parser g = mkParser $ \off -> case f off of Result e1 h -> case g off of Result e2 a -> Result (bothErrors e1 e2) (h a) @@ -182,12 +188,12 @@ -- index into a list after the check of its length. The default of '>>' uses -- '>>=', which stops there. instance Monad Parser where- Parser g >>= k = Parser $ \off -> case g off of+ Parser g >>= k = mkParser $ \off -> case g off of Result NoErrors a -> let Parser h = k a in h off Result e _ -> Result e failed instance MonadFail Parser where- fail msg = Parser $ \off -> failure off msg+ fail msg = mkParser $ \off -> failure off msg -- | Run a parser on a node. Each error is the offset of the node that caused -- it and the message. The errors are in the order of the offsets, and equal@@ -248,7 +254,7 @@ -- | The parser with the note after each of its errors at the offset, e.g. to -- say how the decoder read the node of the error. withNote :: S.Offset -> (S.Offset, String) -> Parser a -> Parser a-withNote off note (Parser g) = Parser $ \o -> case g o of+withNote off note (Parser g) = mkParser $ \o -> case g o of r@(Result NoErrors _) -> r Result e a -> Result (addNote e) a where@@ -260,11 +266,11 @@ -- | Run a parser on a node, so that 'fail' points to the node. parseNode :: (S.Node -> Parser a) -> S.Node -> Parser a-parseNode f n = let Parser g = f n in Parser $ \_ -> g n.offset+parseNode f n = let Parser g = f n in mkParser $ \_ -> g n.offset -- | Fail with an error that points to the given node. failAt :: S.Node -> String -> Parser a-failAt n msg = Parser $ \_ -> failure n.offset msg+failAt n msg = mkParser $ \_ -> failure n.offset msg -- | Fail with an error that the node is not of the expected kind, e.g. -- @typeMismatch "a list" n@ gives "expected a list, but got a string".@@ -304,7 +310,7 @@ -- 1 | [80] -- | ^ orElse :: Parser a -> Parser a -> Parser a-orElse (Parser g) (Parser h) = Parser $ \off -> case g off of+orElse (Parser g) (Parser h) = mkParser $ \off -> case g off of r@(Result NoErrors _) -> r _ -> h off @@ -460,7 +466,7 @@ -- the errors and the values are in accumulators until the end. A decoder of -- a list is @withSequence (parseItems parseYaml)@. parseItems :: forall a. (S.Node -> Parser a) -> [S.Node] -> Parser [a]-parseItems p xs0 = Parser $ \off -> go off NoErrors [] xs0+parseItems p xs0 = mkParser $ \off -> go off NoErrors [] xs0 where go :: S.Offset -> Errors -> [a] -> [S.Node] -> Result [a] go off !errs acc = \case@@ -486,7 +492,7 @@ -- reads the value of the first key. (errs, o) -> let Parser g = f o- in Parser $ \off -> case g off of+ in mkParser $ \off -> case g off of Result e _ -> Result (bothErrors errs e) failed _ -> typeMismatch "a mapping" n where@@ -781,7 +787,7 @@ -- 'findKey', a key with the same text that is not a string is the error -- instead. missingKey :: Object -> T.Text -> Parser a-missingKey o key = Parser $ \off ->+missingKey o key = mkParser $ \off -> let Parser g = findKey o key in case g off of Result NoErrors _@@ -1035,7 +1041,7 @@ -- The value is built lazily. The copy visits a node once per alias of -- it, as the limit of 'prepareWithin' allows. Right r -> pure $! copy r- Left ((off, msg) NE.:| notes) -> Parser $ \_ -> Result (OneError off msg notes) failed+ Left ((off, msg) NE.:| notes) -> mkParser $ \_ -> Result (OneError off msg notes) failed where -- The value in normal form, with copies of its texts. copy :: Value -> Value@@ -1311,7 +1317,7 @@ -> [a] -> Parser s insertUnique node item key insert start msg note xs =- Parser $ \off -> go off start NoErrors [] [] 0 xs+ mkParser $ \off -> go off start NoErrors [] [] 0 xs where -- The duplicates and the positions of the failed items are in reverse. -- The items in a copy of an alias share one offset, so an offset cannot
src/Yamlet/Internal/Schema.hs view
@@ -10,6 +10,7 @@ , resolvePlainExact , resolveTaggedExact , startsNumber+ , isNull , isPlainString , isPlainSafe , isPlainPortable@@ -115,6 +116,8 @@ isPlainPortable :: T.Text -> Bool isPlainPortable t = isPlainSafe t && not (isYaml11NonString t) +-- | The text of a null, e.g. @~@ or @null@, in a plain scalar or with the+-- null tag. isNull :: T.Text -> Bool isNull t = T.null t || t == "~" || t == "null" || t == "Null" || t == "NULL"
src/Yamlet/Internal/View.hs view
@@ -84,8 +84,13 @@ -- | The node is null. isNullNode :: S.Node -> Bool-isNullNode n = case view n of- NullView -> True+isNullNode n = case n.content of+ -- The cases of 'scalarValue' that give null, without resolving the other+ -- scalars, e.g. a number.+ S.ScalarContent style t -> case n.props.tag of+ S.NoTag -> style == S.Plain && isNull t+ S.NonSpecificTag -> False+ S.Tag tag -> tag == nullTag && isNull t _ -> False -- | The text of a string node.
yamlet.cabal view
@@ -1,7 +1,7 @@ cabal-version: 3.8 build-type: Simple name: yamlet-version: 1.0.0.1+version: 1.0.0.2 license: BSD-3-Clause license-file: LICENSE category: YAML