Skip to content
Merged
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
13 changes: 8 additions & 5 deletions src/writer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ struct BSONWriter{D <: DenseVector{UInt8}, C <: BSONConversionRules}
dst::D
offset::Int
conversions::C
skipnull::Bool

function BSONWriter(
dst::D;
conversions::C = DefaultBSONConversions()
conversions::C = DefaultBSONConversions(),
skipnull::Bool = false,
) where {D <: DenseVector{UInt8}, C <: BSONConversionRules}
offset = length(dst)
resize!(dst, offset + 4)
new{D, C}(dst, offset, conversions)
new{D, C}(dst, offset, conversions, skipnull)
end
end

Expand Down Expand Up @@ -133,7 +135,7 @@ function write_field_(writer::BSONWriter, value::BSONCodeWithScope, name::Union{
GC.@preserve dst begin
p = pointer(dst) + offset
wire_store_(p + 4, value.code)
mappings_writer = BSONWriter(dst, writer.conversions)
mappings_writer = BSONWriter(dst; writer.conversions, writer.skipnull)
mappings_writer[] = value.mappings
close(mappings_writer)
p = pointer(dst) + offset
Expand All @@ -145,7 +147,7 @@ end
function write_field_(writer::BSONWriter, generator::Function, name::Union{String, Symbol})
dst = writer.dst
write_header_(dst, BSON_TYPE_DOCUMENT, name, 0)
element_writer = BSONWriter(dst, writer.conversions)
element_writer = BSONWriter(dst; writer.conversions, writer.skipnull)
generator(element_writer)
close(element_writer)
end
Expand All @@ -155,7 +157,7 @@ const SMALL_INDEX_STRINGS = [string(i) for i in 0:99]
function write_field_(writer::BSONWriter, values::Union{AbstractVector, Base.Generator}, name::Union{String, Symbol})
dst = writer.dst
write_header_(dst, BSON_TYPE_ARRAY, name, 0)
element_writer = BSONWriter(dst, writer.conversions)
element_writer = BSONWriter(dst; writer.conversions, writer.skipnull)
element_writer[] = values
close(element_writer)
end
Expand All @@ -177,6 +179,7 @@ end
end

@inline function Base.setindex!(writer::BSONWriter, value::T, name::Union{String, Symbol}) where T
value === nothing && writer.skipnull && return nothing
RT = bson_representation_type(writer.conversions, T)
if RT != T
write_field_(writer, bson_representation_convert(writer.conversions, RT, value), name)
Expand Down
Loading