Give a list one struct type so every item keeps its shape (#254)
A list of structs used to let every item carry its own fields. Adding an item meant typing the same fields again, and nothing kept the items in step with the struct asset from #253. A list can now name a struct type for its items, and the asset keeps them in shape. - A list variable and a list field get the row Items with Mixed, every struct asset and Make struct type. Make struct type collects every field any item has, so no item loses a field. - A typed item carries its type under the key `$struct` (ITEM_TYPE_KEY). listItems does not count it as a field. - A list that holds anything but structs refuses a type, also when the offending item sits in an override. The console names the list and the count, and the list stays as it was. - In a typed list `+ Item` adds an item with the defaults, without the kind menu. Field names are locked, values stay editable. - The rows of a typed item take their kind from the struct asset (declaredKind in InlineList.ts). An asset field shows the asset picker instead of a text field, as reported in the issue. - A struct item in a mixed list gets its own Type row. Switching a list back to Mixed keeps the type on each item. - syncStructTypes brings marked items into shape and carries renames into them, also inside overrides and nested lists. One undo step. - The delete dialog of a struct asset also counts typed lists. - The game never sees the key. World and sharedState strip it with withoutItemTypes and bagWithoutItemTypes, so a struct stays a plain object at runtime. - Add item and Set item get the setting Type. With a type they only accept a struct with exactly its field names (fitsType in SetItem.ts). Anything else is left out and logged. - The item indent from #252 is back to its old width, the narrow one squeezed the value fields. Not done: writing a variable does not check the type, and the List node offers no type. Found on the way: a test helper in list-of-structs.test.ts called nodes without params. It now passes the same arguments as the interpreter. Tests: - ./check.sh: 183 files, 2679 tests, CHECK EXIT 0 - e2e GM_STEPS=1,21,22,32,54,55: ALL PASSED, 150 checks - e2e GM_STEPS=1-6,21,22,32,54,55 before the follow-up: 404 checks - New: tests/core/typed-lists.test.ts, e2e module 55 with 13 checks - Counter-checks bite in unit and browser, originals restored by cmp Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
61bbb96a96
commit
b0d0868f55
25 files changed
+1446
-86
No files matched your search
@@ -163,9 +163,10 @@ export type PropertyDef =
|
||||
| FlatDef
|
||||
/**
|
||||
* A list declares nothing about its items: each one carries its own kind (see `listItems.ts`),
|
||||
* and the graph types a list as a list, so one `list` port fits them all.
|
||||
* and the graph types a list as a list, so one `list` port fits them all. An `itemType` makes
|
||||
* every item a struct of that Struct asset (#254), each marked with it (`ITEM_TYPE_KEY`).
|
||||
*/
|
||||
| { key: string; label: string; kind: 'list'; default: ListValue }
|
||||
| { key: string; label: string; kind: 'list'; default: ListValue; itemType?: string }
|
||||
/**
|
||||
* `fields` is the declaration; the value carries exactly those keys, which is what lets a struct
|
||||
* keep its shape while single fields are written (`mergeStruct`). With a `structType` the fields
|
||||
@@ -196,10 +197,14 @@ export interface PrimitiveFieldDef {
|
||||
kind: FieldKind;
|
||||
}
|
||||
|
||||
/** A list held by a struct field. Its items declare nothing either, and may nest like any list. */
|
||||
/**
|
||||
* A list held by a struct field. Its items declare nothing either, and may nest like any list –
|
||||
* unless `itemType` names the Struct asset every item is (#254), as on a list variable.
|
||||
*/
|
||||
export interface ListFieldDef {
|
||||
key: string;
|
||||
kind: 'list';
|
||||
itemType?: string;
|
||||
}
|
||||
|
||||
/** A struct held by a struct field declares its fields like a struct variable does (#239). */
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
* through the same coercion again, so nothing deeper is dropped.
|
||||
*/
|
||||
import {
|
||||
ITEM_TYPE_KEY,
|
||||
isList,
|
||||
isStruct,
|
||||
type ListValue,
|
||||
@@ -65,5 +66,7 @@ export function structItemOf(value: PropertyValue | undefined): StructValue {
|
||||
/** The shape a struct item has right now: its keys in order, each with the kind of its value. */
|
||||
export function structItemFields(item: PropertyValue | undefined): { key: string; kind: NestedKind }[] {
|
||||
if (!isStruct(item) || tagOf(item) !== undefined) return [];
|
||||
return Object.entries(item).map(([key, value]) => ({ key, kind: itemKindOf(value) }));
|
||||
return Object.entries(item)
|
||||
.filter(([key]) => key !== ITEM_TYPE_KEY)
|
||||
.map(([key, value]) => ({ key, kind: itemKindOf(value) }));
|
||||
}
|
||||
Reference in new issue
Block a user