binary-tagged-0.3: Tagged binary serialisation.

Copyright(C) 2015-2020 Oleg Grenrus
LicenseBSD3
MaintainerOleg Grenrus <oleg.grenrus@iki.fi>
Safe HaskellTrustworthy
LanguageHaskell2010

Data.Binary.Tagged

Contents

Description

Structurally tag binary serialisation stream.

Say you have a data type

data Record = Record
  { _recordFields  :: HM.HashMap Text (Integer, ByteString)
  , _recordEnabled :: Bool
  }
  deriving (Eq, Show, Generic)

instance Binary Record
instance Structured Record

then you can serialise and deserialise Record values with a structure tag by simply

structuredEncode record :: LBS.ByteString
structuredDecode lbs    :: Either String Record

If structure of Record changes in between, deserialisation will fail early.

Synopsis

Encoding and decoding

These functions operate like binary's counterparts, but the serialised version has a structure hash in front.

structuredEncode :: forall a. (Binary a, Structured a) => a -> ByteString Source #

Structured encode. Encode a value to using binary serialisation to a lazy ByteString. Encoding starts with 16 byte large structure hash.

structuredEncodeFile :: (Binary a, Structured a) => FilePath -> a -> IO () Source #

Lazily serialise a value to a file

structuredDecode :: forall a. (Binary a, Structured a) => ByteString -> a Source #

Structured decode. Decode a value from a lazy ByteString, reconstructing the original structure. Throws pure exception on invalid inputs.

structuredDecodeFileOrFail :: (Binary a, Structured a) => FilePath -> IO (Either String a) Source #

Lazily reconstruct a value previously written to a file.

Structured class

class Typeable a => Structured a where #

Class of types with a known Structure.

For regular data types Structured can be derived generically.

data Record = Record { a :: Int, b :: Bool, c :: [Char] } deriving (Generic)
instance Structured Record

Since: structured-3.2.0.0

Minimal complete definition

Nothing

Methods

structure :: Proxy a -> Structure #

Instances
Structured Bool 
Instance details

Defined in Data.Structured.Internal

Structured Char 
Instance details

Defined in Data.Structured.Internal

Structured Double 
Instance details

Defined in Data.Structured.Internal

Structured Float 
Instance details

Defined in Data.Structured.Internal

Structured Int 
Instance details

Defined in Data.Structured.Internal

Structured Int8 
Instance details

Defined in Data.Structured.Internal

Structured Int16 
Instance details

Defined in Data.Structured.Internal

Structured Int32 
Instance details

Defined in Data.Structured.Internal

Structured Int64 
Instance details

Defined in Data.Structured.Internal

Structured Integer 
Instance details

Defined in Data.Structured.Internal

Structured Natural 
Instance details

Defined in Data.Structured.Internal

Structured Ordering 
Instance details

Defined in Data.Structured.Internal

Structured Word 
Instance details

Defined in Data.Structured.Internal

Structured Word8 
Instance details

Defined in Data.Structured.Internal

Structured Word16 
Instance details

Defined in Data.Structured.Internal

Structured Word32 
Instance details

Defined in Data.Structured.Internal

Structured Word64 
Instance details

Defined in Data.Structured.Internal

Structured () 
Instance details

Defined in Data.Structured.Internal

Structured ByteString 
Instance details

Defined in Data.Structured.Internal

Structured ByteString 
Instance details

Defined in Data.Structured.Internal

Structured Scientific 
Instance details

Defined in Data.Structured.Internal

Structured Text 
Instance details

Defined in Data.Structured.Internal

Structured UTCTime 
Instance details

Defined in Data.Structured.Internal

Structured Value 
Instance details

Defined in Data.Structured.Internal

Structured Text 
Instance details

Defined in Data.Structured.Internal

Structured Version 
Instance details

Defined in Data.Structured.Internal

Structured All 
Instance details

Defined in Data.Structured.Internal

Structured Any 
Instance details

Defined in Data.Structured.Internal

Structured ShortByteString 
Instance details

Defined in Data.Structured.Internal

Structured IntSet 
Instance details

Defined in Data.Structured.Internal

Structured LocalTime 
Instance details

Defined in Data.Structured.Internal

Structured TimeOfDay 
Instance details

