tricu

An interpreted language for exploring Tree Calculus
Log | Files | Refs | README | LICENSE

events.tri (2023B)


      1 !import bd2c14d6db6b69308a5252a8e839705a55d30e815f012900b094315263e08d81 *
      2 !import 3f70d95cf1bae0928ee89acd2a9e0cdd8287796efae32a9f08d33e3dd0149c55 as A
      3 !export vendorA
      4 !export vendorB
      5 
      6 -- Event adapter: two producers, one consumer-facing record schema.
      7 -- vendorA accepts [eventName customer totalCents]. eventName is purchase/refund.
      8 -- vendorB accepts [customer refunded totalCents]. refunded is a Boolean.
      9 -- Both return schemaVersion, eventType, customer and amountCents.
     10 -- Try the two exports: their presets describe the same order.created event.
     11 -- A producer can publish the adapter hash alongside its event schema.
     12 -- Records use [fieldName tag value]: 0 = text, 1 = natural, 2 = Boolean.
     13 -- Base supplies arithmetic/Booleans; A.hostString? checks Unicode scalar text.
     14 
     15 failure message = [["status" 0 "error"] ["message" 0 message]]
     16 
     17 normalized event customer cents = case A.hostString? customer of
     18   ^true = (case and? (isNat? cents) (lte_? cents 1000000) of
     19     ^true = [["schemaVersion" 1 1] ["eventType" 0 event]
     20       ["customer" 0 customer] ["amountCents" 1 cents]]
     21     _ = failure "amountCents must be a canonical natural <=1000000.")
     22   _ = failure "customer must be scalar text."
     23 
     24 fromA "purchase" customer cents = normalized "order.created" customer cents
     25 fromA "refund" customer cents = normalized "order.refunded" customer cents
     26 fromA _ _ _ = failure "Unknown vendor A event."
     27 
     28 fromB customer ^true cents = normalized "order.refunded" customer cents
     29 fromB customer ^false cents = normalized "order.created" customer cents
     30 fromB _ _ _ = failure "refunded must be a canonical Boolean."
     31 
     32 -- Preserve list-shape diagnostics without inspecting any field before dispatch.
     33 unpack handler (t _) = failure "Expected a list."
     34 unpack handler [_ | (t _)] = failure "Expected a list."
     35 unpack handler [_ | [_ | (t _)]] = failure "Expected a list."
     36 unpack handler [a b c] = handler a b c
     37 unpack _ _ = failure "Expected exactly three fields."
     38 
     39 vendorA values = unpack fromA values
     40 vendorB values = unpack fromB values