Last active
June 26, 2018 13:04
-
-
Save nmattia/304a4b98b9ffbce8c2a7aa3e412100f6 to your computer and use it in GitHub Desktop.
Config with generics-sop
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 TypeFamilies #-} | |
{-# LANGUAGE TypeOperators #-} | |
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE DeriveGeneric #-} | |
module Foo where | |
import Control.Monad | |
import qualified GHC.Generics as GHC | |
import qualified Generics.SOP as SOP | |
import qualified Generics.SOP.NS as NS | |
import Data.Functor.Identity | |
type AppSettings = AppSettings_ Identity | |
-- Squash Identity | |
type family SQ f t where | |
SQ Identity t = t | |
SQ f t = f t | |
data AppSettings_ f = AppSettings | |
{ port :: SQ f Int | |
, host :: SQ f String | |
} deriving GHC.Generic | |
instance SOP.Generic (AppSettings_ f) | |
as :: AppSettings_ Maybe | |
as = AppSettings (Just 2) (Just "foo") | |
fff :: AppSettings_ Maybe -> Maybe AppSettings | |
fff = fmap SOP.to . NS.sequence'_SOP . NS.fromI_SOP . SOP.from | |
baz2 :: Maybe AppSettings | |
baz2 = fff as | |
maybePort :: Maybe Int | |
maybePort = port <$> baz2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment