Palantir Foundry:TypeScript@Function能否创建临时对象不存本体?
问题
在Palantir Foundry中,能否通过TypeScript创建临时的Ontology对象记录,无需存入Ontology即可返回给Workshop展示?
我做了三次尝试,所有操作在仓库调试器里表现符合预期,但函数始终返回不符合预期的结果:
- 尝试1:新建临时对象,调试器能看到正确属性,但函数返回null
- 尝试2:查询Ontology对象后修改属性,调试器显示修改后的结果,但函数返回原始值
- 尝试3:克隆Ontology对象后修改,调试器显示正确,但返回结果未保留修改
我猜测这和Ontology编辑的读取/应用机制有关,也联想到@OntologyEditFunction必须返回void的规则,但不清楚内部逻辑,想知道失败原因和可行方案。
我的测试代码如下:
@Function() public getFilteredRuns(): ScheduleRuns[] { //Object Type: ScheduleRuns //Attempt 1: Create a new temp object. Works as expected in debugger but returns a null result let testNew : ScheduleRuns = Objects.create().scheduleRuns("testId") testNew.name = "All Schedules" testNew.startTime = Timestamp.now() testNew.buildDuration = "5h 43m 34s" let r1 : ScheduleRuns[] = []; r1.push(testNew) console.log(r1) //Displays new object with updated properties in the debugger return r1 //Actually returns null //Attempt 2: Filter for one object and update it's properties. Displays correct updates in console but doesn't return them from function let test = Objects.search().scheduleRuns().filter(s => s.buildRid.exactMatch("0")).all()[0]; test.name = "All Schedules" test.startTime = Timestamp.now() test.buildDuration = "5h 43m 34s" test.avgDuration = "233333333333333333s" let r1 : ScheduleRuns[] = []; r1.push(test) console.log(r1) //Displays correct updates in console return r1 //Returns object with original values, ignores property updates above //Attempt 3: Clone? Same, appears in console correctly but doesn't return updated object let test = Objects.search().scheduleRuns().filter(s => s.buildRid.exactMatch("0")) let rMutable : ScheduleRuns = test.all()[0]; let cloned = Object.assign({}, rMutable); cloned.displayName = "Why" cloned.name = "All Schedules" cloned.startTime = Timestamp.now() cloned.buildDuration = "5h 43m 34s" console.log(cloned) return cloned }
原因分析
- 尝试1失败原因:
Objects.create().scheduleRuns()创建的是待提交的Ontology编辑对象,这类对象仅在@OntologyEditFunction上下文内有效,用于提交修改到Ontology。纯@Function中返回这类未持久化的对象,Foundry框架会判定为无效,最终返回null。 - 尝试2失败原因:从Ontology查询得到的对象是只读代理对象,本地调试器中看到的属性修改只是内存临时值,Foundry函数执行框架不会追踪这些修改,返回时会直接使用原始的Ontology数据。
- 尝试3失败原因:Ontology对象是Foundry封装的代理实例,不是普通JavaScript对象,
Object.assign无法正确克隆其内部数据,克隆后的对象依然无法被框架正确序列化返回。
可行方案
不要直接返回Ontology对象类型,而是定义普通TypeScript接口来匹配你需要展示的字段,将临时数据映射到这个接口的实例中返回。Workshop可以直接消费这种普通数据结构,不需要依赖Ontology对象。
示例代码:
// 定义和ScheduleRuns字段匹配的普通TS接口 interface ScheduleRunView { id: string; name: string; startTime: Timestamp; buildDuration: string; avgDuration?: string; } @Function() public getFilteredRuns(): ScheduleRunView[] { // 场景1:创建临时数据 const tempRun: ScheduleRunView = { id: "testId", name: "All Schedules", startTime: Timestamp.now(), buildDuration: "5h 43m 34s" }; // 场景2:修改查询到的Ontology对象数据后映射 const ontologyRun = Objects.search().scheduleRuns() .filter(s => s.buildRid.exactMatch("0")) .all()[0]; const modifiedRun: ScheduleRunView = { id: ontologyRun.id, name: "All Schedules", startTime: Timestamp.now(), buildDuration: "5h 43m 34s", avgDuration: "233333333333333333s" }; return [tempRun, modifiedRun]; }
内容的提问来源于stack exchange,提问作者tessa
相关产品推荐
相关产品推荐

