diff --git a/src/Table.jl b/src/Table.jl index 616b81b..477850b 100644 --- a/src/Table.jl +++ b/src/Table.jl @@ -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) diff --git a/test/Table.jl b/test/Table.jl index dbbf9fb..1ed13b9 100644 --- a/test/Table.jl +++ b/test/Table.jl @@ -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