Skip to content

Commit

Permalink
Use eldeser
Browse files Browse the repository at this point in the history
  • Loading branch information
gryumov committed Jan 3, 2025
1 parent e843711 commit 5e02068
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/De/Deser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,6 @@ Computer("N/A", "N/A")
(nulltype(::Type{Union{Nothing,T}})::Nothing) where {T<:Any} = nothing
(nulltype(::Type{Union{Missing,T}})::Missing) where {T<:Any} = missing

_fieldtypes(::Type{T}) where {T} = Tuple(fieldtype(T, x) for x in fieldnames(T))

"""
Serde.deser(::Type{T}, data) -> T
Expand Down Expand Up @@ -352,7 +350,7 @@ function deser(::DictType, ::Type{T}, data::AbstractDict{K,D})::T where {T<:Abst
return target
end

function eldeser(ct::Type, ft::Type, key::K, data::D) where {K<:Union{AbstractString,Symbol},D}
@inline function eldeser(ct::Type, ft::Type, key::K, data::D) where {K,D}
try
return data isa ft ? data : deser(ct, ft, data)
catch e
Expand All @@ -368,18 +366,17 @@ end

function deser(::CustomType, ::Type{T}, data::AbstractVector{A})::T where {T<:Any,A<:Any}
target = Any[]
for (index, type) in enumerate(_fieldtypes(T))
for (index, type) in enumerate(fieldtypes(T))
val = get(data, index, nulltype(type))
val = isempty(T, val) ? nulltype(type) : val
val = !(val isa type) ? deser(T, type, val) : val
push!(target, val)
push!(target, eldeser(T, type, index, val))
end
return T(target...)
end

function deser(::CustomType, ::Type{T}, data::AbstractDict{K,D})::T where {T<:Any,K<:Union{AbstractString,Symbol},D<:Any}
target = Any[]
for (type, name) in zip(_fieldtypes(T), fieldnames(T))
for (type, name) in zip(fieldtypes(T), fieldnames(T))
key = custom_name(T, Val(name))
val = get(data, K(key), default_value(T, Val(name)))
val = isnothing(val) || ismissing(val) || isempty(T, val) ? nulltype(type) : val
Expand All @@ -390,7 +387,7 @@ end

function deser(::CustomType, ::Type{T}, data::N)::T where {T<:Any,N<:NamedTuple}
target = Any[]
for (type, name) in zip(_fieldtypes(T), fieldnames(T))
for (type, name) in zip(fieldtypes(T), fieldnames(T))
key = custom_name(T, Val(name))
val = get(data, key, default_value(T, Val(name)))
val = isnothing(val) || ismissing(val) || isempty(T, val) ? nulltype(type) : val
Expand Down

0 comments on commit 5e02068

Please sign in to comment.