mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-01-02 14:03:06 +00:00
Addressing review findings - better explanation the distinction btw sync/async RequestBroker, usage of parenthesises
This commit is contained in:
parent
73794f5836
commit
2618a4cc41
@ -59,7 +59,7 @@ suite "RequestBroker macro (async mode)":
|
||||
|
||||
test "zero-argument request errors when unset":
|
||||
let res = waitFor SimpleResponse.request()
|
||||
check res.isErr
|
||||
check res.isErr()
|
||||
check res.error.contains("no zero-arg provider")
|
||||
|
||||
test "serves input-based providers":
|
||||
@ -96,7 +96,7 @@ suite "RequestBroker macro (async mode)":
|
||||
|
||||
test "input request errors when unset":
|
||||
let res = waitFor KeyedResponse.request("foo", 2)
|
||||
check res.isErr
|
||||
check res.isErr()
|
||||
check res.error.contains("input signature")
|
||||
|
||||
test "supports both provider types simultaneously":
|
||||
@ -115,11 +115,11 @@ suite "RequestBroker macro (async mode)":
|
||||
.isOk()
|
||||
|
||||
let noInput = waitFor DualResponse.request()
|
||||
check noInput.isOk
|
||||
check noInput.isOk()
|
||||
check noInput.value.note == "base"
|
||||
|
||||
let withInput = waitFor DualResponse.request("-extra")
|
||||
check withInput.isOk
|
||||
check withInput.isOk()
|
||||
check withInput.value.note == "base-extra"
|
||||
check withInput.value.count == 6
|
||||
|
||||
@ -135,7 +135,7 @@ suite "RequestBroker macro (async mode)":
|
||||
DualResponse.clearProvider()
|
||||
|
||||
let res = waitFor DualResponse.request()
|
||||
check res.isErr
|
||||
check res.isErr()
|
||||
|
||||
test "implicit zero-argument provider works by default":
|
||||
check ImplicitResponse
|
||||
@ -146,14 +146,14 @@ suite "RequestBroker macro (async mode)":
|
||||
.isOk()
|
||||
|
||||
let res = waitFor ImplicitResponse.request()
|
||||
check res.isOk
|
||||
check res.isOk()
|
||||
|
||||
ImplicitResponse.clearProvider()
|
||||
check res.value.note == "auto"
|
||||
|
||||
test "implicit zero-argument request errors when unset":
|
||||
let res = waitFor ImplicitResponse.request()
|
||||
check res.isErr
|
||||
check res.isErr()
|
||||
check res.error.contains("no zero-arg provider")
|
||||
|
||||
test "no provider override":
|
||||
@ -177,7 +177,7 @@ suite "RequestBroker macro (async mode)":
|
||||
check DualResponse.setProvider(overrideProc).isErr()
|
||||
|
||||
let noInput = waitFor DualResponse.request()
|
||||
check noInput.isOk
|
||||
check noInput.isOk()
|
||||
check noInput.value.note == "base"
|
||||
|
||||
let stillResponse = waitFor DualResponse.request(" still works")
|
||||
@ -197,7 +197,7 @@ suite "RequestBroker macro (async mode)":
|
||||
check DualResponse.setProvider(overrideProc).isOk()
|
||||
|
||||
let nowSuccWithOverride = waitFor DualResponse.request()
|
||||
check nowSuccWithOverride.isOk
|
||||
check nowSuccWithOverride.isOk()
|
||||
check nowSuccWithOverride.value.note == "something else"
|
||||
check nowSuccWithOverride.value.count == 1
|
||||
|
||||
@ -259,7 +259,7 @@ suite "RequestBroker macro (sync mode)":
|
||||
|
||||
test "zero-argument request errors when unset (sync)":
|
||||
let res = SimpleResponseSync.request()
|
||||
check res.isErr
|
||||
check res.isErr()
|
||||
check res.error.contains("no zero-arg provider")
|
||||
|
||||
test "serves input-based providers (sync)":
|
||||
@ -296,7 +296,7 @@ suite "RequestBroker macro (sync mode)":
|
||||
|
||||
test "input request errors when unset (sync)":
|
||||
let res = KeyedResponseSync.request("foo", 2)
|
||||
check res.isErr
|
||||
check res.isErr()
|
||||
check res.error.contains("input signature")
|
||||
|
||||
test "supports both provider types simultaneously (sync)":
|
||||
@ -315,11 +315,11 @@ suite "RequestBroker macro (sync mode)":
|
||||
.isOk()
|
||||
|
||||
let noInput = DualResponseSync.request()
|
||||
check noInput.isOk
|
||||
check noInput.isOk()
|
||||
check noInput.value.note == "base"
|
||||
|
||||
let withInput = DualResponseSync.request("-extra")
|
||||
check withInput.isOk
|
||||
check withInput.isOk()
|
||||
check withInput.value.note == "base-extra"
|
||||
check withInput.value.count == 6
|
||||
|
||||
@ -335,7 +335,7 @@ suite "RequestBroker macro (sync mode)":
|
||||
DualResponseSync.clearProvider()
|
||||
|
||||
let res = DualResponseSync.request()
|
||||
check res.isErr
|
||||
check res.isErr()
|
||||
|
||||
test "implicit zero-argument provider works by default (sync)":
|
||||
check ImplicitResponseSync
|
||||
@ -346,14 +346,14 @@ suite "RequestBroker macro (sync mode)":
|
||||
.isOk()
|
||||
|
||||
let res = ImplicitResponseSync.request()
|
||||
check res.isOk
|
||||
check res.isOk()
|
||||
|
||||
ImplicitResponseSync.clearProvider()
|
||||
check res.value.note == "auto"
|
||||
|
||||
test "implicit zero-argument request errors when unset (sync)":
|
||||
let res = ImplicitResponseSync.request()
|
||||
check res.isErr
|
||||
check res.isErr()
|
||||
check res.error.contains("no zero-arg provider")
|
||||
|
||||
test "implicit zero-argument provider raises error (sync)":
|
||||
@ -365,7 +365,7 @@ suite "RequestBroker macro (sync mode)":
|
||||
.isOk()
|
||||
|
||||
let res = ImplicitResponseSync.request()
|
||||
check res.isErr
|
||||
check res.isErr()
|
||||
check res.error.contains("simulated failure")
|
||||
|
||||
ImplicitResponseSync.clearProvider()
|
||||
@ -484,15 +484,15 @@ suite "RequestBroker macro (POD/external types)":
|
||||
|
||||
let resA = DistinctStringResponseA.request()
|
||||
let resB = DistinctStringResponseB.request()
|
||||
check resA.isOk
|
||||
check resB.isOk
|
||||
check resA.isOk()
|
||||
check resB.isOk()
|
||||
check string(resA.value) == "a"
|
||||
check string(resB.value) == "b"
|
||||
|
||||
let resEA = ExternalDistinctResponseA.request()
|
||||
let resEB = ExternalDistinctResponseB.request()
|
||||
check resEA.isOk
|
||||
check resEB.isOk
|
||||
check resEA.isOk()
|
||||
check resEB.isOk()
|
||||
check ExternalDefinedTypeShared(resEA.value).label == "ea"
|
||||
check ExternalDefinedTypeShared(resEB.value).label == "eb"
|
||||
|
||||
|
||||
@ -6,8 +6,15 @@
|
||||
## Worth considering using it in a single provider, many requester scenario.
|
||||
##
|
||||
## Provides a declarative way to define an immutable value type together with a
|
||||
## thread-local broker that can register an asynchronous provider, dispatch typed
|
||||
## requests and clear provider.
|
||||
## thread-local broker that can register an asynchronous or synchronous provider,
|
||||
## dispatch typed requests and clear provider.
|
||||
##
|
||||
## For consideration use `sync` mode RequestBroker when you need to provide simple value(s)
|
||||
## where there is no long-running async operation involved.
|
||||
## Typically it act as a accessor for the local state of generic setting.
|
||||
##
|
||||
## `async` mode is better to be used when you request date that may involve some long IO operation
|
||||
## or action.
|
||||
##
|
||||
## Usage:
|
||||
## Declare your desired request type inside a `RequestBroker` macro, add any number of fields.
|
||||
@ -36,7 +43,7 @@
|
||||
## proc signature*(arg1: ArgType): Result[TypeName, string]
|
||||
## ```
|
||||
##
|
||||
## Note: When the request type is declared as a POD / alias / externally-defined
|
||||
## Note: When the request type is declared as a native type / alias / externally-defined
|
||||
## type (i.e. not an inline `object` / `ref object` definition), RequestBroker
|
||||
## will wrap it in `distinct` automatically unless you already used `distinct`.
|
||||
## This avoids overload ambiguity when multiple brokers share the same
|
||||
@ -46,7 +53,7 @@
|
||||
## - construct values with an explicit cast/constructor, e.g. `MyType("x")`
|
||||
## - unwrap with a cast when needed, e.g. `string(myVal)` or `BaseType(myVal)`
|
||||
##
|
||||
## Example (POD response type):
|
||||
## Example (native response type):
|
||||
## ```nim
|
||||
## RequestBroker(sync):
|
||||
## type MyCount = int # exported as: `distinct int`
|
||||
@ -113,7 +120,7 @@
|
||||
##
|
||||
##
|
||||
## ...
|
||||
## # using POD type as response for a synchronous request.
|
||||
## # using native type as response for a synchronous request.
|
||||
## RequestBroker(sync):
|
||||
## type NeedThatInfo = string
|
||||
##
|
||||
@ -220,9 +227,9 @@ proc parseMode(modeNode: NimNode): RequestBrokerMode =
|
||||
## Supported spellings: `sync` / `async` (case-insensitive).
|
||||
let raw = ($modeNode).strip().toLowerAscii()
|
||||
case raw
|
||||
of "sync", "rbsync":
|
||||
of "sync":
|
||||
rbSync
|
||||
of "async", "rbasync":
|
||||
of "async":
|
||||
rbAsync
|
||||
else:
|
||||
error("RequestBroker mode must be `sync` or `async` (default is async)", modeNode)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user