Skip to content

Commit 2ff6a70

Browse files
#28 implemented .config json file data.preview
1 parent 8127395 commit 2ff6a70

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

src/data.preview.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,10 @@ export class DataPreview {
407407
case '.properties':
408408
data = jsonUtils.configToPropertyArray(fs.readFileSync(dataFilePath, 'utf8'));
409409
break;
410+
case '.config':
411+
data = jsonUtils.objectToPropertyArray(
412+
jsonUtils.flattenObject(JSON.parse(fs.readFileSync(dataFilePath, 'utf8')), true)); // preserve parent path
413+
break;
410414
case '.arrow':
411415
data = this.getArrowData(dataFilePath);
412416
break;
@@ -418,10 +422,6 @@ export class DataPreview {
418422
window.showInformationMessage('Parquet Data Preview 🈸 coming soon!');
419423
// data = this.getParquetData(dataFilePath);
420424
break;
421-
case '.config':
422-
window.showInformationMessage('Data view .config driven Data Preview 🈸 coming soon!');
423-
// data = this.getDataFromConfig(dataFilePath);
424-
break;
425425
}
426426
return data;
427427
} // end of getFileData()

src/utils/json.utils.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,38 @@ const logger: Logger = new Logger(`json.utils:`, config.logLevel);
1010
* Flattens objects with nested properties for data view display.
1111
* @param obj Object to flatten.
1212
* @param preservePath Optional flag for generating key path.
13-
* @param prefix Parent key prefix.
13+
* @param path Parent key path.
1414
* @returns Flat Object.
1515
*/
16-
export function flattenObject (obj: any, preservePath: boolean = false, prefix: string = ''): any {
16+
export function flattenObject (obj: any, preservePath: boolean = false, path: string = ''): any {
1717
const flatObject: any = {};
1818
Object.keys(obj).forEach((key) => {
19-
if (preservePath && prefix.length > 0) {
20-
// append parent key prefix
21-
key = `${prefix}.${key}`;
22-
prefix = key;
23-
}
2419
if (typeof obj[key] === 'object' && obj[key] !== null) {
25-
Object.assign(flatObject, this.flattenObject(obj[key]), preservePath, prefix);
20+
Object.assign(flatObject, this.flattenObject(obj[key]), preservePath, path);
2621
} else {
2722
flatObject[key] = obj[key];
2823
}
2924
});
3025
return flatObject;
3126
}
3227

28+
/**
29+
* Converts an object to an array of property key/value objects.
30+
* @param obj Object to convert.
31+
*/
32+
export function objectToPropertyArray(obj: any): Array<any> {
33+
const properties: Array<any> = [];
34+
if (obj && obj !== undefined) {
35+
Object.keys(obj).forEach((key) => {
36+
properties.push({
37+
key: key,
38+
value: obj[key]
39+
});
40+
});
41+
}
42+
return properties;
43+
}
44+
3345
/**
3446
* Converts .env or .properties config file
3547
* to an array of property key/value objects.

templates/data.view.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@
261261
break;
262262
case '.json':
263263
// try to parse JSON array data
264-
tableData = JSON.parse(data);
264+
tableData = JSON.parse(data);
265265
break;
266266
case '.xls':
267267
case '.xlsb':
@@ -276,6 +276,7 @@
276276
case '.avro':
277277
case '.env':
278278
case '.properties':
279+
case '.config':
279280
// pass through loaded data json
280281
tableData = data;
281282
console.log(`data.view:getData(): records count: ${tableData.length}`);

0 commit comments

Comments
 (0)