Skip to content

Commit

Permalink
UUID byte order (#34)
Browse files Browse the repository at this point in the history
* Fix UUID byte order in accordance with RFC4122

* bump version to 1.0.0
  • Loading branch information
ancapdev authored Oct 19, 2024
1 parent a8df966 commit e741085
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
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

2 comments on commit e741085

@ancapdev
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/117628

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.0.0 -m "<description of version>" e7410855435a045db3acb4a34dd7f42444fa24a5
git push origin v1.0.0

Please sign in to comment.