|
| 1 | +import {window} from 'vscode'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as config from '../config'; |
| 4 | +import * as fileUtils from '../utils/file.utils'; |
| 5 | +import * as jsonUtils from '../utils/json.utils'; |
| 6 | +import {Logger, LogLevel} from '../logger'; |
| 7 | +import {IDataProvider} from '../data.manager'; |
| 8 | + |
| 9 | +/** |
| 10 | + * JSON Line data provider. |
| 11 | + * @see http://jsonlines.org/ |
| 12 | + */ |
| 13 | +export class JsonLineDataProvider implements IDataProvider { |
| 14 | + |
| 15 | + // TODO: add mime types later for http data loading |
| 16 | + public supportedDataFileTypes: Array<string> = ['.jsonl', '.ndjson']; |
| 17 | + private logger: Logger = new Logger('json.line.data.provider:', config.logLevel); |
| 18 | + |
| 19 | + /** |
| 20 | + * Creates new JSON Line data provider for .jsonl and .ndjson data files. |
| 21 | + */ |
| 22 | + constructor() { |
| 23 | + this.logger.debug('created for:', this.supportedDataFileTypes); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Gets local or remote data. |
| 28 | + * @param dataUrl Local data file path or remote data url. |
| 29 | + * @param parseOptions Data parse options. |
| 30 | + * @param loadData Load data callback. |
| 31 | + */ |
| 32 | + public async getData(dataUrl: string, parseOptions: any, loadData: Function): Promise<void> { |
| 33 | + let data: any = []; |
| 34 | + let lineIndex = 1; |
| 35 | + try { |
| 36 | + let content: string = String(await fileUtils.readDataFile(dataUrl, 'utf8')); |
| 37 | + const jsonLines: Array<string> = content.split('\n'); |
| 38 | + jsonLines.forEach(jsonLine => { |
| 39 | + const trimmedJsonLine: string = jsonLine.trim(); |
| 40 | + if (trimmedJsonLine.length > 0) { |
| 41 | + data.push(JSON.parse(trimmedJsonLine)); |
| 42 | + } |
| 43 | + lineIndex++; |
| 44 | + }); |
| 45 | + } |
| 46 | + catch (error) { |
| 47 | + this.logger.logMessage(LogLevel.Error, `getData(): Error parsing '${dataUrl}' \ |
| 48 | + \n\t line #: ${lineIndex} Error:`, error.message); |
| 49 | + window.showErrorMessage(`Unable to parse data file: '${dataUrl}'. \ |
| 50 | + \n\t Line #: ${lineIndex} Error: ${error.message}`); |
| 51 | + } |
| 52 | + loadData(jsonUtils.convertJsonData(data)); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Gets data table names for data sources with multiple data sets. |
| 57 | + * @param dataUrl Local data file path or remote data url. |
| 58 | + */ |
| 59 | + public getDataTableNames(dataUrl: string): Array<string> { |
| 60 | + return []; // none for json data files |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Gets data schema in json format for file types that provide it. |
| 65 | + * @param dataUrl Local data file path or remote data url. |
| 66 | + */ |
| 67 | + public getDataSchema(dataUrl: string): any { |
| 68 | + // TODO: auto-gen json schema ??? |
| 69 | + return null; // none for json data files |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Saves JSON data. |
| 74 | + * @param filePath Local data file path. |
| 75 | + * @param fileData Raw data to save. |
| 76 | + * @param tableName Table name for data files with multiple tables support. |
| 77 | + * @param showData Show saved data callback. |
| 78 | + */ |
| 79 | + public saveData(filePath: string, fileData: any, tableName: string, showData?: Function): void { |
| 80 | + fileData = JSON.stringify(fileData, null, 2); |
| 81 | + if ( fileData.length > 0) { |
| 82 | + // TODO: change this to async later |
| 83 | + fs.writeFile(filePath, fileData, (error) => showData(error)); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments