wireform-derive-0.2.0.0: Annotation-driven Template Haskell deriver core for wireform
Safe HaskellNone
LanguageGHC2021

Wireform.Derive.NameStyle

Description

Rich DSL for transforming Haskell field selector names into on-the-wire field names.

NameStyle is intentionally a pure ADT with a Data instance so that it can survive splice-time annotation reflection: a NameStyle value attached via ANN can be reified, fully evaluated at compile time, and inlined into the generated encode/decode code as a literal Text.

A handful of constructors (notably Idiomatic) refer to the active backend rather than to a fixed transformation. These are resolved by resolveIdiomatic before applyStyle is run.

Synopsis

The style DSL

data NameStyle Source #

A composable rename strategy. NameStyle is a closed ADT so that per-format derivers can interpret it at splice time and bake the result into generated wire-key Text literals (zero runtime cost).

Constructors

SnakeCase

snake_case. Boundaries inferred from camelCase / PascalCase humps: "personName" becomes "person_name".

UpperSnake

SCREAMING_SNAKE_CASE.

KebabCase

kebab-case.

UpperKebab

SCREAMING-KEBAB-CASE.

CamelCase

lowerCamelCase. The first character is lowercased; subsequent humps preserved.

PascalCase

UpperCamelCase / PascalCase.

LowerCase

All lowercase.

UpperCase

All uppercase.

StripPrefix !Text

Strip a literal prefix if present; otherwise leave the input unchanged.

StripSuffix !Text

Strip a literal suffix if present; otherwise unchanged.

StripPrefixCI !Text

Strip a prefix case-insensitively.

StripSuffixCI !Text

Strip a suffix case-insensitively.

DropChars !Int

Drop n characters from the start of the input.

TakeChars !Int

Take only the first n characters of the input.

Replace !Text !Text

Replace every occurrence of the first Text with the second.

ReplaceFirst !Text !Text

Replace only the first occurrence.

Compose !NameStyle !NameStyle

Sequential composition: Compose a b first applies a, then b.

NoStyle

Identity transformation.

Idiomatic

Apply the active backend's idiomatic naming convention. This constructor is resolved by resolveIdiomatic before applyStyle is called.

Instances

Instances details
Data NameStyle Source # 
Instance details

Defined in Wireform.Derive.NameStyle

Methods

gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> NameStyle -> c NameStyle #

gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c NameStyle #

toConstr :: NameStyle -> Constr #

dataTypeOf :: NameStyle -> DataType #

dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c NameStyle) #

dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c NameStyle) #

gmapT :: (forall b. Data b => b -> b) -> NameStyle -> NameStyle #

gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> NameStyle -> r #

gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> NameStyle -> r #

gmapQ :: (forall d. Data d => d -> u) -> NameStyle -> [u] #

gmapQi :: Int -> (forall d. Data d => d -> u) -> NameStyle -> u #

gmapM :: Monad m => (forall d. Data d => d -> m d) -> NameStyle -> m NameStyle #

gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> NameStyle -> m NameStyle #

gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> NameStyle -> m NameStyle #

Generic NameStyle Source # 
Instance details

Defined in Wireform.Derive.NameStyle

Associated Types

type Rep NameStyle 
Instance details

Defined in Wireform.Derive.NameStyle

