kernable-sdk 0.4.1

Write event-sourced business logic in Rust; the kernable kernel does the rest.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//! Events, commands and queries as types: defined once, typed, used by reference.
//!
//! The attributes [`event`](crate::event), [`command`](crate::command) and
//! [`query`](crate::query) turn a braced struct into a message of a domain:
//!
//! ```
//! use kernable::{NewEvent, domain, event, command};
//!
//! #[event("shop")]
//! pub struct OrderPlaced { pub customer_id: String, pub note: Option<String> }
//!
//! #[command("shop", aggregate = "order")]
//! pub struct PlaceOrder { pub customer_id: String }
//!
//! #[derive(Clone, Default, serde::Serialize, serde::Deserialize)]
//! struct Order { placed: bool }
//!
//! let shop_domain = domain("shop");
//! let order_aggregate = shop_domain.aggregate("order", Order::default());
//! order_aggregate.apply(OrderPlaced, |s, _d, _evt, _ctx| s.placed = true);
//! order_aggregate.command(PlaceOrder, |_s, d, _cmd, _ctx| {
//!     Ok(vec![OrderPlaced { customer_id: d.customer_id, note: None }.into()])
//! });
//! ```
//!
//! Fields are snake_case in Rust and camelCase in the log (`customer_id` is `customerId`), so a
//! Rust, a Python and a TypeScript process write and read the same events. An event writes an
//! `Option` field that is `None` as `null` (it is part of the record); a command or query leaves
//! it out; a missing field reads as `None`. A field with its own `#[serde(...)]` attribute is left
//! as it says (an event's optional field: `#[serde(default, skip_serializing_if =
//! "Option::is_none")]`). The struct's name is its type (`type = "..."` overrides it), and
//! also a constant of type [`Of`] that stands for the type where the SDK takes one: in
//! `apply`, `command`, `requires`, a listener's `on` and `query`; with [`crate::Submit::command`]
//! and [`crate::Query::of`] the value itself goes.

use std::marker::PhantomData;

use serde::{Deserialize, Deserializer, Serialize, de::DeserializeOwned};

use crate::Domain;

/// A message of a domain: implemented by the attributes, not by hand.
pub trait Message: Serialize + DeserializeOwned + Send + Sync + 'static {
    /// The domain it belongs to.
    const DOMAIN: &'static str;
    /// Its type within the domain.
    const TYPE: &'static str;
    /// Declares its schema on `domain` (an event's version, upcasts and vault fields; a
    /// command's vault fields; a query's target). The SDK calls it once per domain.
    fn declare(domain: &Domain);
}

/// An event: `#[event("domain")]`.
pub trait EventType: Message {}

/// A command of an aggregate: `#[command("domain", aggregate = "name")]`.
pub trait CommandType: Message {
    /// The aggregate type that runs it.
    const AGGREGATE: &'static str;
}

/// A query and its answer: `#[query("domain", answer = Row)]`. A query about one aggregate
/// names it, and the field with its ID: `#[query("shop", answer = OrderRow, target(aggregate =
/// "order", field = order_id))]`. The SDK sends that aggregate with the query, and a middleware
/// decides on it as on a command's aggregate ([`crate::Check::target`]): "only the order's
/// owner may read it". The aggregate type is of the query's domain, or qualified:
/// `shop.order`.
pub trait QueryType: Message {
    /// What the read model answers.
    type Answer: Serialize + DeserializeOwned;
}

/// Stands for the message type `M` where the SDK takes a type: the attributes declare one
/// named like the struct, so `apply(TenantCreated, ...)` reads like the type itself.
pub struct Of<M>(PhantomData<fn() -> M>);

impl<M> Of<M> {
    #[doc(hidden)]
    pub const fn new() -> Self {
        Of(PhantomData)
    }
}

impl<M> Clone for Of<M> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<M> Copy for Of<M> {}

/// What a type parameter of `apply`, `command`, `requires`, `on` and `query` resolves to.
#[doc(hidden)]
pub struct Key {
    /// The type's own domain, for a message type; a string says nothing.
    pub(crate) domain: Option<&'static str>,
    pub(crate) name: String,
    pub(crate) declare: Option<fn(&Domain)>,
}

/// A type as `apply`, `command`, `requires`, `on` and `query` take it: a name (`"ItemAdded"`,
/// `"orders.OrderPlaced"`) with the data as any `D`, or a message type's constant
/// (`OrderPlaced`), whose data is then that type.
pub trait TypeKey<D> {
    #[doc(hidden)]
    fn key(self) -> Key;
}

impl<D> TypeKey<D> for &str {
    fn key(self) -> Key {
        Key {
            domain: None,
            name: self.to_owned(),
            declare: None,
        }
    }
}

