{-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE NoImplicitPrelude #-} -- Integer-based identifier. module Biz.Id ( Id (..), mk, untag, ) where import Alpha import Data.Aeson (FromJSON (..), ToJSON (..)) import Data.Binary (Binary) import Data.Data (Data) newtype Id entity = Id Int deriving (Eq, Ord, Show, Generic, Typeable, Data) mk :: proxy entity -> Int -> Id entity mk _ = Id untag :: Id entity -> Int untag (Id i) = i instance Hashable (Id entity) instance Binary (Id entity) instance Enum (Id entity) where toEnum n = mk (Proxy :: Proxy entity) n fromEnum = untag instance NFData (Id entity) where rnf (Id s) = rnf s instance FromJSON (Id entity) where parseJSON = fmap Id <. parseJSON instance ToJSON (Id entity) where toJSON = toJSON <. untag