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

UUID byte order #34

Merged
merged 2 commits into from
Oct 19, 2024
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
2 changes: 1 addition & 1 deletion 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 <[email protected]>"]
version = "0.2.21"
version = "1.0.0"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Expand Down
8 changes: 4 additions & 4 deletions src/reader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ end
)
len = load_bits_(Int32, p)
len != 16 && error("Unexpected UUID length $len")
return unsafe_load(Ptr{UUID}(p + 5))
return UUID(ntoh(unsafe_load(Ptr{UInt128}(p + 5))))
end
end

Expand All @@ -255,7 +255,7 @@ end
subtype == BSON_SUBTYPE_UUID_OLD || throw(BSONConversionError(reader.type, subtype, BSONUUIDOld))
len = load_bits_(Int32, p)
len != 16 && error("Unexpected UUID length $len")
return BSONUUIDOld(unsafe_load(Ptr{UUID}(p + 5)))
return BSONUUIDOld(UUID(ntoh(unsafe_load(Ptr{UInt128}(p + 5)))))
end
end

Expand Down Expand Up @@ -439,9 +439,9 @@ function read_field_(reader::AbstractBSONReader, ::Type{Any})
x = reader[BSONBinary]
data = x.data
if x.subtype == BSON_SUBTYPE_UUID && length(data) == 16
GC.@preserve data unsafe_load(Ptr{UUID}(pointer(data)))
GC.@preserve data UUID(ntoh(unsafe_load(Ptr{UInt128}(pointer(data)))))
elseif x.subtype == BSON_SUBTYPE_UUID_OLD && length(data) == 16
GC.@preserve data BSONUUIDOld(unsafe_load(Ptr{UUID}(pointer(data))))
GC.@preserve data BSONUUIDOld(UUID(ntoh(unsafe_load(Ptr{UInt128}(pointer(data))))))
else
x
end
Expand Down
4 changes: 2 additions & 2 deletions src/writer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ end
@inline function wire_store_(p::Ptr{UInt8}, x::UUID)
unsafe_store!(Ptr{Int32}(p), Int32(16))
unsafe_store!(p + 4, BSON_SUBTYPE_UUID)
unsafe_store!(Ptr{UUID}(p + 5), x)
unsafe_store!(Ptr{UInt128}(p + 5), hton(UInt128(x.value)))
end

@inline function wire_store_(p::Ptr{UInt8}, x::BSONUUIDOld)
unsafe_store!(Ptr{Int32}(p), Int32(16))
unsafe_store!(p + 4, BSON_SUBTYPE_UUID_OLD)
unsafe_store!(Ptr{UUID}(p + 5), x.value)
unsafe_store!(Ptr{UInt128}(p + 5), hton(UInt128(x.value)))
end

@inline function wire_store_(p::Ptr{UInt8}, x::Union{BSONBinary, UnsafeBSONBinary})
Expand Down
2 changes: 1 addition & 1 deletion test/reader_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ end
io = IOBuffer()
write(io, htol(Int32(16)))
write(io, BSON_SUBTYPE_UUID)
unsafe_write(io, Ref(x), 16)
unsafe_write(io, Ref(hton(UInt128(x))), 16)
reader = BSONReader(single_field_doc_(BSON_TYPE_BINARY, take!(io)))
@test reader["x"][UUID] == x
@test reader["x"][Any] == x
Expand Down
Loading