mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-01-08 00:43: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":
|
test "zero-argument request errors when unset":
|
||||||
let res = waitFor SimpleResponse.request()
|
let res = waitFor SimpleResponse.request()
|
||||||
check res.isErr
|
check res.isErr()
|
||||||
check res.error.contains("no zero-arg provider")
|
check res.error.contains("no zero-arg provider")
|
||||||
|
|
||||||
test "serves input-based providers":
|
test "serves input-based providers":
|
||||||
@ -96,7 +96,7 @@ suite "RequestBroker macro (async mode)":
|
|||||||
|
|
||||||
test "input request errors when unset":
|
test "input request errors when unset":
|
||||||
let res = waitFor KeyedResponse.request("foo", 2)
|
let res = waitFor KeyedResponse.request("foo", 2)
|
||||||
check res.isErr
|
check res.isErr()
|
||||||
check res.error.contains("input signature")
|
check res.error.contains("input signature")
|
||||||
|
|
||||||
test "supports both provider types simultaneously":
|
test "supports both provider types simultaneously":
|
||||||
@ -115,11 +115,11 @@ suite "RequestBroker macro (async mode)":
|
|||||||
.isOk()
|
.isOk()
|
||||||
|
|
||||||
let noInput = waitFor DualResponse.request()
|
let noInput = waitFor DualResponse.request()
|
||||||
check noInput.isOk
|
check noInput.isOk()
|
||||||
check noInput.value.note == "base"
|
check noInput.value.note == "base"
|
||||||
|
|
||||||
let withInput = waitFor DualResponse.request("-extra")
|
let withInput = waitFor DualResponse.request("-extra")
|
||||||
check withInput.isOk
|
check withInput.isOk()
|
||||||
check withInput.value.note == "base-extra"
|
check withInput.value.note == "base-extra"
|
||||||
check withInput.value.count == 6
|
check withInput.value.count == 6
|
||||||
|
|
||||||
@ -135,7 +135,7 @@ suite "RequestBroker macro (async mode)":
|
|||||||
DualResponse.clearProvider()
|
DualResponse.clearProvider()
|
||||||
|
|
||||||
let res = waitFor DualResponse.request()
|
let res = waitFor DualResponse.request()
|
||||||
check res.isErr
|
check res.isErr()
|
||||||
|
|
||||||
test "implicit zero-argument provider works by default":
|
test "implicit zero-argument provider works by default":
|
||||||
check ImplicitResponse
|
check ImplicitResponse
|
||||||
@ -146,14 +146,14 @@ suite "RequestBroker macro (async mode)":
|
|||||||
.isOk()
|
.isOk()
|
||||||
|
|
||||||
let res = waitFor ImplicitResponse.request()
|
let res = waitFor ImplicitResponse.request()
|
||||||
check res.isOk
|
check res.isOk()
|
||||||
|
|
||||||
ImplicitResponse.clearProvider()
|
ImplicitResponse.clearProvider()
|
||||||
check res.value.note == "auto"
|
check res.value.note == "auto"
|
||||||
|
|
||||||
test "implicit zero-argument request errors when unset":
|
test "implicit zero-argument request errors when unset":
|
||||||
let res = waitFor ImplicitResponse.request()
|
let res = waitFor ImplicitResponse.request()
|
||||||
check res.isErr
|
check res.isErr()
|
||||||
check res.error.contains("no zero-arg provider")
|
check res.error.contains("no zero-arg provider")
|
||||||
|
|
||||||
test "no provider override":
|
test "no provider override":
|
||||||
@ -177,7 +177,7 @@ suite "RequestBroker macro (async mode)":
|
|||||||
check DualResponse.setProvider(overrideProc).isErr()
|
check DualResponse.setProvider(overrideProc).isErr()
|
||||||
|
|
||||||
let noInput = waitFor DualResponse.request()
|
let noInput = waitFor DualResponse.request()
|
||||||
check noInput.isOk
|
check noInput.isOk()
|
||||||
check noInput.value.note == "base"
|
check noInput.value.note == "base"
|
||||||
|
|
||||||
let stillResponse = waitFor DualResponse.request(" still works")
|
let stillResponse = waitFor DualResponse.request(" still works")
|
||||||
@ -197,7 +197,7 @@ suite "RequestBroker macro (async mode)":
|
|||||||
check DualResponse.setProvider(overrideProc).isOk()
|
check DualResponse.setProvider(overrideProc).isOk()
|
||||||
|
|
||||||
let nowSuccWithOverride = waitFor DualResponse.request()
|
let nowSuccWithOverride = waitFor DualResponse.request()
|
||||||
check nowSuccWithOverride.isOk
|
check nowSuccWithOverride.isOk()
|
||||||
check nowSuccWithOverride.value.note == "something else"
|
check nowSuccWithOverride.value.note == "something else"
|
||||||
check nowSuccWithOverride.value.count == 1
|
check nowSuccWithOverride.value.count == 1
|
||||||
|
|
||||||
@ -259,7 +259,7 @@ suite "RequestBroker macro (sync mode)":
|
|||||||
|
|
||||||
test "zero-argument request errors when unset (sync)":
|
test "zero-argument request errors when unset (sync)":
|
||||||
let res = SimpleResponseSync.request()
|
let res = SimpleResponseSync.request()
|
||||||
check res.isErr
|
check res.isErr()
|
||||||
check res.error.contains("no zero-arg provider")
|
check res.error.contains("no zero-arg provider")
|
||||||
|
|
||||||
test "serves input-based providers (sync)":
|
test "serves input-based providers (sync)":
|
||||||
@ -296,7 +296,7 @@ suite "RequestBroker macro (sync mode)":
|
|||||||
|
|
||||||
test "input request errors when unset (sync)":
|
test "input request errors when unset (sync)":
|
||||||
let res = KeyedResponseSync.request("foo", 2)
|
let res = KeyedResponseSync.request("foo", 2)
|
||||||
check res.isErr
|
check res.isErr()
|
||||||
check res.error.contains("input signature")
|
check res.error.contains("input signature")
|
||||||
|
|
||||||
test "supports both provider types simultaneously (sync)":
|
test "supports both provider types simultaneously (sync)":
|
||||||
@ -315,11 +315,11 @@ suite "RequestBroker macro (sync mode)":
|
|||||||
.isOk()
|
.isOk()
|
||||||
|
|
||||||
let noInput = DualResponseSync.request()
|
let noInput = DualResponseSync.request()
|
||||||
check noInput.isOk
|
check noInput.isOk()
|
||||||
check noInput.value.note == "base"
|
check noInput.value.note == "base"
|
||||||
|
|
||||||
let withInput = DualResponseSync.request("-extra")
|
let withInput = DualResponseSync.request("-extra")
|
||||||
check withInput.isOk
|
check withInput.isOk()
|
||||||
check withInput.value.note == "base-extra"
|
check withInput.value.note == "base-extra"
|
||||||
check withInput.value.count == 6
|
check withInput.value.count == 6
|
||||||
|
|
||||||
@ -335,7 +335,7 @@ suite "RequestBroker macro (sync mode)":
|
|||||||
DualResponseSync.clearProvider()
|
DualResponseSync.clearProvider()
|
||||||
|
|
||||||
let res = DualResponseSync.request()
|
let res = DualResponseSync.request()
|
||||||
check res.isErr
|
check res.isErr()
|
||||||
|
|
||||||
test "implicit zero-argument provider works by default (sync)":
|
test "implicit zero-argument provider works by default (sync)":
|
||||||
check ImplicitResponseSync
|
check ImplicitResponseSync
|
||||||
@ -346,14 +346,14 @@ suite "RequestBroker macro (sync mode)":
|
|||||||
.isOk()
|
.isOk()
|
||||||
|
|
||||||
let res = ImplicitResponseSync.request()
|
let res = ImplicitResponseSync.request()
|
||||||
check res.isOk
|
check res.isOk()
|
||||||
|
|
||||||
ImplicitResponseSync.clearProvider()
|
ImplicitResponseSync.clearProvider()
|
||||||
check res.value.note == "auto"
|
check res.value.note == "auto"
|
||||||
|
|
||||||
test "implicit zero-argument request errors when unset (sync)":
|
test "implicit zero-argument request errors when unset (sync)":
|
||||||
let res = ImplicitResponseSync.request()
|
let res = ImplicitResponseSync.request()
|
||||||
check res.isErr
|
check res.isErr()
|
||||||
check res.error.contains("no zero-arg provider")
|
check res.error.contains("no zero-arg provider")
|
||||||
|
|
||||||
test "implicit zero-argument provider raises error (sync)":
|
test "implicit zero-argument provider raises error (sync)":
|
||||||
@ -365,7 +365,7 @@ suite "RequestBroker macro (sync mode)":
|
|||||||
.isOk()
|
.isOk()
|
||||||
|
|
||||||
let res = ImplicitResponseSync.request()
|
let res = ImplicitResponseSync.request()
|
||||||
check res.isErr
|
check res.isErr()
|
||||||
check res.error.contains("simulated failure")
|
check res.error.contains("simulated failure")
|
||||||
|
|
||||||
ImplicitResponseSync.clearProvider()
|
ImplicitResponseSync.clearProvider()
|
||||||
@ -484,15 +484,15 @@ suite "RequestBroker macro (POD/external types)":
|
|||||||
|
|
||||||
let resA = DistinctStringResponseA.request()
|
let resA = DistinctStringResponseA.request()
|
||||||
let resB = DistinctStringResponseB.request()
|
let resB = DistinctStringResponseB.request()
|
||||||
check resA.isOk
|
check resA.isOk()
|
||||||
check resB.isOk
|
check resB.isOk()
|
||||||
check string(resA.value) == "a"
|
check string(resA.value) == "a"
|
||||||
check string(resB.value) == "b"
|
check string(resB.value) == "b"
|
||||||
|
|
||||||
let resEA = ExternalDistinctResponseA.request()
|
let resEA = ExternalDistinctResponseA.request()
|
||||||
let resEB = ExternalDistinctResponseB.request()
|
let resEB = ExternalDistinctResponseB.request()
|
||||||
check resEA.isOk
|
check resEA.isOk()
|
||||||
check resEB.isOk
|
check resEB.isOk()
|
||||||
check ExternalDefinedTypeShared(resEA.value).label == "ea"
|
check ExternalDefinedTypeShared(resEA.value).label == "ea"
|
||||||
check ExternalDefinedTypeShared(resEB.value).label == "eb"
|
check ExternalDefinedTypeShared(resEB.value).label == "eb"
|
||||||
|
|
||||||
|
|||||||
@ -6,8 +6,15 @@
|
|||||||
## Worth considering using it in a single provider, many requester scenario.
|
## Worth considering using it in a single provider, many requester scenario.
|
||||||
##
|
##
|
||||||
## Provides a declarative way to define an immutable value type together with a
|
## Provides a declarative way to define an immutable value type together with a
|
||||||
## thread-local broker that can register an asynchronous provider, dispatch typed
|
## thread-local broker that can register an asynchronous or synchronous provider,
|
||||||
## requests and clear 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:
|
## Usage:
|
||||||
## Declare your desired request type inside a `RequestBroker` macro, add any number of fields.
|
## 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]
|
## 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
|
## type (i.e. not an inline `object` / `ref object` definition), RequestBroker
|
||||||
## will wrap it in `distinct` automatically unless you already used `distinct`.
|
## will wrap it in `distinct` automatically unless you already used `distinct`.
|
||||||
## This avoids overload ambiguity when multiple brokers share the same
|
## 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")`
|
## - construct values with an explicit cast/constructor, e.g. `MyType("x")`
|
||||||
## - unwrap with a cast when needed, e.g. `string(myVal)` or `BaseType(myVal)`
|
## - unwrap with a cast when needed, e.g. `string(myVal)` or `BaseType(myVal)`
|
||||||
##
|
##
|
||||||
## Example (POD response type):
|
## Example (native response type):
|
||||||
## ```nim
|
## ```nim
|
||||||
## RequestBroker(sync):
|
## RequestBroker(sync):
|
||||||
## type MyCount = int # exported as: `distinct int`
|
## 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):
|
## RequestBroker(sync):
|
||||||
## type NeedThatInfo = string
|
## type NeedThatInfo = string
|
||||||
##
|
##
|
||||||
@ -220,9 +227,9 @@ proc parseMode(modeNode: NimNode): RequestBrokerMode =
|
|||||||
## Supported spellings: `sync` / `async` (case-insensitive).
|
## Supported spellings: `sync` / `async` (case-insensitive).
|
||||||
let raw = ($modeNode).strip().toLowerAscii()
|
let raw = ($modeNode).strip().toLowerAscii()
|
||||||
case raw
|
case raw
|
||||||
of "sync", "rbsync":
|
of "sync":
|
||||||
rbSync
|
rbSync
|
||||||
of "async", "rbasync":
|
of "async":
|
||||||
rbAsync
|
rbAsync
|
||||||
else:
|
else:
|
||||||
error("RequestBroker mode must be `sync` or `async` (default is async)", modeNode)
|
error("RequestBroker mode must be `sync` or `async` (default is async)", modeNode)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user