MCPcopy Create free account
hub / github.com/Effect-TS/effect / encodeStructPositional

Function encodeStructPositional

packages/effect/src/encoding/SchemaBinary.ts:3549–3586  ·  view source on GitHub ↗
(ctx: EncodeContext, layout: StructLayout, value: object, w: Writer)

Source from the content-addressed store, hash-verified

3547
3548// Fingerprint structs use a presence bitmap followed by positional fields.
3549function encodeStructPositional(ctx: EncodeContext, layout: StructLayout, value: object, w: Writer) {
3550 const obj = value as Record<string, unknown>
3551 // Only a run field with index signatures hands over an intern table. Extras
3552 // encode after the fields here, so claim it before a nested field can.
3553 const keys = ctx.intern
3554 ctx.intern = undefined
3555 const bitmapBytes = (layout.optionalCount + 7) >> 3
3556 let bitmap = 0
3557 if (bitmapBytes > 0) {
3558 bitmap = w.len
3559 for (let i = 0; i < bitmapBytes; i++) w.byte(0)
3560 }
3561 const fields = layout.fields
3562 let optionalIndex = 0
3563 for (let i = 0; i < fields.length; i++) {
3564 const field = fields[i]
3565 const name = field.name
3566 const present = Object.hasOwn(obj, name)
3567 if (field.optional) {
3568 // Resolve the bitmap address again after any arena growth.
3569 if (present) w.buf[w.start + bitmap + (optionalIndex >> 3)] |= 1 << (optionalIndex & 7)
3570 optionalIndex++
3571 if (!present) continue
3572 } else if (!present) {
3573 issuePath[issuePathLen++] = name
3574 throw issueError(new SchemaIssue.MissingKey(field.annotations))
3575 }
3576 issuePath[issuePathLen++] = name
3577 if (field.inline) encodeValue(ctx, field.layout, obj[name], w)
3578 else encodeSized(ctx, field.layout, obj[name], w)
3579 issuePathLen--
3580 }
3581 if (layout.extra.length > 0) {
3582 const pairs = extraPairs(ctx, layout, obj)
3583 w.uvarint(pairs.length)
3584 encodeExtraPairs(ctx, pairs, obj, w, keys)
3585 }
3586}
3587
3588function internKind(layout: Layout): number {
3589 switch (layout._) {

Callers 1

encodeValueFunction · 0.85

Calls 7

issueErrorFunction · 0.85
encodeSizedFunction · 0.85
extraPairsFunction · 0.85
encodeExtraPairsFunction · 0.85
encodeValueFunction · 0.70
byteMethod · 0.45
uvarintMethod · 0.45

Tested by

no test coverage detected