Skip to content

Commit 00cd703

Browse files
committed
rf: Separate sidecar reading from accounting/error reporting
1 parent d798ffc commit 00cd703

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

src/files/inheritance.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { BIDSFile, FileTree } from '../types/filetree.ts'
22
import { readEntities } from '../schema/entities.ts'
3+
import { loadJSON } from './json.ts'
34

45
type Ret<T> = T extends [string, ...string[]] ? (BIDSFile | BIDSFile[]) : BIDSFile
56

@@ -77,3 +78,20 @@ export function* walkBack<T extends string[]>(
7778
fileTree = fileTree.parent
7879
}
7980
}
81+
82+
export async function readSidecars(
83+
source: BIDSFile,
84+
): Promise<Map<string, Record<string, unknown>>> {
85+
const ret: Map<string, Record<string, unknown>> = new Map()
86+
for (const file of walkBack(source)) {
87+
try {
88+
ret.set(file.path, await loadJSON(file))
89+
} catch (e: any) {
90+
// Expect JSON parsing errors to be handled when the file is loaded directly
91+
if (!e?.code) {
92+
throw e
93+
}
94+
}
95+
}
96+
return ret
97+
}

src/schema/context.ts

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { FileTree } from '../types/filetree.ts'
1616
import { ColumnsMap } from '../types/columns.ts'
1717
import { readEntities } from './entities.ts'
1818
import { DatasetIssues } from '../issues/datasetIssues.ts'
19-
import { walkBack } from '../files/inheritance.ts'
19+
import { readSidecars } from '../files/inheritance.ts'
2020
import { parseGzip } from '../files/gzip.ts'
2121
import { loadTSV, loadTSVGZ } from '../files/tsv.ts'
2222
import { parseTIFF } from '../files/tiff.ts'
@@ -194,29 +194,18 @@ export class BIDSContext implements Context {
194194
if (this.extension === '.json') {
195195
return
196196
}
197-
let sidecars: BIDSFile[] = []
197+
let sidecars: Map<string, Record<string, unknown>>
198198
try {
199-
sidecars = [...walkBack(this.file)]
200-
} catch (error) {
201-
if (
202-
error && typeof error === 'object' && 'code' in error &&
203-
error.code === 'MULTIPLE_INHERITABLE_FILES'
204-
) {
205-
// @ts-expect-error
199+
sidecars = await readSidecars(this.file)
200+
} catch (error: any) {
201+
if (error?.code) {
206202
this.dataset.issues.add(error)
203+
return
207204
} else {
208205
throw error
209206
}
210207
}
211-
for (const file of sidecars) {
212-
const json = await loadJSON(file).catch((error): Record<string, unknown> => {
213-
if (error.key) {
214-
this.dataset.issues.add({ code: error.key, location: file.path })
215-
return {}
216-
} else {
217-
throw error
218-
}
219-
})
208+
for (const [path, json] of sidecars.entries()) {
220209
const overrides = Object.keys(this.sidecar).filter((x) => Object.hasOwn(json, x))
221210
for (const key of overrides) {
222211
if (json[key] !== this.sidecar[key]) {
@@ -225,14 +214,14 @@ export class BIDSContext implements Context {
225214
code: 'SIDECAR_FIELD_OVERRIDE',
226215
subCode: key,
227216
location: overrideLocation,
228-
issueMessage: `Sidecar key defined in ${file.path} overrides previous value (${
217+
issueMessage: `Sidecar key defined in ${path} overrides previous value (${
229218
json[key]
230219
}) from ${overrideLocation}`,
231220
})
232221
}
233222
}
234223
this.sidecar = { ...json, ...this.sidecar }
235-
Object.keys(json).map((x) => this.sidecarKeyOrigin[x] ??= file.path)
224+
Object.keys(json).map((x) => this.sidecarKeyOrigin[x] ??= path)
236225
}
237226
// Hack: round RepetitionTime to 3 decimal places; schema should add rounding function
238227
if (typeof this.sidecar.RepetitionTime === 'number') {

0 commit comments

Comments
 (0)