Skip to content

Commit

Permalink
Handle empty strings in tag/field values (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
burmajam authored Jul 11, 2023
1 parent 9a01e7c commit f038f6f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.11.4

- Handle empty strings in tag/field values

## v0.11.3

- Handle nils, atoms, lists and maps in tag/field keys/values
Expand Down
15 changes: 13 additions & 2 deletions lib/fluxter/packet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ defmodule Fluxter.Packet do
defp encode_key(nil), do: "nil"

defp encode_key(val) do
to_string(val) |> escape(' ,')
val
|> to_string()
|> String.trim()
|> case do
"" -> "empty"
other -> to_string(other) |> escape(' ,')
end
end

defp encode_value(nil), do: inspect("nil")
Expand All @@ -67,7 +73,12 @@ defmodule Fluxter.Packet do
Atom.to_string(val)

is_binary(val) ->
[?\", escape(val, '"'), ?\"]
val
|> String.trim()
|> case do
"" -> "empty"
other -> [?\", escape(other, '"'), ?\"]
end

true ->
val
Expand Down
20 changes: 20 additions & 0 deletions test/fluxter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,26 @@ defmodule FluxterTest do
assert_receive {:echo, "foo,bar=baz,qux=baz value=\"nil\""}
end

test "empty string tag" do
TestFluxter.write("foo", [bar: "baz", qux: "baz", empty: ""], 0)
assert_receive {:echo, "foo,bar=baz,empty=empty,qux=baz value=0i"}
end

test "only spaces in tag" do
TestFluxter.write("foo", [bar: "baz", qux: "baz", empty: " "], 0)
assert_receive {:echo, "foo,bar=baz,empty=empty,qux=baz value=0i"}
end

test "empty string field" do
TestFluxter.write("foo", "")
assert_receive {:echo, "foo value=empty"}
end

test "only spaces in field" do
TestFluxter.write("foo", " ")
assert_receive {:echo, "foo value=empty"}
end

test "atom field" do
TestFluxter.write("foo", [bar: "baz", qux: "baz"], :atom)
assert_receive {:echo, "foo,bar=baz,qux=baz value=\":atom\""}
Expand Down

0 comments on commit f038f6f

Please sign in to comment.