Skip to content

Commit

Permalink
Pass conversions and validator through convenience layer
Browse files Browse the repository at this point in the history
  • Loading branch information
ancapdev committed Sep 25, 2024
1 parent b867510 commit 6c6591c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
30 changes: 15 additions & 15 deletions src/convenience.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,50 @@
Read an object from the BSON document encoded in `src`.
"""
bson_read(::Type{T}, bytes::DenseVector{UInt8}) where T = BSONReader(bytes)[T]
bson_read(::Type{T}, bytes::DenseVector{UInt8}; kwargs...) where T = BSONReader(bytes; kwargs...)[T]

"""
bson_read([T=Any], path::AbstractString)
Read an object from the BSON document at `path`.
"""
bson_read(::Type{T}, path::AbstractString) where T = bson_read(T, read(path))
bson_read(::Type{T}, path::AbstractString; kwargs...) where T = bson_read(T, read(path); kwargs...)

function bson_read(::Type{T}, io::IO) where T
function bson_read(::Type{T}, io::IO; kwargs...) where T
buf = UInt8[]
readbytes!(io, buf, typemax(Int))
bson_read(T, buf)
bson_read(T, buf; kwargs...)
end

bson_read(bytes::DenseVector{UInt8}) = bson_read(Any, bytes)
bson_read(path::AbstractString) = bson_read(Any, path)
bson_read(io::IO) = bson_read(Any, io)
bson_read(bytes::DenseVector{UInt8}; kwargs...) = bson_read(Any, bytes; kwargs...)
bson_read(path::AbstractString; kwargs...) = bson_read(Any, path; kwargs...)
bson_read(io::IO; kwargs...) = bson_read(Any, io; kwargs...)

"""
bson_write(dst::Union{IO, DenseVector{UInt8}}, x)
bson_write(dst::Union{IO, DenseVector{UInt8}}, xs::Pair...)
Encode `x` or each element of `xs` as a BSON document in `dst` and return `dst`.
"""
function bson_write(buf::DenseVector{UInt8}, x)
writer = BSONWriter(buf)
function bson_write(buf::DenseVector{UInt8}, x; kwargs...)
writer = BSONWriter(buf; kwargs...)
writer[] = x
close(writer)
buf
end

function bson_write(buf::DenseVector{UInt8}, xs::Pair...)
writer = BSONWriter(buf)
function bson_write(buf::DenseVector{UInt8}, xs::Pair...; kwargs...)
writer = BSONWriter(buf; kwargs...)
for (k, v) in xs
writer[k] = v
end
close(writer)
buf
end

function bson_write(io::IO, x...)
function bson_write(io::IO, x...; kwargs...)
buf = UInt8[]
bson_write(buf, x...)
bson_write(buf, x...; kwargs...)
write(io, buf)
io
end
Expand All @@ -57,9 +57,9 @@ end
Encode `x` or each element of `xs` as fields of a BSON document and write to `path`.
"""
function bson_write(path::AbstractString, x...)
function bson_write(path::AbstractString, x...; kwargs...)
open(path, "w") do io
bson_write(io, x...)
bson_write(io, x...; kwargs...)
end
nothing
end
19 changes: 16 additions & 3 deletions src/reader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,29 @@ struct BSONReader{S <: DenseVector{UInt8}, V <: BSONValidator, C <: BSONConversi
end

@inline function BSONReader(
src::DenseVector{UInt8},
src::DenseVector{UInt8};
validator::BSONValidator = LightBSONValidator(),
conversions::BSONConversionRules = DefaultBSONConversions()
conversions::BSONConversionRules = DefaultBSONConversions(),
)
validate_root(validator, src)
BSONReader(src, 0, BSON_TYPE_DOCUMENT, validator, conversions)
end

# For back compat
@inline BSONReader(
src::DenseVector{UInt8},
validator::BSONValidator,
conversions::BSONConversionRules
) = BSONReader(src; validator, conversions)

# For back compat
@inline BSONReader(src::DenseVector{UInt8}, validator::BSONValidator) = BSONReader(
src; validator
)

# For back compat
@inline BSONReader(src::DenseVector{UInt8}, conversions::BSONConversionRules) = BSONReader(
src, LightBSONValidator(), conversions
src; conversions
)

@inline Base.pointer(reader::BSONReader) = pointer(reader.src) + reader.offset
Expand Down
7 changes: 6 additions & 1 deletion src/writer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ struct BSONWriter{D <: DenseVector{UInt8}, C <: BSONConversionRules}
conversions::C

function BSONWriter(
dst::D,
dst::D;
conversions::C = DefaultBSONConversions()
) where {D <: DenseVector{UInt8}, C <: BSONConversionRules}
offset = length(dst)
Expand All @@ -13,6 +13,11 @@ struct BSONWriter{D <: DenseVector{UInt8}, C <: BSONConversionRules}
end
end

# For back compat
@inline BSONWriter(dst::DenseVector{UInt8}, conversions::BSONConversionRules) = BSONWriter(
dst; conversions
)

function Base.close(writer::BSONWriter)
dst = writer.dst
push!(dst, 0x0)
Expand Down

0 comments on commit 6c6591c

Please sign in to comment.