Skip to content

Commit 654a652

Browse files
committed
automatically determine data types for xlat functions
and add range test which uses it, and the new %range() function
1 parent 8a32c71 commit 654a652

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/lib/unlang/compile.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ RCSID("$Id$")
3939
#include <freeradius-devel/util/time.h>
4040
#include <freeradius-devel/util/dict.h>
4141

42+
#include <freeradius-devel/unlang/xlat_priv.h>
43+
4244
#include "catch_priv.h"
4345
#include "call_priv.h"
4446
#include "caller_priv.h"
@@ -3313,14 +3315,32 @@ static unlang_t *compile_foreach(unlang_t *parent, unlang_compile_t *unlang_ctx,
33133315

33143316
if (tmpl_is_xlat(vpt)) {
33153317
if (!type_name) {
3316-
/*
3317-
* @todo - find a way to get the data type.
3318-
*/
33193318
cf_log_err(cs, "Dynamic expansions MUST specify a data type for the variable");
33203319
return NULL;
33213320
}
33223321

33233322
type = fr_table_value_by_str(fr_type_table, type_name, FR_TYPE_VOID);
3323+
3324+
/*
3325+
* No data type was specified, see if we can get one from the function.
3326+
*/
3327+
if (type == FR_TYPE_NULL) {
3328+
xlat_exp_head_t *xlat = tmpl_xlat(vpt);
3329+
xlat_exp_t *node;
3330+
3331+
node = xlat_exp_head(xlat);
3332+
fr_assert(node);
3333+
if (!xlat_exp_next(xlat, node) &&
3334+
(node->type == XLAT_FUNC) &
3335+
fr_type_is_leaf(node->call.func->return_type)) {
3336+
type = node->call.func->return_type;
3337+
if (fr_type_is_leaf(type)) goto get_name;
3338+
}
3339+
3340+
cf_log_err(cs, "Unable to determine return data type from dynamic expansion");
3341+
return NULL;
3342+
}
3343+
33243344
if (!fr_type_is_leaf(type)) {
33253345
cf_log_err(cs, "Dynamic expansions MUST specify a non-structural data type for the variable");
33263346
return NULL;

src/tests/keywords/range

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
octets foo
2+
3+
foreach i (%range(4)) {
4+
foo += (octets) i
5+
}
6+
7+
if (foo != 0x0000000000000000000000000000000100000000000000020000000000000003) {
8+
test_fail
9+
}
10+
success

0 commit comments

Comments
 (0)