From 78a9e1d2f1094435cbc9bccf73fec38ec94e3b3c Mon Sep 17 00:00:00 2001 From: "Alan T. DeKok" Date: Fri, 30 Aug 2024 14:26:28 -0400 Subject: [PATCH] allow modification of leaf attributes in "foreach" --- src/lib/unlang/foreach.c | 8 ++++++++ src/tests/keywords/foreach-modify | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 src/tests/keywords/foreach-modify diff --git a/src/lib/unlang/foreach.c b/src/lib/unlang/foreach.c index 91c004ea44..623a63bab6 100644 --- a/src/lib/unlang/foreach.c +++ b/src/lib/unlang/foreach.c @@ -135,6 +135,14 @@ static unlang_action_t unlang_foreach_next(rlm_rcode_t *p_result, request_t *req if (is_stack_unwinding_to_break(request->stack)) return UNLANG_ACTION_CALCULATE_RESULT; + vp = fr_dcursor_current(&state->cursor); + fr_assert(vp != NULL); + + if (!fr_type_is_structural(vp->vp_type) && (vp->vp_type == state->key->vp_type)) { + fr_value_box_clear_value(&vp->data); + (void) fr_value_box_copy(vp, &vp->data, &state->key->data); + } + next: vp = fr_dcursor_next(&state->cursor); if (!vp) { diff --git a/src/tests/keywords/foreach-modify b/src/tests/keywords/foreach-modify new file mode 100644 index 0000000000..1115dc5717 --- /dev/null +++ b/src/tests/keywords/foreach-modify @@ -0,0 +1,27 @@ +# +# PRE: foreach +# + +&Tmp-Integer-0 := { 1, 3, 5, 11 } + +foreach uint32 self (&Tmp-Integer-0) { + &self += 19 +} + +if (&Tmp-Integer-0[0] != 20) { + test_fail +} + +if (&Tmp-Integer-0[1] != 22) { + test_fail +} + +if (&Tmp-Integer-0[2] != 24) { + test_fail +} + +if (&Tmp-Integer-0[3] != 30) { + test_fail +} + +success -- 2.47.2