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

Function writeArray

packages/sql/pg/src/PgTypes.ts:1717–1753  ·  view source on GitHub ↗
(sink: ValueSink, value: unknown, elementOid: number, lookup: Lookup)

Source from the content-addressed store, hash-verified

1715 * array of bytes of its own.
1716 */
1717const writeArray = (sink: ValueSink, value: unknown, elementOid: number, lookup: Lookup): void => {
1718 if (!Array.isArray(value)) {
1719 return fail("Expected an array")
1720 }
1721 const count = value.length
1722 // The null flag sits ahead of the elements, so it is the one thing that has
1723 // to be known before any of them are written.
1724 let hasNull = false
1725 for (let i = 0; i < count; i++) {
1726 if (value[i] === null) {
1727 hasNull = true
1728 break
1729 }
1730 }
1731 const dimensions = count === 0 ? 0 : 1
1732 sink.int32(dimensions)
1733 sink.int32(hasNull ? 1 : 0)
1734 sink.int32(elementOid)
1735 if (dimensions === 1) {
1736 sink.int32(count)
1737 sink.int32(1)
1738 }
1739 // One lookup for the whole array. A codec with no writer of its own is left
1740 // to `writeValue`, which allocates for the element anyway.
1741 const write = lookup(elementOid)?.write
1742 for (let i = 0; i < count; i++) {
1743 const element = value[i]
1744 if (element === null) {
1745 sink.int32(-1)
1746 } else {
1747 const token = sink.beginLength()
1748 if (write === undefined) writeValue(sink, element, elementOid, lookup)
1749 else write(sink, element)
1750 sink.endLength(token)
1751 }
1752 }
1753}
1754
1755/**
1756 * The element codec is resolved once for the whole array, and read in place

Callers 1

makeArrayCodecFunction · 0.85

Calls 7

writeValueFunction · 0.85
beginLengthMethod · 0.80
endLengthMethod · 0.80
failFunction · 0.70
lookupFunction · 0.70
writeFunction · 0.70
int32Method · 0.45

Tested by

no test coverage detected