pod: plain-old-data library and serialization

A Lua library for specifying and converting types to/from "plain old data" and methods to serialize/deserialize those types to/from bytes. In Lua, the 5 supported types considered "plain old data" are: nil, boolean, integer, string and any table of only these types.

local M = mod'mymod'
local mty = require'metaty'
local pod = require'pod'

-- enums are already plain-old-data.
M.Job = mty.enum'Job' { BOSS = 1, PEON = 2 }

--- records need to have pod() called on them.
--- This implements __toPod and __fromPod for the metaty record.
M.Worker = pod(mty'Worker' {
  'name [string] #1',    -- must specify type and #id
  'job  [mymod.Job] #2', -- can lookup any type in PKG_LOOKUP
  'salary [int] #3',
  'schedule {int: mymod.Schedule} #4', -- map of weekday[1,7] -> scheduled time
})

M.Schedule = pod(mty'Schedule' {
  'start [int]: start time in seconds since midnight',
  'stop  [int]: stop time in seconds since midnight',
})

local serialized = pod.ser(M.Worker{...})   -- convert to string
local worker = pod.de(serialized, M.Worker) -- convert from string

Explanation of above:

Custom Podder

For making your own podder impl, the signatures are
function MyT.__toPod(T, pod, value) ... end --> p
function MyT.__fromPod(T, pod, p) ... end   --> value
Where:

Serialization Best Practices

Usecases

Usecases of pod are:

library support

In addition to providing methods to de/serialize data to a compact binary form, pod exports the toPod() and fromPod() functions to help other libraries (i.e. lson) de/serialize arbitrary lua types.

Cross Language Tooling

pod is designed to (eventually) serve the same need as protobuf: it can and will generate code for other languages to read/write pod's binary serialization format.

At this time, support for other languages has not started -- but the design of pod is meant to mimick protobuf as much as possible so that such a goal can be met in the future. Work on supporting multiple languages will likely not be part of the civstack project but the civstack project will support such work.

Mod pod

pod: plain old data

Types: Pod Podder key builtin List Map

Functions

Record Pod

Pod: configuration for converting values to/from POD.

Fields:

Record Podder

A type who's sole job is converting values to/from POD.

Fields:

Record key

Handles concrete non-nil types (boolean, number, string)

Record builtin

Handles all native types (nil, boolean, number, string, table)

Record List

Poder for a list of items with a type.

Fields:

Record Map

Poder for a map of key/value pairs.

Fields: