Skip to content

Add JSON5 parsing capabilities for integration configs #1732

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

Merged
merged 13 commits into from
May 2, 2024
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"antlr4ts": "^0.5.0-alpha.4",
"cypress-multi-reporters": "^1.6.3",
"cypress-parallel": "^0.13.0",
"json5": "^2.2.3",
"mime": "^3.0.0",
"mocha": "10.1.0",
"performance-now": "^2.1.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE MATERIALIZED VIEW {table_name}__week_live_mview AS
CREATE MATERIALIZED VIEW {table_name}__live_mview AS
SELECT
cloud.account_uid AS `aws.vpc.cloud_account_uid`,
cloud.region AS `aws.vpc.cloud_region`,
Expand Down
47 changes: 47 additions & 0 deletions server/adaptors/integrations/__test__/local_fs_repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,50 @@ describe('Local Nginx Integration', () => {
);
});
});
describe('JSON5 Integration', () => {
const dataDirectory: string = path.join(__dirname, '../__data__/repository');
const fileSystemAdaptor: FileSystemDataAdaptor = new FileSystemDataAdaptor(dataDirectory);
let files: string[];

beforeAll(async () => {
files = await fs.readdir(dataDirectory);
});

it('Should parse valid JSON5 content without errors', async () => {
if (!files) {
throw new Error('File list not initialized');
}

await Promise.all(
files.map(async (filename: string) => {
const filePath: string = path.join(dataDirectory, filename);
try {
const result = await fileSystemAdaptor.readFile(filename);
expect(result.ok).toBe(true);
expect(result.value).toEqual({ key: 'value' });
} catch (error) {
console.error(`Error reading file ${filePath}:`, error);
}
})
);
});

it('Should handle errors when parsing invalid JSON5 content', async () => {
if (!files) {
throw new Error('File list not initialized');
}

await Promise.all(
files.map(async (filename: string) => {
const filePath: string = path.join(dataDirectory, filename);
try {
const result = await fileSystemAdaptor.readFile(filename);
expect(result.ok).toBe(false);
expect(result.error).toBeDefined();
} catch (error) {
console.error(`Error reading file ${filePath}:`, error);
}
})
);
});
});
3 changes: 2 additions & 1 deletion server/adaptors/integrations/repository/fs_data_adaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import * as fs from 'fs/promises';
import path from 'path';
import JSON5 from 'json5';
import { CatalogDataAdaptor, IntegrationPart } from './catalog_data_adaptor';
import { tryParseNDJson } from './utils';

Expand Down Expand Up @@ -45,7 +46,7 @@ export class FileSystemDataAdaptor implements CatalogDataAdaptor {
}
// First try to parse as JSON, then NDJSON, then fail.
try {
const parsed = JSON.parse(content);
const parsed = JSON5.parse(content);
return { ok: true, value: parsed };
} catch (err) {
const parsed = await tryParseNDJson(content);
Expand Down
4 changes: 2 additions & 2 deletions server/adaptors/integrations/repository/json_data_adaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class JsonCatalogDataAdaptor implements CatalogDataAdaptor {
if (type !== undefined) {
return {
ok: false,
error: new Error('JSON adaptor does not support subtypes (isConfigLocalized: true)'),
error: new Error('JSON5 adaptor does not support subtypes (isConfigLocalized: true)'),
};
}

Expand All @@ -53,7 +53,7 @@ export class JsonCatalogDataAdaptor implements CatalogDataAdaptor {
async readFileRaw(_filename: string, _type?: IntegrationPart): Promise<Result<Buffer>> {
return {
ok: false,
error: new Error('JSON adaptor does not support raw files (isConfigLocalized: true)'),
error: new Error('JSON5 adaptor does not support raw files (isConfigLocalized: true)'),
};
}

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2007,6 +2007,11 @@ json-stringify-safe@~5.0.1:
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"
integrity sha512-ZClg6AaYvamvYEE82d3Iyd3vSSIjQ+odgjaTzRuO3s7toCdFKczob2i0zCh7JE8kWn17yvAWhUVxvqGwUalsRA==

json5@^2.2.3:
version "2.2.3"
resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.3.tgz#78cd6f1a19bdc12b73db5ad0c61efd66c1e29283"
integrity sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==

jsonfile@^6.0.1:
version "6.1.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-6.1.0.tgz#bc55b2634793c679ec6403094eb13698a6ec0aae"
Expand Down
Loading