Skip to content

Commit 13c74d4

Browse files
committed
use vb_foo instead of datum.foo
and update comments to note that '%l' and '%c' are NOT dates, but instead integers. If they were of type 'date', then they would be printed as dates, and not as integers.
1 parent f52028a commit 13c74d4

File tree

2 files changed

+42
-30
lines changed

2 files changed

+42
-30
lines changed

src/lib/unlang/xlat_eval.c

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -829,17 +829,17 @@ xlat_action_t xlat_eval_one_letter(TALLOC_CTX *ctx, fr_value_box_list_t *out,
829829

830830
case 'I': /* Request ID */
831831
MEM(value = fr_value_box_alloc(ctx, FR_TYPE_UINT32, NULL));
832-
value->datum.uint32 = request->packet->id;
832+
value->vb_uint32 = request->packet->id;
833833
break;
834834

835835
case 'n': /* Request number */
836836
MEM(value = fr_value_box_alloc(ctx, FR_TYPE_UINT64, NULL));
837-
value->datum.uint64 = request->number;
837+
value->vb_uint64 = request->number;
838838
break;
839839

840840
case 's': /* First request in this sequence */
841841
MEM(value = fr_value_box_alloc(ctx, FR_TYPE_UINT64, NULL));
842-
value->datum.uint64 = request->seq_start;
842+
value->vb_uint64 = request->seq_start;
843843
break;
844844

845845
/*
@@ -848,19 +848,17 @@ xlat_action_t xlat_eval_one_letter(TALLOC_CTX *ctx, fr_value_box_list_t *out,
848848

849849
case 'c': /* Current epoch time seconds */
850850
/*
851-
* @todo - leave this as FR_TYPE_DATE, but add an enumv which changes the scale to
852-
* seconds?
851+
* Note that this number MUST be an integer,
852+
* otherwise it will get printed as an actual
853+
* date!
853854
*/
854855
MEM(value = fr_value_box_alloc(ctx, FR_TYPE_UINT64, NULL));
855-
value->datum.uint64 = (uint64_t)fr_time_to_sec(fr_time());
856+
value->vb_uint64 = (uint64_t)fr_time_to_sec(fr_time());
856857
break;
857858

858859
case 'C': /* Current epoch time microsecond component */
859-
/*
860-
* @todo - we probably should remove this now that we have FR_TYPE_DATE with scaling.
861-
*/
862860
MEM(value = fr_value_box_alloc(ctx, FR_TYPE_UINT64, NULL));
863-
value->datum.uint64 = (uint64_t)fr_time_to_usec(fr_time()) % 1000000;
861+
value->vb_uint64 = (uint64_t)fr_time_to_usec(fr_time()) % 1000000;
864862
break;
865863

866864
/*
@@ -875,7 +873,7 @@ xlat_action_t xlat_eval_one_letter(TALLOC_CTX *ctx, fr_value_box_list_t *out,
875873
}
876874

877875
MEM(value = fr_value_box_alloc(ctx, FR_TYPE_UINT8, NULL));
878-
value->datum.uint8 = ts.tm_mday;
876+
value->vb_uint8 = ts.tm_mday;
879877
break;
880878

881879
case 'D': /* Request date */
@@ -891,45 +889,43 @@ xlat_action_t xlat_eval_one_letter(TALLOC_CTX *ctx, fr_value_box_list_t *out,
891889
if (!localtime_r(&now, &ts)) goto error;
892890

893891
MEM(value = fr_value_box_alloc(ctx, FR_TYPE_UINT8, NULL));
894-
value->datum.uint8 = ts.tm_sec;
892+
value->vb_uint8 = ts.tm_sec;
895893
break;
896894

897895
case 'G': /* Request minute */
898896
if (!localtime_r(&now, &ts)) goto error;
899897

900898
MEM(value = fr_value_box_alloc(ctx, FR_TYPE_UINT8, NULL));
901-
value->datum.uint8 = ts.tm_min;
899+
value->vb_uint8 = ts.tm_min;
902900
break;
903901

904902
case 'H': /* Request hour */
905903
if (!localtime_r(&now, &ts)) goto error;
906904

907905
MEM(value = fr_value_box_alloc(ctx, FR_TYPE_UINT8, NULL));
908-
value->datum.uint8 = ts.tm_hour;
906+
value->vb_uint8 = ts.tm_hour;
909907
break;
910908

911909
case 'l': /* Request timestamp as seconds since the epoch */
912910
/*
913-
* @todo - leave this as FR_TYPE_DATE, but add an enumv which changes the scale to
914-
* seconds?
911+
* Note that this number MUST be an integer,
912+
* otherwise it will get printed as an actual
913+
* date!
915914
*/
916915
MEM(value = fr_value_box_alloc(ctx, FR_TYPE_UINT64, NULL));
917-
value->datum.uint64 = (uint64_t) now;
916+
value->vb_uint64 = (uint64_t) now;
918917
break;
919918

920919
case 'm': /* Request month */
921920
if (!localtime_r(&now, &ts)) goto error;
922921

923922
MEM(value = fr_value_box_alloc(ctx, FR_TYPE_UINT8, NULL));
924-
value->datum.uint8 = ts.tm_mon + 1;
923+
value->vb_uint8 = ts.tm_mon + 1;
925924
break;
926925

927926
case 'M': /* Request time microsecond component */
928-
/*
929-
* @todo - we probably should remove this now that we have FR_TYPE_DATE with scaling.
930-
*/
931927
MEM(value = fr_value_box_alloc(ctx, FR_TYPE_UINT64, NULL));
932-
value->datum.uint64 = (uint64_t)fr_time_to_usec(request->packet->timestamp) % 1000000;
928+
value->vb_uint64 = (uint64_t)fr_time_to_usec(request->packet->timestamp) % 1000000;
933929
break;
934930

935931
case 'S': /* Request timestamp in SQL format */
@@ -979,7 +975,7 @@ xlat_action_t xlat_eval_one_letter(TALLOC_CTX *ctx, fr_value_box_list_t *out,
979975

980976
MEM(value = fr_value_box_alloc(ctx, FR_TYPE_UINT16, NULL));
981977

982-
value->datum.uint16 = ts.tm_year + 1900;
978+
value->vb_uint16 = ts.tm_year + 1900;
983979
break;
984980

985981
default:

src/tests/keywords/expr

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#
44
uint32 test_integer1
55
uint32 test_integer2
6+
int64 test_integer
7+
string test_string
68
date test_date
79

810
#
@@ -30,7 +32,7 @@ if !(1 + 2 * 3 + 4 == 11) {
3032
#
3133
test_integer1 := 3
3234
test_integer2 := 4
33-
test_date := "%l"
35+
test_date := %l
3436

3537
if (!(%{ 1 + 2 * test_integer1 + 4} == 11)) {
3638
test_fail
@@ -96,12 +98,6 @@ if !(((1 << 2) | 1) == 5) { # needs extra () to resolve precedence
9698
test_fail
9799
}
98100

99-
if ("%{ test_date}" <= 0) {
100-
test_fail
101-
} else {
102-
# nothing! Previous expansion has failed, so it MUST have an "else"
103-
}
104-
105101
if (test_date <= 0) {
106102
test_fail
107103
}
@@ -123,4 +119,24 @@ if !(6 * -test_integer2 == -24) {
123119
test_fail
124120
}
125121

122+
test_string = "%l"
123+
if (test_string !~ /^[0-9]+$/) {
124+
test_fail
125+
}
126+
127+
test_string = "%c"
128+
if (test_string !~ /^[0-9]+$/) {
129+
test_fail
130+
}
131+
132+
#
133+
# %l is request receive time. %c is wall-clock "now"
134+
#
135+
# They should be reasonably close for the unit tests.
136+
#
137+
test_integer = %l - %c
138+
if ((test_integer < -2) || (test_integer > 2)) {
139+
test_fail
140+
}
141+
126142
success

0 commit comments

Comments
 (0)