-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#20 added json.data.provider impl + refined data.provider api interface
- Loading branch information
1 parent
7120611
commit 36e44f2
Showing
2 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |