dataframe-core-2.4.0.0: Core data structures for the dataframe library.
LicenseMIT
Safe HaskellNone
LanguageHaskell2010

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

Type-level schema derivation

data NameCase Source #

Field-name policy applied to record selectors when computing RepToSchema.

  • SnakeCase — translate camelCaseField to "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

Instances details
(GHasColumns a, GHasColumns b) => GHasColumns (a :*: b) Source # 
Instance details

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 # 
Instance details

Defined in DataFrame.Typed.Generic

Methods

gToColumns :: [M1 C meta f p] -> [(Text, Column)]

gFromColumns :: DataFrame -> Either Text [M1 C meta f p]

GHasColumns f => GHasColumns (M1 D meta f) Source # 
Instance details

Defined in DataFrame.Typed.Generic

Methods

gToColumns :: [M1 D meta f p] -> [(Text, Column)]

gFromColumns :: DataFrame -> Either Text [M1 D meta f p]

(KnownSymbol name, Columnable a) => GHasColumns (M1 S ('MetaSel ('Just name) su ss ds) (K1 i a :: Type -> Type)) Source # 
Instance details

Defined in DataFrame.Typed.Generic

Methods

gToColumns :: [M1 S ('MetaSel ('Just name) su ss ds) (K1 i a :: Type -> Type) p] -> [(Text, Column)]

gFromColumns :: DataFrame -> Either Text [M1 S ('MetaSel ('Just name) su ss ds) (K1 i a :: Type -> Type) p]