Skip to content

Commit

Permalink
Require source container to be a row-wise iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed Dec 16, 2019
1 parent f93db10 commit 018a2d1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
21 changes: 10 additions & 11 deletions src/Table.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,17 @@ function append_columnaccess!(t::Table, t2)
return t
end

append_rowaccess!(t::Table, t2) =
mapfoldl(_asnamedtuple(NamedTuple{columnnames(t)}), push!, Tables.rows(t2); init = t)

function Base.append!(t::Table, t2)
if Tables.istable(t2)
if Tables.columnaccess(t2)
return append_columnaccess!(t, t2)
elseif Tables.rowaccess(t2)
return append_rowaccess!(t, t2)
end
append_rows!(t::Table, rows) =
mapfoldl(_asnamedtuple(NamedTuple{columnnames(t)}), push!, rows; init = t)

isrowiterator(t) =
Tables.istable(t) && Tables.rowaccess(t) && Tables.rows(t) === t

function Base.append!(t::Table, rows)
if isrowiterator(rows) && Tables.columnaccess(rows)
return append_columnaccess!(t, rows)
end
throw(ArgumentError(string("Cannot handle non-table type ", typeof(t2))))
return append_rows!(t, rows)
end

function Base.popfirst!(t::Table)
Expand Down
15 changes: 10 additions & 5 deletions test/Table.jl
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,24 @@
@test isequal(Table(t |> rowtable), t)
end

@testset "append!(_, ::$(typeof(t2)))" for t2 in Any[
@testset "append!(_, rows(::$(typeof(t2))))" for t2 in Any[
[(a=3, b=4)],
(a=[3], b=[4]),
]
t = Table(a=[1], b=[2])
@test append!(t, t2) == Table(a = [1, 3], b = [2, 4])
@test append!(t, Tables.rows(t2)) == Table(a = [1, 3], b = [2, 4])
end

@testset "append! error handling" begin
@test_throws(
ArgumentError("Cannot handle non-table type Nothing"),
err = nothing
@test try
append!(Table(a = [1], b = [2]), nothing)
)
false
catch err
true
end
@test err isa MethodError
@test err.f === iterate
end

@testset "group" begin
Expand Down

0 comments on commit 018a2d1

Please sign in to comment.