Skip to content

Commit

Permalink
#20 added json.data.provider impl + refined data.provider api interface
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomFractals committed Jul 1, 2019
1 parent 7120611 commit 36e44f2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/data.providers/data.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ export interface IDataProvider {
/**
* Gets data format data.
* @param dataUrl Local data file path or remote data url.
* @param parseFunction Optional data parse function override.
* @param parseOptions Optional data parsing options.
*/
getData(dataUrl: string): any;
getData(dataUrl: string, parseFunction: Function, parseOptions: any): any;


/**
* Saves raw Data Provider data.
* @param filePath Data file path.
* @param fileData Raw data to save.
* @param stringifyFunction Optional stringiy function override.
*/
saveData(fileData: any): void;
saveData(filePath: string, fileData: any, stringifyFunction: Function): void;
}
52 changes: 52 additions & 0 deletions src/data.providers/json.data.provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

// vscode imports
import {window} from 'vscode';

import * as fs from 'fs';
import * as path from 'path';
import * as config from '../config';
import * as fileUtils from '../utils/file.utils';
import * as jsonUtils from '../utils/json.utils';
import {Logger, LogLevel} from '../logger';
import {IDataProvider} from './data.provider';

/**
* JSON data provider.
*/
export class JsonDataProvider implements IDataProvider {

private logger: Logger = new Logger('json.data.provider:', config.logLevel);

/**
* Gets data format data.
* @param dataUrl Local data file path or remote data url.
* @param parseFunction Optional data parse function override.
* @param parseOptions Optional data parsing options.
*/
public getData(dataUrl: string,
parseFunction: Function,
parseOptions: any = null): any {
let data: any = [];
try {
const content: string = fileUtils.readDataFile(dataUrl, 'utf8');
data = (parseOptions) ? parseFunction(content, parseOptions) : parseFunction(content);
}
catch (error) {
this.logger.logMessage(LogLevel.Error,
`getData(): Error parsing '${dataUrl}' \n\t Error:`, error.message);
window.showErrorMessage(`Unable to parse data file: '${dataUrl}'. \n\t Error: ${error.message}`);
}
return jsonUtils.convertJsonData(data);
}

/**
* Saves raw Data Provider data.
* @param filePath Data file path.
* @param fileData Raw data to save.
* @param stringifyFunction Optional stringiy function override.
*/
public saveData(filePath: string, fileData: any, stringifyFunction: Function): void {
// TODO
}
}

0 comments on commit 36e44f2

Please sign in to comment.