Skip to content

Commit b108d05

Browse files
authored
Fix when omitempties(T) = true is defined in StructTypes.foreachfield (#50)
I noticed in some serialized JSON from JSON3.jl recently that some fields were still getting serialized when I had defined `StructTypes.omitempties(::Type{MyType}) = true`, which is a shorthand we introduced when you want to omit empty values for any field in a type. Unfortunately, we failed to update `StructTypes.foreachfield` to respect this option and double unfortunately, the code still happened to "work", i.e. not throw an error whether `omitempties` returned a `Bool` or `Tuple`. The fix is pretty simple: if `omitempties` returns `true`, then we query the `fieldnames` of the struct.
1 parent ba402e1 commit b108d05

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/StructTypes.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ Various "configurations" are respected when applying `f` to each field:
628628
excl = excludes(T)
629629
nms = names(T)
630630
kwargs = keywordargs(T)
631-
emp = omitempties(T)
631+
emp = omitempties(T) === true ? fieldnames(T) : omitempties(T)
632632
Base.@nexprs 32 i -> begin
633633
k_i = fieldname(T, i)
634634
if !symbolin(excl, k_i) && isdefined(x, i)
@@ -673,7 +673,6 @@ Nothing is returned and results from `f` are ignored. Similar to `Base.foreach`
673673
Various "configurations" are respected when applying `f` to each field:
674674
* If keyword arguments have been defined for a field via `StructTypes.keywordargs`, they will be passed like `f(i, name, FT, v; kw...)`
675675
* If `StructTypes.names` has been defined, `name` will be the serialization name instead of the defined julia field name
676-
* If a field is undefined or empty and `StructTypes.omitempties` is defined, `f` won't be applied to that field
677676
* If a field has been excluded via `StructTypes.excludes`, it will be skipped
678677
"""
679678
@inline function foreachfield(f, ::Type{T}) where {T}
@@ -682,7 +681,6 @@ Various "configurations" are respected when applying `f` to each field:
682681
excl = excludes(T)
683682
nms = names(T)
684683
kwargs = keywordargs(T)
685-
emp = omitempties(T)
686684
Base.@nexprs 32 i -> begin
687685
k_i = fieldname(T, i)
688686
if !symbolin(excl, k_i)

test/runtests.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ end
1313
struct EmptyStruct
1414
end
1515

16+
struct OmitEmp
17+
x::Union{Nothing, Int}
18+
end
19+
1620
@testset "StructTypes" begin
1721

1822
@test StructTypes.StructType(Union{Int, Missing}) == StructTypes.Struct()
@@ -127,6 +131,18 @@ v = v"1.2.3"
127131
x = Dict("hey" => "ho")
128132
@test StructTypes.construct(Dict{String, String}, x) === x
129133

134+
# omitempties(T) = true respected
135+
StructTypes.StructType(::Type{OmitEmp}) = StructTypes.Struct()
136+
StructTypes.omitempties(::Type{OmitEmp}) = true
137+
StructTypes.foreachfield(OmitEmp(1)) do i, nm, T, val
138+
@test val == 1
139+
end
140+
counter = 0
141+
StructTypes.foreachfield(OmitEmp(nothing)) do i, nm, T, val
142+
counter += 1
143+
end
144+
@test counter == 0
145+
130146
end
131147

132148
struct B

0 commit comments

Comments
 (0)