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.
gleam add --dev glerd
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.
If this record exists in a source file:
pub type User {
/// some metadata
User(id: Int, name: String, email: String)
}
the output will be:
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).
If you'd like to try out Glerd, there is a sample Gleam project with instructions and a source file for quick testing.
Run:
gleam test # and then commit generated file