Skip to content

Commit

Permalink
Fix Bug in the hashing of empty tuples (#49)
Browse files Browse the repository at this point in the history
The new parser doesn't correctly parse "{}", which occurs in e.g. an empty tuple type. This adds tests and fixes the parsing of this edge case.
  • Loading branch information
haberdashPI authored Jan 19, 2024
1 parent 6c7ce8f commit 1724ffa
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "StableHashTraits"
uuid = "c5dd0088-6c3f-4803-b00e-f31a60c170fa"
authors = ["Beacon Biosignals, Inc."]
version = "1.1.5"
version = "1.1.6"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Expand Down
7 changes: 4 additions & 3 deletions src/StableNames.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ end
:space => P.satisfy(isspace),
:sep => P.first(P.seq(P.many(:space), P.token(','), P.many(:space)),
P.some(:space)),
:brackets => P.seq(P.token('{'), :clause, P.token('}')),
:brackets => P.first(P.seq(P.token('{'), P.many(:sep), P.token('}')),
P.seq(P.token('{'), :clause, P.token('}'))),
:head_brackets => P.seq(:element, :brackets),
:inclause => P.first(:head_brackets, :brackets, :element),
:clause => P.seq(P.many(:sep),
Expand All @@ -95,7 +96,7 @@ end
return if match.rule (:element, :space, :sep)
String(match.view)
elseif match.rule == :brackets
Parsed(:Brackets, vals[2]...)
Parsed(:Brackets, (isnothing(vals[1][2]) ? [""] : vals[1][2])...)
elseif match.rule == :clause
reduce(vcat, filter(!isnothing, vals); init=[])
elseif match.rule == :head_brackets
Expand Down Expand Up @@ -171,7 +172,7 @@ end
if parsed isa Parsed && parsed.name == :Head &&
endswith(parsed.args[1], "@NamedTuple")
symbols_and_types = split_symbol_and_type.(parsed.args[2].args)
symbol_tuple = join(":" .* first.(symbols_and_types), ",")
symbol_tuple = join(":" .* filter(!isempty, first.(symbols_and_types)), ",")
types = map(t -> parse_walker(fn, t), last.(symbols_and_types))
types_tuple = join(types, ",")
prefix = replace(parsed.args[1], "@NamedTuple" => "")
Expand Down
1 change: 1 addition & 0 deletions test/references/ref22_1_crc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2427942825
1 change: 1 addition & 0 deletions test/references/ref22_1_sha1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
54a26391c9b89dfd2bea64d847ddf4375bd3f0de
1 change: 1 addition & 0 deletions test/references/ref22_1_sha256.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
39821b69f3416b69ab37bf2144578236b5b07afae616dd07e8ecbb46813a3bf4
1 change: 1 addition & 0 deletions test/references/ref22_2_crc32c.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2606483572
1 change: 1 addition & 0 deletions test/references/ref22_2_sha1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
f2f60b99477939b30b470b8718f7ef38cbb2431a
1 change: 1 addition & 0 deletions test/references/ref22_2_sha256.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1ca343199ba59bb3c36204421371ba45be23af7a1b3b2e4d56f4c1e2f6a48659
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ include("setup_tests.jl")
bytes2hex_(test_hash(==("test"))))
@test_reference("references/ref21_$(V)_$(nameof(hashfn)).txt",
bytes2hex_(test_hash((1, (a=1, b=(x=1, y=2), c=(1, 2))))))
@test_reference("references/ref22_$(V)_$(nameof(hashfn)).txt",
bytes2hex_(test_hash((;))))
end
# verifies that transform can be called recursively

Expand Down Expand Up @@ -340,6 +342,10 @@ include("setup_tests.jl")
@test parse_brackets("bob, joe, ") ==
["bob", Parsed(:SepClause, ", ", "joe"), ", "]
@test parse_brackets("bob,joe") == ["bob", Parsed(:SepClause, ",", "joe")]
@test parse_brackets("{}") == Any[Parsed(:Brackets, "")]
@test parse_brackets("{,,}") == Any[Parsed(:Brackets, ",", ",")]
@test parse_brackets("{ }") == Any[Parsed(:Brackets, " ")]
@test parse_brackets("{, joe}") == Any[Parsed(:Brackets, ", ", "joe")]
@test parse_brackets("{bob, joe}") ==
Any[Parsed(:Brackets, "bob", Parsed(:SepClause, ", ", "joe"))]
@test parse_brackets("foo{bob, joe}") ==
Expand All @@ -363,6 +369,7 @@ include("setup_tests.jl")
@test_throws ParseError parse_brackets("{{ joe{bob, bill} }")
@test_throws ParseError parse_brackets("{ joe{bob,} bill} }")
@test_throws ParseError parse_brackets("{ joe{bob, {bill} }")
@test_throws ParseError parse_brackets("")

# verify parser round-trip
round_trips(str) = parse_walker((fn, p) -> nothing, parse_brackets(str)) == str
Expand Down Expand Up @@ -390,6 +397,7 @@ include("setup_tests.jl")
@test replace_bob("foo{bar{baz, bob}, boz}") == "foo{bar{baz, BOB}, boz}"

# validate the named tuple replaceer
@test cleanup_named_tuple_type(string(typeof((;)))) == "NamedTuple{(),Tuple{}}"
@test cleanup_named_tuple_type("@NamedTuple{x::Int, y::Int}") ==
"NamedTuple{(:x,:y),Tuple{Int,Int}}"
@test cleanup_named_tuple_type("FooBar{Baz{Float64, (custom, display(}, " *
Expand Down

3 comments on commit 1724ffa

@haberdashPI
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@haberdashPI
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/99169

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.1.6 -m "<description of version>" 1724ffa1fc3f9311b9b33e36fdf209e8042b8bfc
git push origin v1.1.6

Please sign in to comment.