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:
hypercross 2026-04-19 14:06:11 +08:00
parent e76ae79b2d
commit 9dd4f60c2d
1 changed files with 12 additions and 9 deletions

View File

@ -538,12 +538,13 @@ export interface ReverseReferenceDeclaration {
*/
function parseReverseReferenceDeclaration(
line: string,
commentChar: string = "#",
): ReverseReferenceDeclaration | null {
const trimmed = line.trim();
// Must start with # (comment)
if (!trimmed.startsWith("#")) return null;
// Must start with the comment character
if (!trimmed.startsWith(commentChar)) return null;
const content = trimmed.slice(1).trim();
const content = trimmed.slice(commentChar.length).trim();
// Match pattern: fieldName := ~tableName(foreignKey)
const match = content.match(/^(\w+)\s*:=\s*~(\w+)\((\w+)\)(\?)?$/);
@ -793,7 +794,9 @@ export function parseCsv(
quote,
escape,
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,
skip_empty_lines: true,
relax_column_count: true,
@ -817,10 +820,10 @@ export function parseCsv(
const dataRows: string[][] = [];
for (let i = 2; i < records.length; 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();
if (firstCell.startsWith("#")) {
const decl = parseReverseReferenceDeclaration(firstCell);
if (comment && firstCell.startsWith(comment)) {
const decl = parseReverseReferenceDeclaration(firstCell, comment);
if (decl) {
reverseReferences.push(decl);
}
@ -834,8 +837,8 @@ export function parseCsv(
// (in case they appear as schema cells rather than separate rows)
for (let col = 0; col < schemas.length; col++) {
const cell = (schemas[col] ?? "").trim();
if (cell.startsWith("#")) {
const decl = parseReverseReferenceDeclaration(cell);
if (comment && cell.startsWith(comment)) {
const decl = parseReverseReferenceDeclaration(cell, comment);
if (decl) {
reverseReferences.push(decl);
}