KiwiCodec (kiwi_codec v0.7.0)

Copy Markdown View Source

Encodes and decodes Kiwi messages as Elixir structs.

use KiwiCodec, kind: :message, :struct, or :enum defines a module for a schema definition with the KiwiCodec.DSL macros, and mix kiwi.gen or compile_schema!/2 generates such modules from a schema. encode/1 and decode/3 convert their structs to and from Kiwi binaries.

Data written with a newer schema decodes when decode/3 is given that schema: fields the modules do not define are skipped. KiwiCodec.SchemaInterpreter works without modules, for schemas loaded at runtime.

Summary

Functions

Compiles .kiwi schema text into generated Elixir modules in memory.

Decodes a binary as the given Kiwi module.

Encodes a Kiwi struct.

Encodes a Kiwi struct to iodata.

Parses .kiwi schema text into a schema AST.

Functions

compile_schema!(text, opts)

@spec compile_schema!(
  String.t(),
  keyword()
) :: [module()]

Compiles .kiwi schema text into generated Elixir modules in memory.

This is a convenience for tests and tooling. For application code, prefer the mix kiwi.gen task so generated modules are written to source files.

decode(binary, module, opts \\ [])

@spec decode(binary(), module(), keyword()) :: struct()

Decodes a binary as the given Kiwi module.

Options:

encode(struct)

@spec encode(struct()) :: binary()

Encodes a Kiwi struct.

encode_to_iodata(struct)

@spec encode_to_iodata(struct()) :: iodata()

Encodes a Kiwi struct to iodata.

parse_schema!(text)

@spec parse_schema!(String.t()) :: KiwiCodec.Schema.t()

Parses .kiwi schema text into a schema AST.