impl<M: Message> TypeKey<M> for Of<M> {
    fn key(self) -> Key {
        Key {
            domain: Some(M::DOMAIN),
            name: M::TYPE.to_owned(),
            declare: Some(M::declare),
        }
    }
}

impl Key {
    /// The type within `domain`, declared there; a message type of another domain is an error.
    pub(crate) fn within(self, domain: &Domain, what: &str) -> String {
        if let Some(own) = self.domain {
            assert!(
                own == domain.name(),
                "{own}.{} belongs to {own}; {what} of {} takes the types of its own domain",
                self.name,
                domain.name()
            );
            if let Some(declare) = self.declare {
                domain.declare_once(&self.name, declare);
            }
        }
        self.name
    }

    /// The type as a listener names it: qualified for another domain's message type.
    pub(crate) fn qualified_for(&self, domain: &Domain) -> String {
        match self.domain {
            Some(own) if own != domain.name() => format!("{own}.{}", self.name),
            _ => self.name.clone(),
        }
    }
}

/// The schema of a message type of `M::DOMAIN`, declared on a domain object of that name.
pub(crate) fn domain_of<M: Message>() -> Domain {
    let d = crate::domain(M::DOMAIN);
    d.declare_once(M::TYPE, M::declare);
    d
}

#[doc(hidden)]
pub mod __private {
    pub use serde;
    pub use serde_json;

    use super::{Deserialize, Deserializer};

    /// A vault field: `null` once erased, read as the field's default.
    pub fn null_as_default<'de, D: Deserializer<'de>, T: Deserialize<'de> + Default>(
        d: D,
    ) -> Result<T, D::Error> {
        Ok(Option::<T>::deserialize(d)?.unwrap_or_default())
    }
}

#[cfg(test)]
mod tests {
    use serde::{Deserialize, Serialize};
    use serde_json::{Value, json};

    use crate::{
        CommandContext, FoldEvent, NewEvent, Query, RequestContext, Submit, Timestamp, command,
        domain, event, query,
    };

    fn own(_d: &Value, id: &str) -> String {
        format!("account:{id}")
    }

    #[event("shopt", version = 2, upcasts = add_note, personal(fields = [email_address], subject = own))]
    #[derive(Clone, Debug, PartialEq)]
    pub struct Placed {
        pub customer_id: String,
        pub email_address: String,
        pub note: Option<String>,
        pub r#type: String,
    }

    fn add_note(schema: crate::EventSchema) -> crate::EventSchema {
        schema.upcast(1, |mut d| {
            d["type"] = json!("v1");
            d
        })
    }

    #[event("shopt")]
    pub struct Cancelled {}

    #[command("shopt", aggregate = "order", secret = [password])]
    pub struct Place {
        pub customer_id: String,
        pub password: String,
    }

    #[query("shopt", answer = Vec<String>)]
    pub struct ListOrders {
        pub status: Option<String>,
    }

    #[query("shopt", answer = Vec<String>, target(aggregate = "order", field = order_id))]
    pub struct GetOrder {
        pub order_id: String,
    }

    #[derive(Clone, Default, Serialize, Deserialize)]
    struct Order {
        customer: String,
        cancelled: bool,
    }

    #[test]
    fn fields_are_camel_case_and_an_event_writes_none_as_null() {
        let p = Placed {
            customer_id: "c1".into(),
            email_address: "a@b".into(),
            note: None,
            r#type: "x".into(),
        };
        assert_eq!(
            serde_json::to_value(&p).unwrap(),
            json!({ "customerId": "c1", "emailAddress": "a@b", "note": null, "type": "x" })
        );
        // A query (or command) leaves an unset option out.
        assert_eq!(
            serde_json::to_value(ListOrders { status: None }).unwrap(),
            json!({})
        );
        // An erased vault value is null: read as the field's default.
        let back: Placed = serde_json::from_value(
            json!({ "customerId": "c1", "emailAddress": null, "type": "x" }),
        )
        .unwrap();
        assert_eq!(back.email_address, "");
        assert_eq!(back.note, None);
    }

