Skip to content

Commit

Permalink
fix: Fix missing key-type specifying LogRecord constructor (#7)
Browse files Browse the repository at this point in the history
fix: Added missing indexing/key-querying methods to `LogRecordData`
  • Loading branch information
curtd committed Apr 23, 2024
1 parent 1b443cb commit 0729939
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/records.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ struct LogRecordData{K}
LogRecordData{K}() where {K} = new(Dictionary{K, Any}())
end
@define_interface LogRecordData interface=equality
@forward_methods LogRecordData field=data Base.isempty(_) Base.length(_) Base.pairs(_)
@forward_methods LogRecordData field=data Base.isempty(_) Base.length(_) Base.pairs(_) Base.get(_, key, default) Base.haskey(_, key)
Base.eltype(::Type{LogRecordData{K}}) where {K} = Pair{K, Any}
Base.iterate(d::LogRecordData) = iterate(pairs(d.data))
Base.iterate(d::LogRecordData, st) = iterate(pairs(d.data), st)
Base.collect(d::LogRecordData) = collect(pairs(d.data))
Base.@propagate_inbounds Base.getindex(d::LogRecordData, key) = getindex(d.data, key)

"""
add_record_data!(r, data::Pair)
Expand Down Expand Up @@ -124,7 +125,7 @@ log_record_data() = _log_record_data(Symbol, ())

LogRecordData(::Nothing; kwargs...) = _log_record_data(Symbol, (); kwargs...)
LogRecordData(data; kwargs...) = log_record_data(data; kwargs...)
LogRecordData(args::Pair{Symbol, <:Any}...; kwargs...) = _log_record_data(Symbol, args; kwargs...)
LogRecordData(args::Pair...; kwargs...) = log_record_data(args; kwargs...)



Expand Down Expand Up @@ -210,7 +211,9 @@ end

LogRecord(static_meta::StaticLogRecordMetadata, runtime_meta::RuntimeLogRecordMetadata, record::AbstractLogRecord, data::LogRecordData) = LogRecord{typeof(record)}(static_meta, runtime_meta, data, record)

LogRecord(static_meta::StaticLogRecordMetadata, runtime_meta::RuntimeLogRecordMetadata, record::AbstractLogRecord, args::Pair{<:Any, <:Any}...) = LogRecord(static_meta, runtime_meta, record, LogRecordData(args...))
LogRecord(static_meta::StaticLogRecordMetadata, runtime_meta::RuntimeLogRecordMetadata, record::AbstractLogRecord, arg1::Pair{<:Any,<:Any}, args::Pair{<:Any, <:Any}...) = LogRecord(static_meta, runtime_meta, record, log_record_data((arg1, args...)))

LogRecord(static_meta::StaticLogRecordMetadata, runtime_meta::RuntimeLogRecordMetadata, record::AbstractLogRecord, DataKeyType::Type=Symbol) = LogRecord(static_meta, runtime_meta, record, log_record_data(DataKeyType, ()))

LogRecord(static_meta::StaticLogRecordMetadata, record::AbstractLogRecord, args...; runtime_meta::RuntimeLogRecordMetadata=RuntimeLogRecordMetadata()) = LogRecord(static_meta, runtime_meta, record, args...)

Expand Down
5 changes: 5 additions & 0 deletions test/TestLoggingCommon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ module TestLoggingCommon
@Test v == String
end
end
d[:a] == 1
d[:b] == String
@Test haskey(d, :a)
@Test haskey(d, :c) == false
@Test get(d, :c, nothing) |> isnothing
d = log_record_data(("a" => 1, "b" => 2); exclude="a")
@Test collect(d) == ["b" => 2]
@Test d == log_record_data(String, ("a" => 1, "b" => 2); exclude="a")
Expand Down

0 comments on commit 0729939

Please sign in to comment.