Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using Tricks.static_hasmethod #135

Merged
merged 3 commits into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ EzXML = "8f5d6c58-4d21-5cfd-889c-e3ad7ee6a615"
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
Tricks = "410a4b4d-49e4-4fbc-ab6d-cb71b17b3775"
TypeTransform = "0fb2cfb0-ef68-4639-af2f-cb92dac991ae"

[compat]
Expand All @@ -16,6 +17,7 @@ PrettyTables = "0.8, 0.9"
Reexport = "^0.2.0"
Tables = "0.2, 1.0"
TypeTransform = "0.1.3"
Tricks = "0.1.3"
julia = "1.2"

[extras]
Expand Down
7 changes: 7 additions & 0 deletions benchmark/benchmark_result.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
# Benchmark Result

v 0.10.3
Outside Atom
8.700 μs (100 allocations: 3.61 KiB)
3.957 μs (66 allocations: 3.17 KiB)
201.799 μs (401 allocations: 16.27 KiB)

v 0.10.2
Outside Atom
8.700 μs (100 allocations: 3.61 KiB)
Expand Down
21 changes: 12 additions & 9 deletions src/xmlutils/nodeparse.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# TODO use traits for hasmethod checks
using Tricks: compat_hasmethod
# TODO use traits for compat_hasmethod checks

# Scalar parsers

Expand All @@ -17,9 +18,9 @@ function nodeparse(type::Type, content::String)
# https://julialang.slack.com/archives/C6A044SQH/p1578442480438100
if hasmethod(type, Tuple{String}) && Core.Compiler.return_type(type, Tuple{Node})=== Union{}
return type(content)
elseif hasmethod(convert, Tuple{String, type})
elseif compat_hasmethod(convert, Tuple{String, type})
return convert(type, content)
elseif hasmethod(parse, Tuple{type, String})
elseif compat_hasmethod(parse, Tuple{type, String})
return parse(type, content)
else
error("Could not parse a String as type $type")
Expand All @@ -32,9 +33,9 @@ function nodeparse(type::Type, elm::Node)
content = elm.content
if hasmethod(type, Tuple{String}) && Core.Compiler.return_type(type, Tuple{Node})=== Union{}
return type(content)
elseif hasmethod(convert, Tuple{String, type})
elseif compat_hasmethod(convert, Tuple{String, type})
return convert(type, content)
elseif hasmethod(parse, Tuple{type, String})
elseif compat_hasmethod(parse, Tuple{type, String})
return parse(type, content)
else
# should be the last to avoid invoking generic methods
Expand All @@ -58,17 +59,18 @@ end
function nodeparse(::Type{T}, contents::Vector{String}) where {T}
elms_typed = Vector{T}(undef, length(contents))
i = 1
# TODO https://github.com/oxinabox/Tricks.jl/issues/2#issuecomment-630450764
if hasmethod(type, Tuple{String}) && Core.Compiler.return_type(type, Tuple{Node})=== Union{}
for content in contents
elms_typed[i] = type(content)
i+=1
end
elseif hasmethod(convert, Tuple{String, type})
elseif compat_hasmethod(convert, Tuple{String, type})
for content in contents
elms_typed[i] = convert(type, content)
i+=1
end
elseif hasmethod(parse, Tuple{type, String})
elseif compat_hasmethod(parse, Tuple{type, String})
for content in contents
elms_typed[i] = parse(type, content)
i+=1
Expand All @@ -82,17 +84,18 @@ end
function nodeparse(type::Type{T}, elms::Vector{Node}) where {T}
elms_typed = Vector{T}(undef, length(elms))
i = 1
# TODO https://github.com/oxinabox/Tricks.jl/issues/2#issuecomment-630450764
if hasmethod(type, Tuple{String}) && Core.Compiler.return_type(type, Tuple{Node})=== Union{}
for elm in elms
elms_typed[i] = type(elm.content)
i+=1
end
elseif hasmethod(convert, Tuple{String, type})
elseif compat_hasmethod(convert, Tuple{String, type})
for elm in elms
elms_typed[i] = convert(type, elm.content)
i+=1
end
elseif hasmethod(parse, Tuple{type, String})
elseif compat_hasmethod(parse, Tuple{type, String})
for elm in elms
elms_typed[i] = parse(type, elm.content)
i+=1
Expand Down