# Cheat sheet

Everything you declare with Itinera, in the order the book teaches it, with the chapter to read for more. After the table comes how each line is written in Kotlin.

## What you declare

| What you declare                                | What it does                                                                                                                                                                                   | Chapter                                                                                                                                                                                                                                                                                                                               |
| ----------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| A step                                          | holds one business rule, runs once per attempt and reports an outcome                                                                                                                          | [4](https://itinera-dev.org/book/kotlin/04-a-first-step.md)                                                                                                                                                                                                                                                                           |
| A step's inputs, required or optional           | the data a step needs, which the workflow supplies, each read as its parameter's type                                                                                                          | [4](https://itinera-dev.org/book/kotlin/04-a-first-step.md), [20](https://itinera-dev.org/book/kotlin/20-an-optional-input.md)                                                                                                                                                                                                        |
| An outcome                                      | success, failure with a reason, or skipped                                                                                                                                                     | [4](https://itinera-dev.org/book/kotlin/04-a-first-step.md), [16](https://itinera-dev.org/book/kotlin/16-skipping-a-step.md)                                                                                                                                                                                                          |
| A workflow and its steps                        | the plan: the steps in order, each under its name                                                                                                                                              | [5](https://itinera-dev.org/book/kotlin/05-a-first-journey.md), [6](https://itinera-dev.org/book/kotlin/06-passing-data-on.md)                                                                                                                                                                                                        |
| A workflow instance and its initial data        | one journey's starting point                                                                                                                                                                   | [5](https://itinera-dev.org/book/kotlin/05-a-first-journey.md), [6](https://itinera-dev.org/book/kotlin/06-passing-data-on.md), [24](https://itinera-dev.org/book/kotlin/24-running-many-journeys.md)                                                                                                                                 |
| An executor, and running an instance            | runs one journey at a time and returns its result, or refuses it, keeping nothing between journeys                                                                                             | [5](https://itinera-dev.org/book/kotlin/05-a-first-journey.md), [24](https://itinera-dev.org/book/kotlin/24-running-many-journeys.md)                                                                                                                                                                                                 |
| Reading the result and its output               | how the journey ended and what explains it, and its output, read typed, unless it was aborted                                                                                                  | [5](https://itinera-dev.org/book/kotlin/05-a-first-journey.md), [6](https://itinera-dev.org/book/kotlin/06-passing-data-on.md), [25](https://itinera-dev.org/book/kotlin/25-reading-the-result.md)                                                                                                                                    |
| A contributor                                   | how a step adds data to the data bag                                                                                                                                                           | [6](https://itinera-dev.org/book/kotlin/06-passing-data-on.md)                                                                                                                                                                                                                                                                        |
| A service and its provider                      | a tool a step asks for by a key, and the part of the workflow that supplies it, per journey or per attempt                                                                                     | [7](https://itinera-dev.org/book/kotlin/07-giving-a-step-the-tools-it-needs.md), [13](https://itinera-dev.org/book/kotlin/13-a-carrier-for-each-attempt.md)                                                                                                                                                                           |
| A reporter                                      | receives every event of a journey                                                                                                                                                              | [8](https://itinera-dev.org/book/kotlin/08-seeing-what-happened.md), [10](https://itinera-dev.org/book/kotlin/10-more-than-one-reporter.md)                                                                                                                                                                                           |
| A step's own events                             | what only the step knows, emitted as `step_info`, `step_warning` or `step_error`                                                                                                               | [9](https://itinera-dev.org/book/kotlin/09-saying-what-only-a-step-knows.md)                                                                                                                                                                                                                                                          |
| A journey ID generator                          | names each journey, for example after the order                                                                                                                                                | [11](https://itinera-dev.org/book/kotlin/11-naming-a-journey.md)                                                                                                                                                                                                                                                                      |
| A retry budget                                  | how many times a step may be tried again                                                                                                                                                       | [12](https://itinera-dev.org/book/kotlin/12-trying-again.md)                                                                                                                                                                                                                                                                          |
| An abnormal termination                         | an exception that escapes a running step, which spends the retry budget unless the step's line says otherwise                                                                                  | [12](https://itinera-dev.org/book/kotlin/12-trying-again.md)                                                                                                                                                                                                                                                                          |
| A rescue step                                   | a step that repairs the cause of a failure before the step runs once more                                                                                                                      | [14](https://itinera-dev.org/book/kotlin/14-repairing-then-trying-once-more.md)                                                                                                                                                                                                                                                       |
| A step policy and its hooks                     | reacts to a step's outcome, such as `on step success`                                                                                                                                          | [15](https://itinera-dev.org/book/kotlin/15-reacting-to-what-a-step-did.md)                                                                                                                                                                                                                                                           |
| Giving a step up early                          | `on step retry` or `on step abnormal termination` returning the lifecycle `FailWorkflow`                                                                                                       | [17](https://itinera-dev.org/book/kotlin/17-giving-up-a-step-early.md)                                                                                                                                                                                                                                                                |
| What a hook requests                            | data from the step or the workflow, the step's name, the reason a step failed with, a role, a contributor                                                                                      | [15](https://itinera-dev.org/book/kotlin/15-reacting-to-what-a-step-did.md), [17](https://itinera-dev.org/book/kotlin/17-giving-up-a-step-early.md), [18](https://itinera-dev.org/book/kotlin/18-doing-something-when-the-journey-ends.md), [19](https://itinera-dev.org/book/kotlin/19-letting-policies-act-on-the-outside-world.md) |
| A workflow policy and its hooks                 | reacts to how the journey ended: `on workflow success`, `on workflow failure`                                                                                                                  | [18](https://itinera-dev.org/book/kotlin/18-doing-something-when-the-journey-ends.md)                                                                                                                                                                                                                                                 |
| A role                                          | operations the workflow provides to policies                                                                                                                                                   | [19](https://itinera-dev.org/book/kotlin/19-letting-policies-act-on-the-outside-world.md)                                                                                                                                                                                                                                             |
| An input adapter                                | decides where a step's inputs come from                                                                                                                                                        | [21](https://itinera-dev.org/book/kotlin/21-deciding-where-a-steps-data-comes-from.md)                                                                                                                                                                                                                                                |
| A value of your own type                        | a data class of yours, held in the data bag like any other value, and read back as its class                                                                                                   | [21](https://itinera-dev.org/book/kotlin/21-deciding-where-a-steps-data-comes-from.md)                                                                                                                                                                                                                                                |
| Finishing a journey early                       | `on step success` returning the lifecycle `FinishWorkflow`, which ends the journey as succeeded                                                                                                | [22](https://itinera-dev.org/book/kotlin/22-finishing-a-journey-early.md)                                                                                                                                                                                                                                                             |
| Undoing a step's work when it is given up       | `on step failure`, called once the executor gives a step up by its own rule, and the hook that gives it up earlier, both calling the policy's own code                                         | [23](https://itinera-dev.org/book/kotlin/23-undoing-a-charge-when-the-shipping-fails.md)                                                                                                                                                                                                                                              |
| Steps and hooks that wait                       | a step or a hook that suspends while a service or a role answers, in an asynchronous workflow, so journeys run at the same time, each with an executor of its own                              | [26](https://itinera-dev.org/book/kotlin/26-running-async-journeys-while-others-wait.md)                                                                                                                                                                                                                                              |
| A step tested alone                             | a step built from its inputs, and run with fakes of its services and recorders lending its contributor and reporter                                                                            | [27](https://itinera-dev.org/book/kotlin/27-testing-a-step-alone.md)                                                                                                                                                                                                                                                                  |
| A hook or an input adapter tested alone         | called as a method, with the data it asks for and fakes of its roles                                                                                                                           | [28](https://itinera-dev.org/book/kotlin/28-testing-a-hook-or-an-adapter-alone.md)                                                                                                                                                                                                                                                    |
| A scripted run                                  | in a test, a journey in which a script says how the steps' attempts end, in place of running them                                                                                              | [29](https://itinera-dev.org/book/kotlin/29-testing-the-flow-with-scripted-steps.md)                                                                                                                                                                                                                                                  |
| A fake provider and a journey's path            | in a test, a function in place of a workflow's provider, and the journey's outcomes and decisions as lines a test compares                                                                     | [30](https://itinera-dev.org/book/kotlin/30-testing-a-whole-journey.md)                                                                                                                                                                                                                                                               |
| A reporter that fails                           | a reporter whose report fails aborts the journey with `reporter failed`, and receives nothing more                                                                                             | [31](https://itinera-dev.org/book/kotlin/31-stopping-a-journey-when-its-log-fails.md)                                                                                                                                                                                                                                                 |
| A reporter that prints every event              | a reporter that comes with Itinera, prints each event as it is, and never fails                                                                                                                | [32](https://itinera-dev.org/book/kotlin/32-seeing-every-event-of-a-journey.md)                                                                                                                                                                                                                                                       |
| Global metadata                                 | a key and a value every reporter of a journey receives, set by the instance before the first event or by a step while it runs                                                                  | [33](https://itinera-dev.org/book/kotlin/33-giving-every-reporter-what-it-needs.md)                                                                                                                                                                                                                                                   |
| What only a hook can know                       | the journey's ID, the attempt a step hook is about, and the error the step ended in                                                                                                            | [34](https://itinera-dev.org/book/kotlin/34-saying-which-attempt-took-the-money.md)                                                                                                                                                                                                                                                   |
| A key of its own, and a provider for some steps | an input or a service found under a key other than its field's or parameter's name; a provider attached to some steps only, handing over a client it didn't make, with data saying which it is | [35](https://itinera-dev.org/book/kotlin/35-giving-the-rescue-step-a-service-of-its-own.md)                                                                                                                                                                                                                                           |
| An order stopped before its journey             | an instance not created when its journey ID can't be produced, and a journey refused when a reporter can't take its global metadata                                                            | [36](https://itinera-dev.org/book/kotlin/36-orders-refused-before-they-start.md)                                                                                                                                                                                                                                                      |
| The listing, and the violations of admission    | how a workflow is put together, read without running anything, and the mistakes caught before any journey                                                                                      | [37](https://itinera-dev.org/book/kotlin/37-mistakes-caught-before-a-journey.md)                                                                                                                                                                                                                                                      |
| A workflow declared by hand                     | the long form of everything above, for a declaration put together when the program starts, and the violations `build()` throws                                                                 | [38](https://itinera-dev.org/book/kotlin/38-declaring-by-hand.md)                                                                                                                                                                                                                                                                     |

## In Kotlin

With the annotations, each line of the table is written as follows. Each name is imported from the package of its concept under `org.itineradev.itinera`, as in `import org.itineradev.itinera.step.Step`, and every item's documentation, which your editor shows, has the rest.

| What you declare                                | How you write it                                                                                                                                                                                                                                                                                                                                                 | Chapter        |
| ----------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------- |
| A step                                          | a class marked `@Step` that implements `ItineraStep`, whose `run` returns an `Outcome`                                                                                                                                                                                                                                                                           | 4              |
| A step's inputs                                 | a constructor property marked `@Input`, such as `@Input private val price: Long`; with a nullable type, such as `Long?`, the input is optional, and `null` when it is absent                                                                                                                                                                                     | 4, 20          |
| An outcome                                      | `succeed()`, `fail("code")`, `skip()` or `skipBecause("code")`, which `ItineraStep` gives the class                                                                                                                                                                                                                                                              | 4, 16          |
| A workflow and its steps                        | a class marked `@Workflow(name = "orders")`, then one `@WithStep(Charge::class, name = "charge")` line per step, in order, implementing `ItineraWorkflow`                                                                                                                                                                                                        | 5, 6           |
| A workflow instance and its initial data        | `Orders().instance().data("price", price).create()`, or `.initialData(order)` with the order as a data class                                                                                                                                                                                                                                                     | 5, 6, 24       |
| An executor, and running an instance            | `val executor = LocalExecutor()`, then `executor.run(instance)` for each instance, which returns a `JourneyResult`, from `org.itineradev.itinera.journey`, or throws a `Refusal`, from `org.itineradev.itinera.executor`                                                                                                                                         | 5, 24          |
| Reading the result and its output               | `result.isSucceeded` or `result.isFailed`, or a `when` over `result.status`, a `JourneyStatus.Succeeded`, `Failed`, with its `failure`, or `Aborted`, with its `abort`; and `result.output<Order>()`, from `org.itineradev.itinera.journey.kotlin`                                                                                                               | 5, 6, 25       |
| A contributor                                   | a parameter of `run` of type `Contributor`, then `contributor.contribute("key", value)`                                                                                                                                                                                                                                                                          | 6              |
| A service and its provider                      | an interface such as `Payments`; a parameter of `run` marked `@Service`; a method of the workflow's class marked `@Provider(key = "payments")`, or with `perAttempt = true` for a new object for each attempt, returning `provide(Bank())`, typed `Provided<Payments>`; a provider for each attempt may ask for `attempt: Attempt`                               | 7, 13          |
| A reporter                                      | a class implementing `Reporter`, from `org.itineradev.itinera.report`, whose `report(event: Event)` reads `event.kind`, listed with `reporters = [Log::class, Desk::class]` in `@Workflow(...)`                                                                                                                                                                  | 8, 10          |
| A step's own events                             | a parameter of `run` of type `StepReporter`, then `reporter.info("...")`, `.warning(...)` or `.error(...)`                                                                                                                                                                                                                                                       | 9              |
| A journey ID generator                          | a method of the workflow's class marked `@IdGenerator`, asking for `data: DataBag` and returning a `String`, such as `data.read<String>("order").orNull() ?: error("...")`                                                                                                                                                                                       | 11             |
| A retry budget                                  | `retryBudget = 1` in the step's `@WithStep`                                                                                                                                                                                                                                                                                                                      | 12             |
| An abnormal termination                         | `run` throws an exception; `noRetryOnAbnormalTermination = true` in the step's `@WithStep` keeps it from spending the retry budget                                                                                                                                                                                                                               | 12             |
| A rescue step                                   | a class marked `@RescueStep` that implements `ItineraRescueStep`, whose `run` returns a `RescueOutcome` and whose constructor may ask for `reason: Reason`; attached with `rescue = RenewCard::class` in the step's `@WithStep`                                                                                                                                  | 14             |
| A step policy and its hooks                     | a class marked `@StepPolicy(name = "audit")` that implements `ItineraStepPolicy`, with a method marked with its hook, such as `@OnStepSuccess`, returning an `OnSuccess` made with `keepDefault()`; attached with `policies = [Audit::class]` in a step's `@WithStep`                                                                                            | 15             |
| Giving a step up early                          | a method marked `@OnStepRetry` or `@OnStepAbnormalTermination`, returning an `OnFailure`, with `failWorkflow("code")` or `keepDefault()`                                                                                                                                                                                                                         | 17             |
| What a hook requests                            | parameters marked `@FromStep` or `@FromWorkflow`, such as `@FromStep receipt: String`, and parameters asked for by their type alone: `step: StepName`, `reason: Reason?`, `contributor: Contributor`; a role is marked `@Role`, as `@Role messages: Messages`                                                                                                    | 15, 17, 18, 19 |
| A workflow policy and its hooks                 | a class marked `@WorkflowPolicy(name = "notify")` that implements `ItineraWorkflowPolicy`, with methods marked `@OnWorkflowSuccess` or `@OnWorkflowFailure`, returning nothing; attached with `policies = [Notify::class]` in `@Workflow(...)`                                                                                                                   | 18             |
| A role                                          | an interface marked `@Role`, such as `Messages`, which the workflow's class implements                                                                                                                                                                                                                                                                           | 19             |
| An input adapter                                | a method of the workflow's class marked `@InputAdapter(steps = ["ship"])`, asking for `@Key key: String`, and returning `supply(value)` or `fromDataBag()`, both `Supplied`                                                                                                                                                                                      | 21             |
| A value of your own type                        | a data class, such as `data class Parcel(val weight: Long, val size: List<Long>)`, whose properties are values                                                                                                                                                                                                                                                   | 21             |
| Finishing a journey early                       | `finishWorkflow()`, returned by a method marked `@OnStepSuccess`                                                                                                                                                                                                                                                                                                 | 22             |
| Undoing a step's work when it is given up       | a method marked `@OnStepFailure`, returning an `OnFailure`, which may ask for `cause: FailureCause`; and a private method of the policy, without a mark, that its hooks call                                                                                                                                                                                     | 23             |
| Steps and hooks that wait                       | a workflow class implementing `ItineraAsyncWorkflow`, whose steps, hooks, providers and adapters are `suspend` functions, as are the methods of roles that wait, its reporters `AsyncReporter`s; one `AsyncLocalExecutor()` for each journey running at the same time, started with `async` inside `runBlocking` and gathered with `awaitAll()`                  | 26             |
| A step tested alone                             | in a test file, `Charge(...).run(TestBank.CHARGES, contributions.contributor)`, with `Contributions` from `org.itineradev.itinera.journey.testing` and `StepEventReporter` from `org.itineradev.itinera.step.testing`, the outcome compared with `assertEquals`, and what it contributed read with `contributions.read("receipt", typeToken<String>()).orNull()` | 27             |
| A hook or an input adapter tested alone         | in a test file, `FailedShipping().beforeRetrying(...)`, whose lifecycle says what it decided, as `decision.failsWorkflow?.code`, or `Orders(channel = "email").parcels("weight", parcel)`, compared with `Supplied.Value(60L)`                                                                                                                                   | 28             |
| A scripted run                                  | in a test file, `scripted(instance).step("charge").willFail("code").onAttempt(2).willSucceed().andStep("charge_rescue").willSucceed().run()`, with `scripted` from `org.itineradev.itinera.executor.testing`, which returns a `ScriptedRun`                                                                                                                      | 29             |
| A fake provider and a journey's path            | in a test file, an instance made with `TestInstanceBuilder.of(Orders(channel = "email"))`, from `org.itineradev.itinera.instance.testing`, then `.fakeProvider("bank", Payments::class.java) { Provided.Instance(bank) }` and `.reporter(recorder)` for a `RecordingReporter`, from `org.itineradev.itinera.report.testing`, then `recorder.path`                | 30             |
| A reporter that fails                           | `report` throwing, which aborts the journey; the result's `abort.errorMessage` holds the exception's message, and `abort.error` the exception                                                                                                                                                                                                                    | 31             |
| A reporter that prints every event              | `DebugReporter`, from `org.itineradev.itinera.report`, in `reporters = [DebugReporter::class, ...]`, or `AsyncDebugReporter` in an asynchronous workflow                                                                                                                                                                                                         | 32             |
| Global metadata                                 | `.reportersGlobalMetadata("customer", order.customer)` when the instance is created, beside `initialData`, or `reporter.setGlobalMetadata("receipt", receipt)` in a step, received by a reporter's `setGlobalMetadata(key, value)`                                                                                                                               | 33             |
| What only a hook can know                       | parameters `journey: JourneyId` and `attempt: Attempt`, and `error: Throwable` on `on step abnormal termination`, or `error: Throwable?` on `on step retry` and `on step failure`                                                                                                                                                                                | 34             |
| A key of its own, and a provider for some steps | `@Input("card") private val expired: String` or `@Service("cards") registry: Cards`; `@Provider(steps = ["charge_rescue"])`, returning `provide(client, "eu")`                                                                                                                                                                                                   | 35             |
| An order stopped before its journey             | `create()` throwing an `IllegalStateException` when the ID generator throws; `run` throwing a `Refusal` whose `refused` is `Refused.GlobalMetadataNotSet` when a reporter's `setGlobalMetadata` throws                                                                                                                                                           | 36             |
| The listing, and the violations of admission    | `WorkflowDescriptor.of(Orders::class.java).listing()`, giving a `ListedStep` for each step; with itinera's checker, a violation is an error of the build; some mistakes don't compile at all                                                                                                                                                                     | 37             |
| A workflow declared by hand                     | `WorkflowDescriptor.builder(Orders::class.java, "orders").step(StepDescriptor.builder("ship", Ship::class.java).retryBudget(1).build()).role(Messages::class.java).build()`, which throws a `Violations`; a step without annotations is `StepDescriptor.builder("price") { got -> ... }` with a `request(...)` for each request made with `Requests`             | 38             |
