nupp.serde.codec
A new codec uses these helpers to apply the number, timestamp, error-path, and context rules the built-in codecs share. It implements nupp.serde.Writer and nupp.serde.Reader itself; see Serialization bindings for where a codec sits beside a model library.
A codec that follows these rules accepts and rejects the same values as nupp.serde.json and nupp.serde.native, so a binding behaves the same whichever one carries it.
local codec = require("nupp.serde.codec")
assert(codec.integer("255", 8, false).decimal == "255")
assert(not pcall(codec.integer, "256", 8, false))
assert(codec.formatDateTime(codec.parseEpoch("86400")) == "1970-01-02T00:00:00Z")Module contents
Types
| Type | Kind | Description |
|---|---|---|
ContextBuilder | record | Collects the values of a nupp.serde.Context before it is frozen. |
Functions
| Function | Kind | Description |
|---|---|---|
contextBuilder | function | Starts a context for what a codec reads. |
discardFailed | function | Discards a value a codec decoded and then has to reject, returning the failure to raise. |
float | function | Rounds a number to a float width and checks it is finite. |
formatDateTime | function | Formats a timestamp as an RFC 3339 date-time in UTC. |
formatEpoch | function | Formats a timestamp as epoch seconds. |
integer | function | Checks an integer lexeme against a width and signedness. |
parseDateTime | function | Parses an RFC 3339 date-time. |
parseEpoch | function | Parses epoch seconds, with an optional fraction. |
prefixError | function | Prefixes a failure's path with its parent's. |
wrapError | function | Wraps any failure as a nupp.serde.Error at a path. |
Types#
ContextBuilderrecord#
record ContextBuilder ...Collects the values of a nupp.serde.Context before it is frozen.
Members
| Name | Kind | Description |
|---|---|---|
codec | field | |
profile | field | |
parent | field | |
values | field | |
frozen | field | |
set | method | Stores a value under a key, replacing any value set before. |
freeze | method | Returns the context holding the values set so far. |
setmethod#
Stores a value under a key, replacing any value set before.
Arguments
| Name | Type | Description |
|---|---|---|
self | any | |
key | store.Key<V> | the key to store under |
value | V | the value |
Returns
| Type | Description |
|---|---|
nil |
Raises
| Type | Condition |
|---|---|
string | when the builder is frozen |
freezemethod#
freeze: function freeze(self): core.ContextReturns the context holding the values set so far.
The builder can then no longer change, so the context it returned cannot either. A key the context does not hold is looked up in its parent.
Arguments
| Name | Type | Description |
|---|---|---|
self | any |
Returns
| Type | Description |
|---|---|
core.Context | the context |
Raises
| Type | Condition |
|---|---|
string | when the builder is already frozen |
Functions#
contextBuilderfunction#
Starts a context for what a codec reads.
codec and profile identify where the value came from, so a protocol can refuse to replay a document captured under an incompatible one. Values are set under nupp.util.Keys and must be Copyable.
local codec = require("nupp.serde.codec")
local source: nupp.util.Key<string> = nupp.util.newKey()
local builder = codec.contextBuilder({}, {}, nil)
builder:set(source, "upload.json")
assert(builder:freeze():get(source) == "upload.json")Arguments
| Name | Type | Description |
|---|---|---|
codec | table | |
profile | table | |
parent | core.Context? |
Returns
| Type | Description |
|---|---|
Builder |
discardFailedfunction#
Discards a value a codec decoded and then has to reject, returning the failure to raise.
When the discard itself fails, its failure is recorded in the error's suppressed list and the original cause is kept.
Type parameters
| Name | Description |
|---|---|
T |
Arguments
| Name | Type | Description |
|---|---|---|
owner | core.Discard<T> | |
takes value | T | |
cause | unknown |
Returns
| Type | Description |
|---|---|
unknown |
floatfunction#
function float(value: number, width: 32 | 64, allowNonfinite: boolean?): numberRounds a number to a float width and checks it is finite.
Arguments
| Name | Type | Description |
|---|---|---|
value | number | |
width | 32 | 64 | |
allowNonfinite | boolean? |
Returns
| Type | Description |
|---|---|
number |
formatDateTimefunction#
Formats a timestamp as an RFC 3339 date-time in UTC.
Arguments
| Name | Type | Description |
|---|---|---|
borrows value | core.Timestamp |
Returns
| Type | Description |
|---|---|
string |
formatEpochfunction#
Formats a timestamp as epoch seconds.
Arguments
| Name | Type | Description |
|---|---|---|
borrows value | core.Timestamp |
Returns
| Type | Description |
|---|---|
string |
integerfunction#
Checks an integer lexeme against a width and signedness.
Arguments
| Name | Type | Description |
|---|---|---|
text | string | |
width | 8 | 16 | 32 | 64 | |
signed | boolean |
Returns
| Type | Description |
|---|---|
core.Integer |
parseDateTimefunction#
Parses an RFC 3339 date-time.
Arguments
| Name | Type | Description |
|---|---|---|
text | string |
Returns
| Type | Description |
|---|---|
core.Timestamp |
parseEpochfunction#
Parses epoch seconds, with an optional fraction.
Arguments
| Name | Type | Description |
|---|---|---|
text | string |
Returns
| Type | Description |
|---|---|
core.Timestamp |
prefixErrorfunction#
function prefixError(problem: unknown, parent: string): ErrorPrefixes a failure's path with its parent's.
Arguments
| Name | Type | Description |
|---|---|---|
problem | unknown | the raised value |
parent | string | the path of the enclosing value |
Returns
| Type | Description |
|---|---|
Error | the error, carrying the combined path |
wrapErrorfunction#
Wraps any failure as a nupp.serde.Error at a path.
An existing error is returned unchanged, so wrapping at each level keeps the innermost path.
Arguments
| Name | Type | Description |
|---|---|---|
problem | unknown | the raised value |
path | string | the logical path to record on a new error |
byte | integer? | the input offset to record on a new error, or nil |
Returns
| Type | Description |
|---|---|
Error | the error |