Skip to content

Commit

Permalink
make config_json available to providers (#142)
Browse files Browse the repository at this point in the history
* make config_json available to providers

Signed-off-by: stevelr <[email protected]>

* clippy fixes, fix doc test

Signed-off-by: stevelr <[email protected]>

* config_json just return string

Signed-off-by: stevelr <[email protected]>

* rename config_json to config_json_raw. and return &str to avoid copying

Signed-off-by: stevelr <[email protected]>

---------

Signed-off-by: stevelr <[email protected]>
  • Loading branch information
stevelr authored Feb 24, 2023
1 parent cd7b4a6 commit 619b2f9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
4 changes: 2 additions & 2 deletions rpc-rs/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::collections::HashMap;
/// Open content is useful for modeling unstructured data that has no schema, data that can't be
/// modeled using rigid types, or data that has a schema that evolves outside of the purview of a model.
/// The serialization format of a document is an implementation detail of a protocol.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Default)]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub enum Document {
/// object
// n(0)
Expand Down Expand Up @@ -40,7 +40,7 @@ pub enum Document {
/// Open content is useful for modeling unstructured data that has no schema, data that can't be
/// modeled using rigid types, or data that has a schema that evolves outside of the purview of a model.
/// The serialization format of a document is an implementation detail of a protocol.
#[derive(Clone, Debug, PartialEq, Serialize, Default)]
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
pub enum DocumentRef<'v> {
/// object
// n(0)
Expand Down
28 changes: 24 additions & 4 deletions rpc-rs/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,8 @@ pub struct HostBridge {
}

impl HostBridge {
pub(crate) fn new_client(
nats: async_nats::Client,
host_data: &HostData,
) -> RpcResult<HostBridge> {
#[doc(hidden)]
pub fn new_client(nats: async_nats::Client, host_data: &HostData) -> RpcResult<HostBridge> {
let key = Arc::new(if host_data.is_test() {
KeyPair::new_user()
} else {
Expand Down Expand Up @@ -199,6 +197,28 @@ impl HostBridge {
pub fn lattice_prefix(&self) -> &str {
&self.host_data.lattice_rpc_prefix
}

/// Returns the configuration values as a json string.
/// Caller may need to deserialize, and may want to cache the results
/// if the data is large or this method is called frequently.
/// If there is no configuration defined, returns None.
/// ```no_run
/// # #[tokio::main]
/// # async fn main() {
/// # use std::collections::HashMap;
/// # let host_bridge = wasmbus_rpc::provider::HostBridge::new_client(async_nats::connect("demo.nats.io").await.unwrap(), &wasmbus_rpc::core::HostData::default()).unwrap();
/// // Example: deserialize to a hashmap
/// let settings: HashMap<String,String> = if let Some(json) = host_bridge.config_json_raw() {
/// serde_json::from_str(&json).unwrap() // handle error
/// } else {
/// Default::default()
/// };
/// println!("config: {:?}", &settings);
/// # }
/// ```
pub fn config_json_raw(&self) -> Option<&str> {
self.host_data.config_json.as_deref()
}
}

impl Deref for HostBridge {
Expand Down
3 changes: 1 addition & 2 deletions rpc-rs/src/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl RpcClient {
}

/// Send an rpc message using json-encoded data
pub async fn send_json<Target, Arg, Resp>(
pub async fn send_json<Target, Resp>(
&self,
origin: WasmCloudEntity,
target: Target,
Expand All @@ -167,7 +167,6 @@ impl RpcClient {
data: JsonValue,
) -> RpcResult<JsonValue>
where
Arg: DeserializeOwned + Serialize,
Resp: DeserializeOwned + Serialize,
Target: Into<WasmCloudEntity>,
{
Expand Down

0 comments on commit 619b2f9

Please sign in to comment.