OptionalSchema

class OptionalSchema<T : Any>(val valueSchema: FateSchema<T>) : FateSchema<T?>

A schema for values that may be absent, wrapping the schema for the value itself.

You rarely build one yourself. A nullable property gets one automatically, so class Target(val pose: Pose?) encodes pose as an optional POSE. Nullable type arguments work too, as in Map<String, Double?> and List<String?>.

Binary format

The schema:

[4 bytes: tag] [inner schema blob]

An encoded value:

[1 byte: 0x00 absent]
[1 byte: 0x01 present] [value encoded by the inner schema]

An absent value costs one byte. A present value costs one byte more than it would unwrapped.

Decoder compatibility

This schema uses tag 9, added after MAP. Log entries have no length prefix, so a reader that doesn't know the tag cannot skip the value. It stops at the first channel that uses one. Non-null fields encode as before.

Parameters

T

the value type when present

valueSchema

schema used to encode the value when it is present

Constructors

Link copied to clipboard
constructor(valueSchema: FateSchema<T>)

Properties

Link copied to clipboard
open override val schema: ByteArray

The schema definition as bytes, which the writer puts in the log.

Link copied to clipboard
open override val schemaSize: Int

Number of bytes in this schema's definition.

Link copied to clipboard
open override val tag: Int

The integer that identifies this schema's type in the log.

Link copied to clipboard

Functions

Link copied to clipboard
open override fun encodeObject(buffer: ByteBuffer, obj: T?)

Writes the given object into the buffer.

Link copied to clipboard
open fun encodeSchema(buffer: ByteBuffer)

Writes the whole schema definition, starting with the tag, into the buffer.

Link copied to clipboard
open override fun objSize(obj: T?): Int

Returns the number of bytes encodeObject writes for the given object.