Skip to content

Commit 50f85e9

Browse files
feat(spec): a formula field can return percent (#35565)
<!-- claude-projects-attribution: {"github_login":"os-bill"} --> _Requested by **Bill** · [project thread](https://claude.ai/code/project/chan_016iMXWSZDzqWRWvWzdNo8wg?thread=cmsg_016iMXWSZDzqWRWvWzdNo8wgEpYDjddvdg2ymktFMmNiYa)_ Fixes #35047 Clause-②: yes (widening) Metadata-protocol: added Skills-docs: follow-up #35607 — `skills/objectstack-data/rules/field-types.md` (line 130) lists formula return types without `percent`; skills prose is outside this card's fence ## 维护者速读 1. 用户能看到什么变化:比率类公式字段(如预算使用率、毛利率)可以声明为百分比,记录页和列表里显示为带百分号的数值,与存储的百分比字段一致,而不再是一串裸小数。 2. 为什么做:客户关系应用里的预算使用率、毛利率等比率公式此前只能显示为裸数字,与旁边存储的百分比字段格式不一致,平台没有其他办法补上。 3. 出错会怎样:没有声明百分比的公式不受影响;若显示有误,最坏是某个比率按百分比格式显示错位,回滚本次改动即可恢复。 4. 要你做什么:无。 ## What changes - `@objectstack/spec`: formula `returnType` admits `percent`, mirroring `currency` (#22727). The value is read as a `percent` field reads its stored value: a fraction (`0.25` shows `25%`) unless the formula declares `max` above 1, then percentage points (`percentScaleOf`). `scale` counts the decimal places of the displayed percentage (absent: 0). `percentDisplay` applies to a percent formula too. Declared, never inferred. Readers that switch on `returnType` (display-name eligibility per ADR-0079, filter operator and comparand typing, `stack.zod.ts`, `component.zod.ts`) class `percent` with `number`. The field and object forms offer `Percent`, and the field form's `percentDisplay` row opens for a percent formula. - `@objectstack/objectql`: `formulaRoundingScale` rounds a percent formula's fraction result to `scale + 2` places and a whole percentage to `scale`. - The same rounding step also serves an existing stored `percent` field computed from its `expression`. Before, its computed fraction was rounded to `scale` itself (0.417 at `scale: 1` was stored 0.4, reading 40.0%); it now keeps `scale + 2` places (0.417, 41.7%). This applies from the next computed write; rows written earlier are not rewritten. The changeset says so too. - `@object-ui/fields`: `FormulaField` renders a percent result with the percent formatter. The field and object forms offer `percent` as a return type. `@object-ui/app-shell`: editing the expression in the field inspector keeps a declared `percent` or `currency` unless the new expression proves text, a boolean or a date (an unproven field-over-field quotient no longer clears it). - Docs: `content/docs/data-modeling/formulas.mdx` and `field-types.mdx` list `percent` with its convention; the reference pages and the en metadata-forms bundle are regenerated. ## Tests - New: spec `formula-percent-result.test.ts`; objectql `engine-formula-scale.test.ts` (percent arm); ui `formulaFaces.returnType-11748.test.tsx` and the inspector pins (a `number` verdict, and a field-over-field quotient that proves no type, for `percent` and `currency`; the new arm fails 3 of 11 with the fix taken out). - Moved with the enum: the filter operator / comparand declared-type tests, `formula-currency-result.test.ts`, service-analytics `formula-return-type-measure.test.ts`, and the lint shipped-predicate census (63 → 65, the percent formula's `percentDisplay` gate holds two literal comparisons). ### 席位核对 - Head `bc810f4ad9`: the S1 fix round from the contract review (7017037505), `fe974fb467` (inspector keeps a declared `percent` / `currency` under a `number` or unproven verdict, jsdoc and describe, changeset wording) and `bc810f4ad9` (reference docs regenerated), over `03a17ea72f`, which follows a base-only merge of `main` via `os-regen-merge.sh` at `986de618a`. K = 13. +437/−56 over 28 files, not governed. - os-dev-report 7016442707: spec 857 files / 27395 tests, objectql 616 / 13911, touched ui tests green, six typechecks exit 0; dispatch-gates 128 derived, 128 run, 0 NOT-MEASURED. overlap-check clean against `origin/main` and PRs #35510, #34817. - S1 round, run locally: the inspector pin file 11 passed; with the fix taken out, the 3 new `percent` / `currency` arms fail. `check:generated --fix` regenerated the reference docs; `check-i18n-bundles` in sync after the build closure. - CI on `bc810f4ad9`: 61 success, 9 skipped, 0 failed. - Two-minute check: `pnpm --filter @objectstack/spec exec vitest run src/data/formula-percent-result.test.ts`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01Utp6CajiB6BSQKWowy6YLB <!-- signed: repo:objectstack#5 · os-bill · session_01Utp6CajiB6BSQKWowy6YLB --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent e01b8be commit 50f85e9

28 files changed

Lines changed: 437 additions & 56 deletions
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
'@objectstack/spec': minor
3+
'@objectstack/objectql': patch
4+
'@object-ui/fields': patch
5+
'@object-ui/app-shell': patch
6+
---
7+
8+
feat(spec): a formula field can return `percent`
9+
10+
Clause-②: yes (widening)
11+
Metadata-protocol: added
12+
13+
A ratio formula (budget used, gross margin) can now declare `returnType: 'percent'` and read and display as a `percent` field does, instead of a bare decimal beside the stored percentages.
14+
15+
- **The value.** A `percent` formula result is read exactly as a `percent` field reads its stored value: a fraction (`0.25` shows `25%`, so the expression divides and does not multiply by 100), unless the formula declares a `max` above 1, then percentage points (`percentScaleOf`).
16+
- **Its places.** `scale` counts the decimal places of the displayed percentage, as on a `percent` field (absent: 0). The engine rounds a fraction result to `scale + 2` places and a whole percentage to `scale`, so `scale: 1` keeps 0.417 (41.7%) instead of rounding it to 0.4.
17+
- **An existing `percent` field computed from its `expression` rounds the same way.** The engine shares one rounding step for computed values, so a stored `percent` field that computes from an `expression` now keeps `scale + 2` places for a fraction (and `scale` for a whole percentage). Before, its computed fraction was rounded to `scale` itself: at `scale: 1`, 0.417 was stored as 0.4 and read 40.0%; it is now stored 0.417 and reads 41.7%. The change applies to the next computed write; rows written earlier are not rewritten.
18+
- **Its face.** `percentDisplay` applies to a `percent` formula too (bar or plain). The formula renderer formats the result with the percent formatter, and the field and object forms offer `percent` as a return type. Editing the expression in the Studio field inspector keeps a declared `percent` (or `currency`) unless the new expression proves text, a boolean or a date.
19+
- **Declared, never inferred.** No CEL type proves a quotient is a percentage, so authoring keeps a declared `percent` where it would otherwise stamp `number`. Readers that switch on `returnType` (display-name eligibility, filter operator and comparand typing) treat `percent` as a number.
20+
21+
The enum only gains a member: every formula that parsed before parses the same, and nothing stored needs migrating.

‎content/docs/data-modeling/field-types.mdx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ Calculated field using a CEL expression (see [Expressions](/docs/data-modeling/f
687687
| Property | Type | Default | Description |
688688
|:---|:---|:---|:---|
689689
| `expression` | `string \| Expression` | **required** | CEL calculation expression |
690-
| `returnType` | `'number' \| 'text' \| 'boolean' \| 'date' \| 'currency'` | — | Declared value type of the computed result. `currency`: an amount of money, in the currency the formula's own `currencyConfig` declares (none: dynamic; over record-mode amounts, `record` with their `currencyField`) |
690+
| `returnType` | `'number' \| 'text' \| 'boolean' \| 'date' \| 'currency' \| 'percent'` | — | Declared value type of the computed result. `currency`: an amount of money, in the currency the formula's own `currencyConfig` declares (none: dynamic; over record-mode amounts, `record` with their `currencyField`). `percent`: a ratio, read as a `percent` field reads its stored value — a fraction (`0.25` shows `25%`, so the expression divides and does not multiply by 100) unless the formula declares a `max` above 1, then percentage points; its `scale` is the decimal places of the displayed percentage (absent: 0) and its `percentDisplay` picks the bar or the plain face. Declared, never inferred |
691691

692692
```typescript
693693
{ name: 'total', label: 'Total', type: 'formula', expression: 'record.quantity * record.unit_price', returnType: 'number' }
@@ -697,7 +697,8 @@ Calculated field using a CEL expression (see [Expressions](/docs/data-modeling/f
697697
evaluates the expression. The form and the grid cell draw it by `returnType`: `number` is
698698
formatted as a number field formats it (to the field's `scale`), `boolean` as Yes / No in the
699699
UI language (a non-boolean reads empty), `date` as the date field's absolute face (not the
700-
grid's relative one), and `text` in a monospace font. With no `returnType`, a number is drawn
700+
grid's relative one), `percent` as a percent field draws it (its `scale`, `max` and
701+
`percentDisplay`), and `text` in a monospace font. With no `returnType`, a number is drawn
701702
as a number and anything else as text; nothing is inferred from the expression.
702703

703704
#### `expression` on a stored field

‎content/docs/data-modeling/formulas.mdx‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ evaluates (`objectql` computes a formula virtual field from its `expression`
132132
only). Do **not** pass `type: 'currency' | 'number' | …` to `Field.formula` — it
133133
is rejected by the typed `FieldInput` and, untyped, would override `type:'formula'`
134134
so the field silently never computes. To declare what the formula returns, set
135-
**`returnType`** (`'number' | 'text' | 'boolean' | 'date' | 'currency'`). You declare it
135+
**`returnType`** (`'number' | 'text' | 'boolean' | 'date' | 'currency' | 'percent'`). You declare it
136136
yourself — nothing back-fills it — but the agent-callable `validate_expression`
137137
tool reports the type it infers from the expression (`inferredType`) so an AI
138138
author can stamp the matching value. A `'currency'` result is an amount of money
@@ -141,7 +141,13 @@ and carries its currency the way a currency field does: in the formula's own
141141
record-mode amounts it is `{ currencyMode: 'record', currencyField }`, naming the
142142
same code field). Copy it from
143143
the currency field the formula computes from — `inferFormulaReturn` in
144-
`@objectstack/formula` answers both halves for a formula it can prove is money. Consumers read the declared `returnType`
144+
`@objectstack/formula` answers both halves for a formula it can prove is money. A `'percent'`
145+
result is a ratio, read exactly as a `percent` field reads its stored value: a
146+
fraction (`0.25` shows `25%`, so the expression divides and does not multiply by
147+
100), unless the formula declares a `max` above 1, then percentage points. Its
148+
`scale` counts the decimal places of the displayed percentage (absent: 0), and
149+
`percentDisplay` picks the bar or the plain face. No CEL type proves a quotient
150+
is a percentage, so `'percent'` is only ever declared, never inferred. Consumers read the declared `returnType`
145151
instead of re-parsing the expression (record-title eligibility, for one: a
146152
formula is title-eligible only when its `returnType` is `'text'`, which is what
147153
lets it be *derived* as the record title — an explicit `nameField` pointer is

0 commit comments

Comments
 (0)