refactor: use parseValue for reference ID extraction
Replace the custom `ReferenceValueParser` class with the core `parseValue` function in `reference-resolver.ts`. This simplifies the logic for parsing reference IDs and ensures consistency with the rest of the schema parsing engine.
This commit is contained in:
parent
f66f60aa0e
commit
d0ed1dc92d
|
|
@ -1,16 +1,12 @@
|
|||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
import {
|
||||
parseValue,
|
||||
} from "../index.js";
|
||||
import { parseValue } from "../index.js";
|
||||
import type {
|
||||
Schema,
|
||||
ReferenceSchema,
|
||||
ReverseReferenceSchema,
|
||||
} from "../types.js";
|
||||
import type {
|
||||
ReferenceFieldInfo,
|
||||
} from "./types.js";
|
||||
import type { ReferenceFieldInfo } from "./types.js";
|
||||
import { parseCsv } from "./loader.js";
|
||||
|
||||
/** Cache for loaded referenced tables */
|
||||
|
|
@ -110,12 +106,7 @@ export function parseReferenceIds(
|
|||
if (schema.isOptional && trimmed === "") {
|
||||
return null;
|
||||
}
|
||||
const valueParser = new ReferenceValueParser(trimmed);
|
||||
const ids = valueParser.parseIds(schema.isArray);
|
||||
if (schema.isArray) {
|
||||
return ids;
|
||||
}
|
||||
return ids[0];
|
||||
return parseValue(schema, trimmed);
|
||||
}
|
||||
|
||||
export function parseValueWithReferenceIds(
|
||||
|
|
@ -164,7 +155,10 @@ export function parseValueWithReferenceIds(
|
|||
}
|
||||
}
|
||||
|
||||
export function extractNestedReferenceIds(value: unknown, schema: Schema): unknown {
|
||||
export function extractNestedReferenceIds(
|
||||
value: unknown,
|
||||
schema: Schema,
|
||||
): unknown {
|
||||
switch (schema.type) {
|
||||
case "reference":
|
||||
if (value === null || value === undefined) return value;
|
||||
|
|
@ -477,101 +471,12 @@ export function parseReferenceValue(
|
|||
currentFilePath,
|
||||
);
|
||||
|
||||
const valueParser = new ReferenceValueParser(trimmed);
|
||||
const ids = valueParser.parseIds(schema.isArray);
|
||||
const ids = parseValue(schema, trimmed) as string | string[] | null;
|
||||
if (ids === null) return null;
|
||||
|
||||
if (schema.isArray) {
|
||||
if (schema.isArray && Array.isArray(ids)) {
|
||||
return ids.map((id) => resolveReferenceId(id, lookup, schema.tableName));
|
||||
}
|
||||
|
||||
return resolveReferenceId(ids[0], lookup, schema.tableName);
|
||||
}
|
||||
|
||||
class ReferenceValueParser {
|
||||
private input: string;
|
||||
private pos: number = 0;
|
||||
|
||||
constructor(input: string) {
|
||||
this.input = input;
|
||||
}
|
||||
|
||||
private peek(): string {
|
||||
return this.input[this.pos] || "";
|
||||
}
|
||||
|
||||
private consume(): string {
|
||||
return this.input[this.pos++] || "";
|
||||
}
|
||||
|
||||
private skipWhitespace(): void {
|
||||
while (this.pos < this.input.length && /\s/.test(this.input[this.pos])) {
|
||||
this.pos++;
|
||||
}
|
||||
}
|
||||
|
||||
private consumeStr(str: string): boolean {
|
||||
if (this.input.slice(this.pos, this.pos + str.length) === str) {
|
||||
this.pos += str.length;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
parseIds(isArray: boolean): string[] {
|
||||
this.skipWhitespace();
|
||||
|
||||
if (isArray) {
|
||||
// Parse array format: [id1; id2; id3]
|
||||
if (this.peek() === "[") {
|
||||
this.consume();
|
||||
}
|
||||
|
||||
this.skipWhitespace();
|
||||
|
||||
if (this.peek() === "]") {
|
||||
this.consume();
|
||||
return [];
|
||||
}
|
||||
|
||||
const ids: string[] = [];
|
||||
while (true) {
|
||||
this.skipWhitespace();
|
||||
let id = "";
|
||||
while (
|
||||
this.pos < this.input.length &&
|
||||
this.peek() !== ";" &&
|
||||
this.peek() !== "]"
|
||||
) {
|
||||
id += this.consume();
|
||||
}
|
||||
const trimmedId = id.trim();
|
||||
if (trimmedId) {
|
||||
ids.push(trimmedId);
|
||||
}
|
||||
this.skipWhitespace();
|
||||
|
||||
if (!this.consumeStr(";")) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
this.skipWhitespace();
|
||||
if (this.peek() === "]") {
|
||||
this.consume();
|
||||
}
|
||||
|
||||
return ids;
|
||||
} else {
|
||||
// Parse single ID
|
||||
let id = "";
|
||||
while (this.pos < this.input.length) {
|
||||
const char = this.peek();
|
||||
if (char === ";" || char === "]" || char === ",") {
|
||||
break;
|
||||
}
|
||||
id += this.consume();
|
||||
}
|
||||
return [id.trim()];
|
||||
}
|
||||
}
|
||||
return resolveReferenceId(ids as string, lookup, schema.tableName);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue