Skip to content

Commit a42848a

Browse files
authored
Fix applyfield when applied to non-existent fields (#63)
* Fix applyfield when applied to non-existent fields `applyfield` was inconsistent with `applyfield!` in that it threw an error on non-existent fields rather than return `false`, which the docuementation _said_ it did. This fixes `applyfield` to ignore non-existent fieldnames and return `false`. Alternative fix to adding teh `ignoreextras` function proposed in #52. * fix test
1 parent dc3f601 commit a42848a

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

src/StructTypes.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ mappings will be applied, and the function will be passed the Julia field name.
827827
# unroll the first 32 field checks to avoid dynamic dispatch if possible
828828
Base.@nif(
829829
33,
830-
i -> (i <= N && Base.fieldindex(T, nm) === i && !symbolin(excl, nm)),
830+
i -> (i <= N && fieldname(T, i) === nm && !symbolin(excl, nm)),
831831
i -> begin
832832
FT_i = fieldtype(T, i)
833833
if haskey(kwargs, nm)
@@ -839,7 +839,7 @@ mappings will be applied, and the function will be passed the Julia field name.
839839
end,
840840
i -> begin
841841
for j in 33:N
842-
(Base.fieldindex(T, nm) === j && !symbolin(excl, nm)) || continue
842+
(fieldname(T, j) === nm && !symbolin(excl, nm)) || continue
843843
FT_j = fieldtype(T, j)
844844
if haskey(kwargs, nm)
845845
y_j = f(j, nm, FT_j; kwargs[nm]...)

test/runtests.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ StructTypes.mapfields!((i, nm, T) -> (1, 3.14, "hey")[i], x2)
370370

371371
# NamedTuple
372372
@test StructTypes.construct((i, nm, T) -> (1, 3.14, "hey")[i], NamedTuple{(:a, :b, :c), Tuple{Int64, Float64, String}}) == (a=1, b=3.14, c="hey")
373-
373+
374374
x3 = D(nothing, 3.14, "")
375375
@inline StructTypes.omitempties(::Type{D}) = true
376376
all_i = Int[]
@@ -438,6 +438,8 @@ CNT = Ref(0)
438438
StructTypes.foreachfield((args...) -> CNT[] += 1, EmptyStruct)
439439
@test CNT[] == 0
440440

441+
@test !StructTypes.applyfield((x...) -> 10, A, :z)
442+
441443
@testset "StructTypes.constructfrom(T, ::Vector{Any})" begin
442444

443445
end

0 commit comments

Comments
 (0)