nori/codegen/naming
Identifier naming shared by every Gleam generator.
This existed as five separate copies, one per generator, which only
happened to agree. They do not have independent behaviour to have: a schema
name becomes a type in types.gleam and a cross-module call like
types.<name>_decoder() in client.gleam, so any drift between two copies
produces a call to a function that was never generated.
Types
Argument names for one function’s parameters, unique within it.
pub opaque type ParamNames
Values
pub fn doc_comment(description: String) -> String
A description as a Gleam doc comment, one /// per line.
⚠️ Prefixing only the first line leaves the rest as bare text, and the
generated module does not parse — a spec is free to use a YAML block scalar
for a description, and several do. Trailing blank lines are dropped so the
comment does not end with a stray ///.
pub fn param_keys(
params: List(ir.EndpointParam),
reserved: List(String),
) -> ParamNames
JSON keys for parameters that share one object, such as an MCP tool’s
arguments: the spec name, unless an earlier parameter or a reserved key
(the request body) has it; then path_query and so on. Kubernetes’ proxy
endpoints have a {path} segment and a ?path= query parameter.
pub fn param_name(
names: ParamNames,
p: ir.EndpointParam,
) -> String
p’s name in names; a parameter that was not in the list gets its plain
field name.
pub fn param_names(params: List(ir.EndpointParam)) -> ParamNames
Names for params, in spec order.
A parameter is unique by name and location, so a path and a query parameter
may share a name: Kubernetes’ proxy endpoints have both a {path} segment
and a ?path=. Names can also meet once punctuation is gone: Twilio has
DateCreated, DateCreated< and DateCreated>. The first keeps the plain
name, a later one gets its location appended, then a number.
Built once per function and looked up per parameter: generators ask for a name in several places, and recomputing the set each time made the work cubic in the parameter count.
pub fn to_field_name(name: String) -> String
A spec name as a Gleam field label, argument, or local variable.
snake_case alone is not enough: a property called type (the standard
member of an RFC 9457 problem) becomes the label type, and the generated
record does not parse. Keywords, including the ones Gleam reserves for
future use, get a trailing underscore. Only the identifier changes; the JSON
key and the URL placeholder keep the name from the spec.
A label cannot start with an underscore or a digit either. _links and
_type are common (HAL, Jira), so leading underscores move to the end
(links_), and a leading digit gets a letter in front.
pub fn to_pascal_case(name: String) -> String
Convert a snake_case or camelCase name to PascalCase.
gleam_routes emits the Route variants and gleam_middleware matches on them, so this has the same must-agree property as to_snake_case.
An operationId such as remove-items-playlist is legal in OpenAPI, so any
character that cannot appear in an identifier separates words like _ does.
pub fn to_snake_case(name: String) -> String
Convert a PascalCase, camelCase, hyphenated, or dotted name to snake_case.
Names arrive straight from the spec, where “X-Request-Id”, “api.key” and “Order_Item” are all legal; every character that cannot appear in a Gleam identifier becomes an underscore.
Runs of underscores are left alone. Collapsing them would be prettier but buys nothing, and every caller has to agree character for character.
pub fn to_type_name(name: String) -> String
Turn a schema name into a Gleam type name.
Schema names are [a-zA-Z0-9._-]+ in OpenAPI, but a Gleam type name is
letters and digits only and must start with an uppercase letter. “Order_Item”
is a legal schema name that generates pub type Order_Item, which does not
parse.
Every reference to the type has to route through here too, or the definition and its uses end up with different names.
pub fn unique_field_names(names: List(String)) -> List(String)
Labels for a record’s properties, in order. +1 and -1 (GitHub’s
reactions) are both n1_ as field names; the second becomes n1__2.