Glerd

Package Version Hex Docs Erlang-compatible JavaScript-compatible

Glerd is tooling, written in Gleam, that generates metadata that describes Gleam Records. This metadata can be used to generate Gleam code that works with those records, as a substitute for runtime reflection, which does not exist in Gleam.

Installing

gleam add --dev glerd

Usage

Run:

gleam run -m glerd

This reads Gleam source files in the src directory of a project and creates a glerd_gen.gleam output file containing metadata for each of the Records in those files.

Example output

If this record exists in a source file:

src/user.gleam
pub type User {
  /// some metadata
  User(id: Int, name: String, email: String)
}

the output will be:

src/glerd_gen.gleam
import glerd/types

pub const record_info = [
  #(
    "User", // record name
    "user", // module name
    [
      #("id", types.IsInt), #("name", types.IsString),
      #("email", types.IsString),
    ],
    "some metadata",
  ),
]

This metadata can then be used to generate Gleam code that works with the User record (see glerd_json for an example of reading this metadata and generating Gleam boilerplate code for encoding/decoding records to/from JSON).

Sample project

If you’d like to try out Glerd, there is a sample Gleam project with instructions and a source file for quick testing.

Development

Run:

gleam test # and then commit generated file

See also

Search Document