Files
zap/v1/build_helpers.go
zeekayandHanzo Dev 95935cbc52 builder: value-typed ListBuilder + fix latent zap/v1 breakage
StartList returns ListBuilder by value (mirrors the value-typed ObjectBuilder):
Add*/Finish keep pointer receivers, every caller binds to an addressable local so
Go auto-addresses it. Dropped the dead elemSize field (never read — stride is
implicit in which Add* is called). Byte-identical wire (golden+fuzz+all round-trips
green). NOTE: not a measured alloc win — escape analysis already stack-allocated
the &ListBuilder{} at all 9 call sites (composite stays 583ns/5allocs, the 5 being
mandatory writeEnvelopePrefix pool-egress copies). Value is code-cleanliness +
consistency.

Also fixes zap/v1 subpackage: v1.2.4's ObjectBuilder value change left v1/*.go
referencing *zap.ObjectBuilder → the subpackage didn't build (latent; only the
root package is imported downstream so it went unnoticed). Converted to the value
type. Full module (root + v1) builds + tests green.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-14 14:06:56 -07:00

36 lines
1.5 KiB
Go

// Copyright (C) 2026, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package zapv1
import "github.com/luxfi/zap"
// SetterFrom adapts a raw v1 ([github.com/luxfi/zap]) ObjectBuilder +
// Builder pair into a typed Setter[S]. It is the bridge the hand-rolled
// codegen fast path uses to reach the typed list/variable-length writers
// (WriteList, WriteString, WriteBytes) without abandoning the
// straight-line `b.StartObject` construction that inlines at the call
// site. The (ob, b) must be the pair returned by the same StartObject —
// passing a mismatched pair is a programming error.
func SetterFrom[S Schema](ob zap.ObjectBuilder, b *zap.Builder) Setter[S] {
return Setter[S]{ob: ob, b: b}
}
// WriteString writes a variable-length UTF-8 string field to the object
// tail and stores its {relOffset, length} pointer at byteOffset in the
// fixed payload. The typed peer of [zap.ObjectBuilder.SetText] for the
// Setter-based build path. Read it back with the generated string
// accessor (which calls v1's zero-copy Object.Text).
func WriteString[S Schema](s Setter[S], byteOffset uint32, v string) {
ob, _ := s.raw()
ob.SetText(int(byteOffset), v)
}
// WriteBytes writes a variable-length byte slice to the object tail and
// stores its {relOffset, length} pointer at byteOffset. The typed peer
// of [zap.ObjectBuilder.SetBytes]. Zero-copy on read via Object.Bytes.
func WriteBytes[S Schema](s Setter[S], byteOffset uint32, v []byte) {
ob, _ := s.raw()
ob.SetBytes(int(byteOffset), v)
}