Skip to content

Commit 98520b9

Browse files
authored
add methods for length and size of Row (#13)
* add methods for length and size of Row * fix badges pointing to WeakRefStrings This changes the CI badges from failing to passing and code coverage from 55% to 95%. * forgot to throw the BoundsError * make Row a Tables.AbstractRow
1 parent 6b7ff04 commit 98520b9

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "DBFTables"
22
uuid = "75c7ada1-017a-5fb6-b8c7-2125ff2d6c93"
3-
version = "0.2.2"
3+
version = "0.2.3"
44

55
[deps]
66
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# DBFTables
22

3-
[![CI](https://github.com/JuliaData/WeakRefStrings.jl/workflows/CI/badge.svg)](https://github.com/JuliaData/WeakRefStrings.jl/actions?query=workflow%3ACI)
4-
[![codecov](https://codecov.io/gh/JuliaData/WeakRefStrings.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/JuliaData/WeakRefStrings.jl)
3+
[![CI](https://github.com/JuliaData/DBFTables.jl/workflows/CI/badge.svg)](https://github.com/JuliaData/DBFTables.jl/actions?query=workflow%3ACI)
4+
[![codecov](https://codecov.io/gh/JuliaData/DBFTables.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/JuliaData/DBFTables.jl)
55
[![deps](https://juliahub.com/docs/DBFTables/deps.svg)](https://juliahub.com/ui/Packages/DBFTables/P7Qdk?t=2)
66
[![version](https://juliahub.com/docs/DBFTables/version.svg)](https://juliahub.com/ui/Packages/DBFTables/P7Qdk)
77
[![pkgeval](https://juliahub.com/docs/DBFTables/pkgeval.svg)](https://juliahub.com/ui/Packages/DBFTables/P7Qdk)

src/DBFTables.jl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct Table
3333
end
3434

3535
"Struct representing a single row or record of the DBF Table"
36-
struct Row
36+
struct Row <: Tables.AbstractRow
3737
table::Table
3838
row::Int
3939
end
@@ -172,6 +172,8 @@ gettable(row::Row) = getfield(row, :table)
172172
Base.length(dbf::Table) = Int(getheader(dbf).records)
173173
Base.size(dbf::Table) = (length(dbf), length(getfields(dbf)))
174174
Base.size(dbf::Table, i) = size(dbf)[i]
175+
Base.size(row::Row) = (length(row),)
176+
Base.size(row::Row, i) = i == 1 ? length(row) : throw(BoundsError(row, i))
175177

176178
"""
177179
DBFTables.Table(source) => DBFTables.Table
@@ -232,8 +234,7 @@ function Base.show(io::IO, row::Row)
232234
end
233235

234236
function Base.show(io::IO, dbf::Table)
235-
nr = length(dbf)
236-
nc = length(getfields(dbf))
237+
nr, nc = size(dbf)
237238
println(io, "DBFTables.Table with $nr rows and $nc columns")
238239
println(io, Tables.schema(dbf))
239240
end
@@ -262,7 +263,7 @@ function Base.iterate(dbf::Table, st = 1)
262263
return Row(dbf, st), st + 1
263264
end
264265

265-
function Base.getproperty(row::Row, name::Symbol)
266+
function Tables.getcolumn(row::Row, name::Symbol)
266267
dbf = gettable(row)
267268
header = getheader(dbf)
268269
str = getstrings(dbf)
@@ -273,6 +274,14 @@ function Base.getproperty(row::Row, name::Symbol)
273274
return @inbounds dbf_value(type, str[colidx, rowidx])
274275
end
275276

277+
function Tables.getcolumn(row::Row, i::Int)
278+
dbf = gettable(row)
279+
str = getstrings(dbf)
280+
type = getfields(dbf)[i].type
281+
rowidx = getrow(row)
282+
return @inbounds dbf_value(type, str[i, rowidx])
283+
end
284+
276285
Tables.istable(::Type{Table}) = true
277286
Tables.rowaccess(::Type{Table}) = true
278287
Tables.columnaccess(::Type{Table}) = true
@@ -289,7 +298,7 @@ end
289298

290299
"List all available DBF column names"
291300
Base.propertynames(dbf::Table) = getfield.(getfield(dbf, :header).fields, :name)
292-
Base.propertynames(row::Row) = propertynames(gettable(row))
301+
Tables.columnnames(row::Row) = propertynames(gettable(row))
293302

294303
"Create a copy of an entire DBF column as a Vector. Usage: `dbf.myfield`"
295304
function Base.getproperty(dbf::Table, name::Symbol)

test/runtests.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ row, st = iterate(dbf)
4646

4747
@testset "iterate" begin
4848
@test st === 2
49+
@test haskey(row, :CHAR)
4950
@test row.CHAR === "Bob"
51+
@test row[2] === "19900102"
5052
@test_throws ArgumentError row.nonexistent_field
5153
firstrow = (
5254
CHAR = "Bob",
@@ -58,9 +60,15 @@ row, st = iterate(dbf)
5860
)
5961
@test NamedTuple(row) === firstrow
6062
@test row isa DBFTables.Row
63+
@test row isa Tables.AbstractRow
64+
@test length(row) === 6
65+
@test size(row) === (6,)
66+
@test size(row, 1) === 6
67+
@test_throws BoundsError size(row, 2)
6168
@test DBFTables.getrow(row) === 1
6269
@test DBFTables.gettable(row) === dbf
6370
@test sum(1 for row in dbf) === 7
71+
@test sum(1 for cell in row) === 6
6472
@test propertynames(dbf) == [:CHAR, :DATE, :BOOL, :FLOAT, :NUMERIC, :INTEGER]
6573
@test propertynames(row) == [:CHAR, :DATE, :BOOL, :FLOAT, :NUMERIC, :INTEGER]
6674
end

0 commit comments

Comments
 (0)