Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions pkgs/id/examples/roc-cap-demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# roc-cap-demo — a capability-dependent demo library + app (#5957)

A small worked example of the Roc library import model for worlds:
a **library that is not pure** (it depends on a platform capability)
and an **app** that imports it and runs on a platform providing that
capability.

- `lib/` — the demo library, a Roc package. `lib/Greeter.roc` greets
world participants by name. The names live in the world records, so
the library issues a `records.read` capability request through the
platform's `RocCap` protocol helpers: it cannot do its job on a
platform that does not provide that capability. `lib/main.roc` is
the package root and binds the library to
`../roc-world/platform.roc`.
- `main.roc` — the demo app. On init it asks for the names through
the library; its view greets every participant once the result
arrives. All protocol handling (issuing, announcing in `wants`,
routing the result by id, retiring it, snapshot/restore of the
session) lives in the library.

## Tests

`roc-cap-demo-test.sh` (wired into `scripts/roc-test.sh`, i.e. the `roc-test`
just recipe and flake check) runs:

1. The library's `expect` tests (`roc test lib/main.roc`) and a
typecheck of the demo app (`roc check main.roc`) — the positive
path.
2. **Negative test A** — `main-missing-cap.roc`, the same app and
library bound to the reduced platform
`../roc-world/platform-no-caps.roc`, which does not expose
`RocCap`. Compilation must fail, and the failure must name the
missing `pf.RocCap` module. (`lib-no-caps/` is the library package
bound to the reduced platform; its `Greeter.roc` is a
byte-identical copy of `lib/Greeter.roc` — Roc package modules
cannot be symlinked in from outside the package root — and
`roc-cap-demo-test.sh` fails if the copies drift.)
3. **Negative test B** — `main-wrong-usage.roc`, the app against the
full platform but calling `RocCap.schedule_at_request` with a
wrong signature. Compilation must fail with a type mismatch at
that call.

Each negative test pins *why* it fails (asserted error content), not
just that it fails.
151 changes: 151 additions & 0 deletions pkgs/id/examples/roc-cap-demo/lib-no-caps/Greeter.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
## Demo library: greet world participants by name.
##
## The names live in the world records, not in this library, so the
## library is NOT pure: it depends on the platform providing the
## records.read capability (through the RocCap protocol helpers) to
## fetch them. A platform without that capability cannot host this
## library at all — see the negative tests in ../roc-cap-demo-test.sh, which are
## wired into scripts/roc-test.sh (#5957).
import pf.RocCap
import pf.RocJson

Greeter := {
session : RocCap,
names : List(Str),
}.{
## A greeter that has not asked for names yet.
new : Greeter
new = { session: RocCap.empty, names: [] }

## Ask the platform for the participant-name records (prefix
## "names/"). Returns the updated greeter and the request id; the
## app announces the greeter requests (see wants) and routes the
## result back through absorb_result.
ask_for_names : Greeter -> { greeter : Greeter, id : Str }
ask_for_names = |greeter| {
issued = RocCap.issue(greeter.session, "records.read")
{
greeter: { session: RocCap.attach(issued.session, issued.id, RocCap.records_request(issued.id, "names/", "")), names: greeter.names },
id: issued.id,
}
}

## Absorb one decoded {"cap":"result",...} event. A records.read
## answer teaches the greeter the participant names; any other
## outstanding request is simply retired. Results for ids this
## greeter never issued are ignored, so journal replay and stale
## answers cannot corrupt the model.
absorb_result : Greeter, RocJson -> Greeter
absorb_result = |greeter, event| {
id = string_field(event, "id")
answered = RocCap.answered(greeter.session, id)
if answered == "records.read" and bool_field(event, "ok") {
{ session: RocCap.done(greeter.session, id), names: names_from_page(field(event, "value")) }
} else if answered != "" {
{ session: RocCap.done(greeter.session, id), names: greeter.names }
} else {
greeter
}
}

## The wants document announcing the outstanding requests, in the
## shape the world host expects (compare roc-chat).
wants : Greeter -> Str
wants = |greeter| encode_or_crash(
RocJson.Object([
{ key: "v", value: RocJson.Number("1") },
{ key: "requests", value: RocJson.Array(RocCap.requests(greeter.session)) },
]),
)

## The greeting for every participant name learned so far.
greetings : Greeter -> List(Str)
greetings = |greeter| List.map(greeter.names, greet)

## The greeting for one participant name.
greet : Str -> Str
greet = |name| Str.concat("hello, ", name)

## The participant names from a records.read result page: each
## record entry carries its name as the string value.
names_from_page : RocJson -> List(Str)
names_from_page = |page|
List.fold(RocCap.records_page(page), [], |names, entry|
match RocJson.as_string(field(entry, "value")) {
Ok(name) => List.append(names, name)
Err(_) => names
})

## The names learned so far.
known_names : Greeter -> List(Str)
known_names = |greeter| greeter.names

## The session request counter, for snapshotting.
counter : Greeter -> U64
counter = |greeter| RocCap.counter(greeter.session)

## The outstanding request documents, for snapshotting.
pending_documents : Greeter -> List(RocJson)
pending_documents = |greeter| RocCap.requests(greeter.session)

## Rebuild a greeter from its snapshot parts (compare
## RocCap.from_pending).
from_parts : U64, List(RocJson), List(Str) -> Greeter
from_parts = |next, documents, names| { session: RocCap.from_pending(next, documents), names: names }

field : RocJson, Str -> RocJson
field = |value, key| match RocJson.get(value, key) {
Ok(found) => found
Err(_) => RocJson.Null
}

string_field : RocJson, Str -> Str
string_field = |value, key| match RocJson.as_string(field(value, key)) {
Ok(text) => text
Err(_) => ""
}

bool_field : RocJson, Str -> Bool
bool_field = |value, key| match RocJson.as_bool(field(value, key)) {
Ok(flag) => flag
Err(_) => Bool.False
}

encode_or_crash : RocJson -> Str
encode_or_crash = |value| match RocJson.encode(value) {
Ok(text) => text
Err(_) => crash "Greeter attempted to encode an invalid or oversized JSON value"
}
}

