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

Function encodeKeys

packages/effect/src/Schema.ts:3664–3696  ·  view source on GitHub ↗
(mapping: M)

Source from the content-addressed store, hash-verified

3662 * @since 4.0.0
3663 */
3664export function encodeKeys<
3665 S extends Constraint & { readonly fields: Struct.Fields },
3666 const M extends { readonly [K in keyof S["fields"]]?: PropertyKey }
3667>(mapping: M) {
3668 return function(self: S): encodeKeys<S, M> {
3669 const fields: any = {}
3670 const appliedMapping: any = Object.create(null)
3671 const reverseMapping: any = Object.create(null)
3672 const seenEncodedKeys = new Set<string | symbol>()
3673 for (const k of Reflect.ownKeys(self.fields)) {
3674 const encoded = toEncoded(self.fields[k])
3675 const hasMapping = Object.hasOwn(mapping, k)
3676 const encodedKey = hasMapping ? (mapping as any)[k] as PropertyKey : k
3677 const canonical = canonicalPropertyKey(encodedKey)
3678 if (seenEncodedKeys.has(canonical)) {
3679 throw new globalThis.Error(`Duplicate encoded keys: ${formatPropertyKey(encodedKey)}`)
3680 }
3681 seenEncodedKeys.add(canonical)
3682 InternalRecord.assignProperty(fields, encodedKey, encoded)
3683 if (hasMapping) {
3684 appliedMapping[k] = encodedKey
3685 reverseMapping[encodedKey] = k
3686 }
3687 }
3688 return Struct(fields).pipe(decodeTo(
3689 self,
3690 SchemaTransformation.transform<any, any>({
3691 decode: Struct_.renameKeys(reverseMapping),
3692 encode: Struct_.renameKeys(appliedMapping)
3693 })
3694 )) as any
3695 }
3696}
3697
3698/**
3699 * Adds derived fields to a struct schema during decoding.

Callers

nothing calls this directly

Calls 9

formatPropertyKeyFunction · 0.90
toEncodedInterface · 0.85
canonicalPropertyKeyFunction · 0.85
StructFunction · 0.70
decodeToFunction · 0.70
addMethod · 0.65
pipeMethod · 0.65
transformMethod · 0.65
hasMethod · 0.45

Tested by

no test coverage detected