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

Function encodeValue

packages/effect/src/encoding/SchemaBinary.ts:4121–4331  ·  view source on GitHub ↗
(ctx: EncodeContext, layout: Layout, value: unknown, w: Writer)

Source from the content-addressed store, hash-verified

4119}
4120
4121function encodeValue(ctx: EncodeContext, layout: Layout, value: unknown, w: Writer): void {
4122 switch (layout._) {
4123 case "literal":
4124 if (!hasLiteral(layout, value)) encodeFail(literalExpected(layout), value, ctx.options)
4125 encodeValue(ctx, layout.leaf, value, w)
4126 return
4127 case "bool":
4128 if (typeof value !== "boolean") encodeFail("a boolean", value, ctx.options)
4129 w.byte(value === true ? 1 : 0)
4130 return
4131 case "null":
4132 if (value !== null) encodeFail("null", value, ctx.options)
4133 return
4134 case "undefined":
4135 if (value !== undefined) encodeFail("undefined", value, ctx.options)
4136 return
4137 case "number": {
4138 if (typeof value !== "number") encodeFail("a number", value, ctx.options)
4139 if (isVarintNumber(value)) {
4140 w.numberVarint(value)
4141 return
4142 }
4143 const scale = decimalScale(value)
4144 if (scale > 0) {
4145 w.numberVarint(Math.round(value * POW10[scale]))
4146 w.byte(scale)
4147 } else {
4148 w.f64(value)
4149 }
4150 return
4151 }
4152 case "int": {
4153 // `disableChecks` can bypass the integer check used to select this layout.
4154 if (!Number.isSafeInteger(value)) encodeFail("an integer", value, ctx.options)
4155 w.numberVarint(value as number)
4156 return
4157 }
4158 case "string": {
4159 if (typeof value !== "string") encodeFail("a string", value, ctx.options)
4160 const dict = ctx.dict
4161 if (dict === undefined) w.string(value)
4162 else encodeString(dict, layout, value, w)
4163 return
4164 }
4165 case "symbol":
4166 if (typeof value !== "symbol") encodeFail("a symbol", value, ctx.options)
4167 encodeSymbol(ctx, value, w)
4168 return
4169 case "bytes":
4170 if (!(value instanceof Uint8Array)) encodeFail("a Uint8Array", value, ctx.options)
4171 w.bytes(value)
4172 return
4173 case "bigint":
4174 if (typeof value !== "bigint") encodeFail("a bigint", value, ctx.options)
4175 w.zigzag(value)
4176 return
4177 // `disableChecks` can bypass the range check used to select these layouts.
4178 case "i32":

Callers 11

encodeSizedFunction · 0.70
encodeReasonFunction · 0.70
encodeWirePayloadFunction · 0.70
encodeStructFieldFunction · 0.70
encodeStructPositionalFunction · 0.70
encodeInternedFunction · 0.70
encodeRunRowFunction · 0.70
encodeArrayFunction · 0.70
encodeVariantFunction · 0.70
encodeUnionFunction · 0.70
writeFrameFunction · 0.70

Calls 15

hasLiteralFunction · 0.85
encodeFailFunction · 0.85
literalExpectedFunction · 0.85
isVarintNumberFunction · 0.85
decimalScaleFunction · 0.85
encodeSymbolFunction · 0.85
matchesLayoutFunction · 0.85
isCyclicFunction · 0.85
encodeReasonFunction · 0.85
encodeStructPositionalFunction · 0.85
encodeStructFieldsFunction · 0.85
encodeUnionFunction · 0.85

Tested by

no test coverage detected