From 465e8289762598d9bdec9a0f859d1123dfb8fca6 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 28 Nov 2026 02:33:59 +0100 Subject: [PATCH] Thing 2.0 cut 4b: provider state is registered under its schema id; load_store routes by entry type --- crates/thing-testkit/src/mem_state.rs | 25 ++++++++++++++++++++----- crates/thing-testkit/src/state.rs | 15 +++++++++------ 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/crates/thing-testkit/src/mem_state.rs b/crates/thing-testkit/src/mem_state.rs index 935861a..b50f8fb 100644 --- a/crates/thing-testkit/src/mem_state.rs +++ b/crates/thing-testkit/src/mem_state.rs @@ -101,6 +101,18 @@ impl Held { } } +/// The schema id of the contract kind whose CultCache entry type is `entry_type`. +fn contract_schema(entry_type: &str) -> Option<&'static str> { + [ + (SurfaceDocument::TYPE, SurfaceDocument::SCHEMA_NAME), + (CompositionDocument::TYPE, CompositionDocument::SCHEMA_NAME), + (CommandResult::TYPE, CommandResult::SCHEMA_NAME), + (AssetCatalog::TYPE, AssetCatalog::SCHEMA_NAME), + ] + .into_iter() + .find_map(|(entry, schema)| (entry == entry_type).then_some(schema)) +} + fn is_contract(schema: &SchemaId) -> bool { by_schema!(schema.as_str(), _T => true, else false) } @@ -221,12 +233,15 @@ impl MemState { let _ = std::fs::remove_dir_all(&work); envelopes.sort_by(|a, b| a.key.cmp(&b.key)); for envelope in envelopes { - let schema = SchemaId::new(envelope.schema_id.clone().unwrap_or_else(|| envelope.r#type.clone())); - let address = Address { provider: provider.clone(), schema, key: RecordKey::new(envelope.key) }; - if is_contract(&address.schema) { - self.put_raw(address, envelope.payload); + // A store names a record's schema by its entry type: a contract kind's type maps to + // its schema id, and any other type is the schema id of provider state. + let key = RecordKey::new(envelope.key); + if let Some(schema) = contract_schema(&envelope.r#type) { + self.put_raw(Address { provider: provider.clone(), schema: SchemaId::new(schema), key }, envelope.payload); } else { - let rows = resolve_schema_entry(&catalog, address.schema.as_str()).map(|entry| entry.members.clone()); + let schema = envelope.schema_id.unwrap_or(envelope.r#type); + let rows = resolve_schema_entry(&catalog, &schema).map(|entry| entry.members.clone()); + let address = Address { provider: provider.clone(), schema: SchemaId::new(schema), key }; self.put_state_raw(address, envelope.payload, &rows.unwrap_or_default()); } } diff --git a/crates/thing-testkit/src/state.rs b/crates/thing-testkit/src/state.rs index 3525c77..7b92911 100644 --- a/crates/thing-testkit/src/state.rs +++ b/crates/thing-testkit/src/state.rs @@ -1,6 +1,9 @@ //! The provider state the fixtures bind: test schemas whose Rust field names, PascalCased, are //! the member names the fixtures read, and the script that fills a `MemState` with each //! fixture's records. Fixture builders name these schemas by id; none of them is a contract kind. +//! +//! A CultCache store names a record's schema by its entry type, not by `SCHEMA_NAME`, so each type +//! here is registered under its schema id: the id a Bind names is the id the store holds. use cultcache_rs::DatabaseEntry; use serde::{Deserialize, Serialize}; @@ -11,7 +14,7 @@ use crate::fixture::Fixture; use crate::mem_state::MemState; #[derive(Clone, Debug, PartialEq, DatabaseEntry)] -#[cultcache(type = "gamecult.thing.test.swarm", schema = "gamecult.thing.test.swarm.v1")] +#[cultcache(type = "gamecult.thing.test.swarm.v1", schema = "gamecult.thing.test.swarm.v1")] pub struct Swarm { #[cultcache(key = 0)] pub id: String, @@ -31,19 +34,19 @@ pub struct SwarmWithoutRows { } impl DatabaseEntry for SwarmWithoutRows { - const TYPE: &'static str = "gamecult.thing.test.swarm"; + const TYPE: &'static str = "gamecult.thing.test.swarm.v1"; const SCHEMA_NAME: &'static str = "gamecult.thing.test.swarm.v1"; } #[derive(Clone, Debug, PartialEq, DatabaseEntry)] -#[cultcache(type = "gamecult.thing.test.target", schema = "gamecult.thing.test.target.v1")] +#[cultcache(type = "gamecult.thing.test.target.v1", schema = "gamecult.thing.test.target.v1")] pub struct Target { #[cultcache(key = 0)] pub label: String, } #[derive(Clone, Debug, PartialEq, DatabaseEntry)] -#[cultcache(type = "gamecult.thing.test.settings", schema = "gamecult.thing.test.settings.v1")] +#[cultcache(type = "gamecult.thing.test.settings.v1", schema = "gamecult.thing.test.settings.v1")] pub struct Settings { #[cultcache(key = 0)] pub enabled: bool, @@ -54,7 +57,7 @@ pub struct Settings { } #[derive(Clone, Debug, PartialEq, DatabaseEntry)] -#[cultcache(type = "gamecult.thing.test.item", schema = "gamecult.thing.test.item.v1")] +#[cultcache(type = "gamecult.thing.test.item.v1", schema = "gamecult.thing.test.item.v1")] pub struct Item { #[cultcache(key = 0)] pub id: String, @@ -63,7 +66,7 @@ pub struct Item { } #[derive(Clone, Debug, PartialEq, DatabaseEntry)] -#[cultcache(type = "gamecult.thing.test.series", schema = "gamecult.thing.test.series.v1")] +#[cultcache(type = "gamecult.thing.test.series.v1", schema = "gamecult.thing.test.series.v1")] pub struct Series { #[cultcache(key = 0)] pub values: Vec,