msgpack/encode
decode-mp-list
(decode-mp-list [A])
decode a msgpack array into a `(Cons A)`, the mirror of
| tree | the owning tree | |
| node | the array node |
(Cons A) -- the decoded elements, or the empty list.
(:: (decode-mp-list t (mp-tree-root t)) (Cons int))
Since: Phase MP2
derive-msgpack
(derive-msgpack [type-name & fields])
emit both EncodeMp and DecodeMp instances for a
(defstruct User [id : int name : cstr active : bool]) (derive-msgpack User (id int) (name cstr) (active bool)) ;; (encode-mp (make-struct User 7 "alice" true)) ;; => 83 a2 69 64 07 a4 6e 61 6d 65 a5 61 6c 69 63 65 a6 61 63 74 69 76 65 c3
Since: Phase MP3
derive-msgpack-encode
(derive-msgpack-encode [type-name & fields])
emit only the EncodeMp instance for a defstruct.
(defstruct LogEntry [level : cstr msg : cstr]) (derive-msgpack-encode LogEntry (level cstr) (msg cstr))
Since: Phase MP3
derive-msgpack-decode
(derive-msgpack-decode [type-name & fields])
emit only the DecodeMp instance for a defstruct.
(defstruct Config [port : int host : cstr]) (derive-msgpack-decode Config (port int) (host cstr)) ;; (:: (decode-mp t root) (Result Config cstr))
Since: Phase MP3
derive-msgpack-opaque
(derive-msgpack-opaque [type-name _as carrier])
emit EncodeMp + DecodeMp for a `defopaque`
(defopaque UserId :int) (derive-msgpack-opaque UserId :as int) ;; (encode-mp (:: 42 UserId)) => 0x2a ;; (:: (decode-mp t n) (Result UserId cstr)) => parses an int as UserId
Since: Phase MP3
derive-msgpack-opaque-encode
(derive-msgpack-opaque-encode [type-name _as carrier])
only the EncodeMp half of
Since: Phase MP3
derive-msgpack-opaque-decode
(derive-msgpack-opaque-decode [type-name _as carrier])
only the DecodeMp half of
Since: Phase MP3
derive-msgpack-sum-encode
(derive-msgpack-sum-encode [type-name & ctors])
only the EncodeMp instance for a `defdata`
Since: Phase MP3
derive-msgpack-sum-decode
(derive-msgpack-sum-decode [type-name & ctors])
only the DecodeMp instance for a `defdata`
Since: Phase MP3
derive-msgpack-sum
(derive-msgpack-sum [type-name & ctors])
emit both EncodeMp and DecodeMp for a `defdata`
(defdata Event :copy [] (Click :int :int) (Scroll :int))
(derive-msgpack-sum Event (Click (x int) (y int)) (Scroll (dy int)))
;; (encode-mp (Click 3 4)) => 81 a5 43 6c 69 63 6b 82 a1 78 03 a1 79 04
Limitations (carried from the struct slice):
- Field-name policy is identity.
- Inner field decode does not accumulate structured errors.
- A nullary constructor encodes as `{"Ctor":{}}` (an empty inner map).
Since: Phase MP3
MpDecodeErrors
(MpDecodeErrors)
opaque handle to an accumulated list of decode
Since: Phase MP4
mp-decode-errors-count
(mp-decode-errors-count [e : MpDecodeErrors] :)
the number of accumulated violations.
| e | the accumulator from a failed checked decode |
int -- the violation count.
(mp-decode-errors-count (err-val r)) ; => 2
Since: Phase MP4
mp-decode-error-path
(mp-decode-error-path [e : MpDecodeErrors i : int] :)
the map key of the i-th violation.
| e | the accumulator | |
| i | zero-based violation index |
cstr -- a borrowed key name (the empty string when i is out of range).
(mp-decode-error-path errs 0) ; => "age"
Since: Phase MP4
mp-decode-error-expected
(mp-decode-error-expected [e : MpDecodeErrors i : int] :)
the type the i-th violation's field wanted.
| e | the accumulator | |
| i | zero-based violation index |
cstr -- a borrowed schema type name ("int", "string", ...).
(mp-decode-error-expected errs 0) ; => "int"
Since: Phase MP4
mp-decode-error-got
(mp-decode-error-got [e : MpDecodeErrors i : int] :)
the type actually present at the i-th violation.
| e | the accumulator | |
| i | zero-based violation index |
cstr -- a borrowed schema type name, or "missing" when the key was absent.
(mp-decode-error-got errs 0) ; => "string"
Since: Phase MP4
mp-decode-errors-free
(mp-decode-errors-free [e : MpDecodeErrors] :)
release the accumulator and its owned strings.
| e | the accumulator to release |
(mp-decode-errors-free (err-val r))
Since: Phase MP4
derive-mp-decoder
(derive-mp-decoder [type-name & fields])
emit a `DecodeMpChecked` instance for a `defstruct` of
(defstruct Person [name : cstr age : int])
(derive-mp-decoder Person (name cstr) (age int))
;; (:: (decode-mp-checked t root) (Result Person MpDecodeErrors))
;; {"name":"al","age":30} => (ok <Person>)
;; {"name":123} => (err <2: name/string/int, age/int/missing>)
Limitations (v0): primitive fields only (nested maps and arrays are a
follow-up), the path is the bare key with no dot-nesting, and the field cap
follows `__mp-decode-make-struct` (<=5). For a richer shape, hand-write the
DecodeMpChecked instance.
Since: Phase MP4
Internal definitions
__mp-enc-int-raw-- internal: smallest integer format that fits.__mp-enc-bool-raw-- internal: 0xc2 / 0xc3.__mp-enc-float-raw-- internal: always float64 (0xcb).__mp-enc-str-raw-- internal: fixstr / str8 / str16 / str32 by length.__mp-enc-nil-raw-- internal: the nil byte (0xc0).__mp-nil-frag-- the MessagePack `nil` byte as a fresh owned fragment, soEncodeMp[int]-- the narrowest integer format the value fits in: positiveEncodeMp[bool]-- 0xc3 for true, 0xc2 for false.EncodeMp[float]-- always float64 (0xcb). MessagePack has a float32 formEncodeMp[cstr]-- a msgpack `str` (never a `bin`), sized fixstr / str8 /__mp-arr-build-- internal: join a cons-list of element fragments under an__mp-arr-build-raw__mp-arr-fragsEncodeMp[Cons]-- a homogeneous list `(Cons A)` encodes as a msgpackEncodeMp[Option]-- a present value encodes as its element's fragment, an__mp-map-build-- internal: walk a cons-list of alternating (key, fragment)__mp-map-build-raw__mp-tag-- internal: wrap an already-encoded fragment in the one-entry map__mp-tag-raw__mp-chainDecodeMp[int]-- every integer format; rejects floats, strings, bools,DecodeMp[cstr]-- a msgpack `str`, copied out of the tree so the resultDecodeMp[bool]-- `true` / `false` only.DecodeMp[float]-- float32 / float64, and the integer formats widened, so__mp-missing?-- internal: true when a slot is absent (no such key) or anDecodeMp[Option]-- a missing slot or `nil` decodes to `none`; anything__mp-arr-decode__mp-node-absent?-- internal: the value node for `key`, or the absent node.__mp-field-node__mp-decode-field__mp-decode-fields__mp-decode-make-structEncodeMp[]DecodeMp[]EncodeMp[]DecodeMp[]__mp-sum-fnames__mp-sum-vchain__mp-sum-enc-arms__mp-sum-dfields__mp-sum-dec-armsEncodeMp[]DecodeMp[]__mp-errs-new-raw-- internal: a fresh accumulator, as a bare pointer.__mp-errs-new-- internal: a fresh, empty violation accumulator.__mp-errs-push-- internal: append one {path, expected, got} violation.__mp-errs-count-- internal: how many violations have accumulated.__mp-errs-free-- internal: release the accumulator and its owned strings.__mp-check-int-- internal: the field must be an integer.__mp-check-cstr-- internal: the field must be a msgpack str.__mp-check-bool-- internal: the field must be a boolean.__mp-check-float-- internal: the field must be numeric. An integer counts,__mp-derive-checks