Skip to content

Commit 543cd3c

Browse files
committed
Add unweakref to help sinks that can't support WeakRefStrings to convert them into Strings
1 parent 1e2e6cd commit 543cd3c

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/Tables.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ function __init__()
2020
using .WeakRefStrings
2121
allocatecolumn(::Type{WeakRefString{T}}, rows) where {T} = StringVector(rows)
2222
allocatecolumn(::Type{Union{Missing, WeakRefString{T}}}, rows) where {T} = StringVector{Union{Missing, String}}(rows)
23+
unweakref(wk::WeakRefString) = string(wk)
24+
unweakreftype(::Type{<:WeakRefString}) = String
2325
end
2426
@require IteratorInterfaceExtensions="82899510-4779-5014-852e-03e436cf321d" begin
2527
using .IteratorInterfaceExtensions

src/namedtuples.jl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Vector of NamedTuples
22
const RowTable{T} = Vector{T} where {T <: NamedTuple}
33

4+
# RowTables can't support WeakRefStrings
5+
unweakref(x) = x
6+
unweakreftype(T) = T
7+
Base.@pure unweakreftypes(::Type{T}) where {T <: Tuple} = Tuple{Any[unweakreftype(fieldtype(T, i)) for i = 1:fieldcount(T)]...}
8+
49
# interface implementation
510
istable(::Type{<:RowTable}) = true
611
rowaccess(::Type{<:RowTable}) = true
@@ -13,25 +18,25 @@ struct NamedTupleIterator{S, T}
1318
x::T
1419
end
1520
Base.IteratorEltype(::Type{<:NamedTupleIterator{S}}) where {S} = S === nothing ? Base.EltypeUnknown() : Base.HasEltype()
16-
Base.eltype(rows::NamedTupleIterator{Schema{names, T}}) where {names, T} = NamedTuple{names, T}
21+
Base.eltype(rows::NamedTupleIterator{Schema{names, T}}) where {names, T} = NamedTuple{names, unweakreftypes(T)}
1722
Base.IteratorSize(::Type{NamedTupleIterator{S, T}}) where {S, T} = Base.IteratorSize(T)
1823
Base.length(nt::NamedTupleIterator) = length(nt.x)
1924
Base.size(nt::NamedTupleIterator) = (length(nt.x),)
2025

2126
function Base.iterate(rows::NamedTupleIterator{Schema{names, T}}, st=()) where {names, T}
2227
if @generated
23-
vals = Tuple(:(getproperty(row, $(fieldtype(T, i)), $i, $(Meta.QuoteNode(names[i])))) for i = 1:fieldcount(T))
28+
vals = Tuple(:(unweakref(getproperty(row, $(fieldtype(T, i)), $i, $(Meta.QuoteNode(names[i]))))) for i = 1:fieldcount(T))
2429
return quote
2530
x = iterate(rows.x, st...)
2631
x === nothing && return nothing
2732
row, st = x
28-
return $(NamedTuple{names, T})(($(vals...),)), (st,)
33+
return $(NamedTuple{names, unweakreftypes(T)})(($(vals...),)), (st,)
2934
end
3035
else
3136
x = iterate(rows.x, st...)
3237
x === nothing && return nothing
3338
row, st = x
34-
return NamedTuple{names, T}(Tuple(getproperty(row, fieldtype(T, i), i, nm) for i = 1:fieldcount(T))), (st,)
39+
return NamedTuple{names, unweakreftypes(T)}(Tuple(unweakref(getproperty(row, fieldtype(T, i), i, nm)) for i = 1:fieldcount(T))), (st,)
3540
end
3641
end
3742

@@ -41,7 +46,7 @@ function Base.iterate(rows::NamedTupleIterator{Nothing, T}, st=()) where {T}
4146
x === nothing && return nothing
4247
row, st = x
4348
names = Tuple(propertynames(row))
44-
return NamedTuple{names}(Tuple(getproperty(row, nm) for nm in names)), (st,)
49+
return NamedTuple{names}(Tuple(unweakref(getproperty(row, nm)) for nm in names)), (st,)
4550
end
4651

4752
namedtupleiterator(::Type{T}, rows::S) where {T <: NamedTuple, S} = rows

0 commit comments

Comments
 (0)