Skip to content

Commit

Permalink
DataEncoder encodes bignums as strings
Browse files Browse the repository at this point in the history
Closes #88.
  • Loading branch information
jeffkreeftmeijer committed Feb 10, 2017
1 parent ccb0788 commit b34b686
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
6 changes: 6 additions & 0 deletions lib/appsignal/utils/data_encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ defmodule Appsignal.Utils.DataEncoder do
def encode(resource, {key, value}) when is_binary(value) do
Nif.data_set_string(resource, key, value)
end
def encode(resource, {key, value}) when is_integer(value) and value >= 9223372036854775808 do
Nif.data_set_string(resource, key, "bigint:#{value}")
end
def encode(resource, {key, value}) when is_integer(value) do
Nif.data_set_integer(resource, key, value)
end
Expand Down Expand Up @@ -58,6 +61,9 @@ defmodule Appsignal.Utils.DataEncoder do
def encode(resource, value) when is_binary(value) do
Nif.data_set_string(resource, value)
end
def encode(resource, value) when is_integer(value) and value >= 9223372036854775808 do
Nif.data_set_string(resource, "bigint:#{value}")
end
def encode(resource, value) when is_integer(value) do
Nif.data_set_integer(resource, value)
end
Expand Down
22 changes: 18 additions & 4 deletions test/appsignal/utils/data_encoder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,15 @@ defmodule Appsignal.Utils.DataEncoderTest do
end

test "encode a map with an integer value" do
resource = DataEncoder.encode(%{foo: 1})
assert {:ok, '{"foo":1}'} == Nif.data_to_json(resource)
resource = DataEncoder.encode(%{foo: 9223372036854775807})
assert {:ok, '{"foo":9223372036854775807}'} == Nif.data_to_json(resource)
end

test "encode a map with an integer too big for C-lang longs to fit" do
resource = DataEncoder.encode(%{foo: 9223372036854775808})
assert {:ok, '{"foo":"bigint:9223372036854775808"}'} == Nif.data_to_json(resource)
resource = DataEncoder.encode(%{foo: 9223372036854775809})
assert {:ok, '{"foo":"bigint:9223372036854775809"}'} == Nif.data_to_json(resource)
end

test "encode a map with a float value" do
Expand Down Expand Up @@ -102,8 +109,15 @@ defmodule Appsignal.Utils.DataEncoderTest do
end

test "encode a list with an integer item" do
resource = DataEncoder.encode([1])
assert {:ok, '[1]'} == Nif.data_to_json(resource)
resource = DataEncoder.encode([9223372036854775807])
assert {:ok, '[9223372036854775807]'} == Nif.data_to_json(resource)
end

test "encode a list with an integer item too big for C-lang longs to fit" do
resource = DataEncoder.encode([9223372036854775808])
assert {:ok, '["bigint:9223372036854775808"]'} == Nif.data_to_json(resource)
resource = DataEncoder.encode([9223372036854775809])
assert {:ok, '["bigint:9223372036854775809"]'} == Nif.data_to_json(resource)
end

test "encode a list with an float item" do
Expand Down

0 comments on commit b34b686

Please sign in to comment.