Skip to content

Commit 0a37254

Browse files
committed
rf: Separate sidecar reading from accounting/error reporting
1 parent acc89e7 commit 0a37254

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
@@ -17,7 +17,7 @@ import { ColumnsMap } from '../types/columns.ts'
1717
import { readEntities } from './entities.ts'
1818
import { findDatatype } from './datatypes.ts'
1919
import { DatasetIssues } from '../issues/datasetIssues.ts'
20-
import { walkBack } from '../files/inheritance.ts'
20+
import { readSidecars } from '../files/inheritance.ts'
2121
import { parseGzip } from '../files/gzip.ts'
2222
import { loadTSV, loadTSVGZ } from '../files/tsv.ts'
2323
import { parseTIFF } from '../files/tiff.ts'
@@ -201,29 +201,18 @@ export class BIDSContext implements Context {
201201
if (this.extension === '.json') {
202202
return
203203
}
204-
let sidecars: BIDSFile[] = []
204+
let sidecars: Map<string, Record<string, unknown>>
205205
try {
206-
sidecars = [...walkBack(this.file)]
207-
} catch (error) {
208-
if (
209-
error && typeof error === 'object' && 'code' in error &&
210-
error.code === 'MULTIPLE_INHERITABLE_FILES'
211-
) {
212-
// @ts-expect-error
206+
sidecars = await readSidecars(this.file)
207+
} catch (error: any) {
208+
if (error?.code) {
213209
this.dataset.issues.add(error)
210+
return
214211
} else {
215212
throw error
216213
}
217214
}
218-
for (const file of sidecars) {
219-
const json = await loadJSON(file).catch((error): Record<string, unknown> => {
220-
if (error.key) {
221-
this.dataset.issues.add({ code: error.key, location: file.path })
222-
return {}
223-
} else {
224-
throw error
225-
}
226-
})
215+
for (const [path, json] of sidecars.entries()) {
227216
const overrides = Object.keys(this.sidecar).filter((x) => Object.hasOwn(json, x))
228217
for (const key of overrides) {
229218
if (json[key] !== this.sidecar[key]) {
@@ -232,14 +221,14 @@ export class BIDSContext implements Context {
232221
code: 'SIDECAR_FIELD_OVERRIDE',
233222
subCode: key,
234223
location: overrideLocation,
235-
issueMessage: `Sidecar key defined in ${file.path} overrides previous value (${
224+
issueMessage: `Sidecar key defined in ${path} overrides previous value (${
236225
json[key]
237226
}) from ${overrideLocation}`,
238227
})
239228
}
240229
}
241230
this.sidecar = { ...json, ...this.sidecar }
242-
Object.keys(json).map((x) => this.sidecarKeyOrigin[x] ??= file.path)
231+
Object.keys(json).map((x) => this.sidecarKeyOrigin[x] ??= path)
243232
}
244233
// Hack: round RepetitionTime to 3 decimal places; schema should add rounding function
245234
if (typeof this.sidecar.RepetitionTime === 'number') {

0 commit comments

Comments
 (0)