    #[test]
    fn apply_and_command_take_the_type_and_declare_its_schema() {
        let shop = domain("shopt");
        let order = shop.aggregate("order", Order::default());
        order.apply(Placed, |s, d, _, _| s.customer = d.customer_id);
        order.apply(Cancelled, |s, _d, _, _| s.cancelled = true);
        order.command(Place, |_s, d, _, _| {
            Ok(vec![
                Placed {
                    customer_id: d.customer_id,
                    email_address: "a@b".into(),
                    note: None,
                    r#type: "t".into(),
                }
                .into(),
            ])
        });
        // The event's version and upcast, from its declaration.
        assert_eq!(shop.schema_of("Placed"), 2);
        let v1 = FoldEvent {
            schema: 1,
            ..FoldEvent::new(
                "Placed",
                json!({ "customerId": "c1", "emailAddress": "a@b" }),
            )
        };
        let state = order.fold([v1], None).unwrap();
        assert_eq!(state.customer, "c1");
        let mut ctx = CommandContext::new("cmd-1", RequestContext::default(), Timestamp(0));
        let events = order
            .handle(
                &state,
                "Place",
                json!({ "customerId": "c2", "password": "p" }),
                &mut ctx,
            )
            .unwrap();
        assert_eq!(
            events,
            vec![NewEvent::new(
                "Placed",
                json!({ "customerId": "c2", "emailAddress": "a@b", "note": null, "type": "t" })
            )]
        );
        // The command's vault fields and the event's personal ones, declared on the domain.
        assert_eq!(
            shop.command_vault("Place").unwrap().secret,
            vec!["password"]
        );
        assert_eq!(
            shop.event_def("Placed").unwrap().vault.personal[0].0,
            vec!["emailAddress"]
        );
    }

    #[test]
    #[should_panic(expected = "belongs to shopt")]
    fn another_domain_s_message_type_is_refused() {
        domain("other")
            .aggregate("order", Order::default())
            .apply(Placed, |_, _, _, _| {});
    }

    #[test]
    fn a_listener_learns_another_domain_s_event_schema() {
        let billing = domain("billingt");
        let listener = billing
            .read_model("orders")
            .on(Placed, |d, _evt, _ctx| async move {
                assert!(!d.customer_id.is_empty());
                Ok(())
            });
        assert_eq!(
            listener.domain_of("shopt").map(|d| d.schema_of("Placed")),
            Some(2)
        );
        listener.query(ListOrders, |_d, _q, _ctx| async move {
            Ok(vec!["o1".to_owned()])
        });
    }

    #[test]
    fn submit_and_query_carry_the_type() {
        let s = Submit::command(
            Place {
                customer_id: "c".into(),
                password: "p".into(),
            },
            "o1",
        );
        let shown = format!("{s:?}");
        assert!(
            shown.contains("domain: \"shopt\"")
                && shown.contains("aggregate_type: \"order\"")
                && shown.contains("command_type: \"Place\""),
            "{shown}"
        );
        assert!(shown.contains("secret: [\"password\"]"), "{shown}");
        let q = format!("{:?}", Query::of(ListOrders { status: None }));
        assert!(
            q.contains("query_type: \"ListOrders\"") && q.contains("data: Object {}"),
            "{q}"
        );
        assert!(q.contains("target: None"), "{q}");
    }

    #[test]
    fn a_query_declares_its_target_and_sends_it_qualified_with_the_id_from_its_data() {
        let target = crate::messages::domain_of::<GetOrder>()
            .query_target("GetOrder")
            .unwrap();
        assert_eq!(
            target,
            crate::QueryTarget {
                aggregate_type: "order".into(),
                field: "orderId".into(),
            }
        );
        let q = format!(
            "{:?}",
            Query::of(GetOrder {
                order_id: "o1".into()
            })
        );
        assert!(q.contains("field: \"orderId\""), "{q}");
        let wire = target.wire("shopt", &json!({ "orderId": "o1" })).unwrap();
        assert_eq!(
            (wire.aggregate_type.as_str(), wire.aggregate_id.as_str()),
            ("shopt.order", "o1")
        );
        // No ID, an empty one or one that is no string: no target.
        for data in [json!({}), json!({ "orderId": "" }), json!({ "orderId": 7 })] {
            assert!(target.wire("shopt", &data).is_none());
        }
        // Qualified, by name.
        let shop = domain("shopt2");
        shop.query("GetCustomer")
            .target("crm.customer", "customerId");
        let wire = shop
            .query_target("GetCustomer")
            .unwrap()
            .wire("shopt2", &json!({ "customerId": "c1" }))
            .unwrap();
        assert_eq!(wire.aggregate_type, "crm.customer");
        assert_eq!(shop.query_target("Other"), None);
    }

    #[test]
    fn a_read_model_answering_a_query_type_declares_its_target_on_its_domain() {
        // So a client that knows the domain (`with_domain`) sends it when it asks by name.
        let shop = domain("shopt");
        shop.read_model("orders")
            .query(GetOrder, |_d, _q, _ctx| async move { Ok(()) });
        assert_eq!(
            shop.query_target("GetOrder").map(|t| t.field),
            Some("orderId".to_owned())
        );
        // A declaration by name stays.
        let other = domain("shopt");
        other.query("GetOrder").target("customer", "customerId");
        other
            .read_model("orders")
            .query(GetOrder, |_d, _q, _ctx| async move { Ok(()) });
        assert_eq!(
            other.query_target("GetOrder").map(|t| t.aggregate_type),
            Some("customer".to_owned())
        );
    }
}