Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add methods for length and size of Row #13

Merged
merged 4 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "DBFTables"
uuid = "75c7ada1-017a-5fb6-b8c7-2125ff2d6c93"
version = "0.2.2"
version = "0.2.3"

[deps]
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# DBFTables

[![CI](https://github.com/JuliaData/WeakRefStrings.jl/workflows/CI/badge.svg)](https://github.com/JuliaData/WeakRefStrings.jl/actions?query=workflow%3ACI)
[![codecov](https://codecov.io/gh/JuliaData/WeakRefStrings.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/JuliaData/WeakRefStrings.jl)
[![CI](https://github.com/JuliaData/DBFTables.jl/workflows/CI/badge.svg)](https://github.com/JuliaData/DBFTables.jl/actions?query=workflow%3ACI)
[![codecov](https://codecov.io/gh/JuliaData/DBFTables.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/JuliaData/DBFTables.jl)
[![deps](https://juliahub.com/docs/DBFTables/deps.svg)](https://juliahub.com/ui/Packages/DBFTables/P7Qdk?t=2)
[![version](https://juliahub.com/docs/DBFTables/version.svg)](https://juliahub.com/ui/Packages/DBFTables/P7Qdk)
[![pkgeval](https://juliahub.com/docs/DBFTables/pkgeval.svg)](https://juliahub.com/ui/Packages/DBFTables/P7Qdk)
Expand Down
19 changes: 14 additions & 5 deletions src/DBFTables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct Table
end

"Struct representing a single row or record of the DBF Table"
struct Row
struct Row <: Tables.AbstractRow
table::Table
row::Int
end
Expand Down Expand Up @@ -172,6 +172,8 @@ gettable(row::Row) = getfield(row, :table)
Base.length(dbf::Table) = Int(getheader(dbf).records)
Base.size(dbf::Table) = (length(dbf), length(getfields(dbf)))
Base.size(dbf::Table, i) = size(dbf)[i]
Base.size(row::Row) = (length(row),)
Base.size(row::Row, i) = i == 1 ? length(row) : throw(BoundsError(row, i))

"""
DBFTables.Table(source) => DBFTables.Table
Expand Down Expand Up @@ -232,8 +234,7 @@ function Base.show(io::IO, row::Row)
end

function Base.show(io::IO, dbf::Table)
nr = length(dbf)
nc = length(getfields(dbf))
nr, nc = size(dbf)
println(io, "DBFTables.Table with $nr rows and $nc columns")
println(io, Tables.schema(dbf))
end
Expand Down Expand Up @@ -262,7 +263,7 @@ function Base.iterate(dbf::Table, st = 1)
return Row(dbf, st), st + 1
end

function Base.getproperty(row::Row, name::Symbol)
function Tables.getcolumn(row::Row, name::Symbol)
dbf = gettable(row)
header = getheader(dbf)
str = getstrings(dbf)
Expand All @@ -273,6 +274,14 @@ function Base.getproperty(row::Row, name::Symbol)
return @inbounds dbf_value(type, str[colidx, rowidx])
end

function Tables.getcolumn(row::Row, i::Int)
dbf = gettable(row)
str = getstrings(dbf)
type = getfields(dbf)[i].type
rowidx = getrow(row)
return @inbounds dbf_value(type, str[i, rowidx])
end

Tables.istable(::Type{Table}) = true
Tables.rowaccess(::Type{Table}) = true
Tables.columnaccess(::Type{Table}) = true
Expand All @@ -289,7 +298,7 @@ end

"List all available DBF column names"
Base.propertynames(dbf::Table) = getfield.(getfield(dbf, :header).fields, :name)
Base.propertynames(row::Row) = propertynames(gettable(row))
Tables.columnnames(row::Row) = propertynames(gettable(row))

"Create a copy of an entire DBF column as a Vector. Usage: `dbf.myfield`"
function Base.getproperty(dbf::Table, name::Symbol)
Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ row, st = iterate(dbf)

@testset "iterate" begin
@test st === 2
@test haskey(row, :CHAR)
@test row.CHAR === "Bob"
@test row[2] === "19900102"
@test_throws ArgumentError row.nonexistent_field
firstrow = (
CHAR = "Bob",
Expand All @@ -58,9 +60,15 @@ row, st = iterate(dbf)
)
@test NamedTuple(row) === firstrow
@test row isa DBFTables.Row
@test row isa Tables.AbstractRow
@test length(row) === 6
@test size(row) === (6,)
@test size(row, 1) === 6
@test_throws BoundsError size(row, 2)
@test DBFTables.getrow(row) === 1
@test DBFTables.gettable(row) === dbf
@test sum(1 for row in dbf) === 7
@test sum(1 for cell in row) === 6
@test propertynames(dbf) == [:CHAR, :DATE, :BOOL, :FLOAT, :NUMERIC, :INTEGER]
@test propertynames(row) == [:CHAR, :DATE, :BOOL, :FLOAT, :NUMERIC, :INTEGER]
end
Expand Down