Skip to content

Commit 5a61a8e

Browse files
committed
feat: formatting JSON output
closes llvm#43
1 parent a2ad7e1 commit 5a61a8e

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

package.json

+11
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,17 @@
9595
"trace"
9696
],
9797
"default": "info"
98+
},
99+
"parquet-viewer.jsonSpace": {
100+
"markdownDescription": "JSON indentation space, passed to `JSON.stringify` as is, see [mdn](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#parameters) for details. Only applies when `#parquet-viewer.useParquetTools#` is `false`.",
101+
"type": [
102+
"string",
103+
"number"
104+
],
105+
"default": 0,
106+
"minimum": 0,
107+
"maximum": 10,
108+
"maxLength": 10
98109
}
99110
}
100111
}

src/parquets-backend.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { getLogger } from './logger';
33
import { ParquetReader } from '@dvirtz/parquets';
44
import * as os from 'os';
55
import { ParquetBackend } from './parquet-backend';
6+
import { jsonSpace } from './settings';
67

78
export class ParquetsBackend implements ParquetBackend {
89
public async * toJson(parquetPath: string, token?: vscode.CancellationToken): AsyncGenerator<string> {
@@ -20,7 +21,7 @@ export class ParquetsBackend implements ParquetBackend {
2021
// read all records from the file and print them
2122
let record = null;
2223
while (!token?.isCancellationRequested && (record = await cursor.next())) {
23-
yield `${JSON.stringify(record)}${os.EOL}`;
24+
yield `${JSON.stringify(record, null, jsonSpace())}${os.EOL}`;
2425
}
2526

2627
await reader.close();

src/settings.ts

+4
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@ export async function setLogLevel(logLevel: LogLevel | undefined): Promise<void>
4040
export function useParquetTools(): boolean {
4141
return settings().get('useParquetTools', false);
4242
}
43+
44+
export function jsonSpace(): number | string | undefined {
45+
return settings().get('jsonSpace');
46+
}

test/unit/parquets-backend.test.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { createReadStream } from 'fs';
44
import * as path from 'path';
55
import { createInterface } from 'readline';
66
import { ParquetsBackend } from '../../src/parquets-backend';
7+
import { jsonSpace } from '../../src/settings';
78

89

910
const rootDir = path.join(__dirname, '..', '..');
@@ -20,13 +21,15 @@ jest.mock('vscode', () => {
2021
};
2122
});
2223

24+
jest.mock('../../src/settings');
25+
2326
describe("ParquetsBackend tests", () => {
2427
const backend = new ParquetsBackend();
28+
const workspace = path.join(rootDir, 'test', 'workspace');
2529

2630
test.each(
2731
["small", "large"]
2832
)('Converts %s parquet to JSON', async function (name) {
29-
const workspace = path.join(rootDir, 'test', 'workspace');
3033
const json = (await toArray(backend.toJson(path.join(workspace, `${name}.parquet`)))).map(line => line.trim());
3134
const expected = await toArray(createInterface({input: createReadStream(path.join(workspace, `${name}.json`))}));
3235

@@ -38,4 +41,14 @@ describe("ParquetsBackend tests", () => {
3841
'message': expect.stringMatching(/while reading no-such-file: Error: ENOENT: no such file or directory, stat '.*no-such-file'/)
3942
});
4043
});
44+
45+
test.each([0, 2, 10, "\t", "###"])('Test space %s', async function (space) {
46+
jest.mocked(jsonSpace).mockReturnValue(space);
47+
48+
const json = (await toArray(backend.toJson(path.join(workspace, `small.parquet`)))).map(line => line.trim());
49+
const records = await toArray(createInterface({input: createReadStream(path.join(workspace, `small.json`))}));
50+
const expected = records.map(record => JSON.stringify(JSON.parse(record), null, space));
51+
52+
expect(json).toEqual(expected);
53+
});
4154
});

0 commit comments

Comments
 (0)