Skip to content

Commit

Permalink
Hashing (#42)
Browse files Browse the repository at this point in the history
Resolves #38 - Enable Set and Dict operations with SPDX data types

Created a custom hash function for AbstractSpdx data types. Got the hooks in so that the hash could be used in the future with SPDX analysis functions.
  • Loading branch information
SamuraiAku authored Jan 9, 2024
1 parent 937cd8f commit 473a3ca
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/SPDX.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ include("readJSON.jl")
include("readTagValue.jl")
include("checksums.jl")
include("api.jl")
include("../test/build_testDocument.jl") # This is our precompilation

# Precompilation
include("../test/build_testDocument.jl")
h= hash(a)

end
12 changes: 12 additions & 0 deletions src/api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,15 @@ for pred in (:(==), :(isequal))
return all(f -> $pred(getproperty(x, f), getproperty(y, f)), fieldnames(typeof(x)))
end
end

function _hash(A::AbstractSpdx, h::UInt, skipproperties::Vector{Symbol}= Symbol[])
propset= setdiff(propertynames(A), skipproperties)
for p in propset
h= hash(getproperty(A, p), h)
end
return h
end

function Base.hash(A::AbstractSpdx, h::UInt)
return _hash(A, h)
end
12 changes: 12 additions & 0 deletions test/test_api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,20 @@
end

@testset "compare" begin
@test SPDX.compare_b(mysbom, mysbom2)
@test !SPDX.compare_b(mysbom, mysbom3)
end

@testset "hash" begin
@test hash(mysbom) == hash(mysbom2)
@test hash(mysbom) !== hash(mysbom3)

r1= mysbom.Relationships
r2= mysbom2.Relationships[end:-1:1]
@test !all(r1 .== r2)
@test issetequal(r1, r2)
@test allunique(r1)
end
end

@testset "Helper API" begin
Expand Down

0 comments on commit 473a3ca

Please sign in to comment.