Defined in Data.Structured.Internal

Structured TimeZone 
Instance details

Defined in Data.Structured.Internal

Structured UniversalTime 
Instance details

Defined in Data.Structured.Internal

Structured NominalDiffTime 
Instance details

Defined in Data.Structured.Internal

Structured DiffTime 
Instance details

Defined in Data.Structured.Internal

Structured Day 
Instance details

Defined in Data.Structured.Internal

Structured DayOfWeek 
Instance details

Defined in Data.Structured.Internal

Structured UUID 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured [a] 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (Maybe a) 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (Ratio a) 
Instance details

Defined in Data.Structured.Internal

(Typeable a, HasResolution a) => Structured (Fixed a) 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (Min a) 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (Max a) 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (First a) 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (Last a) 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (WrappedMonoid a) 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (First a) 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (Last a) 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (Dual a) 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (Endo a) 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (Sum a) 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (Product a) 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (NonEmpty a) 
Instance details

Defined in Data.Structured.Internal

Structured v => Structured (IntMap v) 
Instance details

Defined in Data.Structured.Internal

Structured v => Structured (Seq v) 
Instance details

Defined in Data.Structured.Internal

Structured k => Structured (Set k) 
Instance details

Defined in Data.Structured.Internal

Structured k => Structured (HashSet k) 
Instance details

Defined in Data.Structured.Internal

Structured v => Structured (Vector v) 
Instance details

Defined in Data.Structured.Internal

Structured v => Structured (Vector v) 
Instance details

Defined in Data.Structured.Internal

Structured v => Structured (Vector v) 
Instance details

Defined in Data.Structured.Internal

(Structured a, Structured b) => Structured (a -> b) 
Instance details

Defined in Data.Structured.Internal

Methods

structure :: Proxy (a -> b) -> Structure #

structureHash' :: Tagged (a -> b) MD5 #

(Structured a, Structured b) => Structured (Either a b) 
Instance details

Defined in Data.Structured.Internal

(Structured a1, Structured a2) => Structured (a1, a2) 
Instance details

Defined in Data.Structured.Internal

Methods

structure :: Proxy (a1, a2) -> Structure #

structureHash' :: Tagged (a1, a2) MD5 #

(Structured k, Structured v) => Structured (HashMap k v) 
Instance details

Defined in Data.Structured.Internal

(Structured k, Structured v) => Structured (Map k v) 
Instance details

Defined in Data.Structured.Internal

Methods

structure :: Proxy (Map k v) -> Structure #

structureHash' :: Tagged (Map k v) MD5 #

(Structured i, Structured e) => Structured (UArray i e) 
Instance details

Defined in Data.Structured.Internal

(Structured i, Structured e) => Structured (Array i e) 
Instance details

Defined in Data.Structured.Internal

(Structured a1, Structured a2, Structured a3) => Structured (a1, a2, a3) 
Instance details

Defined in Data.Structured.Internal

Methods

structure :: Proxy (a1, a2, a3) -> Structure #

structureHash' :: Tagged (a1, a2, a3) MD5 #

(Typeable k, Typeable b, Structured a) => Structured (Tagged b a) 
Instance details

Defined in Data.Structured.Internal

(Structured a1, Structured a2, Structured a3, Structured a4) => Structured (a1, a2, a3, a4) 
Instance details

Defined in Data.Structured.Internal

Methods

structure :: Proxy (a1, a2, a3, a4) -> Structure #

structureHash' :: Tagged (a1, a2, a3, a4) MD5 #

(Structured a1, Structured a2, Structured a3, Structured a4, Structured a5) => Structured (a1, a2, a3, a4, a5) 
Instance details

Defined in Data.Structured.Internal

Methods

structure :: Proxy (a1, a2, a3, a4, a5) -> Structure #

structureHash' :: Tagged (a1, a2, a3, a4, a5) MD5 #

(Structured a1, Structured a2, Structured a3, Structured a4, Structured a5, Structured a6) => Structured (a1, a2, a3, a4, a5, a6) 
Instance details

Defined in Data.Structured.Internal

Methods

structure :: Proxy (a1, a2, a3, a4, a5, a6) -> Structure #

structureHash' :: Tagged (a1, a2, a3, a4, a5, a6) MD5 #

