From d21902d911bd9b9989e7ea87f9daec9ea81dffe8 Mon Sep 17 00:00:00 2001 From: iphydf Date: Tue, 20 Dec 2016 17:57:56 +0000 Subject: [PATCH] Add type names --- src/Data/Schema.hs | 17 ++++++++++++----- test/Data/SchemaSpec.hs | 19 +++++++++++-------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/Data/Schema.hs b/src/Data/Schema.hs index e9e53d7..8f5e28e 100644 --- a/src/Data/Schema.hs +++ b/src/Data/Schema.hs @@ -7,7 +7,7 @@ import Data.Proxy (Proxy) data Field = Field { fieldName :: String - , fieldType :: Schema + , fieldType :: Type } deriving (Eq, Show, Read) @@ -19,15 +19,22 @@ data Schema deriving (Eq, Show, Read) +data Type = Type + { typeName :: String + , typeSchema :: Schema + } + deriving (Eq, Show, Read) + + class HasSchema a where - schema :: Proxy a -> Schema + schema :: Proxy a -> Type instance HasSchema Int where - schema _ = SchemaInt + schema _ = Type "Int" SchemaInt instance HasSchema Double where - schema _ = SchemaDouble + schema _ = Type "Double" SchemaDouble instance HasSchema String where - schema _ = SchemaString + schema _ = Type "String" SchemaString diff --git a/test/Data/SchemaSpec.hs b/test/Data/SchemaSpec.hs index d3c4ad0..ec6a0f8 100644 --- a/test/Data/SchemaSpec.hs +++ b/test/Data/SchemaSpec.hs @@ -8,7 +8,7 @@ import Test.QuickCheck import Data.Schema -data Record = Record +data MyRecord = MyRecord { recordField1 :: Int , recordField2 :: Double , recordField3 :: String @@ -16,16 +16,19 @@ data Record = Record deriving (Eq, Show, Read) -instance HasSchema Record where - schema p = SchemaRecord - [ Field "recordField1" $ schema $ recordField1 <$> p - , Field "recordField2" $ schema $ recordField2 <$> p - , Field "recordField3" $ schema $ recordField3 <$> p - ] +instance HasSchema MyRecord where + schema p = Type + { typeName = "MyRecord" + , typeSchema = SchemaRecord + [ Field "recordField1" $ schema $ recordField1 <$> p + , Field "recordField2" $ schema $ recordField2 <$> p + , Field "recordField3" $ schema $ recordField3 <$> p + ] + } spec :: Spec spec = do describe "schemas" $ do it "can be printed to stdout" $ - print (schema (Proxy :: Proxy Record)) + print (schema (Proxy :: Proxy MyRecord))