Flatbuf.Gen (flatbuf v0.1.0)

Copy Markdown View Source

Shared helpers for the flatbuf.gen, flatbuf.gen.check, and compile.flatbuf Mix tasks.

Resolves a list of .fbs paths into a flat list of {module_atom, source_string, output_path} artifacts so each task can do its own thing (write to disk, compare to disk, manifest).

Summary

Functions

The niceties the code generator knows about.

Translate a module atom to its on-disk path under out_dir, mirroring Elixir's standard Macro.underscore-per-segment layout.

Convert a dotted module-name string ("My.App") or already-atom into a module atom. Idempotent.

Parse a comma-separated nicety list ("behaviour,jason") into a list of atoms. nil returns [] so this can be used directly on OptionParser output.

Resolve each schema path, generate artifacts for the entire set, and return a list keyed by absolute output path.

Validate a list of nicety atoms (the niceties: config form used by compile.flatbuf). Returns the list unchanged, or raises ArgumentError naming the unknown entries and the valid set.

Types

artifact()

@type artifact() :: %{module: module(), source: String.t(), path: Path.t()}

plan_options()

@type plan_options() :: [
  out: Path.t(),
  wire_module: module() | String.t(),
  namespace: String.t() | nil,
  include: [Path.t()],
  niceties: [atom()]
]

Functions

known_niceties()

@spec known_niceties() :: [atom()]

The niceties the code generator knows about.

output_path(out_dir, module)

@spec output_path(Path.t(), module()) :: Path.t()

Translate a module atom to its on-disk path under out_dir, mirroring Elixir's standard Macro.underscore-per-segment layout.

parse_module_name(name)

@spec parse_module_name(module() | String.t()) :: module()

Convert a dotted module-name string ("My.App") or already-atom into a module atom. Idempotent.

parse_niceties(str)

@spec parse_niceties(nil | String.t()) :: [atom()]

Parse a comma-separated nicety list ("behaviour,jason") into a list of atoms. nil returns [] so this can be used directly on OptionParser output.

Raises ArgumentError for names outside known_niceties/0, so a typo (--niceties behavior) fails loudly instead of silently doing nothing.

plan(paths, opts)

@spec plan([Path.t()], plan_options()) :: {:ok, [artifact()]} | {:error, term()}

Resolve each schema path, generate artifacts for the entire set, and return a list keyed by absolute output path.

Duplicate module names across schemas are collapsed (the first schema to emit a module wins). Returns {:ok, [artifact]} or the first resolver error.

validate_niceties!(niceties)

@spec validate_niceties!([atom()]) :: [atom()]

Validate a list of nicety atoms (the niceties: config form used by compile.flatbuf). Returns the list unchanged, or raises ArgumentError naming the unknown entries and the valid set.