feat(csv-loader): allow configurable comment character
Allow `parseCsv` to accept a custom comment character for parsing reverse reference declarations, instead of being hardcoded to `#`.
This commit is contained in:
parent
e76ae79b2d
commit
9dd4f60c2d
|
|
@ -538,12 +538,13 @@ export interface ReverseReferenceDeclaration {
|
||||||
*/
|
*/
|
||||||
function parseReverseReferenceDeclaration(
|
function parseReverseReferenceDeclaration(
|
||||||
line: string,
|
line: string,
|
||||||
|
commentChar: string = "#",
|
||||||
): ReverseReferenceDeclaration | null {
|
): ReverseReferenceDeclaration | null {
|
||||||
const trimmed = line.trim();
|
const trimmed = line.trim();
|
||||||
// Must start with # (comment)
|
// Must start with the comment character
|
||||||
if (!trimmed.startsWith("#")) return null;
|
if (!trimmed.startsWith(commentChar)) return null;
|
||||||
|
|
||||||
const content = trimmed.slice(1).trim();
|
const content = trimmed.slice(commentChar.length).trim();
|
||||||
|
|
||||||
// Match pattern: fieldName := ~tableName(foreignKey)
|
// Match pattern: fieldName := ~tableName(foreignKey)
|
||||||
const match = content.match(/^(\w+)\s*:=\s*~(\w+)\((\w+)\)(\?)?$/);
|
const match = content.match(/^(\w+)\s*:=\s*~(\w+)\((\w+)\)(\?)?$/);
|
||||||
|
|
@ -793,7 +794,9 @@ export function parseCsv(
|
||||||
quote,
|
quote,
|
||||||
escape,
|
escape,
|
||||||
bom,
|
bom,
|
||||||
comment: undefined, // Don't let csv-parse skip comments; we need to parse them for reverse references
|
// Don't let csv-parse skip comments; we need to parse them for reverse references.
|
||||||
|
// Comment lines are filtered out manually below using the configured comment character.
|
||||||
|
comment: undefined,
|
||||||
trim,
|
trim,
|
||||||
skip_empty_lines: true,
|
skip_empty_lines: true,
|
||||||
relax_column_count: true,
|
relax_column_count: true,
|
||||||
|
|
@ -817,10 +820,10 @@ export function parseCsv(
|
||||||
const dataRows: string[][] = [];
|
const dataRows: string[][] = [];
|
||||||
for (let i = 2; i < records.length; i++) {
|
for (let i = 2; i < records.length; i++) {
|
||||||
const row = records[i];
|
const row = records[i];
|
||||||
// Check if this is a single-column row starting with # (comment with reverse ref declaration)
|
// Check if this is a comment line (starting with the configured comment character)
|
||||||
const firstCell = (row[0] ?? "").trim();
|
const firstCell = (row[0] ?? "").trim();
|
||||||
if (firstCell.startsWith("#")) {
|
if (comment && firstCell.startsWith(comment)) {
|
||||||
const decl = parseReverseReferenceDeclaration(firstCell);
|
const decl = parseReverseReferenceDeclaration(firstCell, comment);
|
||||||
if (decl) {
|
if (decl) {
|
||||||
reverseReferences.push(decl);
|
reverseReferences.push(decl);
|
||||||
}
|
}
|
||||||
|
|
@ -834,8 +837,8 @@ export function parseCsv(
|
||||||
// (in case they appear as schema cells rather than separate rows)
|
// (in case they appear as schema cells rather than separate rows)
|
||||||
for (let col = 0; col < schemas.length; col++) {
|
for (let col = 0; col < schemas.length; col++) {
|
||||||
const cell = (schemas[col] ?? "").trim();
|
const cell = (schemas[col] ?? "").trim();
|
||||||
if (cell.startsWith("#")) {
|
if (comment && cell.startsWith(comment)) {
|
||||||
const decl = parseReverseReferenceDeclaration(cell);
|
const decl = parseReverseReferenceDeclaration(cell, comment);
|
||||||
if (decl) {
|
if (decl) {
|
||||||
reverseReferences.push(decl);
|
reverseReferences.push(decl);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue