|
| 1 | +import { assertParamsObject } from "@platforma-sdk/block-kind"; |
| 2 | +import { isImportFileHandleIndex, isImportFileHandleUpload } from "@milaboratories/pl-model-common"; |
| 3 | +import { isBoolean, isPlainObject, isString } from "es-toolkit"; |
| 4 | +import { isArray } from "es-toolkit/compat"; |
| 5 | +import type { |
| 6 | + BlockParams, |
| 7 | + DSAny, |
| 8 | + DSType, |
| 9 | + ImportFileHandle, |
| 10 | + MTColumn, |
| 11 | + MTValueType, |
| 12 | + PlId, |
| 13 | +} from "./types"; |
| 14 | + |
| 15 | +/** |
| 16 | + * The contract at runtime, for params that arrive from a template file rather |
| 17 | + * than from typed code. |
| 18 | + * |
| 19 | + * Each field the contract names is read and checked; a key it does not name is |
| 20 | + * dropped by never being read, so it needs no rejection here. Params written |
| 21 | + * against a different version of the contract are caught by the version in the |
| 22 | + * template entry's `{name}@{selector}` reference, not by a key-set check. |
| 23 | + */ |
| 24 | +export function parseInitializationParams(value: unknown): BlockParams { |
| 25 | + assertParamsObject(value); |
| 26 | + |
| 27 | + const params: Record<string, unknown> = {}; |
| 28 | + for (const [field, { is, must }] of Object.entries(CONTRACT)) { |
| 29 | + const raw = value[field]; |
| 30 | + if (raw === undefined) continue; |
| 31 | + if (!is(raw)) throw new Error(`'${field}' must be ${must}.`); |
| 32 | + params[field] = raw; |
| 33 | + } |
| 34 | + // Every value placed here passed its own field's guard, and `CONTRACT` is |
| 35 | + // proven exhaustive over `BlockParams` by the `satisfies` below. |
| 36 | + return params as BlockParams; |
| 37 | +} |
| 38 | + |
| 39 | +// --------------------------------------------------------------------------- |
| 40 | +// Internals |
| 41 | +// --------------------------------------------------------------------------- |
| 42 | + |
| 43 | +type Guard<T> = (value: unknown) => value is T; |
| 44 | + |
| 45 | +/** A guard plus how to finish the sentence "'field' must be …". */ |
| 46 | +type Check<T> = { readonly is: Guard<T>; readonly must: string }; |
| 47 | + |
| 48 | +function check<T>(is: Guard<T>, must: string): Check<T> { |
| 49 | + return { is, must }; |
| 50 | +} |
| 51 | + |
| 52 | +function arrayOf<T>(item: Guard<T>): Guard<T[]> { |
| 53 | + return (v): v is T[] => isArray(v) && v.every((e) => item(e)); |
| 54 | +} |
| 55 | + |
| 56 | +function recordOf<T>(item: Guard<T>): Guard<Record<string, T>> { |
| 57 | + return (v): v is Record<string, T> => isPlainObject(v) && Object.values(v).every((e) => item(e)); |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * A guard over the keys of one of the tag sets below, so the runtime check and |
| 62 | + * the type it guards are both read off the same object. |
| 63 | + */ |
| 64 | +function keyOf<T extends string>(set: Record<T, true>): Guard<T> { |
| 65 | + return (v): v is T => isString(v) && v in set; |
| 66 | +} |
| 67 | + |
| 68 | +function keyList(set: Record<string, true>): string { |
| 69 | + return Object.keys(set).join(", "); |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * The value types a metadata column may declare, as a runtime set. `satisfies |
| 74 | + * Record<MTValueType, true>` is what keeps it in step: a value type added to the |
| 75 | + * union and forgotten here fails to compile, rather than turning into a kind |
| 76 | + * that refuses a correct file. |
| 77 | + */ |
| 78 | +const MT_VALUE_TYPES = { |
| 79 | + Long: true, |
| 80 | + Double: true, |
| 81 | + String: true, |
| 82 | +} as const satisfies Record<MTValueType, true>; |
| 83 | + |
| 84 | +/** The dataset kinds this block knows, kept in step with the union the same way. */ |
| 85 | +const DATASET_TYPES = { |
| 86 | + Fastq: true, |
| 87 | + MultilaneFastq: true, |
| 88 | + TaggedFastq: true, |
| 89 | + Fasta: true, |
| 90 | + Xsv: true, |
| 91 | + TaggedXsv: true, |
| 92 | + BulkCountMatrix: true, |
| 93 | + CellRangerMTX: true, |
| 94 | + MultiplexedFastq: true, |
| 95 | + H5AD: true, |
| 96 | + H5: true, |
| 97 | + Seurat: true, |
| 98 | + MultiSampleH5AD: true, |
| 99 | + MultiSampleSeurat: true, |
| 100 | +} as const satisfies Record<DSType, true>; |
| 101 | + |
| 102 | +/** |
| 103 | + * A `PlId` is a branded 24-char base32 string, but the brand is erased at |
| 104 | + * runtime and the block treats these ids as opaque keys, so the envelope check |
| 105 | + * is the string test. |
| 106 | + */ |
| 107 | +const isPlId = isString as Guard<PlId>; |
| 108 | + |
| 109 | +/** |
| 110 | + * Both handle forms are accepted. The projection only sends `index://` handles, |
| 111 | + * but `upload://` is a legitimate value of the type, and the two SDK guards are |
| 112 | + * prefix tests — the cast only gets a checked string past a signature that |
| 113 | + * expects the union. |
| 114 | + */ |
| 115 | +const isFileHandle: Guard<ImportFileHandle> = (v): v is ImportFileHandle => |
| 116 | + isString(v) && |
| 117 | + (isImportFileHandleIndex(v as ImportFileHandle) || |
| 118 | + isImportFileHandleUpload(v as ImportFileHandle)); |
| 119 | + |
| 120 | +/** |
| 121 | + * A metadata column at the envelope: its identity, its declared value type, and |
| 122 | + * nothing about the values. What the values mean is settled where they are used. |
| 123 | + */ |
| 124 | +const isMetadataColumn: Guard<MTColumn> = (v): v is MTColumn => |
| 125 | + isPlainObject(v) && |
| 126 | + isString(v.id) && |
| 127 | + isString(v.label) && |
| 128 | + isBoolean(v.global) && |
| 129 | + keyOf(MT_VALUE_TYPES)(v.valueType); |
| 130 | + |
| 131 | +/** |
| 132 | + * A dataset at the envelope: its identity and the discriminator of its content. |
| 133 | + * |
| 134 | + * The file handles inside are deliberately not walked. Their shape differs per |
| 135 | + * dataset kind, and whether a handle still resolves is knowable only where the |
| 136 | + * block runs, not here — the same limit every kind has on a handle. Likewise |
| 137 | + * the per-sample and per-group maps: the block's own `args` is what holds |
| 138 | + * datasets, groups and rules to each other, and rejecting a half-configured |
| 139 | + * study here would refuse states the editor can reach. |
| 140 | + */ |
| 141 | +const isDataset: Guard<DSAny> = (v): v is DSAny => |
| 142 | + isPlainObject(v) && |
| 143 | + isString(v.id) && |
| 144 | + isString(v.label) && |
| 145 | + isPlainObject(v.content) && |
| 146 | + keyOf(DATASET_TYPES)(v.content.type) && |
| 147 | + isBoolean(v.content.gzipped); |
| 148 | + |
| 149 | +/** |
| 150 | + * The contract, field by field, at runtime. |
| 151 | + * |
| 152 | + * The `satisfies` clause is the drift guard: it demands an entry for every key |
| 153 | + * `BlockParams` declares, and types each guard against that key's own type. Add |
| 154 | + * a field to the contract and this stops compiling until the check exists — |
| 155 | + * which matters here because every field is optional, so a parser that simply |
| 156 | + * forgot one would otherwise return a valid `BlockParams` and say nothing. |
| 157 | + */ |
| 158 | +const CONTRACT = { |
| 159 | + datasets: check(arrayOf(isDataset), `an array of datasets of: ${keyList(DATASET_TYPES)}`), |
| 160 | + metadata: check( |
| 161 | + arrayOf(isMetadataColumn), |
| 162 | + `an array of metadata columns valued: ${keyList(MT_VALUE_TYPES)}`, |
| 163 | + ), |
| 164 | + sampleIds: check(arrayOf(isPlId), "an array of sample ids"), |
| 165 | + sampleLabelColumnLabel: check(isString, "a string"), |
| 166 | + sampleLabels: check(recordOf(isString), "an object of sample id to label"), |
| 167 | + h5adFilesToPreprocess: check(arrayOf(isFileHandle), "an array of file handles"), |
| 168 | + seuratFilesToPreprocess: check(arrayOf(isFileHandle), "an array of file handles"), |
| 169 | +} satisfies { [K in keyof BlockParams]-?: Check<NonNullable<BlockParams[K]>> }; |
0 commit comments