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

Function getCandidateIndex

packages/effect/src/SchemaAST.ts:3524–3667  ·  view source on GitHub ↗
(types: ReadonlyArray<AST>)

Source from the content-addressed store, hash-verified

3522
3523/** @internal */
3524export function getCandidateIndex(types: ReadonlyArray<AST>): CandidateIndex {
3525 let index = candidateIndexCache.get(types)
3526 if (index) return index
3527
3528 let bySentinel: SentinelIndex | undefined
3529 let sentinelCandidateCount = 0
3530 let otherwise: { [K in Type]?: Array<number> } | undefined
3531 let literalCandidates: Map<LiteralValue | symbol, Array<number>> | undefined
3532 let onlyLiterals = true
3533 const literalOf: Array<LiteralValue | symbol | undefined> = []
3534 for (let i = 0; i < types.length; i++) {
3535 const a = types[i]
3536 const encoded = toCandidate(a)
3537 if (isNever(encoded)) continue
3538
3539 if (isLiteral(encoded) || isUniqueSymbol(encoded)) {
3540 literalCandidates ??= new Map()
3541 const literal = isLiteral(encoded) ? encoded.literal : encoded.symbol
3542 literalOf[i] = literal
3543 let arr = literalCandidates.get(literal)
3544 if (!arr) literalCandidates.set(literal, arr = [])
3545 arr.push(i)
3546 } else {
3547 onlyLiterals = false
3548 }
3549
3550 const sentinels = collectSentinels(encoded)
3551
3552 if (sentinels.length) { // discriminated variants
3553 bySentinel ??= new Map()
3554 sentinelCandidateCount++
3555 for (const { key, literal } of sentinels) {
3556 let entry = bySentinel.get(key)
3557 if (!entry) bySentinel.set(key, entry = [new Map(), new Set()])
3558 entry[1].add(i)
3559 let indexes = entry[0].get(literal)
3560 if (!indexes) entry[0].set(literal, indexes = new Set())
3561 indexes.add(i)
3562 }
3563 } else { // non-discriminated
3564 otherwise ??= {}
3565 const candidateTypes = getCandidateTypes(encoded)
3566 for (const t of candidateTypes) (otherwise[t] ??= []).push(i)
3567 }
3568 }
3569
3570 // Non-discriminated members are grouped by runtime type once and reused by every decode.
3571 const fallbacks: { [K in Type]?: ReadonlyArray<number> } = {}
3572 const getFallback = (type: Type): ReadonlyArray<number> =>
3573 fallbacks[type] ??= Object.freeze(otherwise?.[type] ?? emptyCandidates)
3574
3575 if (onlyLiterals && literalCandidates) {
3576 literalCandidates.forEach(Object.freeze)
3577 index = (input) => literalCandidates.get(input) ?? emptyCandidates
3578 } else if (bySentinel?.size === 1 && !otherwise) {
3579 const [key, [byValue]] = bySentinel.entries().next().value!
3580 const candidates = new Map<LiteralValue | symbol, ReadonlyArray<number>>()
3581 for (const [literal, indexes] of byValue) {

Callers 1

getParserMethod · 0.85

Calls 15

isNeverFunction · 0.85
isLiteralFunction · 0.85
collectSentinelsFunction · 0.85
getCandidateTypesFunction · 0.85
hasPropertySignatureFunction · 0.85
getRuntimeTypeFunction · 0.85
getFallbackFunction · 0.85
fromMethod · 0.80
getMethod · 0.65
setMethod · 0.65
pushMethod · 0.65
addMethod · 0.65

Tested by

no test coverage detected