sample_page : RocJson
sample_page = RocJson.Object([
{
key: "records",
value: RocJson.Array([
RocJson.Object([
{ key: "key", value: RocJson.String("names/1") },
{ key: "value", value: RocJson.String("ada") },
]),
RocJson.Object([
{ key: "key", value: RocJson.String("names/2") },
{ key: "value", value: RocJson.String("grace") },
]),
]),
},
{ key: "next_after", value: RocJson.String("") },
])

sample_result : RocJson
sample_result = RocJson.Object([
{ key: "cap", value: RocJson.String("result") },
{ key: "id", value: RocJson.String("r0") },
{ key: "ok", value: RocJson.Bool(Bool.True) },
{ key: "value", value: sample_page },
])

expect Greeter.greet("ada") == "hello, ada"
expect Greeter.ask_for_names(Greeter.new).id == "r0"
expect Greeter.names_from_page(sample_page) == ["ada", "grace"]
expect Greeter.greetings(Greeter.absorb_result(Greeter.ask_for_names(Greeter.new).greeter, sample_result)) == ["hello, ada", "hello, grace"]
expect Str.contains(Greeter.wants(Greeter.ask_for_names(Greeter.new).greeter), "records.read") == Bool.True
6 changes: 6 additions & 0 deletions pkgs/id/examples/roc-cap-demo/lib-no-caps/main.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## The roc-cap-demo library bound to the reduced platform
## (roc-world/platform-no-caps.roc), which does NOT provide the
## RocCap capability module. Negative test A (#5957): Greeter.roc
## imports pf.RocCap, so anything using this package must fail to
## compile — the capability is not on the platform.
package [Greeter] { pf: platform "../../roc-world/platform-no-caps.roc" }
151 changes: 151 additions & 0 deletions pkgs/id/examples/roc-cap-demo/lib/Greeter.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
## Demo library: greet world participants by name.
##
## The names live in the world records, not in this library, so the
## library is NOT pure: it depends on the platform providing the
## records.read capability (through the RocCap protocol helpers) to
## fetch them. A platform without that capability cannot host this
## library at all — see the negative tests in ../roc-cap-demo-test.sh, which are
## wired into scripts/roc-test.sh (#5957).
import pf.RocCap
import pf.RocJson

