Skip to content

Commit

Permalink
Add IO interface to readspdx/writespdx (#44)
Browse files Browse the repository at this point in the history
Add IO methods for readspdx and writespdx to support working with in memory data.
  • Loading branch information
omus authored Jun 1, 2024
1 parent c6dc50f commit c369cf7
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 64 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ writespdx(jsonDoc, "jsonread.spdx")

# Write SPDX document in JSON format
writespdx(tvDoc, "tagvalueread.spdx.json")

```

## Creating and populating/modifying a new SPDX document
Expand Down
102 changes: 51 additions & 51 deletions src/api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,88 +5,88 @@ export addcreator!, getcreators, deletecreator!, setcreationtime!
export readspdx, writespdx

########################
function printJSON(doc::SpdxDocumentV2, fname::AbstractString)
open(fname, "w") do f
jsonDoc= convert_to_JSON(doc, SpdxDocumentV2_NameTable)
JSON.print(f, jsonDoc, 4)
end
function printJSON(io::IO, doc::SpdxDocumentV2)
jsonDoc= convert_to_JSON(doc, SpdxDocumentV2_NameTable)
JSON.print(io, jsonDoc, 4)
return nothing
end

#########################
function printTagValue(doc::SpdxDocumentV2, fname::AbstractString)
TagValueDoc= IOBuffer()
open(fname, "w") do f
convert_doc_to_TagValue!(TagValueDoc, doc, SpdxDocumentV2_NameTable)
write(f, take!(TagValueDoc))
end
function printTagValue(io::IO, doc::SpdxDocumentV2)
convert_doc_to_TagValue!(io, doc, SpdxDocumentV2_NameTable)
return nothing
end

#########################
function readJSON(fname::AbstractString)
JSONfile= JSON.parsefile(fname)
function readJSON(io::IO)
JSONfile= JSON.parse(io)
doc= convert_from_JSON(JSONfile, SpdxDocumentV2_NameTable, SpdxDocumentV2)
return doc
end

#########################
function readTagValue(fname::AbstractString)
doc= nothing
open(fname) do TVfile
doc= parse_TagValue(TVfile, SpdxDocumentV2_NameTable, SpdxDocumentV2)
end
function readTagValue(io::IO)
doc= parse_TagValue(io, SpdxDocumentV2_NameTable, SpdxDocumentV2)
return doc
end

#########################
function readspdx(io::IO; format::AbstractString)
doc= if format == "JSON"
readJSON(io)
elseif format == "TagValue"
readTagValue(io)
else
error("Specified Format ", format, " is not supported")
end

return doc
end

function readspdx(fname::AbstractString; format::Union{AbstractString, Nothing}=nothing)
if !isnothing(format)
if format=="JSON"
fext= ".json"
elseif format=="TagValue"
fext= ".spdx"
if isnothing(format)
fext= last(splitext(fname))
format= if fext == ".json"
"JSON"
elseif fext == ".spdx"
"TagValue"
else
error("Specified Format ", format, " is not supported")
error("File format ", fext, " is not supported")
end
else
temp= splitext(fname)
if !in(temp[2], (".json", ".spdx"))
error("File format ", temp[2], " is not supported")
end
fext= temp[2]
end

if fext == ".json"
doc= readJSON(fname)
elseif fext == ".spdx"
doc= readTagValue(fname)
doc= open(fname) do io
readspdx(io; format)
end

return doc
end

#########################
function writespdx(io::IO, doc::SpdxDocumentV2; format::AbstractString)
if format == "JSON"
printJSON(io, doc)
elseif format == "TagValue"
printTagValue(io, doc)
else
error("Specified Format ", format, " is not supported")
end
end

function writespdx(doc::SpdxDocumentV2, fname::AbstractString; format::Union{AbstractString, Nothing}=nothing)
if !isnothing(format)
if format=="JSON"
fext= ".json"
elseif format=="TagValue"
fext= ".spdx"
if isnothing(format)
fext= last(splitext(fname))
format= if fext == ".json"
"JSON"
elseif fext == ".spdx"
"TagValue"
else
error("Specified Format ", format, " is not supported")
end
else
temp= splitext(fname)
if !in(temp[2], (".json", ".spdx"))
error("File format ", temp[2], " is not supported")
error("File format ", fext, " is not supported")
end
fext= temp[2]
end

if fext == ".json"
printJSON(doc, fname)
elseif fext == ".spdx"
printTagValue(doc, fname)
open(fname, "w") do io
writespdx(io, doc; format)
end
end

Expand Down Expand Up @@ -143,4 +143,4 @@ end

function Base.hash(A::AbstractSpdx, h::UInt)
return _hash(A, h)
end
end
35 changes: 23 additions & 12 deletions test/test_read_write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,25 @@
@test spdxDoc isa SpdxDocumentV2
testdir= mktempdir()

@testset "JSON format roundtrips" begin
@testset "JSON format I/O roundtrips" begin
io = IOBuffer()
@test_throws "Specified Format YAML is not supported" writespdx(io, spdxDoc; format= "YAML")
@test_throws UndefKeywordError writespdx(io, spdxDoc)
writespdx(io, spdxDoc; format= "JSON")
rt_spdx = readspdx(seekstart(io); format= "JSON")
@test isequal(spdxDoc, rt_spdx)

@test isequal(spdxDoc, readJSON(seekstart(io)))
@test_throws Exception readTagValue(seekstart(io))
end

@testset "JSON format file roundtrips" begin
rt_path = joinpath(testdir, "out.spdx.json")
@test_throws "Specified Format YAML is not supported" writespdx(spdxDoc, rt_path; format= "YAML")
@test_throws "File format .yml is not supported" writespdx(spdxDoc, replace(rt_path, "json" => "yml"))
writespdx(spdxDoc, rt_path)
rt_spdx = readspdx(rt_path)
@test isequal(spdxDoc, rt_spdx)

@test isequal(spdxDoc, readJSON(rt_path))
@test_throws Exception readTagValue(rt_path)

# Change the describes relationship in the SPDX document to get more code coverage
spdxDoc2 = readspdx(joinpath(spdxdir, "SPDX.spdx.json"))
idx= findfirst(x -> isequal(x.RelationshipType, "DESCRIBES"), spdxDoc2.Relationships)
Expand All @@ -45,16 +53,19 @@
temp= readspdx(joinpath(spdxdir, "test/SPDX_badfields.spdx.json"));
@test temp isa SpdxDocumentV2
end
@testset "TagValue format roundtrips" begin
rt_path = joinpath(testdir, "out.spdx")
writespdx(spdxDoc, rt_path)
rt_spdx = readspdx(rt_path)

@testset "TagValue format I/O roundtrips" begin
io = IOBuffer()
writespdx(io, spdxDoc; format= "TagValue")
rt_spdx = readspdx(seekstart(io); format= "TagValue")
@test isequal(spdxDoc, rt_spdx)

@test isequal(spdxDoc, readTagValue(rt_path))
@test_throws Exception readJSON(rt_path)
@test isequal(spdxDoc, readTagValue(seekstart(io)))
@test_throws Exception readJSON(seekstart(io))
end

@testset "TagValue format file roundtrips" begin
rt_path = joinpath(testdir, "out.spdx")
# Write and read a second SPDX file for more coverage.
spdxDoc2= c # from build_testDocument.jl
writespdx(spdxDoc2, rt_path; format= "TagValue")
Expand Down

0 comments on commit c369cf7

Please sign in to comment.