diff --git a/Project.toml b/Project.toml index 0c84a51..4d62a41 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "LightBSON" uuid = "a4a7f996-b3a6-4de6-b9db-2fa5f350df41" authors = ["Christian Rorvik "] -version = "1.4.0" +version = "1.5.0" [deps] DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" @@ -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" @@ -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"] diff --git a/ext/LightBSONFixedPointDecimalsExt.jl b/ext/LightBSONFixedPointDecimalsExt.jl new file mode 100644 index 0000000..135f748 --- /dev/null +++ b/ext/LightBSONFixedPointDecimalsExt.jl @@ -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 diff --git a/test/fixed_decimal_tests.jl b/test/fixed_decimal_tests.jl new file mode 100644 index 0000000..b232863 --- /dev/null +++ b/test/fixed_decimal_tests.jl @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index f07b703..132bedc 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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