From 543cd3c9336e790c9105ed3e72d9b384be4a8e13 Mon Sep 17 00:00:00 2001 From: quinnj Date: Mon, 24 Sep 2018 22:03:13 -0600 Subject: [PATCH] Add unweakref to help sinks that can't support WeakRefStrings to convert them into Strings --- src/Tables.jl | 2 ++ src/namedtuples.jl | 15 ++++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Tables.jl b/src/Tables.jl index 36b0b2b..cc52d89 100644 --- a/src/Tables.jl +++ b/src/Tables.jl @@ -20,6 +20,8 @@ function __init__() using .WeakRefStrings allocatecolumn(::Type{WeakRefString{T}}, rows) where {T} = StringVector(rows) allocatecolumn(::Type{Union{Missing, WeakRefString{T}}}, rows) where {T} = StringVector{Union{Missing, String}}(rows) + unweakref(wk::WeakRefString) = string(wk) + unweakreftype(::Type{<:WeakRefString}) = String end @require IteratorInterfaceExtensions="82899510-4779-5014-852e-03e436cf321d" begin using .IteratorInterfaceExtensions diff --git a/src/namedtuples.jl b/src/namedtuples.jl index a1fedbf..e6d0024 100644 --- a/src/namedtuples.jl +++ b/src/namedtuples.jl @@ -1,6 +1,11 @@ # Vector of NamedTuples const RowTable{T} = Vector{T} where {T <: NamedTuple} +# RowTables can't support WeakRefStrings +unweakref(x) = x +unweakreftype(T) = T +Base.@pure unweakreftypes(::Type{T}) where {T <: Tuple} = Tuple{Any[unweakreftype(fieldtype(T, i)) for i = 1:fieldcount(T)]...} + # interface implementation istable(::Type{<:RowTable}) = true rowaccess(::Type{<:RowTable}) = true @@ -13,25 +18,25 @@ struct NamedTupleIterator{S, T} x::T end Base.IteratorEltype(::Type{<:NamedTupleIterator{S}}) where {S} = S === nothing ? Base.EltypeUnknown() : Base.HasEltype() -Base.eltype(rows::NamedTupleIterator{Schema{names, T}}) where {names, T} = NamedTuple{names, T} +Base.eltype(rows::NamedTupleIterator{Schema{names, T}}) where {names, T} = NamedTuple{names, unweakreftypes(T)} Base.IteratorSize(::Type{NamedTupleIterator{S, T}}) where {S, T} = Base.IteratorSize(T) Base.length(nt::NamedTupleIterator) = length(nt.x) Base.size(nt::NamedTupleIterator) = (length(nt.x),) function Base.iterate(rows::NamedTupleIterator{Schema{names, T}}, st=()) where {names, T} if @generated - vals = Tuple(:(getproperty(row, $(fieldtype(T, i)), $i, $(Meta.QuoteNode(names[i])))) for i = 1:fieldcount(T)) + vals = Tuple(:(unweakref(getproperty(row, $(fieldtype(T, i)), $i, $(Meta.QuoteNode(names[i]))))) for i = 1:fieldcount(T)) return quote x = iterate(rows.x, st...) x === nothing && return nothing row, st = x - return $(NamedTuple{names, T})(($(vals...),)), (st,) + return $(NamedTuple{names, unweakreftypes(T)})(($(vals...),)), (st,) end else x = iterate(rows.x, st...) x === nothing && return nothing row, st = x - return NamedTuple{names, T}(Tuple(getproperty(row, fieldtype(T, i), i, nm) for i = 1:fieldcount(T))), (st,) + return NamedTuple{names, unweakreftypes(T)}(Tuple(unweakref(getproperty(row, fieldtype(T, i), i, nm)) for i = 1:fieldcount(T))), (st,) end end @@ -41,7 +46,7 @@ function Base.iterate(rows::NamedTupleIterator{Nothing, T}, st=()) where {T} x === nothing && return nothing row, st = x names = Tuple(propertynames(row)) - return NamedTuple{names}(Tuple(getproperty(row, nm) for nm in names)), (st,) + return NamedTuple{names}(Tuple(unweakref(getproperty(row, nm)) for nm in names)), (st,) end namedtupleiterator(::Type{T}, rows::S) where {T <: NamedTuple, S} = rows