Skip to content

Commit 85059b1

Browse files
author
Pietro Vertechi
committed
Remove special pair constructor
1 parent 6752251 commit 85059b1

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/structarray.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@ end
2121
_dims(c::Tup) = length(axes(c[1]))
2222
_dims(c::EmptyTup) = 1
2323

24-
function StructArray{T}(c::C) where {T, C<:Union{Tup, Pair}}
24+
function StructArray{T}(c::C) where {T, C<:Tup}
2525
cols = strip_types(staticschema(T))(c)
2626
StructArray{T, _dims(cols), typeof(cols)}(cols)
2727
end
2828

2929
StructArray(c::C) where {C<:NamedTuple} = StructArray{eltypes(C)}(c)
3030
StructArray(c::Tuple; names = nothing) = _structarray(c, names)
31-
StructArray(c::Pair{P, Q}) where {P, Q} = StructArray{Pair{eltype(P), eltype(Q)}}(c)
3231

3332
StructArray{T}(; kwargs...) where {T} = StructArray{T}(values(kwargs))
3433
StructArray(; kwargs...) = StructArray(values(kwargs))

test/runtests.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -420,34 +420,36 @@ end
420420
@test isequal(StructArrays.fieldarrays(t)[1], [1, missing, 3])
421421
end
422422

423+
pair_structarray((first, last)) = StructArray{Pair{eltype(first), eltype(last)}}((first, last))
424+
423425
@testset "collectpairs" begin
424426
v = (i=>i+1 for i in 1:3)
425-
@test collect_structarray_rec(v) == StructArray{Pair{Int, Int}}(([1,2,3], [2,3,4]))
427+
@test collect_structarray_rec(v) == pair_structarray([1,2,3] => [2,3,4])
426428
@test eltype(collect_structarray_rec(v)) == Pair{Int, Int}
427429

428430
v = (i == 1 ? (1.2 => i+1) : (i => i+1) for i in 1:3)
429-
@test collect_structarray_rec(v) == StructArray{Pair{Real, Int}}(([1.2,2,3], [2,3,4]))
431+
@test collect_structarray_rec(v) == pair_structarray([1.2,2,3] => [2,3,4])
430432
@test eltype(collect_structarray_rec(v)) == Pair{Real, Int}
431433

432434
v = ((a=i,) => (b="a$i",) for i in 1:3)
433-
@test collect_structarray_rec(v) == StructArray(StructArray((a = [1,2,3],)) => StructArray((b = ["a1","a2","a3"],)))
435+
@test collect_structarray_rec(v) == pair_structarray(StructArray((a = [1,2,3],)) => StructArray((b = ["a1","a2","a3"],)))
434436
@test eltype(collect_structarray_rec(v)) == Pair{NamedTuple{(:a,), Tuple{Int64}}, NamedTuple{(:b,), Tuple{String}}}
435437

436438
v = (i == 1 ? (a="1",) => (b="a$i",) : (a=i,) => (b="a$i",) for i in 1:3)
437-
@test collect_structarray_rec(v) == StructArray(StructArray((a = ["1",2,3],)) => StructArray((b = ["a1","a2","a3"],)))
439+
@test collect_structarray_rec(v) == pair_structarray(StructArray((a = ["1",2,3],)) => StructArray((b = ["a1","a2","a3"],)))
438440
@test eltype(collect_structarray_rec(v)) == Pair{NamedTuple{(:a,), Tuple{Any}}, NamedTuple{(:b,), Tuple{String}}}
439441

440442
# empty
441443
v = ((a=i,) => (b="a$i",) for i in 0:-1)
442-
@test collect_structarray_rec(v) == StructArray(StructArray((a = Int[],)) => StructArray((b = String[],)))
444+
@test collect_structarray_rec(v) == pair_structarray(StructArray((a = Int[],)) => StructArray((b = String[],)))
443445
@test eltype(collect_structarray_rec(v)) == Pair{NamedTuple{(:a,), Tuple{Int}}, NamedTuple{(:b,), Tuple{String}}}
444446

445447
v = Iterators.filter(t -> t.first.a == 4, ((a=i,) => (b="a$i",) for i in 1:3))
446-
@test collect_structarray_rec(v) == StructArray(StructArray((a = Int[],)) => StructArray((b = String[],)))
448+
@test collect_structarray_rec(v) == pair_structarray(StructArray((a = Int[],)) => StructArray((b = String[],)))
447449
@test eltype(collect_structarray_rec(v)) == Pair{NamedTuple{(:a,), Tuple{Int}}, NamedTuple{(:b,), Tuple{String}}}
448450

449451
t = collect_structarray_rec((b = 1,) => (a = i,) for i in (2, missing, 3))
450-
s = StructArray(StructArray(b = [1,1,1]) => StructArray(a = [2, missing, 3]))
452+
s = pair_structarray(StructArray(b = [1,1,1]) => StructArray(a = [2, missing, 3]))
451453
@test s[1] == t[1]
452454
@test ismissing(t[2].second.a)
453455
@test s[3] == t[3]

0 commit comments

Comments
 (0)