| License | MIT |
|---|---|
| Safe Haskell | None |
| Language | Haskell2010 |
DataFrame.Typed.Generic
Description
Generic-based opt-in for record-to-schema derivation. Mirrors the Template
Haskell splice in DataFrame.Typed.TH but builds the schema type from a
GHC.Generics.Generic instance instead of reify.
Use it like this:
data Order = Order
{ orderId :: Int64
, region :: Text
, amount :: Double
} deriving (Show, Eq, Generic)
type OrderSchema = SchemaOf Order
instance HasSchema Order OrderSchema where
toColumns = genericToColumns
fromColumns = genericFromColumns
Field names are translated with the CamelCase -> snake_case rule
(matching camelToSnake); use SchemaOfRaw if you
want the schema to keep the record selector names verbatim — in that
case you cannot use genericToColumns / genericFromColumns and must
either hand-roll the instance or use the TH splice with a custom name
transform.
Synopsis
- data NameCase
- type SchemaOf a = RepToSchema 'SnakeCase (Rep a)
- type SchemaOfRaw a = RepToSchema 'IdentityCase (Rep a)
- type family RepToSchema (nc :: NameCase) (r :: Type -> Type) :: [(Symbol, Type)] where ...
- type family CamelToSnake (s :: Symbol) :: Symbol where ...
- genericToColumns :: (Generic a, GHasColumns (Rep a)) => [a] -> [(Text, Column)]
- genericFromColumns :: (Generic a, GHasColumns (Rep a)) => DataFrame -> Either Text [a]
- class GHasColumns (r :: Type -> Type)
Type-level schema derivation
Field-name policy applied to record selectors when computing
RepToSchema.
SnakeCase— translatecamelCaseFieldto"camel_case_field".IdentityCase— keep the selector name verbatim.
Constructors
| SnakeCase | |
| IdentityCase |
type SchemaOf a = RepToSchema 'SnakeCase (Rep a) Source #
Snake_case schema derived from a's Generic representation.
type SchemaOfRaw a = RepToSchema 'IdentityCase (Rep a) Source #
Identity-cased schema derived from a's Generic representation.
type family RepToSchema (nc :: NameCase) (r :: Type -> Type) :: [(Symbol, Type)] where ... Source #
The schema type '[ '(name, ty), ...] derived from the Rep of a
record type, with the given NameCase applied to each field name.
Equations
| RepToSchema nc (M1 D _1 f) = RepToSchema nc f | |
| RepToSchema nc (M1 C _1 f) = RepToSchema nc f | |
| RepToSchema nc (a :*: b) = Append (RepToSchema nc a) (RepToSchema nc b) | |
| RepToSchema nc (M1 S ('MetaSel ('Just name) _1 _2 _3) (K1 _4 a :: Type -> Type)) = '['(TransformName nc name, a)] |
type family CamelToSnake (s :: Symbol) :: Symbol where ... Source #
Type-level camelCase -> snake_case. Matches camelToSnake at the value level.
Equations
| CamelToSnake s = SnakeStart (UnconsSymbol s) |
Value-level default methods
genericToColumns :: (Generic a, GHasColumns (Rep a)) => [a] -> [(Text, Column)] Source #
Default implementation of toColumns for any
Generic record. Field names are translated with camelCase -> snake_case.
instance HasSchema Order (SchemaOf Order) where toColumns = genericToColumns fromColumns = genericFromColumns
genericFromColumns :: (Generic a, GHasColumns (Rep a)) => DataFrame -> Either Text [a] Source #
Default implementation of fromColumns for any
Generic record.
class GHasColumns (r :: Type -> Type) Source #
Walks the Rep tree of a record, producing or consuming a list of
named columns. Used by genericToColumns / genericFromColumns.
Minimal complete definition
gToColumns, gFromColumns
Instances
| (GHasColumns a, GHasColumns b) => GHasColumns (a :*: b) Source # | |
Defined in DataFrame.Typed.Generic Methods gToColumns :: [(a :*: b) p] -> [(Text, Column)] gFromColumns :: DataFrame -> Either Text [(a :*: b) p] | |
| GHasColumns f => GHasColumns (M1 C meta f) Source # | |
Defined in DataFrame.Typed.Generic | |
| GHasColumns f => GHasColumns (M1 D meta f) Source # | |
Defined in DataFrame.Typed.Generic | |
| (KnownSymbol name, Columnable a) => GHasColumns (M1 S ('MetaSel ('Just name) su ss ds) (K1 i a :: Type -> Type)) Source # | |