T0042 — Functions: projection and repeat
Part of the roadmap. Phase 1 · wave 5 · size M.
Goal
N1 §5.2: select, repeat, ofType completion. Filtering itself is delivered by the narrowed T0012.
Scope
Preserve the original T0012 projection/repeat/type-filtering contract after where moved into the small 0.2 scope. This task has no selected release or due date. Existing where behaviour and all original acceptance below remain part of regression coverage.
- Lambda functions built on the T0008 helpers.
repeatdeduplicates per the spec. That only guarantees termination for projections that navigate a finite input; a projection that generates values, such as'x'.repeat($this & 'x'), never runs out of new items.repeattherefore enforceslimits.maxRepeatItems(with a finite default chosen and documented in this task) and raiseseval/limit-exceeded(architecture: security and robustness).- Implement
ofTypewith System types and the T0008 model-less resource-name matcher using the existing type-test kernels, and with model types through thematchesTypeTarget()rule'ofType'that T0018 delivered inruntime/type-target.ts. Reuse the same target resolution and execution-error contract asis/as; preserve the existing FHIR primitive mapping distinction forofType()(spec notes). - FHIR versions (roadmap): the model-typed
ofType()cases also run against the realr4,r5andr6providers inpackages/core/test/generated-models.test.ts, with each version's allowed types forObservation.value[x](Referencein R5 only,Attachmentin R5 and R6) and the R6-onlyDeviceAlertas a type under R6 and aneval/unknown-typetarget under R4 and R5. The R6 draft's FHIR note onofType()(§2.1.9.4.2) is the same as R5's. R6 results are engine tests on the pinned draft, not conformance.
Design
select,repeatandofTypeare infunctions/filtering.ts, next towhere(N1 §5.2).selectandrepeatare lambda functions on the internal sync/async path;ofTypetakes a type argument, resolved at compile time, and reusesassertResolved()andmatchesTypeTarget(…, 'ofType')fromruntime/type-target.ts.repeatfollows 3.0.0's queue, so its result is breadth first. An item is found when=is true for an item of the result, so undetermined comparisons are new, as for|, or when it is the same input node (isSameNode()inruntime/node-value.ts), so an element that holds only extensions, which=cannot compare, is found once and a walk back to it ends. It checks T0021's cancellation signal (throwIfAborted()) before each item it projects. The projection is bound withforInput()for one item at a time: 3.0.0 does not set$indexthere, so it stays that of the call (spec notes).- Limit:
limits.maxRepeatItemsdefaults to 5,000 (DEFAULT_LIMITSinapi/compile.ts).repeatcompares each new item with every item found, so a tree of thousands of complex items already takes seconds to deduplicate; at 5,000 a value-generating projection stops within a fraction of a second. The internalEvaluateOptions.limitsoption has the shape of T0038'sEvaluationLimits; it is checked for every evaluation (a positive integer, otherwiseeval/invalid-argument) and resolved intoEvalContext.limits. The public facade gets the option with T0038.limits.maxCollectionSize, and with it #186 (descendants()on cyclic input), are not part of this task. - The
area:corechanges are that option and field, inapi/compile.ts,runtime/context.tsand the two tests that build anEvalContextby hand, andisSameNode()inruntime/node-value.ts.
Acceptance criteria
- Each function is tested in sync and async mode, and
$this/$indexare correct in nested lambdas. -
repeatterminates on nested input structures (for exampleQuestionnaire.repeat(item)). - A value-generating projection (
'x'.repeat($this & 'x')) stops witheval/limit-exceededin both sync and async mode, within the default limit and within a customlimits.maxRepeatItems. -
ofType(System.Patient)and model-lessofType(FHIR.Patient)raiseeval/unknown-type, including on empty input. A resolved nonmatching type gives empty. Without a model,Bundle.entry.resource.ofType(Patient)keeps only objects whose ownresourceTypeequalsPatient; absent/misspelled resource-name targets give empty (#58, #59). - With the real
r4,r5andr6providers,Observation.value.ofType(Reference)andofType(Attachment)follow each version's allowed types, andBundle.entry.resource.ofType(DeviceAlert)keeps the alert under R6 and raiseseval/unknown-typeunder R4 and R5. No R6 conformance result is claimed. - Conformance groups:
testWhere,testSelect,testRepeat, and theofType()tests amongtestFHIRPathAsFunction17–24that T0018 left to this task, in R4 and R5 (16stays a suite exception). The tests of other groups that waited forselect()pass too:testAllTrue1,testAllTrue2,testDistinct3andtestDistinct6(T0041), andtestUnion9–11(T0013).