Skip to content
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
14 changes: 11 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "LightBSON"
uuid = "a4a7f996-b3a6-4de6-b9db-2fa5f350df41"
authors = ["Christian Rorvik <christian.rorvik@gmail.com>"]
version = "1.4.0"
version = "1.5.0"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Expand All @@ -20,6 +20,7 @@ WeakRefStrings = "ea10d353-3f73-51f8-a26c-33c1cb351aa5"
DataStructures = "0.18, 0.19"
Dates = "1"
DecFP = "1"
FixedPointDecimals = "0.5, 0.6"
FNVHash = "0.1"
JSON3 = "1"
Sockets = "1"
Expand All @@ -28,10 +29,17 @@ Transducers = "0.4"
UUIDs = "1"
UnsafeArrays = "1"
WeakRefStrings = "1"
julia = "1.8"
julia = "1.9"

[weakdeps]
FixedPointDecimals = "fb4d412d-6eee-574d-9565-ede6634db7b0"

[extensions]
LightBSONFixedPointDecimalsExt = "FixedPointDecimals"

[extras]
FixedPointDecimals = "fb4d412d-6eee-574d-9565-ede6634db7b0"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
test = ["Test", "FixedPointDecimals"]
15 changes: 15 additions & 0 deletions ext/LightBSONFixedPointDecimalsExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module LightBSONFixedPointDecimalsExt

using LightBSON
using FixedPointDecimals
using DecFP

@inline LightBSON.bson_representation_type(::Type{FixedDecimal{T, f}}) where {T, f} = Dec128

@inline LightBSON.bson_representation_convert(::Type{Dec128}, x::FixedDecimal{T, f}) where {T, f} =
Dec128(x.i, -f)

@inline LightBSON.bson_representation_convert(::Type{FixedDecimal{T, f}}, x::Dec128) where {T, f} =
reinterpret(FixedDecimal{T, f}, T(x * Dec128(1, f)))

end
26 changes: 26 additions & 0 deletions test/fixed_decimal_tests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using FixedPointDecimals

@testset "FixedDecimal" begin

@testset "FixedDecimal{Int64, $f} roundtrip" for (f, v) in [(0, 125), (2, 1.25), (4, 1.25)]
buf = empty!(fill(0xff, 1000))
writer = BSONWriter(buf)
x = FixedDecimal{Int64, f}(v)
writer["x"] = x
close(writer)
@test BSONReader(buf, StrictBSONValidator())["x"][Dec128] == Dec128(x.i, -f)
@test BSONReader(buf, StrictBSONValidator())["x"][FixedDecimal{Int64, f}] == x
end

@testset "FixedDecimal precise conversion" begin
x = FixedDecimal{Int64, 2}(1.23)
@test Dec128(x.i, -2) == d128"1.23"
buf = empty!(fill(0xff, 1000))
writer = BSONWriter(buf)
writer["x"] = x
close(writer)
@test BSONReader(buf, StrictBSONValidator())["x"][Dec128] == d128"1.23"
@test BSONReader(buf, StrictBSONValidator())["x"][FixedDecimal{Int64, 2}] == x
end

end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ struct EmptyStruct end
include("object_id_tests.jl")
include("convenience_tests.jl")
include("representation_tests.jl")
include("fixed_decimal_tests.jl")
end
Loading