From: Arran Cudbard-Bell Date: Fri, 2 Jan 2015 23:36:29 +0000 (-0500) Subject: Allow foreach to operate on lists X-Git-Tag: release_3_0_7~368 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dadf916e700e738f4fbf8fcce45447da163c2703;p=thirdparty%2Ffreeradius-server.git Allow foreach to operate on lists No extra code here, just removing restrictions. This was made possible a while ago by the tmpl cursor functions. --- diff --git a/src/main/modcall.c b/src/main/modcall.c index 3bf428039f7..00593c6adf6 100644 --- a/src/main/modcall.c +++ b/src/main/modcall.c @@ -2051,8 +2051,8 @@ static modcallable *do_compile_modforeach(modcallable *parent, return NULL; } - if (vpt && (vpt->type != TMPL_TYPE_ATTR)) { - cf_log_err_cs(cs, "MUST use attribute reference in 'foreach'"); + if (vpt && (vpt->type != TMPL_TYPE_ATTR) && (vpt->type != TMPL_TYPE_LIST)) { + cf_log_err_cs(cs, "MUST use attribute or list reference in 'foreach'"); return NULL; } @@ -2061,9 +2061,7 @@ static modcallable *do_compile_modforeach(modcallable *parent, * the attribute. In a perfect consistent world, users would do * foreach &attr[*], but that's taking the consistency thing a bit far. */ - if (vpt && (vpt->type == TMPL_TYPE_ATTR)) { - vpt->tmpl_num = NUM_ALL; - } + vpt->tmpl_num = NUM_ALL; csingle = do_compile_modgroup(parent, component, cs, GROUPTYPE_SIMPLE, GROUPTYPE_SIMPLE, @@ -3624,7 +3622,7 @@ bool modcall_pass2(modcallable *mc) if (slen < 0) goto parse_error; check_children: - rad_assert(g->vpt->type == TMPL_TYPE_ATTR); + rad_assert((g->vpt->type == TMPL_TYPE_ATTR) || (g->vpt->type == TMPL_TYPE_LIST)); if (g->vpt->tmpl_num != NUM_ALL) { cf_log_err_cs(g->cs, "MUST NOT use instance selectors in 'foreach'"); return false; diff --git a/src/tests/keywords/foreach-list b/src/tests/keywords/foreach-list new file mode 100644 index 00000000000..4780e4fdf1e --- /dev/null +++ b/src/tests/keywords/foreach-list @@ -0,0 +1,5 @@ +foreach &request: { + update reply { + Called-Station-Id += "%{Foreach-Variable-0}" + } +} diff --git a/src/tests/keywords/foreach-list.attrs b/src/tests/keywords/foreach-list.attrs new file mode 100644 index 00000000000..aedd5990c49 --- /dev/null +++ b/src/tests/keywords/foreach-list.attrs @@ -0,0 +1,21 @@ +# +# Input packet +# +User-Name = "bob" +User-Password = "hello" +Filter-Id = "1" +Filter-Id += "2" +Filter-Id += "3" +Filter-Id += "4" + +# +# Expected answer +# +Response-Packet-Type == Access-Accept +Called-Station-Id == 'bob' +Called-Station-Id == 'hello' +Called-Station-Id == '1' +Called-Station-Id == '2' +Called-Station-Id == '3' +Called-Station-Id == '4' +