Skip to content

Commit

Permalink
Format files using DocumentFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Feb 17, 2020
1 parent 84995a5 commit 4a78820
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 132 deletions.
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using Documenter, TableTraitsUtils
makedocs(
modules = [TableTraitsUtils],
sitename = "TableTraitsUtils.jl",
analytics="UA-132838790-1",
analytics = "UA-132838790-1",
pages = [
"Introduction" => "index.md"
]
Expand Down
16 changes: 8 additions & 8 deletions src/TableTraitsUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export create_tableiterator, create_columns_from_iterabletable

# T is the type of the elements produced
# TS is a tuple type that stores the columns of the table
struct TableIterator{T, TS}
struct TableIterator{T,TS}
columns::TS
end

Expand All @@ -19,28 +19,28 @@ function create_tableiterator(columns, names::Vector{Symbol})
push!(field_types, i)
end
end
return TableIterator{NamedTuple{(names...,), Tuple{field_types...}}, Tuple{typeof.(columns)...}}((columns...,))
return TableIterator{NamedTuple{(names...,),Tuple{field_types...}},Tuple{typeof.(columns)...}}((columns...,))
end

function Base.length(iter::TableIterator{T,TS}) where {T,TS}
return length(iter.columns)==0 ? 0 : length(iter.columns[1])
return length(iter.columns) == 0 ? 0 : length(iter.columns[1])
end

Base.eltype(::Type{TableIterator{T,TS}}) where {T,TS} = T

@generated function Base.iterate(iter::TableIterator{T,TS}, state=1) where {T,TS}
@generated function Base.iterate(iter::TableIterator{T,TS}, state = 1) where {T,TS}
columns = map(1:length(TS.parameters)) do i
if fieldtype(T,i) <: DataValue && eltype(TS.parameters[i]) >: Missing
return :($(fieldtype(T,i))(iter.columns[$i][state]))
if fieldtype(T, i) <: DataValue && eltype(TS.parameters[i]) >: Missing
return :($(fieldtype(T, i))(iter.columns[$i][state]))
else
return :(iter.columns[$i][state])
end
end
return quote
if state > length(iter)
return nothing
else
return $(T)(($(columns...),)), state+1
else
return $(T)(($(columns...),)), state + 1
end
end
end
Expand Down
104 changes: 52 additions & 52 deletions src/collect1.jl
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
# Main entry point
function create_columns_from_iterabletable(itr; sel_cols=:all, na_representation=:datavalue, errorhandling=:error)
function create_columns_from_iterabletable(itr; sel_cols = :all, na_representation = :datavalue, errorhandling = :error)
in(errorhandling, (:error, :returnvalue)) || throw(ArgumentError("'$errorhandling' is not a valid argument for errorhandling."))
in(na_representation, (:datavalue, :missing)) || throw(ArgumentError("'$na_representation' is not a valid argument for na_representation."))

if TableTraits.isiterabletable(itr)===false
if errorhandling==:error
if TableTraits.isiterabletable(itr) === false
if errorhandling == :error
throw(ArgumentError("itr is not a table."))
elseif errorhandling==:returnvalue
elseif errorhandling == :returnvalue
return nothing
end
else

array_factory = if na_representation==:datavalue
(t,rows) -> begin
if t <: DataValue
return DataValueArray{eltype(t)}(rows)
else
return Array{t}(undef, rows)
end
array_factory = if na_representation == :datavalue
(t, rows)->begin
if t <: DataValue
return DataValueArray{eltype(t)}(rows)
else
return Array{t}(undef, rows)
end
elseif na_representation==:missing
(t,rows) -> begin
if t <: DataValue
return Array{Union{eltype(t),Missing}}(undef, rows)
else
return Array{t}(undef, rows)
end
end
elseif na_representation == :missing
(t, rows)->begin
if t <: DataValue
return Array{Union{eltype(t),Missing}}(undef, rows)
else
return Array{t}(undef, rows)
end
end
end

itr2 = IteratorInterfaceExtensions.getiterator(itr)
return _collect_columns(itr2, Base.IteratorSize(itr2), array_factory, sel_cols, errorhandling)
Expand All @@ -37,41 +37,41 @@ end
function collect_empty_columns(itr::T, ::Base.EltypeUnknown, array_factory, sel_cols, errorhandling) where {T}
S = Core.Compiler.return_type(first, Tuple{T})
if S == Union{} || !(S <: NamedTuple)
if errorhandling==:error
if errorhandling == :error
throw(ArgumentError("itr is not a table."))
elseif errorhandling==:returnvalue
elseif errorhandling == :returnvalue
return nothing
end
end
dest = getdest(S,0, array_factory, sel_cols)
dest = getdest(S, 0, array_factory, sel_cols)
return collect(values(dest)), collect(keys(dest))
end

function collect_empty_columns(itr::T, ::Base.HasEltype, array_factory, sel_cols, errorhandling) where {T}
if eltype(itr) <: NamedTuple
dest = getdest(eltype(itr),0, array_factory, sel_cols)
dest = getdest(eltype(itr), 0, array_factory, sel_cols)
return collect(values(dest)), collect(keys(dest))
else
if errorhandling==:error
if errorhandling == :error
throw(ArgumentError("itr is not a table."))
elseif errorhandling==:returnvalue
elseif errorhandling == :returnvalue
return nothing
end
end
end

function getdest(T, n, array_factory, sel_cols)
if sel_cols==:all
return NamedTuple{fieldnames(T)}(tuple((array_factory(fieldtype(T,i),n) for i in 1:length(fieldnames(T)))...))
if sel_cols == :all
return NamedTuple{fieldnames(T)}(tuple((array_factory(fieldtype(T, i), n) for i in 1:length(fieldnames(T)))...))
else
return NamedTuple{fieldnames(T)}(tuple((i in sel_cols ? array_factory(fieldtype(T,i),n) : nothing for i in 1:length(fieldnames(T)))...))
return NamedTuple{fieldnames(T)}(tuple((i in sel_cols ? array_factory(fieldtype(T, i), n) : nothing for i in 1:length(fieldnames(T)))...))
end
end

@generated function _setrow(dest::NamedTuple{NAMES,TYPES}, i, el::T) where {T,NAMES,TYPES}
push_exprs = Expr(:block)
for col_idx in 1:length(fieldnames(T))
if fieldtype(TYPES, col_idx)!==Nothing
if fieldtype(TYPES, col_idx) !== Nothing
if fieldtype(TYPES, col_idx) == Array{Any,1} && fieldtype(T, col_idx) == DataValue{Any}
ex = :( dest[$col_idx][i] = get(el[$col_idx], missing) )
else
Expand All @@ -87,7 +87,7 @@ end
@generated function _pushrow(dest::NamedTuple{NAMES,TYPES}, el::T) where {T,NAMES,TYPES}
push_exprs = Expr(:block)
for col_idx in 1:length(fieldnames(T))
if fieldtype(TYPES, col_idx)!==Nothing
if fieldtype(TYPES, col_idx) !== Nothing
if fieldtype(TYPES, col_idx) == Array{Any,1} && fieldtype(T, col_idx) == DataValue{Any}
ex = :( push!(dest[$col_idx], get(el[$col_idx], missing)) )
else
Expand All @@ -100,73 +100,73 @@ end
return push_exprs
end

function _collect_columns(itr, ::Union{Base.HasShape, Base.HasLength}, array_factory, sel_cols, errorhandling)
function _collect_columns(itr, ::Union{Base.HasShape,Base.HasLength}, array_factory, sel_cols, errorhandling)
y = iterate(itr)
y===nothing && return collect_empty_columns(itr, Base.IteratorEltype(itr), array_factory, sel_cols, errorhandling)
y === nothing && return collect_empty_columns(itr, Base.IteratorEltype(itr), array_factory, sel_cols, errorhandling)

if !(typeof(y[1])<:NamedTuple)
if errorhandling==:error
if !(typeof(y[1]) <: NamedTuple)
if errorhandling == :error
throw(ArgumentError("itr is not a table."))
elseif errorhandling==:returnvalue
elseif errorhandling == :returnvalue
return nothing
end
end

dest = getdest(typeof(y[1]), length(itr), array_factory, sel_cols)

_setrow(dest,1,y[1])
_setrow(dest, 1, y[1])

_collect_to_columns!(dest, itr, 2, y[2], sel_cols, errorhandling)
end

function _collect_to_columns!(dest::T, itr, offs, st, sel_cols, errorhandling) where {T<:NamedTuple}
function _collect_to_columns!(dest::T, itr, offs, st, sel_cols, errorhandling) where {T <: NamedTuple}
i = offs
y = iterate(itr,st)
while y!==nothing
_setrow(dest,i,y[1])
y = iterate(itr, st)
while y !== nothing
_setrow(dest, i, y[1])
i += 1
y = iterate(itr,y[2])
y = iterate(itr, y[2])
end

if sel_cols==:all
if sel_cols == :all
return collect(values(dest)), collect(keys(dest))
else
names_to_use = tuple((fieldname(T,i) for i in sel_cols)...)
names_to_use = tuple((fieldname(T, i) for i in sel_cols)...)
r = NamedTuple{names_to_use}(dest)
return collect(values(r)), collect(keys(r))
end
end

function _collect_columns(itr, ::Base.SizeUnknown, array_factory, sel_cols, errorhandling)
y = iterate(itr)
y===nothing && return collect_empty_columns(itr, Base.IteratorEltype(itr), array_factory, sel_cols, errorhandling)
y === nothing && return collect_empty_columns(itr, Base.IteratorEltype(itr), array_factory, sel_cols, errorhandling)

if !(typeof(y[1])<:NamedTuple)
if errorhandling==:error
if !(typeof(y[1]) <: NamedTuple)
if errorhandling == :error
throw(ArgumentError("itr is not a table."))
elseif errorhandling==:returnvalue
elseif errorhandling == :returnvalue
return nothing
end
end

dest = getdest(typeof(y[1]), 1, array_factory, sel_cols)

_setrow(dest,1,y[1])
_setrow(dest, 1, y[1])

_grow_to_columns!(dest, itr, y[2], sel_cols, errorhandling)
end

function _grow_to_columns!(dest::T, itr, st, sel_cols, errorhandling) where {T<:NamedTuple}
function _grow_to_columns!(dest::T, itr, st, sel_cols, errorhandling) where {T <: NamedTuple}
y = iterate(itr, st)
while y!==nothing
while y !== nothing
_pushrow(dest, y[1])
y = iterate(itr,y[2])
y = iterate(itr, y[2])
end

if sel_cols==:all
if sel_cols == :all
return collect(values(dest)), collect(keys(dest))
else
names_to_use = tuple((fieldname(T,i) for i in sel_cols)...)
names_to_use = tuple((fieldname(T, i) for i in sel_cols)...)
r = NamedTuple{names_to_use}(dest)
return collect(values(r)), collect(keys(r))
end
Expand Down
Loading

0 comments on commit 4a78820

Please sign in to comment.