Add typing to readJsonlFile

We're still not verifying the actual object returned by JSON.parse so
this isn't any safer at runtime than using 'any', but it helps add more
static typing to the code calling readJsonlFile.
This commit is contained in:
Robert
2024-02-07 13:58:12 +00:00
parent d7e9606bfa
commit f8e825287c
3 changed files with 5 additions and 5 deletions

View File

@@ -10,9 +10,9 @@ import { readFile } from "fs-extra";
* @param path The path to the file.
* @param handler Callback to be invoked for each top-level JSON object in order.
*/
export async function readJsonlFile(
export async function readJsonlFile<T>(
path: string,
handler: (value: any) => Promise<void>,
handler: (value: T) => Promise<void>,
): Promise<void> {
const logSummary = await readFile(path, "utf-8");
@@ -20,7 +20,7 @@ export async function readJsonlFile(
const jsonSummaryObjects: string[] = logSummary.split(/\r?\n\r?\n/g);
for (const obj of jsonSummaryObjects) {
const jsonObj = JSON.parse(obj);
const jsonObj = JSON.parse(obj) as T;
await handler(jsonObj);
}
}

View File

@@ -103,7 +103,7 @@ export class EvaluationLogScannerSet {
p.createScanner(problemReporter),
);
await readJsonlFile(jsonSummaryLocation, async (obj) => {
await readJsonlFile<SummaryEvent>(jsonSummaryLocation, async (obj) => {
scanners.forEach((scanner) => {
scanner.onEvent(obj);
});

View File

@@ -19,7 +19,7 @@ export async function parseViewerData(
): Promise<EvalLogData[]> {
const viewerData: EvalLogData[] = [];
await readJsonlFile(jsonSummaryPath, async (jsonObj) => {
await readJsonlFile<EvalLogData>(jsonSummaryPath, async (jsonObj) => {
// Only convert log items that have an RA and millis field
if (jsonObj.ra !== undefined && jsonObj.millis !== undefined) {
const newLogData: EvalLogData = {