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

DataEncoder encodes bignums as strings #123

Merged
merged 1 commit into from
Feb 10, 2017
Merged
Show file tree
Hide file tree
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
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