Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation - QuoteChar #164

Open
wants to merge 1 commit into
base: feat/c4gt
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion impl/c-qube/ingest/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"globals": {
"onlyCreateWhitelisted": true
"onlyCreateWhitelisted": true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the code related to onlyCreateWhiteList from this PR.

"quoteChar": "`"
},
"dimensions": {
"namespace": "dimensions",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ describe('remove empty lines', () => {
'test/fixtures/test-csvs/csvcleaner/withEmpty.csv',
'test/fixtures/test-csvs/csvcleaner/withoutEmpty.csv',
'test/fixtures/test-csvs/csvreader/invalid.reader.csv',
'test/fixtures/test-csvs/csvreader/quote.csv',
'test/fixtures/test-csvs/csvreader/valid.reader.csv',
'test/fixtures/test-csvs/event-grammars/test-dimension.grammar.csv',
]);
]);
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { readCSV, readCSVFile } from './csvreader';
import { readCSV, readCSVFile, getquoteChar } from './csvreader';
const fs1 = require('fs');



describe('CSVReader', () => {
test('parse the file fine', async () => {
Expand Down Expand Up @@ -37,4 +40,34 @@ describe('CSVReader', () => {
test('benchmarking', () => {
// add a benchmark test here similar to date parser
});
});

test('should return the quote character from the config file', () => {
jest.spyOn(fs1, 'readFileSync').mockReturnValue('{"globals": {"quoteChar": "`"}}');
const configPath ='ingest/config.json';
const quoteChar = getquoteChar(configPath);
expect(quoteChar).toBe('`');
});

it('should take quote value from config , incase both are defined ', async () => {
let rows = await readCSV('./test/fixtures/test-csvs/csvreader/quote.csv', 'ingest/config.json', '`');
console.log(rows)
expect(rows).toEqual([
[ 'Name', 'Age', 'Country' ],
[ 'John Doe', '30', 'USA' ],
[ 'Jane Smith', '25', 'Canada' ],
[ 'Backtick User', '40', 'Australia' ]
]);
});

it('should take quote value from config , incase one is defined ', async () => {
let rows = await readCSV('./test/fixtures/test-csvs/csvreader/quote.csv', 'ingest/config.json', undefined);
console.log(rows)
expect(rows).toEqual([
[ 'Name', 'Age', 'Country' ],
[ 'John Doe', '30', 'USA' ],
[ 'Jane Smith', '25', 'Canada' ],
[ 'Backtick User', '40', 'Australia' ]
]);
});

});
23 changes: 20 additions & 3 deletions impl/c-qube/src/services/csv-adapter/parser/utils/csvreader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ const fs = require('fs').promises;

import * as csv from 'csv-parser';

export async function readCSV(filePath: string): Promise<string[][]> {
export async function readCSV(filePath: string, configPath?: string, quoteChar?: string): Promise<string[][]> {
return new Promise((resolve, reject) => {
const rows: string[][] = [];
const rows: string[][] = configPath ? [] : [[]];
if (configPath) {
const configquote = getquoteChar(configPath);
quoteChar = configquote || quoteChar || "'";
} else {
quoteChar = quoteChar || "'";
}

// TODO: Add checking here
fs1
.createReadStream(filePath)
.pipe(csv({ separator: ',', headers: false, quote: "'" }))
.pipe(csv({ separator: ',', headers: false, quote: quoteChar }))
.on('data', (data) => {
rows.push(Object.values(data));
})
Expand All @@ -30,3 +37,13 @@ export async function readCSVFile(filePath: string): Promise<string[]> {
.map((row: string) => row.trim())
.filter((row: string) => row !== '');
}

export function getquoteChar (configPath: string): string | undefined {
try {
const configContent = fs1.readFileSync(configPath, 'utf-8');
const config = JSON.parse(configContent);
return config.globals.quoteChar;
} catch (error) {
return undefined;
}
}
4 changes: 4 additions & 0 deletions impl/c-qube/test/fixtures/test-csvs/csvreader/quote.csv
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add more complex test cases checking for this quoteChar

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Name,Age,Country
John Doe,30,USA
Jane Smith,25,Canada
`Backtick User`,40,Australia