From 68d368b5b1a908127fcd4a424c72afd66d2a9a7c Mon Sep 17 00:00:00 2001 From: Michael Ries Date: Wed, 17 Apr 2024 13:24:04 -0700 Subject: [PATCH] treat no streams as an empty list --- lib/gnat/jetstream/api/stream.ex | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/gnat/jetstream/api/stream.ex b/lib/gnat/jetstream/api/stream.ex index 41950de..563379e 100644 --- a/lib/gnat/jetstream/api/stream.ex +++ b/lib/gnat/jetstream/api/stream.ex @@ -405,10 +405,18 @@ defmodule Gnat.Jetstream.API.Stream do }) with {:ok, decoded} <- request(conn, "#{js_api(domain)}.STREAM.NAMES", payload) do + # Recent versions of NATS sometimes return `"streams": null` in their JSON payload to indicate + # that no streams are defined. But, that would mean callers have to handle both `nil` and a list, so + # we coerce that to an empty list to represent no streams being defined. + streams = case Map.get(decoded, "streams") do + nil -> [] + names when is_list(names) -> names + end + result = %{ limit: Map.get(decoded, "limit"), offset: Map.get(decoded, "offset"), - streams: Map.get(decoded, "streams"), + streams: streams, total: Map.get(decoded, "total") }