nori/codegen/gleam_types

Generates Gleam type definitions, decoders, and encoders as string output.

Converts CodegenIR into a complete Gleam module source string containing type definitions, JSON decoders (using gleam/dynamic/decode), and JSON encoders (using gleam/json).

Values

pub const float_decoder: String

Decodes a JSON number. On Erlang decode.float rejects a value written without a fraction (1), which a number field legitimately holds, so an integer is accepted and converted. The module using it imports gleam/int.

pub fn generate(ir: ir.CodegenIR) -> String

Generates a complete Gleam module string from the CodegenIR. The types module: type definitions and the enums’ to_string and from_string. Decoders and encoders are in the codecs modules (generate_codecs).

pub fn generate_codecs(
  ir: ir.CodegenIR,
  module_prefix: String,
) -> List(#(String, String))

The codec modules, as paths relative to the types module’s directory with their contents. module_prefix is the module path of that directory ("generated"), so that a part can import the types and earlier parts.

Every decoder is a chain of nested funs, and the Erlang compiler’s time grows with the square of the funs in one module (erlang/otp#12258); the analysis of calls between functions also grows faster than the module. Cloudflare’s codecs in one module took 149s to compile. Up to types_per_part types’ codecs go in codecs.gleam; above that they are split into codecs/part_N.gleam, which compile in parallel, and codecs.gleam delegates to them so every codec keeps its address.

Gleam modules cannot import each other in a cycle, so types whose codecs call each other stay in one part, and a part only calls parts before it.

pub fn to_snake_case(name: String) -> String

Convert a name to snake_case. See naming.to_snake_case.

pub fn value_decoder(
  ref: ir.TypeRef,
  c: ir.Constraints,
  qualifier: String,
) -> String

The decoder for ref, rejecting values its constraints rule out. An optional or nullable value is checked when present; an array’s count is checked on the list and its element constraints on each element. value_decoder for code outside the types module: named types are called through qualifier ("types."), and a constraint check still applies.

✨ Search Document