Created
October 31, 2018 11:50
-
-
Save Akii/5fe70f27a83a46103ce17d83c70a4be2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DeriveDataTypeable #-} | |
{-# LANGUAGE TypeApplications #-} | |
{-# LANGUAGE DefaultSignatures #-} | |
{-# LANGUAGE DeriveGeneric #-} | |
{-# LANGUAGE AllowAmbiguousTypes #-} | |
{-# LANGUAGE ScopedTypeVariables #-} | |
module Test where | |
import ClassyPrelude | |
import Data.Data | |
data Foo | |
= Bar | |
| Baz | |
deriving (Generic, Data) | |
class HasNamespace a where | |
getNamespace :: Text | |
class Event a where | |
eventType :: a -> Text | |
eventTypes :: [Text] | |
default eventType :: (HasNamespace a, Data a) => a -> Text | |
eventType = (getNamespace @a <>) . fromString . showConstr . toConstr | |
default eventTypes :: (HasNamespace a, Data a) => [Text] | |
eventTypes = fmap (x (getNamespace @a)) (dataTypeConstrs $ dataTypeOf (undefined :: a)) | |
x :: Text -> Constr -> Text | |
x ns ctor = ns <> "." <> fromString (showConstr ctor) | |
instance HasNamespace Foo where | |
getNamespace = "Some.V1" | |
instance Event Foo | |
baz :: [Text] | |
baz = eventTypes @Foo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment