From 3b4d93adfb2a5272e7e3075d9eff3b36aaee4154 Mon Sep 17 00:00:00 2001 From: Curt Da Silva Date: Tue, 23 Apr 2024 14:29:47 -0700 Subject: [PATCH] fix: Fix missing key-type specifying `LogRecord` constructor fix: Added missing indexing/key-querying methods to `LogRecordData` --- src/records.jl | 9 ++++++--- test/TestLoggingCommon.jl | 5 +++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/records.jl b/src/records.jl index e5b05db..c56502f 100644 --- a/src/records.jl +++ b/src/records.jl @@ -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) @@ -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...) @@ -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...) diff --git a/test/TestLoggingCommon.jl b/test/TestLoggingCommon.jl index d435eb8..2601a28 100644 --- a/test/TestLoggingCommon.jl +++ b/test/TestLoggingCommon.jl @@ -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")