| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
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
- data NameStyle
- = SnakeCase
- | UpperSnake
- | KebabCase
- | UpperKebab
- | CamelCase
- | PascalCase
- | LowerCase
- | UpperCase
- | StripPrefix !Text
- | StripSuffix !Text
- | StripPrefixCI !Text
- | StripSuffixCI !Text
- | DropChars !Int
- | TakeChars !Int
- | Replace !Text !Text
- | ReplaceFirst !Text !Text
- | Compose !NameStyle !NameStyle
- | NoStyle
- | Idiomatic
- andThen :: NameStyle -> NameStyle -> NameStyle
- applyStyle :: NameStyle -> Text -> Text
- idiomaticFor :: Backend -> NameStyle
- resolveIdiomatic :: Backend -> NameStyle -> NameStyle
- toSnakeCase :: Text -> Text
- toUpperSnake :: Text -> Text
- toKebabCase :: Text -> Text
- toUpperKebab :: Text -> Text
- toCamelCase :: Text -> Text
- toPascalCase :: Text -> Text
The style DSL
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 |
|
| UpperSnake |
|
| KebabCase |
|
| UpperKebab |
|
| CamelCase |
|
| 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 |
| TakeChars !Int | Take only the first |
| Replace !Text !Text | Replace every occurrence of the first |
| ReplaceFirst !Text !Text | Replace only the first occurrence. |
| Compose !NameStyle !NameStyle | Sequential composition: |
| NoStyle | Identity transformation. |
| Idiomatic | Apply the active backend's idiomatic naming convention. This
constructor is resolved by |
Instances
andThen :: NameStyle -> NameStyle -> NameStyle infixl 1 Source #
Left-to-right composition. a first applies andThen ba, 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:
backendJSON/backendProto→CamelCase(proto3'sjson_namedefault islowerCamelCase; common JS / TS APIs use the same).backendEDN/backendYAML→KebabCase.backendTOML→SnakeCase.backendXML→PascalCase.backendCBORbackendMsgPackbackendThriftbackendBinarybackendCSV/backendTextFormat→NoStyle(selector base used verbatim).
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.