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:
- metaty.enum are already plain-old-data (no need to call pod() on them).
- metaty record types can be made plain old data by calling the
pod() module on them.
This simply implements the __toPod and __fromPod methods on them --
you can do this yourself if you prefer!
- pod parses the metaty field type stanza as [singleType
,
{listType} or
{map: type} repsecitively. The collection types are converted to
pod.List and
pod.Map respectively.