Skip to content

Commit

Permalink
I47 fix CSV deserialization of Union{Nulltype,AnyType} (#52)
Browse files Browse the repository at this point in the history
Co-authored-by: Stas Gryumov <[email protected]>
  • Loading branch information
RongkunWang and gryumov authored May 23, 2024
1 parent 7f36b84 commit 4508992
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

The latest version of this file can be found at the master branch of the [Serde.jl](https://bhftbootcamp.github.io/Serde.jl) repository.

## 3.0.4 (21/05/2024)
- Supports deserialization of Union{Nulltype,AnyType} type

## 3.0.0 (22/03/2024)

### Added
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Serde"
uuid = "db9b398d-9517-45f8-9a95-92af99003e0e"
version = "3.0.3"
version = "3.0.4"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Expand Down
6 changes: 2 additions & 4 deletions src/De/Deser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,7 @@ function deser(
for (type, name) in zip(_field_types(D), fieldnames(D))
key = custom_name(D, Val(name))
val = get(data, K(key), default_value(D, Val(name)))
val = isnothing(val) ? nulltype(type) : val
val = isempty(D, val) ? nulltype(type) : val
val = isnothing(val) || ismissing(val) || isempty(D, val) ? nulltype(type) : val
push!(vals, eldeser(D, type, key, val))
end

Expand All @@ -538,8 +537,7 @@ function deser(::CustomType, ::Type{D}, data::N)::D where {D<:Any,N<:NamedTuple}
for (type, name) in zip(_field_types(D), fieldnames(D))
key = custom_name(D, Val(name))
val = get(data, key, default_value(D, Val(name)))
val = isnothing(val) ? nulltype(type) : val
val = isempty(D, val) ? nulltype(type) : val
val = isnothing(val) || ismissing(val) || isempty(D, val) ? nulltype(type) : val
push!(vals, eldeser(D, type, key, val))
end

Expand Down
18 changes: 18 additions & 0 deletions test/deser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -664,4 +664,22 @@ using Test, Dates
exp_str,
)
end

@testset "Case №38: Deserialization with missing field with Union{Nulltype,AnyType}" begin
struct WithNothing
y::Int64
x::Union{String,Nothing}
end

@test Serde.deser(WithNothing, Dict("y" => 1, "x" => nothing)) == WithNothing(1, nothing)
@test Serde.deser(WithNothing, Dict("y" => 2, "x" => missing)) == WithNothing(2, nothing)

struct WithMissing
y::Int64
x::Union{String,Missing}
end

@test Serde.deser(WithMissing, Dict("y" => 3, "x" => nothing)) == WithMissing(3, missing)
@test Serde.deser(WithMissing, Dict("y" => 4, "x" => missing)) == WithMissing(4, missing)
end
end

2 comments on commit 4508992

@gryumov
Copy link
Member

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

Release notes:

Fix deserialization of Union{Nulltype,AnyType}

@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/107477

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 v3.0.4 -m "<description of version>" 4508992e0b825d26c0059cc3cffa72f48705349e
git push origin v3.0.4

Please sign in to comment.