Comments (4)
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TemplateHaskell #-}
module Main where
import Control.Applicative
import GHC.Generics
import Data.Typeable
import Data.Monoid
import qualified Data.Set as S
import Control.Lens hiding (to, from)
newtype SchemaName = SchemaName String deriving (Show, Eq)
instance Monoid SchemaName where
mempty = SchemaName mempty
SchemaName x `mappend` _ = SchemaName x
data Schema = Schema {
_schemaName :: SchemaName
, _schemaFields :: [(String, TypeRep)]
, _requiredFields :: [String]
} deriving (Show, Eq, Generic)
-- lensies
$(makeLenses ''Schema)
instance Monoid Schema where
mempty = Schema mempty mempty mempty
Schema x1 y1 z1 `mappend` Schema x2 y2 z2
= Schema (x1 <> x2) (y1 <> y2) (z1 <> z2)
class Generic a => Example a where example :: a
class GToSchema f where
gToSchema :: f a -- * type rep.
-> Schema -- * accumulator
-> Schema -- * final schema
class ToSchema a where
toSchema :: a -> Schema
default toSchema :: (Example a, Generic a, GToSchema (Rep a)) => a -> Schema
toSchema x = gToSchema (from x) mempty
data Person = Person {
name :: String
, age :: Int
, dogName :: Maybe String
} deriving (Generic)
instance Example Person where
example = Person "fizruk" 28 (Just "fido")
instance ToSchema Person
-- Unit
instance GToSchema U1 where
gToSchema = undefined -- don't need
-- DataType
instance (Datatype d, GToSchema a) => GToSchema (D1 d a) where
gToSchema m@(M1 x) schema = gToSchema x newSchema
where
newSchema = schema & schemaName .~ SchemaName (datatypeName m)
-- Constructors
instance (Constructor i, GToSchema a) => GToSchema (C1 i a) where
gToSchema m@(M1 x) = gToSchema x
-- already capturing this info at the dataType (same as Con in this case)
-- where
-- newSchema = schema & schemaName .~ conName m
-- Selectors + Values
instance (Typeable a, Selector s) => GToSchema (S1 s (K1 i a)) where
gToSchema m@(M1 (K1 x)) schema = newSchema
where
newSchema = schema & schemaFields %~ (:) (selName m, typeOf x)
& requiredFields %~ (:) (selName m)
instance (Typeable a, Selector s) => GToSchema (S1 s (K1 i (Maybe a))) where
gToSchema m@(M1 (K1 x)) schema = newSchema
where
newSchema = schema & schemaFields %~ (:) (selName m, typeOf x)
-- Sums
instance (GToSchema left, GToSchema right) => GToSchema (left :*: right) where
gToSchema (l :*: r) schema = gToSchema l schema <> gToSchema r schema
-- Products... I say we don't support these now for the sake of simplicity
instance (GToSchema left, GToSchema right) => GToSchema (left :+: right) where
gToSchema (L1 x) = gToSchema x
gToSchema (R1 x) = gToSchema x
-- λ> toSchema (example :: Person)
-- Schema { _schemaName = fromList ["Person"]
-- , _schemaFields = [("name",[Char]),("age",Int)]
-- }
cc @fizruk, this is just a prototype
from envy.
@dmjio I am a beginner Haskell user and wanted to say kudos to this library and especially its great examples & documentation ❤️
from envy.
This is implemented in HEAD
from envy.
@gasi, what a kind thing to say :) I'm happy you find it useful.
from envy.
Related Issues (20)
- Allow using unit as an environment variable HOT 4
- Indirect environment variables HOT 1
- Generics without def config HOT 9
- ToEnv purpose HOT 1
- write against MonadIO
- Error Compiling Example Program HOT 1
- No `Var` instance for `Last` HOT 1
- fromVar should return an Either String a HOT 3
- Dont't make implementors of FromEnv instance think of the default value HOT 7
- instances for tuples HOT 4
- Question: Semantics of `envMaybe` HOT 3
- provide a way to extract/print the names of environment variables used?
- Constraints on base are incorrect for 2.0 and 2.1 HOT 3
- Examples with DefConfig don't work
- envy-2.0.0.0 and 2.1.0.0: metadata revisions published to Hackage HOT 1
- Poor error messages for bool variables HOT 2
- What is the proper way of grouping env vars into product Haskell types?
- Relax dependency on bytestring HOT 5
- Race conditions in testing environmental variables HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from envy.