Greeter := {
session : RocCap,
names : List(Str),
}.{
## A greeter that has not asked for names yet.
new : Greeter
new = { session: RocCap.empty, names: [] }

## Ask the platform for the participant-name records (prefix
## "names/"). Returns the updated greeter and the request id; the
## app announces the greeter requests (see wants) and routes the
## result back through absorb_result.
ask_for_names : Greeter -> { greeter : Greeter, id : Str }
ask_for_names = |greeter| {
issued = RocCap.issue(greeter.session, "records.read")
{
greeter: { session: RocCap.attach(issued.session, issued.id, RocCap.records_request(issued.id, "names/", "")), names: greeter.names },
id: issued.id,
}
}

## Absorb one decoded {"cap":"result",...} event. A records.read
## answer teaches the greeter the participant names; any other
## outstanding request is simply retired. Results for ids this
## greeter never issued are ignored, so journal replay and stale
## answers cannot corrupt the model.
absorb_result : Greeter, RocJson -> Greeter
absorb_result = |greeter, event| {
id = string_field(event, "id")
answered = RocCap.answered(greeter.session, id)
if answered == "records.read" and bool_field(event, "ok") {
{ session: RocCap.done(greeter.session, id), names: names_from_page(field(event, "value")) }
} else if answered != "" {
{ session: RocCap.done(greeter.session, id), names: greeter.names }
} else {
greeter
}
}

## The wants document announcing the outstanding requests, in the
## shape the world host expects (compare roc-chat).
wants : Greeter -> Str
wants = |greeter| encode_or_crash(
RocJson.Object([
{ key: "v", value: RocJson.Number("1") },
{ key: "requests", value: RocJson.Array(RocCap.requests(greeter.session)) },
]),
)

## The greeting for every participant name learned so far.
greetings : Greeter -> List(Str)
greetings = |greeter| List.map(greeter.names, greet)

## The greeting for one participant name.
greet : Str -> Str
greet = |name| Str.concat("hello, ", name)

## The participant names from a records.read result page: each
## record entry carries its name as the string value.
names_from_page : RocJson -> List(Str)
names_from_page = |page|
List.fold(RocCap.records_page(page), [], |names, entry|
match RocJson.as_string(field(entry, "value")) {
Ok(name) => List.append(names, name)
Err(_) => names
})

## The names learned so far.
known_names : Greeter -> List(Str)
known_names = |greeter| greeter.names

## The session request counter, for snapshotting.
counter : Greeter -> U64
counter = |greeter| RocCap.counter(greeter.session)

## The outstanding request documents, for snapshotting.
pending_documents : Greeter -> List(RocJson)
pending_documents = |greeter| RocCap.requests(greeter.session)

## Rebuild a greeter from its snapshot parts (compare
## RocCap.from_pending).
from_parts : U64, List(RocJson), List(Str) -> Greeter
from_parts = |next, documents, names| { session: RocCap.from_pending(next, documents), names: names }

field : RocJson, Str -> RocJson
field = |value, key| match RocJson.get(value, key) {
Ok(found) => found
Err(_) => RocJson.Null
}

string_field : RocJson, Str -> Str
string_field = |value, key| match RocJson.as_string(field(value, key)) {
Ok(text) => text
Err(_) => ""
}

bool_field : RocJson, Str -> Bool
bool_field = |value, key| match RocJson.as_bool(field(value, key)) {
Ok(flag) => flag
Err(_) => Bool.False
}

encode_or_crash : RocJson -> Str
encode_or_crash = |value| match RocJson.encode(value) {
Ok(text) => text
Err(_) => crash "Greeter attempted to encode an invalid or oversized JSON value"
}
}

sample_page : RocJson
sample_page = RocJson.Object([
{
key: "records",
value: RocJson.Array([
RocJson.Object([
{ key: "key", value: RocJson.String("names/1") },
{ key: "value", value: RocJson.String("ada") },
]),
RocJson.Object([
{ key: "key", value: RocJson.String("names/2") },
{ key: "value", value: RocJson.String("grace") },
]),
]),
},
{ key: "next_after", value: RocJson.String("") },
])

sample_result : RocJson
sample_result = RocJson.Object([
{ key: "cap", value: RocJson.String("result") },
{ key: "id", value: RocJson.String("r0") },
{ key: "ok", value: RocJson.Bool(Bool.True) },
{ key: "value", value: sample_page },
])

expect Greeter.greet("ada") == "hello, ada"
expect Greeter.ask_for_names(Greeter.new).id == "r0"
expect Greeter.names_from_page(sample_page) == ["ada", "grace"]
expect Greeter.greetings(Greeter.absorb_result(Greeter.ask_for_names(Greeter.new).greeter, sample_result)) == ["hello, ada", "hello, grace"]
expect Str.contains(Greeter.wants(Greeter.ask_for_names(Greeter.new).greeter), "records.read") == Bool.True
8 changes: 8 additions & 0 deletions pkgs/id/examples/roc-cap-demo/lib/main.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Package root for the roc-cap-demo demo library.
##
## The library (Greeter.roc) depends on the world platform: it uses
## the platform-provided RocCap/RocJson modules, so this package can
## only be used with a platform that provides them. ../main.roc is
## the demo app that imports it; ../lib-no-caps is the same library
## bound to a reduced platform, for the negative tests.
package [Greeter] { pf: platform "../../roc-world/platform.roc" }
Loading
Loading