structured-0.1.0.1: Structure (hash) of your data types
Copyright(c) 2019-2020 Oleg Grenrus
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.Structured

Description

Structurally tag data.

See binary-tagged package taking use of this. Useful when most Binary instances are Generic derived.

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.

Technically, Structured is not related to Binary, and may be useful in other ways.

Synopsis

Structured class

class Typeable a => Structured a where Source #

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: 3.2.0.0

Minimal complete definition

Nothing

Methods

structure :: Proxy a -> Structure Source #

default structure :: (Generic a, GStructured (Rep a)) => Proxy a -> Structure Source #

Instances

Instances details
Structured Bool Source # 
Instance details

Defined in Data.Structured.Internal

Structured Char Source # 
Instance details

Defined in Data.Structured.Internal

Structured Double Source # 
Instance details

Defined in Data.Structured.Internal

Structured Float Source # 
Instance details

Defined in Data.Structured.Internal

Structured Int Source # 
Instance details

Defined in Data.Structured.Internal

Structured Int8 Source # 
Instance details

Defined in Data.Structured.Internal

Structured Int16 Source # 
Instance details

Defined in Data.Structured.Internal

Structured Int32 Source # 
Instance details

Defined in Data.Structured.Internal

Structured Int64 Source # 
Instance details

Defined in Data.Structured.Internal

Structured Integer Source # 
Instance details

Defined in Data.Structured.Internal

Structured Natural Source # 
Instance details

Defined in Data.Structured.Internal

Structured Ordering Source # 
Instance details

Defined in Data.Structured.Internal

Structured Word Source # 
Instance details

Defined in Data.Structured.Internal

Structured Word8 Source # 
Instance details

Defined in Data.Structured.Internal

Structured Word16 Source # 
Instance details

Defined in Data.Structured.Internal

Structured Word32 Source # 
Instance details

Defined in Data.Structured.Internal

Structured Word64 Source # 
Instance details

Defined in Data.Structured.Internal

Structured () Source # 
Instance details

Defined in Data.Structured.Internal

Structured ByteString Source # 
Instance details

Defined in Data.Structured.Internal

Structured ByteString Source # 
Instance details

Defined in Data.Structured.Internal

Structured Scientific Source # 
Instance details

Defined in Data.Structured.Internal

Structured Text Source # 
Instance details

Defined in Data.Structured.Internal

Structured UTCTime Source # 
Instance details

Defined in Data.Structured.Internal

Structured Value Source # 
Instance details

Defined in Data.Structured.Internal

Structured Text Source # 
Instance details

Defined in Data.Structured.Internal

Structured Version Source # 
Instance details

Defined in Data.Structured.Internal

Structured All Source # 
Instance details

Defined in Data.Structured.Internal

Structured Any Source # 
Instance details

Defined in Data.Structured.Internal

Structured ShortByteString Source # 
Instance details

Defined in Data.Structured.Internal

Structured IntSet Source # 
Instance details

Defined in Data.Structured.Internal

Structured LocalTime Source # 
Instance details

Defined in Data.Structured.Internal

Structured TimeOfDay Source # 
Instance details

Defined in Data.Structured.Internal

Structured TimeZone Source # 
Instance details

Defined in Data.Structured.Internal

Structured UniversalTime Source # 
Instance details

Defined in Data.Structured.Internal

Structured NominalDiffTime Source # 
Instance details

Defined in Data.Structured.Internal

Structured DiffTime Source # 
Instance details

Defined in Data.Structured.Internal

Structured DayOfWeek Source # 
Instance details

Defined in Data.Structured.Internal

Structured Day Source # 
Instance details

Defined in Data.Structured.Internal

Structured UUID Source # 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured [a] Source # 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (Maybe a) Source # 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (Ratio a) Source # 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (Min a) Source # 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (Max a) Source # 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (First a) Source # 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (Last a) Source # 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (WrappedMonoid a) Source # 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (First a) Source # 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (Last a) Source # 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (Dual a) Source # 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (Endo a) Source # 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (Sum a) Source # 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (Product a) Source # 
Instance details

Defined in Data.Structured.Internal

Structured a => Structured (NonEmpty a) Source # 
Instance details

Defined in Data.Structured.Internal

Structured v => Structured (IntMap v) Source # 
Instance details

Defined in Data.Structured.Internal

Structured v => Structured (Seq v) Source # 
Instance details

Defined in Data.Structured.Internal

Structured k => Structured (Set k) Source # 
Instance details

Defined in Data.Structured.Internal

Structured k => Structured (HashSet k) Source # 
Instance details

Defined in Data.Structured.Internal

Structured v => Structured (Vector v) Source # 
Instance details

Defined in Data.Structured.Internal

Structured v => Structured (Vector v) Source # 
Instance details

Defined in Data.Structured.Internal

Structured v => Structured (Vector v) Source # 
Instance details

Defined in Data.Structured.Internal

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

Defined in Data.Structured.Internal

Methods

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

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

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

Defined in Data.Structured.Internal

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

Defined in Data.Structured.Internal

Methods

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

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

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

Defined in Data.Structured.Internal

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

Defined in Data.Structured.Internal

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

Defined in Data.Structured.Internal

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

Defined in Data.Structured.Internal

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

Defined in Data.Structured.Internal

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

Defined in Data.Structured.Internal

Methods

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

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

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

Defined in Data.Structured.Internal

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

Defined in Data.Structured.Internal

Methods

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

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

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

Defined in Data.Structured.Internal

Methods

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

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

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

Defined in Data.Structured.Internal

Methods

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

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

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

Defined in Data.Structured.Internal

Methods

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

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

structureHash :: forall a. Structured a => Proxy a -> MD5 Source #

Semantically hashStructure . structure.

structureBuilder :: Structure -> Builder Source #

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 :: forall a. (Typeable a, Generic a, GStructured (Rep a)) => Proxy a -> Structure Source #

Derive structure genrically.

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

Used to implement genericStructure.

Minimal complete definition

gstructured

Instances

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

Defined in Data.Structured.Internal

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

Use Typeable to infer name

containerStructure :: forall f a. (Typeable f, Structured a) => Proxy (f a) -> Structure Source #

Structure type

data Structure Source #

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

Instances details
Eq Structure Source # 
Instance details

Defined in Data.Structured.Internal

Ord Structure Source # 
Instance details

Defined in Data.Structured.Internal

Show Structure Source # 
Instance details

Defined in Data.Structured.Internal

Generic Structure Source # 
Instance details

Defined in Data.Structured.Internal

Associated Types

type Rep Structure :: Type -> Type #

type Rep Structure Source # 
Instance details

Defined in Data.Structured.Internal

type Rep Structure = D1 ('MetaData "Structure" "Data.Structured.Internal" "structured-0.1.0.1-98bxZG43j8z3UGZ7W3DB5E" '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 Source #

A sematic version of a data type. Usually 0.

hashStructure :: Structure -> MD5 Source #

A MD5 hash digest of Structure.

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

A van-Laarhoven lens into TypeName of Structure

typeName :: Lens' Structure TypeName

MD5

showMD5 :: MD5 -> String Source #

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 Source #

@since 3.2.0.0

md5FromInteger :: Integer -> MD5 Source #

>>> 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: 3.4.0.0