Skip to content

Commit 325f059

Browse files
committed
Fix accessory lookups for optional pointers
1 parent 4b38e38 commit 325f059

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/necsus/compiletime/converters.nim

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ proc newAdapter*(
3939

4040
proc addAccessoryCondition(existing: NimNode, adapter: TupleAdapter, predicate: NimNode): NimNode =
4141
## Adds a boolean check to see if an accessory component passes a predicate
42-
if adapter.optionIn and not adapter.optionOut:
42+
if adapter.optionIn and (not adapter.optionOut or adapter.pointerOut):
4343
let read = nnkBracketExpr.newTree(adapter.source, adapter.index.newLit)
4444
return quote: `existing` and `predicate`(`read`)
4545
else:
@@ -76,6 +76,8 @@ proc copyTuple(fromArch: Archetype[ComponentDef], newVals: openarray[ComponentDe
7676

7777
of DirectiveArgKind.Optional:
7878
if adapter.index >= 0:
79+
if adapter.optionIn:
80+
condition = condition.addAccessoryCondition(adapter, bindSym("isSome"))
7981
adapter.build()
8082
else:
8183
newCall(nnkBracketExpr.newTree(bindSym("none"), arg.type))

tests/t_accessory_lookup.nim

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,22 @@ proc exec(
1313
spawn3: FullSpawn[(Person, Name, Age)],
1414
lookup1: Lookup[(Name, Not[Age])],
1515
lookup2: Lookup[(Name, Age)],
16+
lookup3: Lookup[(Name, Option[ptr Age])],
1617
) =
1718
let first = spawn1.with(Person())
1819
check(first.lookup1().isNone())
1920
check(first.lookup2().isNone())
21+
check(first.lookup3().isNone())
2022

2123
let second = spawn2.with(Person(), "Jack")
2224
check(second.lookup1().get()[0] == "Jack")
2325
check(second.lookup2().isNone())
26+
check(second.lookup3().isNone())
2427

2528
let third = spawn3.with(Person(), "Jack", 25)
26-
check(third.lookup1().get()[0] == "Jack")
29+
check(third.lookup1().isNone())
2730
check(third.lookup2() == some(("Jack", 25)))
31+
check(third.lookup3().isSome())
2832

2933
proc runner(tick: proc(): void) = tick()
3034

0 commit comments

Comments
 (0)