Skip to content

Commit

Permalink
Fix singletonof(TableType, ::IteratorRow)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed Nov 11, 2019
1 parent 9f69bfb commit f03d25d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
12 changes: 10 additions & 2 deletions src/NoBang/singletonof.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,17 @@ julia> using TypedTables: Table
julia> @assert singletonof(Table, (a=1, b=2)) == Table(a=[1], b=[2])
```
"""
singletonof(::Type{T}, x) where T = T(SingletonVector(x))
singletonof(::Type{T}, x) where {T} =
T(SingletonVector(_maybeasnamedtuple(x, T)))

function singletonof(::T, x) where T
C = constructorof(T)
return singletonof(C isa Type ? C : T, x)
return singletonof(C isa Type ? C : T, _maybeasnamedtuple(x, T))
end
# `StructVector` is a table but `StructArray` is not. So, converting
# to a named tuple before stripping off the type parameters.

asnamedtuple(x::NamedTuple) = x
asnamedtuple(x) = (; map(n -> n => getproperty(x, n), propertynames(x))...)

_maybeasnamedtuple(x, T) = Tables.istable(T) ? asnamedtuple(x) : x
16 changes: 14 additions & 2 deletions test/test_structarrays.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module TestStructArrays

using Test
using BangBang
using StructArrays
using StructArrays: StructVector
using Tables: Tables
using Test

@testset "push!!" begin
xs = StructVector{ComplexF64}(([1.0, 2.0], [3.0, 4.0]))
Expand All @@ -13,6 +14,17 @@ using StructArrays
ys = push!!(xs, 5.0 + 6.0im)
@test ys == [1 + 3im, 2 + 4im, 5 + 6im]
@test_broken eltype(ys) == ComplexF64

@test_broken push!!(
StructVector(a = [1]),
Tables.IteratorRow((a = 2,)),
) == StructVector(a = [1, 2])
@test invoke(
push!!,
Tuple{AbstractVector,Any},
StructVector(a = [1]),
Tables.IteratorRow((a = 2,)),
) == StructVector(a = [1, 2])
end

@testset "append!!" begin
Expand Down
7 changes: 5 additions & 2 deletions test/test_typedtables.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module TestTypedTables

using BangBang: append!!, push!!, singletonof
using Tables: Tables
using Test
using BangBang
using TypedTables
using TypedTables: Table

@testset "push!!" begin
tints = Table(a = [1], b = [2])
Expand All @@ -12,6 +13,8 @@ using TypedTables
tints = Table(a = [1], b = [2])
@test push!!(tints, (a = 3, b = 4)) === tints
@test tints == Table(a = [1, 3], b = [2, 4])

@test push!!(Table(a = [1]), Tables.IteratorRow((a = 2,))) == Table(a = [1, 2])
end

@testset "append!!" begin
Expand Down

0 comments on commit f03d25d

Please sign in to comment.