on master help sign in
Simplify Column type family

Now that the Field type family only needs a Necessity and a Type, we don't need to "parse" the Type to find the Necessity, we can instead just make it so that Column creates a Required field, and repurpose the name Default to be a type family that creates an Optional field. The previous "parsing" approach was brittle sometimes, particularly in the case of polymorphism.

commit11a22291aba47e5d0ebf269dbca166a552112c65 authorShane O'Brien <shane.obrien@circuithub.com> dateWed 14 Apr 2021, 07:25 (6 years ago) parent5e4df50
2 files changed+7−20
src/Rel8/Schema/Column.hs+5−18 src/Rel8/Schema/Generic/Test.hs+2−2
src/Rel8/Schema/Column.hs+5−18
@@ -17,26 +17,13 @@ import Rel8.Schema.Field ( Field )
1717import qualified Rel8.Schema.Kind as K
1818
1919
20−type Default :: Type -> Type
21−data Default a
22−
23−
24−type GetNecessity :: Type -> Necessity
25−type family GetNecessity a where
26− GetNecessity (Default _) = 'Optional
27− GetNecessity _ = 'Required
28−
29−
30−type UnwrapDefault :: Type -> Type
31−type family UnwrapDefault a where
32− UnwrapDefault (Default a) = a
33− UnwrapDefault a = a
34−
35−
3620-- | The @Column@ type family should be used to indicate which fields of your
3721-- data types are single columns in queries. This type family has special
3822-- support when a query is executed, allowing you to use a single data type for
3923-- both query data and rows decoded to Haskell.
4024type Column :: K.Context -> Type -> Type
41−type Column context a =
42− Field context (GetNecessity a) (UnwrapDefault a)
25+type Column context a = Field context 'Required a
26+
27+
28+type Default :: K.Context -> Type -> Type
29+type Default context a = Field context 'Optional a
src/Rel8/Schema/Generic/Test.hs+2−2
@@ -31,7 +31,7 @@ data Table f = Table
3131
3232
3333data TablePair f = TablePair
34− { foo :: Column f (Default Bool)
34+ { foo :: Default f Bool
3535 , bars :: (Column f Text, Column f Text)
3636 }
3737 deriving stock Generic
@@ -72,7 +72,7 @@ data TableList f = TableList
7272
7373data TableNonEmpty f = TableNonEmpty
7474 { foo :: Column f Bool
75− , bars :: HNonEmpty f (TableList f)
75+ , bars :: HNonEmpty f (TableList f, Default f Char)
7676 }
7777 deriving stock Generic
7878 deriving anyclass Rel8able