(Structured a1, Structured a2, Structured a3, Structured a4, Structured a5, Structured a6, Structured a7) => Structured (a1, a2, a3, a4, a5, a6, a7) 
Instance details

Defined in Data.Structured.Internal

Methods

structure :: Proxy (a1, a2, a3, a4, a5, a6, a7) -> Structure #

structureHash' :: Tagged (a1, a2, a3, a4, a5, a6, a7) MD5 #

structureBuilder :: Structure -> Builder #

Flatten Structure into something we can calculate hash of.

As Structure can be potentially infinite. For mutually recursive types, we keep track of TypeReps, and put just TypeRep name when it's occurred another time.

genericStructure :: (Typeable a, Generic a, GStructured (Rep a)) => Proxy a -> Structure #

Derive structure genrically.

class GStructured (f :: Type -> Type) #

Used to implement genericStructure.

Minimal complete definition

gstructured

Instances
(i ~ D, Datatype c, GStructuredSum f) => GStructured (M1 i c f) 
Instance details

Defined in Data.Structured.Internal

Methods

gstructured :: TypeRep -> Proxy (M1 i c f) -> TypeVersion -> Structure #

nominalStructure :: Typeable a => Proxy a -> Structure #

Use Typeable to infer name

Structure type

data Structure #

Structure of a datatype.

It can be infinite, as far as TypeReps involved are finite. (e.g. polymorphic recursion might cause troubles).

Constructors

Nominal !TypeRep !TypeVersion TypeName [Structure]

nominal, yet can be parametrised by other structures.

Newtype !TypeRep !TypeVersion TypeName Structure

a newtype wrapper

Structure !TypeRep !TypeVersion TypeName SopStructure

sum-of-products structure

Instances
Eq Structure 
Instance details

Defined in Data.Structured.Internal

Ord Structure 
Instance details

Defined in Data.Structured.Internal

Show Structure 
Instance details

Defined in Data.Structured.Internal

Generic Structure 
Instance details

Defined in Data.Structured.Internal

Associated Types

type Rep Structure :: Type -> Type #

type Rep Structure 
Instance details

Defined in Data.Structured.Internal

type Rep Structure = D1 (MetaData "Structure" "Data.Structured.Internal" "structured-0.1-7OlXsBuGaoOHMb8ojbhIJr" False) (C1 (MetaCons "Nominal" PrefixI False) ((S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 TypeRep) :*: S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 TypeVersion)) :*: (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 TypeName) :*: S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [Structure]))) :+: (C1 (MetaCons "Newtype" PrefixI False) ((S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 TypeRep) :*: S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 TypeVersion)) :*: (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 TypeName) :*: S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Structure))) :+: C1 (MetaCons "Structure" PrefixI False) ((S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 TypeRep) :*: S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 TypeVersion)) :*: (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 TypeName) :*: S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 SopStructure)))))

type TypeVersion = Word32 #

A sematic version of a data type. Usually 0.

hashStructure :: Structure -> MD5 #

A MD5 hash digest of Structure.

typeName :: Functor f => (TypeName -> f TypeName) -> Structure -> f Structure #

A van-Laarhoven lens into TypeName of Structure

typeName :: Lens' Structure TypeName

MD5

showMD5 :: MD5 -> String #

Show MD5 in human readable form

>>> showMD5 (Fingerprint 123 456)
"000000000000007b00000000000001c8"
>>> showMD5 $ md5 $ BS.pack [0..127]
"37eff01866ba3f538421b30b7cbefcac"

@since 3.2.0.0

md5 :: ByteString -> MD5 #

@since 3.2.0.0

md5FromInteger :: Integer -> MD5 #

>>> showMD5 $ md5FromInteger 0x37eff01866ba3f538421b30b7cbefcac
"37eff01866ba3f538421b30b7cbefcac"

Note: the input is truncated:

>>> showMD5 $ md5FromInteger 0x1230000037eff01866ba3f538421b30b7cbefcac
"37eff01866ba3f538421b30b7cbefcac"

Yet, negative numbers are not a problem...

>>> showMD5 $ md5FromInteger (-1)
"ffffffffffffffffffffffffffffffff"

Since: structured-3.4.0.0