merge: integrate review-13-set-literal-types
This commit is contained in:
commit
fae36f8c96
4 files changed
+189
-176
No files matched your search
@@ -88,3 +88,5 @@ for await (const rawRecord of iterateAllEntityRecords(runtime.clients.ekbClient,
|
||||
|
||||
|
||||
`--set` 按引号、转义和括号边界拆分赋值项,支持 `名称="ACME, Inc.", 编码=ACME`。缺少等号、空字段或未闭合引号会在发起更新前明确报错,不再静默截断或忽略。
|
||||
|
||||
`--set` 中带单引号或双引号的值明确表示字符串,并解码字符串转义;例如 `编码="001"` 保留前导零,`名称="true"` 保留文本。未加引号的数值、`true/false/null` 继续按对应类型转换。
|
||||
@@ -1281,16 +1281,17 @@ export function parseSetClause(
|
||||
if (!rawKey) throw new ValidationError("赋值字段不能为空");
|
||||
let rawVal = assign.slice(eqIdx + 1).trim();
|
||||
|
||||
// 去除外层引号
|
||||
if ((rawVal.startsWith('"') && rawVal.endsWith('"')) || (rawVal.startsWith("'") && rawVal.endsWith("'"))) {
|
||||
rawVal = rawVal.slice(1, -1);
|
||||
}
|
||||
// 去除外层引号,同时保留显式字符串的类型与转义语义。
|
||||
const quoted = (rawVal.startsWith('"') && rawVal.endsWith('"')) || (rawVal.startsWith("'") && rawVal.endsWith("'"));
|
||||
const literal = quoted ? predicateLiteral(rawVal) : undefined;
|
||||
|
||||
const field = resolveEntityField(entity, rawKey);
|
||||
const targetKey = field ? field.name : rawKey;
|
||||
|
||||
// 类型智能转换
|
||||
if (rawVal === "true") {
|
||||
if (quoted) {
|
||||
result[targetKey] = literal;
|
||||
} else if (rawVal === "true") {
|
||||
result[targetKey] = true;
|
||||
} else if (rawVal === "false") {
|
||||
result[targetKey] = false;
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
import { expect, test } from "bun:test";
|
||||
import { parseSetClause } from "./services.js";
|
||||
const entity = { id: "entity", fields: [{ name: "code", label: "编码", type: "text" }, { name: "name", label: "名称", type: "text" }] } as any;
|
||||
test("quoted numbers, booleans and null remain text while bare literals retain conversion", () => {
|
||||
expect(parseSetClause('编码="001", 名称="true"', entity)).toEqual({ code: "001", name: "true" });
|
||||
expect(parseSetClause("编码='false', 名称='null'", entity)).toEqual({ code: "false", name: "null" });
|
||||
expect(parseSetClause("code=42, name=false", entity)).toEqual({ code: 42, name: false });
|
||||
expect(parseSetClause("code=null", entity)).toEqual({ code: null });
|
||||
expect(parseSetClause(String.raw`code="A\"B"`, entity)).toEqual({ code: 'A"B' });
|
||||
});
|
||||
Reference in new issue
Block a user