Skip to content

Commit

Permalink
Merge pull request #296 from GenieFramework/hh-parsing
Browse files Browse the repository at this point in the history
Better support for Union types with Nothing
  • Loading branch information
hhaensel authored Oct 29, 2024
2 parents c57a9c6 + 5b924cc commit 50c7ca6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Stipple"
uuid = "4acbeb90-81a0-11ea-1966-bdaff8155998"
authors = ["Adrian <[email protected]>"]
version = "0.30.10"
version = "0.30.11"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
11 changes: 10 additions & 1 deletion src/stipple/parsers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ function stipple_parse(::Type{T}, value) where T
end
end

function stipple_parse(::Type{T}, value::AbstractString) where T<:AbstractString
convert(T, value)
end

# function stipple_parse(::Type{T}, value::Dict) where T <: AbstractDict
# convert(T, value)
# end
Expand Down Expand Up @@ -79,11 +83,16 @@ function stipple_parse(::Type{Union{Nothing, T}}, ::Nothing) where T
nothing
end

# Union with Nothing
# Union with Nothing, part I
function stipple_parse(::Type{Union{Nothing, T}}, value) where T
stipple_parse(T, value)
end

# Union with Nothing, part II
function stipple_parse(::Type{Union{Nothing, T}}, value::Union{Nothing, T}) where T
stipple_parse(T, value)
end

# define an explicit function for Type{Any} to avoid ambiguities between Type{Union{Nothing, T}} and Type{T} (line 35 and line 64) in case of T == Any
function stipple_parse(::Type{Any}, v::T) where {T}
v::T
Expand Down
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -594,4 +594,15 @@ end
@test Stipple.stipple_parse(T2, t2_dict) == T2(1, T1(2, 3))
@test Stipple.stipple_parse(T3, Dict()) == T3(1, 3)
@test Stipple.stipple_parse(T4, Dict()) == T4(1, T3(1, 3))

@test Stipple.stipple_parse(Union{Nothing, String}, "hi") == "hi"
@test Stipple.stipple_parse(Union{Nothing, String}, SubString("hi")) == "hi"
# the following test is only valid for Julia 1.7 and above because specifity of methods
# changed in Julia 1.7. As the latest LTS version of Julia is now 1.10, we accept that
# this specific stipple_parse for Union{Nothing, T} fails for Julia 1.6
# people can define explicit methods for their types if they need this functionality
@static if VERSION v"1.7"
@test Stipple.stipple_parse(Union{Nothing, SubString}, "hi") == SubString("hi")
end
@test Stipple.stipple_parse(Union{Nothing, String}, nothing) === nothing
end

2 comments on commit 50c7ca6

@hhaensel
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
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/118317

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 v0.30.11 -m "<description of version>" 50c7ca6624653620a1270f5354e1573caab7254c
git push origin v0.30.11

Please sign in to comment.