Files
zeekay 592493a51d v1/codegen: emit variable-length LIST fields (zero-copy, typed)
Completes the variable-length codegen (strings/bytes already shipped; lists now
join them). A list field of a fixed-size element schema generates: a per-element
value-input struct, a typed constructor param []Elem, an inline WriteList build
(bridged onto the hand-rolled ObjectBuilder by the new zapv1.SetterFrom, one
zapv1.Write per element field), and a zero-copy ListAt read accessor returning
zapv1.List[E] (range-over-func ready) — mechanically reproducing examples/
batch_tx.go.

Adds: Schema.Element (flat list-element slots carry no kind byte — fields may
start at offset 0; Wrap skips the kind check); runtime helpers SetterFrom /
WriteString / WriteBytes bridging the hand-rolled fast path to the typed writers.

Proven end-to-end: testpkg/listwire round-trips a BatchTx{scalar + []Item}
through build -> wrap -> ListAt.All() with every element read zero-copy
(TestRoundTrip_List), plus generator-output tests (valid Go via go/parser +
construct assertions). Existing golden tests unchanged.
2026-06-23 15:46:50 -07:00

86 lines
3.0 KiB
Go

// Copyright (C) 2026, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
//
// Code generated by github.com/luxfi/zap/v1/codegen — DO NOT EDIT.
// Source schema: BatchItem (kind=0x22, size=16).
package listwire
import (
"github.com/luxfi/zap"
zapv1 "github.com/luxfi/zap/v1"
)
// KindBatchItem is the wire discriminator for BatchItem.
const KindBatchItem zapv1.KindByte = 0x22
// SizeBatchItem is the fixed object payload size in bytes.
const SizeBatchItem = 16
// ItemSchema is the v2 schema marker for BatchItem. Methods are constant-
// returning so concrete-typed [zapv1.Wrap[ItemSchema]] calls fold to
// literals at the call site.
type ItemSchema struct{}
func (ItemSchema) Kind() zapv1.KindByte { return KindBatchItem }
func (ItemSchema) Size() int { return SizeBatchItem }
func (ItemSchema) Name() string { return "BatchItem" }
// OffsetBatchItem_ID is the byte offset of the ID field.
const OffsetBatchItem_ID = 0
// OffsetBatchItem_Value is the byte offset of the Value field.
const OffsetBatchItem_Value = 8
// ItemSchemaFields is the namespace of every scalar field accessor for BatchItem.
// Use with [zapv1.Read] / [zapv1.Write] for type-safe access.
// Byte-array fields are emitted as standalone functions, see below.
var ItemSchemaFields = struct {
ID zapv1.Field[ItemSchema, uint64]
Value zapv1.Field[ItemSchema, uint64]
}{
ID: zapv1.At[ItemSchema, uint64](0),
Value: zapv1.At[ItemSchema, uint64](8),
}
// NewBatchItem builds a fresh BatchItem in a ZAP buffer.
//
// Hand-rolled fast path: inlines v1 primitives ([zap.NewBuilder],
// [*Builder.StartObject], [*ObjectBuilder.Set*], [*Builder.Finish])
// so the entire build expands at the call site. Scalars and fixed byte
// arrays live in the fixed payload; variable-length string/bytes go in
// the object tail (the fixed payload holds their 8-byte pointers).
func NewBatchItem(iD uint64, value uint64) (zapv1.View[ItemSchema], []byte) {
b := zap.NewBuilder(zap.HeaderSize + SizeBatchItem)
ob := b.StartObject(SizeBatchItem)
ob.SetUint64(OffsetBatchItem_ID, iD)
ob.SetUint64(OffsetBatchItem_Value, value)
rootOff := ob.FinishAsRoot()
buf := b.Finish()
end := rootOff + SizeBatchItem
return zapv1.AsView[ItemSchema](zapv1.RawFromSlices(buf, rootOff, end)), buf
}
// WrapBatchItem parses a ZAP buffer into a typed
// [zapv1.View[BatchItem]]. Validates the wire frame and the kind
// discriminator at offset 0; returns [*zapv1.SchemaError] on kind
// mismatch.
//
// Hand-rolled fast path: inlines [zap.Parse] (which is itself
// inlineable). Zero heap allocs on the happy path; matches v1
// hand-rolled performance.
func WrapBatchItem(b []byte) (zapv1.View[ItemSchema], error) {
msg, err := zap.Parse(b)
if err != nil {
return zapv1.View[ItemSchema]{}, err
}
root := msg.Root()
data := msg.Bytes()
rootOff := root.Offset()
end := rootOff + SizeBatchItem
if end > len(data) {
end = len(data) // Read returns zero on OOB; v1 graceful degradation
}
return zapv1.AsView[ItemSchema](zapv1.RawFromSlices(data, rootOff, end)), nil
}