on master help sign in
Change the API and update doc of a few APIs in Serialize.TH
commit13f91aad8c3d7cb3b81dd74599ecbfe38cfc6a6e authorAdithya Kumar <adithya@composewell.com> dateTue 5 Sep 2023, 12:39 (3 years ago) parent791a5ee
3 files changed+52−49
core/src/Streamly/Internal/Data/Serialize/TH.hs+36−33 test/Streamly/Test/Data/Serialize.hs+9−9 test/Streamly/Test/Data/Unbox.hs+7−7
core/src/Streamly/Internal/Data/Serialize/TH.hs+36−33
@@ -10,8 +10,8 @@
1010--
1111module Streamly.Internal.Data.Serialize.TH
1212 ( deriveSerialize
13− , SerializeTHConfig(..)
14− , defaultSerializeTHConfig
13+ , Config(..)
14+ , defaultConfig
1515 , deriveSerializeWith
1616 ) where
1717
@@ -324,7 +324,7 @@ mkSerializeExpr tyOfTy =
324324-- Usage:
325325-- @
326326-- $(deriveSerializeInternal
327−-- defaultSerializeTHConfig
327+-- defaultConfig
328328-- [AppT (ConT ''Serialize) (VarT (mkName "b"))]
329329-- (AppT
330330-- (AppT (ConT ''CustomDataType) (VarT (mkName "a")))
@@ -339,8 +339,8 @@ mkSerializeExpr tyOfTy =
339339-- ])
340340-- @
341341deriveSerializeInternal ::
342− SerializeTHConfig -> Cxt -> Type -> [DataCon] -> Q [Dec]
343−deriveSerializeInternal (SerializeTHConfig{..}) preds headTy cons = do
342+ Config -> Cxt -> Type -> [DataCon] -> Q [Dec]
343+deriveSerializeInternal (Config{..}) preds headTy cons = do
344344 sizeOfMethod <- mkSizeOfExpr (typeOfType headTy cons)
345345 peekMethod <- mkDeserializeExpr headTy (typeOfType headTy cons)
346346 pokeMethod <- mkSerializeExpr (typeOfType headTy cons)
@@ -373,9 +373,9 @@ deriveSerializeInternal (SerializeTHConfig{..}) preds headTy cons = do
373373 return [plainInstanceD preds (AppT (ConT ''Serialize) headTy) methods]
374374
375375-- | Config to control how the 'Serialize' instance is generated.
376−data SerializeTHConfig =
377− SerializeTHConfig
378− { unconstrainedTypeVars :: [String]
376+data Config =
377+ Config
378+ { unconstrained :: [String]
379379 -- ^ Type variables that should not have the 'Serialize' constraint.
380380 --
381381 -- @
@@ -383,26 +383,26 @@ data SerializeTHConfig =
383383 -- @
384384 --
385385 -- @
386− -- conf = defaultConf {unconstrainedTypeVars = ["b", "d"]}
387− -- $(deriveSerializeWith conf ''CustomDataType)
386+ -- conf = defaultConf {unconstrained = ["b", "d"]}
387+ -- \$(deriveSerializeWith conf ''CustomDataType)
388388 -- @
389389 --
390390 -- @
391391 -- instance (Serialize a, Serialize c) => Serialize (CustomDataType a b c d) where
392392 -- ...
393393 -- @
394− , typeVarSubstitutions :: [(String, Type)]
395− -- ^ Substitute the type variable with the given type. All type
394+ , specializations :: [(String, Type)]
395+ -- ^ Specialize the type variable with the given type. All type
396396 -- variables listed here will not have the 'Serialize' constriant as
397− -- they are replaced.
397+ -- they are specialized.
398398 --
399399 -- @
400400 -- data CustomDataType f a = CustomDataType (f a)
401401 -- @
402402 --
403403 -- @
404− -- conf = defaultConf {typeVarSubstitutions = [("f", ''Identity)]}
405− -- $(deriveSerializeWith conf ''CustomDataType)
404+ -- conf = defaultConf {specializations = [("f", ''Identity)]}
405+ -- \$(deriveSerializeWith conf ''CustomDataType)
406406 -- @
407407 --
408408 -- @
@@ -418,20 +418,22 @@ data SerializeTHConfig =
418418 , inlineDeserialize :: Inline
419419 -- ^ Inline value for 'deserialize'. Default is Inline.
420420 , constructorTagAsString :: Bool
421− -- ^ If True, encode constructors as Latin-1 byte sequence. This
422− -- allows addition, removal, and reordering of constructors. If False
423− -- encode them as numbers. The default value is 'False'.
421+ -- ^ __Experimental__
422+ --
423+ -- If True, encode constructors using the constructor names as Latin-1
424+ -- byte sequence.
424425 , recordSyntaxWithHeader :: Bool
425− -- ^ If True, constructors with record syntax will be encoded in a
426− -- more compatible way. Allows addition, removal, and reordering of
427− -- fields. The default value is 'False'.
426+ -- ^ __Experimental__
427+ --
428+ -- If True, encode the keys of the record as a header and then
429+ -- serialize the data.
428430 }
429431
430−defaultSerializeTHConfig :: SerializeTHConfig
431−defaultSerializeTHConfig =
432− SerializeTHConfig
433− { unconstrainedTypeVars = []
434− , typeVarSubstitutions = []
432+defaultConfig :: Config
433+defaultConfig =
434+ Config
435+ { unconstrained = []
436+ , specializations = []
435437 , inlineSize = Inline
436438 , inlineSerialize = Inline
437439 , inlineDeserialize = Inline
@@ -439,12 +441,12 @@ defaultSerializeTHConfig =
439441 , recordSyntaxWithHeader = False
440442 }
441443
442−-- | Similar to 'deriveSerialize,' but take a 'SerializeTHConfig' to control how
444+-- | Similar to 'deriveSerialize,' but take a 'Config' to control how
443445-- the instance is generated.
444446--
445447-- Usage: @$(deriveSerializeWith config ''CustomDataType)@
446−deriveSerializeWith :: SerializeTHConfig -> Name -> Q [Dec]
447−deriveSerializeWith conf@(SerializeTHConfig {..}) name = do
448+deriveSerializeWith :: Config -> Name -> Q [Dec]
449+deriveSerializeWith conf@(Config {..}) name = do
448450 dt <- reifyDataType name
449451 let preds = map (unboxPred . VarT) (filterOutVars (dtTvs dt))
450452 headTy = appsT (ConT name) (map substituteVar (dtTvs dt))
@@ -453,13 +455,13 @@ deriveSerializeWith conf@(SerializeTHConfig {..}) name = do
453455
454456 where
455457 allUnconstrainedTypeVars =
456− unconstrainedTypeVars ++ map fst typeVarSubstitutions
458+ unconstrained ++ map fst specializations
457459 filterOutVars vs =
458460 map mkName
459461 $ filter (not . flip elem allUnconstrainedTypeVars)
460462 $ map nameBase vs
461463 substituteVar v =
462− case lookup (nameBase v) typeVarSubstitutions of
464+ case lookup (nameBase v) specializations of
463465 Nothing -> VarT v
464466 Just ty -> ty
465467
@@ -489,6 +491,7 @@ deriveSerializeWith conf@(SerializeTHConfig {..}) name = do
489491-- To control which type variables don't get the Serialize constraint, use
490492-- 'deriveSerializeWith'.
491493--
492−-- >>> deriveSerialize = deriveSerializeWith 'defaultSerializeTHConfig'
494+-- >>> import qualified Streamly.Internal.Data.Serialize.TH as Serialize
495+-- >>> deriveSerialize = Serialize.deriveSerializeWith Serialize.defaultConfig
493496deriveSerialize :: Name -> Q [Dec]
494−deriveSerialize name = deriveSerializeWith defaultSerializeTHConfig name
497+deriveSerialize name = deriveSerializeWith defaultConfig name
test/Streamly/Test/Data/Serialize.hs+9−9
@@ -23,11 +23,11 @@ module Streamly.Test.Data.Serialize (main) where
2323import Streamly.Internal.Data.Unbox (newBytes)
2424import GHC.Generics (Generic)
2525import Streamly.Test.Data.Serialize.TH (genDatatype)
26−import Streamly.Internal.Data.Serialize.TH
26+import qualified Streamly.Internal.Data.Serialize.TH as Serialize
2727 ( deriveSerialize
2828 , deriveSerializeWith
29− , defaultSerializeTHConfig
30− , SerializeTHConfig(..)
29+ , defaultConfig
30+ , Config(..)
3131 )
3232import Data.Functor.Identity (Identity (..))
3333
@@ -43,7 +43,7 @@ import Test.Hspec as H
4343--------------------------------------------------------------------------------
4444
4545$(genDatatype "CustomDatatype" 15)
46−$(deriveSerialize ''CustomDatatype)
46+$(Serialize.deriveSerialize ''CustomDatatype)
4747
4848--------------------------------------------------------------------------------
4949-- Types with functional parameters
@@ -61,10 +61,10 @@ instance (Eq (f Int), Eq (f Char)) => Eq (HigherOrderType f) where
6161instance (Show (f Int), Show (f Char)) => Show (HigherOrderType f) where
6262 show a = "HigherOrderType " ++ show (field0 a) ++ " " ++ show (field1 a)
6363
64−$(deriveSerialize ''Identity)
65−$(deriveSerializeWith
66− (defaultSerializeTHConfig
67− {typeVarSubstitutions = [("f", ConT ''Identity)]})
64+$(Serialize.deriveSerialize ''Identity)
65+$(Serialize.deriveSerializeWith
66+ (Serialize.defaultConfig
67+ {Serialize.specializations = [("f", ConT ''Identity)]})
6868 ''HigherOrderType)
6969
7070--------------------------------------------------------------------------------
@@ -77,7 +77,7 @@ data BinTree a
7777 | Leaf a
7878 deriving (Show, Read, Eq, Generic)
7979
80−$(deriveSerialize ''BinTree)
80+$(Serialize.deriveSerialize ''BinTree)
8181
8282instance Arbitrary a => Arbitrary (BinTree a) where
8383 arbitrary = oneof [Leaf <$> arbitrary, Tree <$> arbitrary <*> arbitrary]
test/Streamly/Test/Data/Unbox.hs+7−7
@@ -30,7 +30,7 @@ import GHC.Real (Ratio(..))
3030#ifdef USE_SERIALIZE
3131
3232import Streamly.Internal.Data.Serialize (Serialize(..))
33−import Streamly.Internal.Data.Serialize.TH
33+import qualified Streamly.Internal.Data.Serialize.TH as Serialize
3434
3535#else
3636
@@ -63,7 +63,7 @@ import Test.Hspec as H
6363#ifdef USE_SERIALIZE
6464
6565#define MODULE_NAME "Data.Serialize.Deriving.TH"
66−#define DERIVE_UNBOX(typ) $(deriveSerialize ''typ)
66+#define DERIVE_UNBOX(typ) $(Serialize.deriveSerialize ''typ)
6767#define PEEK(i, arr, sz) (deserialize i arr sz)
6868#define POKE(i, arr, val) (serialize i arr val)
6969#define TYPE_CLASS Serialize
@@ -190,12 +190,12 @@ DERIVE_UNBOX(NestedSOP)
190190deriving instance Generic (Ratio Int)
191191
192192#if defined(USE_SERIALIZE)
193−$(deriveSerialize ''Complex)
194−$(deriveSerialize ''Ratio)
195−$(deriveSerializeWith
196− (defaultSerializeTHConfig {unconstrainedTypeVars = ["b"]})
193+$(Serialize.deriveSerialize ''Complex)
194+$(Serialize.deriveSerialize ''Ratio)
195+$(Serialize.deriveSerializeWith
196+ (Serialize.defaultConfig {Serialize.unconstrained = ["b"]})
197197 ''Const)
198−$(deriveSerialize ''Identity)
198+$(Serialize.deriveSerialize ''Identity)
199199#endif
200200
201201--------------------------------------------------------------------------------