1
1
# Vector of NamedTuples
2
2
const RowTable{T} = Vector{T} where {T <: NamedTuple }
3
3
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
+
4
9
# interface implementation
5
10
istable (:: Type{<:RowTable} ) = true
6
11
rowaccess (:: Type{<:RowTable} ) = true
@@ -13,25 +18,25 @@ struct NamedTupleIterator{S, T}
13
18
x:: T
14
19
end
15
20
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) }
17
22
Base. IteratorSize (:: Type{NamedTupleIterator{S, T}} ) where {S, T} = Base. IteratorSize (T)
18
23
Base. length (nt:: NamedTupleIterator ) = length (nt. x)
19
24
Base. size (nt:: NamedTupleIterator ) = (length (nt. x),)
20
25
21
26
function Base. iterate (rows:: NamedTupleIterator{Schema{names, T}} , st= ()) where {names, T}
22
27
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))
24
29
return quote
25
30
x = iterate (rows. x, st... )
26
31
x === nothing && return nothing
27
32
row, st = x
28
- return $ (NamedTuple{names, T })(($ (vals... ),)), (st,)
33
+ return $ (NamedTuple{names, unweakreftypes (T) })(($ (vals... ),)), (st,)
29
34
end
30
35
else
31
36
x = iterate (rows. x, st... )
32
37
x === nothing && return nothing
33
38
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,)
35
40
end
36
41
end
37
42
@@ -41,7 +46,7 @@ function Base.iterate(rows::NamedTupleIterator{Nothing, T}, st=()) where {T}
41
46
x === nothing && return nothing
42
47
row, st = x
43
48
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,)
45
50
end
46
51
47
52
namedtupleiterator (:: Type{T} , rows:: S ) where {T <: NamedTuple , S} = rows
0 commit comments