type Rep NameStyle = D1 ('MetaData "NameStyle" "Wireform.Derive.NameStyle" "wireform-derive-0.2.0.0-IMXfvDNp3z41Zxps8Wb46e" 'False) ((((C1 ('MetaCons "SnakeCase" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "UpperSnake" 'PrefixI 'False) (U1 :: Type -> Type)) :+: (C1 ('MetaCons "KebabCase" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "UpperKebab" 'PrefixI 'False) (U1 :: Type -> Type))) :+: ((C1 ('MetaCons "CamelCase" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "PascalCase" 'PrefixI 'False) (U1 :: Type -> Type)) :+: (C1 ('MetaCons "LowerCase" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "UpperCase" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "StripPrefix" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text)))))) :+: (((C1 ('MetaCons "StripSuffix" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text)) :+: C1 ('MetaCons "StripPrefixCI" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text))) :+: (C1 ('MetaCons "StripSuffixCI" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text)) :+: (C1 ('MetaCons "DropChars" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int)) :+: C1 ('MetaCons "TakeChars" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int))))) :+: ((C1 ('MetaCons "Replace" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text)) :+: C1 ('MetaCons "ReplaceFirst" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text))) :+: (C1 ('MetaCons "Compose" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 NameStyle) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 NameStyle)) :+: (C1 ('MetaCons "NoStyle" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "Idiomatic" 'PrefixI 'False) (U1 :: Type -> Type))))))
Show NameStyle Source # 
Instance details

Defined in Wireform.Derive.NameStyle

NFData NameStyle Source # 
Instance details

Defined in Wireform.Derive.NameStyle

Methods

rnf :: NameStyle -> () #

Eq NameStyle Source # 
Instance details

Defined in Wireform.Derive.NameStyle

Ord NameStyle Source # 
Instance details

Defined in Wireform.Derive.NameStyle

Hashable NameStyle Source # 
Instance details

Defined in Wireform.Derive.NameStyle

Lift NameStyle Source # 
Instance details

Defined in Wireform.Derive.NameStyle

Methods

lift :: Quote m => NameStyle -> m Exp #

liftTyped :: forall (m :: Type -> Type). Quote m => NameStyle -> Code m NameStyle #

type Rep NameStyle Source # 
Instance details

Defined in Wireform.Derive.NameStyle

type Rep NameStyle = D1 ('MetaData "NameStyle" "Wireform.Derive.NameStyle" "wireform-derive-0.2.0.0-IMXfvDNp3z41Zxps8Wb46e" 'False) ((((C1 ('MetaCons "SnakeCase" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "UpperSnake" 'PrefixI 'False) (U1 :: Type -> Type)) :+: (C1 ('MetaCons "KebabCase" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "UpperKebab" 'PrefixI 'False) (U1 :: Type -> Type))) :+: ((C1 ('MetaCons "CamelCase" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "PascalCase" 'PrefixI 'False) (U1 :: Type -> Type)) :+: (C1 ('MetaCons "LowerCase" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "UpperCase" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "StripPrefix" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text)))))) :+: (((C1 ('MetaCons "StripSuffix" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text)) :+: C1 ('MetaCons "StripPrefixCI" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text))) :+: (C1 ('MetaCons "StripSuffixCI" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text)) :+: (C1 ('MetaCons "DropChars" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int)) :+: C1 ('MetaCons "TakeChars" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int))))) :+: ((C1 ('MetaCons "Replace" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text)) :+: C1 ('MetaCons "ReplaceFirst" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Text))) :+: (C1 ('MetaCons "Compose" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 NameStyle) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 NameStyle)) :+: (C1 ('MetaCons "NoStyle" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "Idiomatic" 'PrefixI 'False) (U1 :: Type -> Type))))))

andThen :: NameStyle -> NameStyle -> NameStyle infixl 1 Source #

Left-to-right composition. a andThen b first applies a, then b.

Application

applyStyle :: NameStyle -> Text -> Text Source #

Apply a style. Idiomatic constructors that have not been resolved by resolveIdiomatic degrade to NoStyle rather than raise.

Idiomatic resolution

idiomaticFor :: Backend -> NameStyle Source #

The conventional rename style for a given backend.

Defaults follow the dominant on-the-wire convention for each format:

Downstream backends not listed here fall through to NoStyle.

resolveIdiomatic :: Backend -> NameStyle -> NameStyle Source #

Substitute every Idiomatic marker with the concrete style for the given backend.

Building blocks (re-exported for testing)

toSnakeCase :: Text -> Text Source #

Lowercase, underscore-separated.

toUpperSnake :: Text -> Text Source #

Uppercase, underscore-separated.

toKebabCase :: Text -> Text Source #

Lowercase, hyphen-separated.

toUpperKebab :: Text -> Text Source #

Uppercase, hyphen-separated.

toCamelCase :: Text -> Text Source #

lowerCamelCase.

toPascalCase :: Text -> Text Source #

UpperCamelCase / PascalCase.