CustomStructSchema

class CustomStructSchema<T : Any>(val type: String, val componentNames: List<String>, val componentSchemas: List<FateSchema<*>>, val encoder: (T) -> List<Any>) : FateSchema<T>

A flexible schema for custom serialization of objects with user-defined encoding logic.

CustomStructSchema provides maximum flexibility for serializing objects by allowing users to define exactly how their objects should be decomposed into primitive components. This is particularly useful for:

  • Legacy Objects: Classes that can't be modified to use reflection-based serialization

  • Optimized Encoding: Custom logic to minimize serialized size or improve performance

  • Data Transformation: Converting objects during serialization (e.g., coordinate transformations)

  • Selective Serialization: Including only specific fields or computed values

  • Version Compatibility: Maintaining backward compatibility with different object versions

Architecture

The schema uses a component-based approach where:

  1. Objects are decomposed into a list of primitive components via the encoder function

  2. Each component has a corresponding name and schema for serialization

  3. Components are serialized in the order they appear in componentNames

  4. A type string is automatically prepended to identify the object type

Binary Format

Schema Encoding:

[4 bytes: tag] [4 bytes: component count]
[4 bytes: ".type" length] [".type" bytes] [StringSchema]
[4 bytes: name1 length] [name1 bytes] [schema1]
[4 bytes: name2 length] [name2 bytes] [schema2]
...

Object Encoding:

[4 bytes: type length] [type string bytes]
[component1 data] [component2 data] ...

Usage Examples

Basic Usage

data class Person(val name: String, val age: Int, val email: String)

val personSchema = CustomStructSchema<Person>(
type = "Person",
componentNames = listOf("name", "age", "email"),
componentSchemas = listOf(StringSchema, IntSchema, StringSchema),
encoder = { person -> listOf(person.name, person.age, person.email) }
)

Data Transformation

val transformedSchema = CustomStructSchema<Person>(
type = "NormalizedPerson",
componentNames = listOf("displayName", "ageCategory"),
componentSchemas = listOf(StringSchema, StringSchema),
encoder = { person ->
listOf(
person.name.uppercase(),
when {
person.age < 18 -> "MINOR"
person.age < 65 -> "ADULT"
else -> "SENIOR"
}
)
}
)

Selective Field Serialization

val minimalSchema = CustomStructSchema<ComplexObject>(
type = "Essential",
componentNames = listOf("id", "status"),
componentSchemas = listOf(LongSchema, StringSchema),
encoder = { obj -> listOf(obj.id, obj.currentStatus.name) }
)

Parameters

T

The type of objects this schema can serialize

type

A string identifier for this object type, written to the log for identification

componentNames

Names of the components in serialization order. Used for schema documentation.

componentSchemas

Schemas for each component, must match the order of componentNames

encoder

Function that converts objects of type T into a list of primitive components

Throws

if componentNames and componentSchemas have different sizes

Constructors

Link copied to clipboard
constructor(type: String, componentNames: List<String>, componentSchemas: List<FateSchema<*>>, encoder: (T) -> List<Any>)

Creates a new CustomStructSchema with the specified configuration

Types

Link copied to clipboard
object Companion

Internal type field definition used for schema metadata.

Properties

Link copied to clipboard

Names of the serialized components in order.

Link copied to clipboard

Schemas for each component in the same order as componentNames.

Link copied to clipboard
val encoder: (T) -> List<Any>

Function that decomposes objects into serializable components.

Link copied to clipboard
open override val schema: ByteArray

The complete schema definition as a byte array.

Link copied to clipboard
open override val schemaSize: Int

Number of bytes required to encode this schema's metadata.

Link copied to clipboard
open override val tag: Int

Unique identifier for this schema type within the FateWeaver format.

Link copied to clipboard

String identifier for this object type, included in the serialized data for type safety.

Functions

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

Encodes the given object into the provided buffer.

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

Encodes this schema's metadata into the provided buffer.

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

Calculates the number of bytes required to encode the given object.