diff --git a/Project.toml b/Project.toml index e26314e..1c02a65 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "NATSBridge" uuid = "f2724d33-f338-4a57-b9f8-1be882570d10" -version = "0.4.4" +version = "0.4.5" authors = ["narawat "] [deps] diff --git a/README.md b/README.md index 762bc15..be3f1ce 100644 --- a/README.md +++ b/README.md @@ -249,8 +249,8 @@ env, env_json_str = NATSBridge.smartsend( msg_id::String = string(uuid4()), # Message ID (auto-generated UUID) sender_id::String = string(uuid4()) # Sender ID (auto-generated UUID) ) -# Returns: (msgEnvelope_v1, JSON string) -# - env: msgEnvelope_v1 object with all envelope metadata and payloads +# Returns: ::Tuple{msg_envelope_v1, String} +# - env: msg_envelope_v1 object with all envelope metadata and payloads # - env_json_str: JSON string representation of the envelope for publishing ``` @@ -271,7 +271,7 @@ env = NATSBridge.smartreceive( base_delay::Int = 100, max_delay::Int = 5000 ) -# Returns: Dict with envelope metadata and payloads array +# Returns: ::JSON.Object{String, Any} - key-value structure resemble msg_envelope_v1 ``` ### publish_message diff --git a/docs/architecture.md b/docs/architecture.md index 0c77786..8272384 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -99,11 +99,12 @@ smartsend( broker_url="nats://localhost:4222" ) -# Receive returns a dictionary envelope with all metadata and deserialized payloads +# Receive returns a JSON object envelope with all metadata and deserialized payloads env = smartreceive(msg; fileserver_download_handler=_fetch_with_backoff, max_retries=5, base_delay=100, max_delay=5000) +# Returns: ::JSON.Object{String, Any} - key-value structure resemble msg_envelope_v1 # env["payloads"] = [("dataname1", data1, type1), ("dataname2", data2, type2), ...] # env["correlation_id"], env["msg_id"], etc. -# env is a dictionary containing envelope metadata and payloads field +# env is a JSON object containing envelope metadata and payloads field ``` ## Architecture Diagram diff --git a/docs/implementation.md b/docs/implementation.md index 2f817f8..c9a29e2 100644 --- a/docs/implementation.md +++ b/docs/implementation.md @@ -105,11 +105,12 @@ smartsend( broker_url="nats://localhost:4222" ) -# Receive returns a dictionary envelope with all metadata and deserialized payloads +# Receive returns a JSON object envelope with all metadata and deserialized payloads env = smartreceive(msg; fileserver_download_handler=_fetch_with_backoff, max_retries=5, base_delay=100, max_delay=5000) +# Returns: ::JSON.Object{String, Any} - key-value structure resemble msg_envelope_v1 # env["payloads"] = [("dataname1", data1, type1), ("dataname2", data2, type2), ...] # env["correlation_id"], env["msg_id"], etc. -# env is a dictionary containing envelope metadata and payloads field +# env is a JSON object containing envelope metadata and payloads field ``` ## Architecture @@ -147,6 +148,7 @@ The `smartsend` function now returns a tuple containing both the envelope object ```julia env, env_json_str = smartsend(...) +# Returns: ::Tuple{msg_envelope_v1, String} # env::msg_envelope_v1 - The envelope object with all metadata and payloads # env_json_str::String - JSON string for publishing to NATS ``` @@ -280,8 +282,9 @@ smartsend("/test", [("single_data", mydata, "dictionary")], broker_url="nats://l ```julia using NATSBridge -# Receive returns a dictionary with envelope metadata and payloads field +# Receive returns a JSON object with envelope metadata and payloads field env = smartreceive(msg) +# Returns: ::JSON.Object{String, Any} - key-value structure resemble msg_envelope_v1 # env["payloads"] = [(dataname1, data1, "dictionary"), (dataname2, data2, "table"), ...] ``` diff --git a/examples/tutorial.md b/examples/tutorial.md index 474f867..22ebbdf 100644 --- a/examples/tutorial.md +++ b/examples/tutorial.md @@ -107,6 +107,7 @@ using NATSBridge # Receive and process message env = smartreceive(msg; fileserver_download_handler=_fetch_with_backoff) +# Returns: ::JSON.Object{String, Any} - key-value structure resemble msg_envelope_v1 for (dataname, data, type) in env["payloads"] println("Received $dataname: $data") end diff --git a/examples/walkthrough.md b/examples/walkthrough.md index e74bca0..995134f 100644 --- a/examples/walkthrough.md +++ b/examples/walkthrough.md @@ -163,6 +163,7 @@ end function handle_message(handler::ChatHandler, msg::NATS.Msg) env = smartreceive(msg, fileserver_download_handler=_fetch_with_backoff) + # Returns: ::JSON.Object{String, Any} - key-value structure resemble msg_envelope_v1 # Extract sender info from envelope sender = get(env, "sender_name", "Anonymous") @@ -281,6 +282,7 @@ end function download_file(service::FileDownloadService, msg::NATS.Msg, sender::String, download_id::String) # Subscribe to sender's file channel env = smartreceive(msg, fileserver_download_handler=fetch_from_url) + # Returns: ::JSON.Object{String, Any} - key-value structure resemble msg_envelope_v1 # Process each payload for (dataname, data, type) in env["payloads"] @@ -518,6 +520,7 @@ end function process_reading(receiver::SensorReceiver, msg::NATS.Msg) env = smartreceive(msg, receiver.fileserver_download_handler) + # Returns: ::JSON.Object{String, Any} - key-value structure resemble msg_envelope_v1 for (dataname, data, data_type) in env["payloads"] if data_type == "dictionary" diff --git a/src/NATSBridge.jl b/src/NATSBridge.jl index e409089..e53d388 100644 --- a/src/NATSBridge.jl +++ b/src/NATSBridge.jl @@ -388,7 +388,7 @@ Each payload can have a different type, enabling mixed-content messages (e.g., c - `sender_id::String = string(uuid4())` - Sender ID (auto-generated UUID if not provided) # Return: - - A tuple `(env, env_json_str)` where: + - `::Tuple{msg_envelope_v1, String}` - A tuple containing: - `env::msg_envelope_v1` - The envelope object containing all metadata and payloads - `env_json_str::String` - JSON string representation of the envelope for publishing @@ -447,7 +447,7 @@ function smartsend( NATS_connection::Union{NATS.Connection, Nothing} = nothing, # a provided connection saves establishing connection overhead. msg_id::String = string(uuid4()), # Message ID sender_id::String = string(uuid4()) # Sender ID -) where {T1<:Any} +)::Tuple{msg_envelope_v1, String} where {T1<:Any} # Log start of send operation log_trace(correlation_id, "Starting smartsend for subject: $subject") @@ -754,14 +754,14 @@ A HTTP file server is required along with its download function. - `max_delay::Int = 5000` - Maximum delay for exponential backoff in ms # Return: - - JSON object of envelope with list of (dataname, data, data_type) tuples in payloads field + - `::JSON.Object{String, Any}` - key-value structure resemble msg_envelope_v1 # Example ```jldoctest # Receive and process message msg = nats_message # NATS message -payloads = smartreceive(msg; fileserver_download_handler=_fetch_with_backoff, max_retries=5, base_delay=100, max_delay=5000) -# payloads = [("dataname1", data1, "type1"), ("dataname2", data2, "type2"), ...] +env = smartreceive(msg; fileserver_download_handler=_fetch_with_backoff, max_retries=5, base_delay=100, max_delay=5000) +# env["payloads"] = [("dataname1", data1, "type1"), ("dataname2", data2, "type2"), ...] ``` """ function smartreceive( @@ -770,7 +770,7 @@ function smartreceive( max_retries::Int = 5, base_delay::Int = 100, max_delay::Int = 5000 -) +)::JSON.Object{String, Any} # Parse the JSON envelope env_json_obj = JSON.parse(String(msg.payload)) log_trace(env_json_obj["correlation_id"], "Processing received message") # Log message processing start @@ -818,7 +818,7 @@ function smartreceive( end end env_json_obj["payloads"] = payloads_list - return env_json_obj # JSON object of envelope with list of (dataname, data, data_type) tuples in payloads field + return env_json_obj # key-value structure resemble msg_envelope_v1 end diff --git a/test/test_julia_mix_payloads_sender.jl b/test/test_julia_mix_payloads_sender.jl index 541a4d0..47e1842 100644 --- a/test/test_julia_mix_payloads_sender.jl +++ b/test/test_julia_mix_payloads_sender.jl @@ -186,7 +186,7 @@ function test_mix_send() ] # Use smartsend with mixed content - env, env_json_str = NATSBridge.smartsend( + sendinfo = NATSBridge.smartsend( SUBJECT, payloads; # List of (dataname, data, type) tuples broker_url = NATS_URL, @@ -202,7 +202,8 @@ function test_mix_send() reply_to_msg_id = "", is_publish = true # Publish the message to NATS ) - + + env, env_json_str = sendinfo log_trace("Sent message with $(length(env.payloads)) payloads") # Log transport type for each payload