Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow parse message data as json #816

Merged
merged 1 commit into from
Jan 24, 2023
Merged
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
14 changes: 12 additions & 2 deletions nats/examples/nats-box/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ enum Command {
#[structopt(name = "pub", about = "Publishes a message to a given subject")]
Pub { subject: String, msg: String },
#[structopt(name = "sub", about = "Subscribes to a given subject")]
Sub { subject: String },
Sub {
subject: String,
#[structopt(long, help = "parse message data as json")]
json: bool,
},
#[structopt(name = "request", about = "Sends a request and waits on reply")]
Request { subject: String, msg: String },
#[structopt(name = "reply", about = "Listens for requests and sends the reply")]
Expand Down Expand Up @@ -57,11 +61,17 @@ fn main() -> CliResult {
nc.publish(&subject, &msg)?;
println!("Published to '{subject}': '{msg}'");
}
Command::Sub { subject } => {
Command::Sub { subject, json } => {
let sub = nc.subscribe(&subject)?;
println!("Listening on '{subject}'");
for msg in sub.messages() {
println!("Received a {msg:?}");
if json {
match serde_json::from_slice::<serde_json::Value>(&msg.data) {
Ok(parsed_msg) => println!("{}", parsed_msg),
Err(e) => eprintln!("failed to parse as json: {e:?}"),
}
}
}
}
Command::Request { subject, msg } => {
Expand Down