Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow zson output #3160

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/zed-wasm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@brimdata/zed-wasm",
"version": "0.0.17",
"version": "0.0.18",
"homepage": "https://github.com/brimdata/zed",
"type": "module",
"files": [
Expand Down
13 changes: 9 additions & 4 deletions packages/zed-wasm/src/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@ export function createInterface(go, deps) {
// input?: string | File | Blob | ReadableStream | any[],
// inputFormat?: LoadFormat,
// outputFormat?: 'js' | 'zed',

zq: async (opts) => {
const result = await go.zq({
input: deps.getInput(opts.input),
inputFormat: opts.inputFormat,
program: opts.program,
outputFormat: 'zjson',
outputFormat: opts.outputFormat,
});

const zed = decode(ndjson.parseLines(result));
if (opts.outputFormat === 'zed') return zed;
return zed.map((val) => val.toJS());
if (opts.decodeAs) {
const zed = decode(ndjson.parseLines(result));
if (opts.decodeAs === 'zed') return zed;
return zed.map((val) => val.toJS());
} else {
return result;
}
},

parse: (string) => go.parse(string),
Expand Down
2 changes: 1 addition & 1 deletion packages/zed-wasm/test/browser/zq.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ test('run zq', async ({ page }) => {
await page.fill('[name=input]', '1 2 3');
await page.fill('[name=script]', 'this * 10');
await page.click('button');
await expect(page.locator('pre')).toContainText('[10,20,30]');
await expect(page.locator('pre')).toContainText('102030');
});
2 changes: 2 additions & 0 deletions packages/zed-wasm/test/cjs/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ test('zq works in cjs env', async () => {
const resp = await wasm.zq({
input: '1 2 3',
program: 'this * 10',
outputFormat: 'zjson',
decodeAs: 'js',
});
expect(resp).toEqual([10, 20, 30]);
});
16 changes: 15 additions & 1 deletion packages/zed-wasm/test/node/zq.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ test.before(async () => {

test('input is string', async (t) => {
const input = '1 2 3';
const resp = await zq({ input, program: 'this + 1' });
const resp = await zq({
input,
program: 'this + 1',
outputFormat: 'zjson',
decodeAs: 'js',
});
assert.deepEqual(resp, [2, 3, 4]);
});

test('output as zson', async (t) => {
const input = '1 2 3';
const resp = await zq({
input,
program: 'this + 1',
});
assert.deepEqual(resp, '2\n3\n4\n');
});
2 changes: 1 addition & 1 deletion packages/zed-wasm/test/pages/zq.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
input: input,
program: script,
});
puts(JSON.stringify(result));
puts(result);
} catch (e) {
puts(JSON.stringify(e, null, 2));
}
Expand Down
Loading