You can support non-standard data layouts in StructArray by overloading three specific methods for your type T. This allows StructArray to unpack nested or complex structures (like NamedTuples inside a struct) into individual fields.
To implement a custom layout, you must provide:
StructArrays.staticschema(::Type{T}): Defines the names and element types of the fields that StructArray should expose.StructArrays.component(m::T, key::Symbol): A component-extractor that retrieves a specific field from an instance m given a key.StructArrays.createinstance(::Type{T}, x, args...): A constructor-like method that recreates an instance of T from its constituent components.
An implementation is successful if createinstance(T, (component(x, f) for f in fieldnames(staticschema(T)))...) returns a valid instance of T.
# Example: Unpacking a NamedTuple field into top-level StructArray fields
struct MyType{T, NT<:NamedTuple}
data::T
rest::NT
end
# 1. Define schema
function StructArrays.staticschema(::Type{MyType{T, NamedTuple{names, types}}}) where {T, names, types}
return NamedTuple{(:data, names...), Base.tuple_type_cons(T, types)}
end;
# 2. Define extractor
function StructArrays.component(m::MyType, key::Symbol)
return key === :data ? getfield(m, 1) : getfield(getfield(m, 2), key)
end;
# 3. Define re-constructor
function StructArrays.createinstance(::Type{MyType{T, NT}}, x, args...) where {T, NT}
return MyType(x, NT(args))
end;