Skip to content

Instantly share code, notes, and snippets.

@Akii
Created October 31, 2018 11:50
Show Gist options
  • Save Akii/5fe70f27a83a46103ce17d83c70a4be2 to your computer and use it in GitHub Desktop.
Save Akii/5fe70f27a83a46103ce17d83c70a4be2 to your computer and use it in GitHub Desktop.
{-# 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