Skip to content

Commit

Permalink
#20 moved .env .ini and .properties data loading to new properties.da…
Browse files Browse the repository at this point in the history
…ta.provider.ts impl.
  • Loading branch information
RandomFractals committed Aug 13, 2019
1 parent 957c1f3 commit dc80f59
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 36 deletions.
4 changes: 3 additions & 1 deletion src/data.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {ExcelDataProvider} from './data.providers/excel.data.provider';
import {JsonDataProvider} from './data.providers/json.data.provider';
import {MarkdownDataProvider} from './data.providers/markdown.data.provider';
import {TextDataProvider} from './data.providers/text.data.provider';
import {PropertiesDataProvider} from './data.providers/properties.data.provider';

/**
* Data Manager API interface.
Expand Down Expand Up @@ -124,7 +125,8 @@ export class DataManager implements IDataManager {
this.addDataProvider(dataProviders, new ExcelDataProvider());
this.addDataProvider(dataProviders, new JsonDataProvider());
this.addDataProvider(dataProviders, new MarkdownDataProvider());
this.addDataProvider(dataProviders, new TextDataProvider());
this.addDataProvider(dataProviders, new TextDataProvider());
this.addDataProvider(dataProviders, new PropertiesDataProvider());
if (this._logger.logLevel === LogLevel.Debug) {
this._logger.debug('loadDataProviders(): loaded data providers:', Object.keys(dataProviders));
}
Expand Down
42 changes: 7 additions & 35 deletions src/data.providers/json.data.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import {window} from 'vscode';
import * as hjson from 'hjson';
import * as json5 from 'json5';
import * as yaml from 'js-yaml';
import * as props from 'properties';

// json data provider imports
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.manager';

/**
Expand All @@ -19,15 +18,15 @@ import {IDataProvider} from '../data.manager';
export class JsonDataProvider implements IDataProvider {

// TODO: add mime types later for http data loading
// TODO: consider implementing separate data providers for config and json data file types
// TODO: move .config and .yml to separate data.provider impl.
public supportedDataFileTypes: Array<string> =
['.config', '.env', '.ini', '.json', '.json5', '.hjson', '.properties', '.yaml', '.yml'];
['.config', '.json', '.json5', '.hjson', '.yaml', '.yml'];

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

/**
* Creates new JSON data provider for .json, .json5, .hjson data files,
* .config, .env, .ini, .properties and .yml config files.
* .config and .yml config files.
*/
constructor() {
this.logger.debug('created for:', this.supportedDataFileTypes);
Expand All @@ -40,11 +39,6 @@ export class JsonDataProvider implements IDataProvider {
private getDataParseFunction(dataFileType: string): Function {
let dataParseFunction: Function = JSON.parse; // default
switch (dataFileType) {
case '.env':
case '.ini':
case '.properties':
dataParseFunction = props.parse;
break;
case '.json5':
dataParseFunction = json5.parse;
break;
Expand All @@ -59,27 +53,6 @@ export class JsonDataProvider implements IDataProvider {
return dataParseFunction;
}

/**
* Gets data provider parse options for the specified data file type.
* @param dataFileType Data file type.
*/
private getDataParseOptions(dataFileType: string): Function {
let dataParseOptions: any = null; // default
switch (dataFileType) {
case '.env':
dataParseOptions = {sections: true, comments: ['#']};
break;
case '.ini':
// NOTE: some INI files consider # as a comment
dataParseOptions = {sections: true, comments: [';', '#']};
break;
case '.properties':
dataParseOptions = {sections: true};
break;
}
return dataParseOptions;
}

/**
* Gets local or remote data.
* @param dataUrl Local data file path or remote data url.
Expand All @@ -97,15 +70,14 @@ export class JsonDataProvider implements IDataProvider {
const comments: RegExp = new RegExp(/\/\*[\s\S]*?\*\/|\/\/.*/g);
content = content.replace(comments, '');
}
const parseFunction: Function = this.getDataParseFunction(dataFileType);
const parseOptions: any = this.getDataParseOptions(dataFileType);
data = (parseOptions) ? parseFunction(content, parseOptions) : parseFunction(content);
const dataParseFunction: Function = this.getDataParseFunction(dataFileType);
data = dataParseFunction(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}`);
}
loadData( jsonUtils.convertJsonData(data) );
loadData(jsonUtils.convertJsonData(data));
}

/**
Expand Down
95 changes: 95 additions & 0 deletions src/data.providers/properties.data.provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import {window} from 'vscode';
import * as props from 'properties';
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.manager';

/**
* Properties data provider for .properties, .ini and .env data files.
*/
export class PropertiesDataProvider implements IDataProvider {

// TODO: add mime types later for http data loading
public supportedDataFileTypes: Array<string> = ['.env', '.ini', '.properties'];

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

/**
* Creates new properties data provider for .env, .ini, and .properties config files.
*/
constructor() {
this.logger.debug('created for:', this.supportedDataFileTypes);
}

/**
* Gets local or remote data.
* @param dataUrl Local data file path or remote data url.
* @param parseOptions Data parse options.
* @param loadData Load data callback.
*/
public getData(dataUrl: string, parseOptions: any, loadData: Function): void {
let data: any = [];
// TODO: add mime types later for remote http data loading
const dataFileType: string = dataUrl.substr(dataUrl.lastIndexOf('.')); // file extension
try {
// read and parse properties file type data
let content: string = fileUtils.readDataFile(dataUrl, 'utf8');
data = props.parse(content, this.getDataParseOptions(dataFileType));
}
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}`);
}
loadData(jsonUtils.convertJsonData(data));
}

/**
* Gets data provider parse options for the specified data file type.
* @param dataFileType Data file type.
*/
private getDataParseOptions(dataFileType: string): Function {
let dataParseOptions: any = null; // default
switch (dataFileType) {
case '.env':
dataParseOptions = {sections: true, comments: ['#']};
break;
case '.ini':
// NOTE: some INI files consider # as a comment
dataParseOptions = {sections: true, comments: [';', '#']};
break;
case '.properties':
dataParseOptions = {sections: true};
break;
}
return dataParseOptions;
}

/**
* Gets data table names for data sources with multiple data sets.
* @param dataUrl Local data file path or remote data url.
*/
public getDataTableNames(dataUrl: string): Array<string> {
return []; // none for properties data files
}

/**
* Gets data schema in json format for file types that provide it.
* @param dataUrl Local data file path or remote data url.
*/
public getDataSchema(dataUrl: string): any {
// TODO: auto-gen json schema ???
return null; // none for properties data files
}

/**
* 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 dc80f59

Please sign in to comment.