Skip to content

Commit 22e772c

Browse files
iamed2quinnj
authored andcommitted
Allow writing to IO as shown in examples (#157)
* Add single outer testset * Test/allow writing to an IOBuffer()
1 parent ff5977d commit 22e772c

3 files changed

Lines changed: 22 additions & 8 deletions

File tree

src/Sink.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function Sink(fullpath::AbstractString;
1+
function Sink(fullpath::Union{AbstractString, IO};
22
delim::Char=',',
33
quotechar::Char='"',
44
escapechar::Char='\\',
@@ -33,7 +33,7 @@ Data.streamtypes(::Type{CSV.Sink}) = [Data.Field]
3333
Data.weakrefstrings(::Type{CSV.Sink}) = true
3434

3535
# Constructors
36-
function Sink(sch::Data.Schema, T, append, file::AbstractString; reference::Vector{UInt8}=UInt8[], kwargs...)
36+
function Sink(sch::Data.Schema, T, append, file::Union{AbstractString, IO}; reference::Vector{UInt8}=UInt8[], kwargs...)
3737
sink = Sink(file; append=append, colnames=Data.header(sch), kwargs...)
3838
return sink
3939
end
@@ -70,7 +70,7 @@ end
7070
function Data.close!(sink::CSV.Sink)
7171
io = isa(sink.fullpath, AbstractString) ? open(sink.fullpath, sink.append ? "a" : "w") : sink.fullpath
7272
Base.write(io, take!(sink.io))
73-
applicable(close, io) && close(io)
73+
isa(sink.fullpath, AbstractString) && close(io)
7474
return sink
7575
end
7676

@@ -130,11 +130,11 @@ CSV.write("sqlite_table.csv", sqlite_source)
130130
"""
131131
function write end
132132

133-
function write(file::AbstractString, ::Type{T}, args...; append::Bool=false, transforms::Dict=Dict{Int,Function}(), kwargs...) where {T}
133+
function write(file::Union{AbstractString, IO}, ::Type{T}, args...; append::Bool=false, transforms::Dict=Dict{Int,Function}(), kwargs...) where {T}
134134
sink = Data.stream!(T(args...), CSV.Sink, file; append=append, transforms=transforms, kwargs...)
135135
return Data.close!(sink)
136136
end
137-
function write(file::AbstractString, source; append::Bool=false, transforms::Dict=Dict{Int,Function}(), kwargs...)
137+
function write(file::Union{AbstractString, IO}, source; append::Bool=false, transforms::Dict=Dict{Int,Function}(), kwargs...)
138138
sink = Data.stream!(source, CSV.Sink, file; append=append, transforms=transforms, kwargs...)
139139
return Data.close!(sink)
140140
end

test/runtests.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ if VERSION < v"0.7.0-DEV.2575"
1212
else
1313
using Dates
1414
end
15-
include("parsefields.jl")
16-
include("io.jl")
1715

18-
dir = joinpath(dirname(@__FILE__),"test_files/")
16+
const dir = joinpath(dirname(@__FILE__),"test_files/")
1917
# dir = joinpath(Pkg.dir("CSV"), "test/test_files")
2018

19+
@testset "CSV" begin
20+
21+
include("parsefields.jl")
22+
include("io.jl")
23+
2124
include("source.jl")
25+
include("sink.jl")
2226
include("multistream.jl")
2327
include("validate.jl")
28+
29+
end

test/sink.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@testset "Write to IOBuffer" begin
2+
csv_string = chomp(read(joinpath(dir, "test_basic.csv"), String))
3+
df = CSV.read(IOBuffer(csv_string))
4+
io = IOBuffer()
5+
CSV.write(io, df)
6+
written = chomp(String(take!(io)))
7+
@test written == csv_string
8+
end

0 commit comments

Comments
 (0)