From: Alan T. DeKok Date: Tue, 28 Oct 2025 13:06:35 +0000 (-0400) Subject: omit printing a union if it has only one child. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f64f714cec48c0809764e94763902a32b866bd38;p=thirdparty%2Ffreeradius-server.git omit printing a union if it has only one child. this simplifies the output and makes it easier to understand. it's also the counterpart to adding the ALIAS which points from the parent of the union, to the union member. Other parts of the server will need to be updated to omit unions, too. But for now, this is good enough for tests. --- diff --git a/src/lib/util/pair_print.c b/src/lib/util/pair_print.c index 10bf684302e..75412ac5897 100644 --- a/src/lib/util/pair_print.c +++ b/src/lib/util/pair_print.c @@ -121,6 +121,15 @@ ssize_t fr_pair_print(fr_sbuff_t *out, fr_dict_attr_t const *parent, fr_pair_t c PAIR_VERIFY(vp); + /* + * Omit the union if we can. + */ + if ((vp->vp_type == FR_TYPE_UNION) && + (fr_pair_list_num_elements(&vp->vp_group) == 1)) { + parent = vp->da; + vp = fr_pair_list_head(&vp->vp_group); + } + if ((vp->op > T_INVALID) && (vp->op < T_TOKEN_LAST)) { token = fr_tokens[vp->op]; } else { @@ -143,7 +152,9 @@ ssize_t fr_pair_print(fr_sbuff_t *out, fr_dict_attr_t const *parent, fr_pair_t c FR_SBUFF_IN_CHAR_RETURN(&our_out, ':', ':'); FR_SBUFF_IN_STRCPY_RETURN(&our_out, name); + } else { + no_enumv: FR_SBUFF_RETURN(fr_pair_print_value_quoted, &our_out, vp, T_DOUBLE_QUOTED_STRING); } diff --git a/src/tests/unit/protocols/radius/union.txt b/src/tests/unit/protocols/radius/union.txt index bec9babdda8..601e94d6ed4 100644 --- a/src/tests/unit/protocols/radius/union.txt +++ b/src/tests/unit/protocols/radius/union.txt @@ -18,7 +18,7 @@ fuzzer-out radius # attribute "truncated" of type bool. # decode-pair fd 04 01 21 -match Test-Struct2 = { Key-Field = ::Sub-Struct, Data = { Sub-Struct = { Nested-Uint1 = 33 } } } +match Test-Struct2 = { Key-Field = ::Sub-Struct, Sub-Struct = { Nested-Uint1 = 33 } } #match raw.Test-Struct2 = 0x0121 # @@ -33,7 +33,7 @@ match fd 05 01 21 00 # @todo - perhaps we want an internal attribute for "extra" data, of type octets? # decode-pair fd 06 01 21 12 33 -match Test-Struct2 = { Key-Field = ::Sub-Struct, Data = { Sub-Struct = { Nested-Uint1 = 33, Nested-Uint2 = 18 } } } +match Test-Struct2 = { Key-Field = ::Sub-Struct, Sub-Struct = { Nested-Uint1 = 33, Nested-Uint2 = 18 } } encode-pair - match fd 05 01 21 12 @@ -42,7 +42,7 @@ match fd 05 01 21 12 # Just right. # decode-pair fd 05 01 21 12 -match Test-Struct2 = { Key-Field = ::Sub-Struct, Data = { Sub-Struct = { Nested-Uint1 = 33, Nested-Uint2 = 18 } } } +match Test-Struct2 = { Key-Field = ::Sub-Struct, Sub-Struct = { Nested-Uint1 = 33, Nested-Uint2 = 18 } } encode-pair - match fd 05 01 21 12 @@ -50,14 +50,14 @@ match fd 05 01 21 12 # # Without Key-Field - it automatically figures it out # -encode-pair Test-Struct2 = { Data = { Sub-Struct = { Nested-Uint1 = 33, Nested-Uint2 = 18 } } } +encode-pair Test-Struct2 = { Sub-Struct = { Nested-Uint1 = 33, Nested-Uint2 = 18 } } match fd 05 01 21 12 # # Skip the UNION # pair Test-Struct2 = { Sub-Struct = { Nested-Uint1 = 33, Nested-Uint2 = 18 } } -match Test-Struct2 = { Data = { Sub-Struct = { Nested-Uint1 = 33, Nested-Uint2 = 18 } } } +match Test-Struct2 = { Sub-Struct = { Nested-Uint1 = 33, Nested-Uint2 = 18 } } encode-pair Test-Struct2 = { Sub-Struct = { Nested-Uint1 = 33, Nested-Uint2 = 18 } } match fd 05 01 21 12 @@ -67,11 +67,11 @@ match fd 05 01 21 12 # # @todo - to we want to update the parser to disallow bare key fields like this? # -encode-pair Test-Struct2 = { Key-Field = 2, Data = { Sub-Struct = { Nested-Uint1 = 33, Nested-Uint2 = 18 } } } +encode-pair Test-Struct2 = { Key-Field = 2, Sub-Struct = { Nested-Uint1 = 33, Nested-Uint2 = 18 } } match fd 05 01 21 12 decode-pair fd 05 02 43 41 -match Test-Struct2 = { Key-Field = ::CC, Data = { CC = "CA" } } +match Test-Struct2 = { Key-Field = ::CC, CC = "CA" } encode-pair - match fd 05 02 43 41