diff --git a/Project.toml b/Project.toml index 1805f906..53acf600 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Stipple" uuid = "4acbeb90-81a0-11ea-1966-bdaff8155998" authors = ["Adrian "] -version = "0.30.10" +version = "0.30.11" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" diff --git a/src/stipple/parsers.jl b/src/stipple/parsers.jl index 4993ab48..ab623adb 100644 --- a/src/stipple/parsers.jl +++ b/src/stipple/parsers.jl @@ -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 @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 32393c5e..dffaf611 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 \ No newline at end of file