merge: integrate review-12-set-quoted-separators
This commit is contained in:
commit
c705cffaf4
4 files changed
+197
-176
No files matched your search
@@ -86,3 +86,5 @@ for await (const rawRecord of iterateAllEntityRecords(runtime.clients.ekbClient,
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
`--set` 按引号、转义和括号边界拆分赋值项,支持 `名称="ACME, Inc.", 编码=ACME`。缺少等号、空字段或未闭合引号会在发起更新前明确报错,不再静默截断或忽略。
|
||||
@@ -1270,12 +1270,15 @@ export function parseSetClause(
|
||||
entity: DatalinkEntity
|
||||
): Record<string, unknown> {
|
||||
const result: Record<string, unknown> = {};
|
||||
const assignments = setClause.split(",").map((s) => s.trim()).filter(Boolean);
|
||||
let assignments: string[];
|
||||
try { assignments = splitPredicateParts(setClause, ","); }
|
||||
catch { throw new ValidationError("赋值表达式引号或括号不匹配,且赋值项不能为空"); }
|
||||
|
||||
for (const assign of assignments) {
|
||||
const eqIdx = assign.indexOf("=");
|
||||
if (eqIdx === -1) continue;
|
||||
if (eqIdx === -1) throw new ValidationError(`赋值项缺少等号: ${assign}`);
|
||||
const rawKey = assign.slice(0, eqIdx).trim();
|
||||
if (!rawKey) throw new ValidationError("赋值字段不能为空");
|
||||
let rawVal = assign.slice(eqIdx + 1).trim();
|
||||
|
||||
// 去除外层引号
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import { expect, test } from "bun:test";
|
||||
import { parseSetClause } from "./services.js";
|
||||
const entity = { id: "entity", fields: [{ name: "name", label: "名称", type: "text" }, { name: "code", label: "编码", type: "text" }] } as any;
|
||||
test("commas inside single and double quoted strings do not split assignments", () => {
|
||||
expect(parseSetClause('名称="ACME, Inc.", 编码=ACME', entity)).toEqual({ name: "ACME, Inc.", code: "ACME" });
|
||||
expect(parseSetClause("名称='A,B', 编码=C", entity)).toEqual({ name: "A,B", code: "C" });
|
||||
});
|
||||
test("escaped quotes preserve the quoted comma boundary", () => {
|
||||
const parsed = parseSetClause(String.raw`name="ACME\", Inc.", code=C`, entity);
|
||||
expect(parsed.code).toBe("C");
|
||||
expect(parsed.name).toEndWith(", Inc.");
|
||||
expect(Object.keys(parsed)).toHaveLength(2);
|
||||
});
|
||||
for (const clause of ['名称="unfinished', "名称=A,broken", "名称=A,", "=A"]) {
|
||||
test(`rejects incomplete assignments: ${clause}`, () => { expect(() => parseSetClause(clause, entity)).toThrow(); });
|
||||
}
|
||||
Reference in new issue
Block a user