<p dir="auto">As suggested in previous PR (<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="1056082604" data-permission-text="Title is private" data-url="https://github.com/kamailio/kamailio/issues/2936" data-hovercard-type="pull_request" data-hovercard-url="/kamailio/kamailio/pull/2936/hovercard" href="https://github.com/kamailio/kamailio/pull/2936">#2936</a>).<br>
This is much more flexible as it can also handle sub XAVPs, and indexes.</p>
<p dir="auto">This ended up being more complicated than I anticipated, but I wanted to handle all possible cases.<br>
The tests I performed are detailled after, they should cover every possible use case.</p>
<h4 dir="auto">Type Of Change</h4>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> New feature (non-breaking change which adds new functionality)</li>
</ul>
<h4 dir="auto">Checklist:</h4>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> Tested changes locally</li>
</ul>
<h4 dir="auto">Description</h4>
<p dir="auto">Allows counting XAVPs with <code>$cnt($xavp(...))</code><br>
In addition to the <code>$cnt($avp(...))</code> - which is not modified.</p>
<div class="snippet-clipboard-content position-relative overflow-auto" data-snippet-clipboard-copy-content=" /* Usage:
*
* $cnt($xavp(key[*])) : number of XAVPs "key".
* $cnt($xavp(key[n]=>sub[*])) : number of children "sub" in XAVP "key[n]".
* $cnt($xavp(key[*]=>sub[*])) : total number of children "sub" in all XAVPs "key".
*
* $cnt($xavp(key[n])) : 1 or 0 (if this index exists or not).
* $cnt($xavp(key[-n])) : same but with reverse indexing (-1 is the last index).
*
* $cnt($xavp(key[*]=>sub[n])) : number of children "sub[n]" that exist in all XAPVs "key".
*
* $cnt($xavp(key)) is the same as $cnt($xavp(key[*])).
* $cnt($xavp(key=>sub)) is the same as $cnt($xavp(key[*]=>sub[*])).
*
* Note: Usually for a XAVP no index means "index 0", not all.
* But this would be less intuitive in our case for counting.
*/
"><pre><code> /* Usage:
*
* $cnt($xavp(key[*])) : number of XAVPs "key".
* $cnt($xavp(key[n]=>sub[*])) : number of children "sub" in XAVP "key[n]".
* $cnt($xavp(key[*]=>sub[*])) : total number of children "sub" in all XAVPs "key".
*
* $cnt($xavp(key[n])) : 1 or 0 (if this index exists or not).
* $cnt($xavp(key[-n])) : same but with reverse indexing (-1 is the last index).
*
* $cnt($xavp(key[*]=>sub[n])) : number of children "sub[n]" that exist in all XAPVs "key".
*
* $cnt($xavp(key)) is the same as $cnt($xavp(key[*])).
* $cnt($xavp(key=>sub)) is the same as $cnt($xavp(key[*]=>sub[*])).
*
* Note: Usually for a XAVP no index means "index 0", not all.
* But this would be less intuitive in our case for counting.
*/
</code></pre></div>
<h4 dir="auto">Tests</h4>
<p dir="auto">I tried to be exhaustive. All the test cases below do work as expected.</p>
<div class="snippet-clipboard-content position-relative overflow-auto" data-snippet-clipboard-copy-content="$xavp(case1) = "case1.value1";
$xavp(case1) = "case1.value2";
$xavp(case1) = "case1.value3";
$xavp(case2=>sub1) = "case2.value1 sub1.value1";
$xavp(case2[0]=>sub1) = "case2.value1 sub1.value2";
$xavp(case2[0]=>sub1) = "case2.value1 sub1.value3";
$xavp(case2[0]=>sub2) = "case2.value1 sub2.value1";
$xavp(case2[0]=>sub2) = "case2.value1 sub2.value2";
# => will become case2[1] after adding the next entry
$xavp(case2=>sub1) = "case2.value2 sub1.value1";
$xavp(case2[0]=>sub1) = "case2.value2 sub1.value2";
xlog("L_INFO", "----------- TESTS $$cnt($$xavp...) - root XAVPs with no sub XAVPs\n");
xlog("L_INFO", "xavp(case1[0]) : value = $xavp(case1[0]) : cnt (expect: 1) = $cnt($xavp(case1[0]))\n");
xlog("L_INFO", "xavp(case1[1]) : value = $xavp(case1[1]) : cnt (expect: 1) = $cnt($xavp(case1[1]))\n");
xlog("L_INFO", "xavp(case1[-1]) : value = $xavp(case1[-1]) : cnt (expect: 1) = $cnt($xavp(case1[-1]))\n");
xlog("L_INFO", "xavp(case1[-2]) : value = $xavp(case1[-2]) : cnt (expect: 1) = $cnt($xavp(case1[-2]))\n");
xlog("L_INFO", "xavp(case1[-66]) : value = $xavp(case1[-66]) : cnt (expect: 0) = $cnt($xavp(case1[-66]))\n");
xlog("L_INFO", "xavp(case1[66]) : value = $xavp(case1[66]) : cnt (expect: 0) = $cnt($xavp(case1[66]))\n");
xlog("L_INFO", "xavp(case1[*]) : cnt (expect: 3) = $cnt($xavp(case1[*]))\n");
xlog("L_INFO", "xavp(case1) : cnt (expect: 3) = $cnt($xavp(case1))\n");
xlog("L_INFO", "xavp(case1=>sub) : cnt (expect: 0) = $cnt($xavp(case1=>sub))\n");
xlog("L_INFO", "xavp(case1[0]=>sub) : cnt (expect: 0) = $cnt($xavp(case1[0]=>sub))\n");
xlog("L_INFO", "xavp(case1[0]=>sub[0]) : value = $xavp(case1[0]=>sub[0]) : cnt (expect: 0) = $cnt($xavp(case1[0]=>sub[0]))\n");
xlog("L_INFO", "----------- TESTS $$cnt($$xavp...) - root XAVPs using single index\n");
xlog("L_INFO", "xavp(case2[1]=>sub1[0]) : value = $xavp(case2[1]=>sub1[0]) : cnt (expect: 1) = $cnt($xavp(case2[1]=>sub1[0]))\n");
xlog("L_INFO", "xavp(case2[1]=>sub1[-1]) : value = $xavp(case2[1]=>sub1[-1]) : cnt (expect: 1) = $cnt($xavp(case2[1]=>sub1[-1]))\n");
xlog("L_INFO", "xavp(case2[1]=>sub1[3]) : value = $xavp(case2[1]=>sub1[3]) : cnt (expect: 0) = $cnt($xavp(case2[1]=>sub1[3]))\n");
xlog("L_INFO", "xavp(case2[1]=>sub1[*]) : cnt (expect: 3) = $cnt($xavp(case2[1]=>sub1[*]))\n");
xlog("L_INFO", "xavp(case2[1]=>sub1) : cnt (expect: 3) = $cnt($xavp(case2[1]=>sub1))\n");
xlog("L_INFO", "xavp(case2[1]=>sub2[*]) : cnt (expect: 2) = $cnt($xavp(case2[1]=>sub2[*]))\n");
xlog("L_INFO", "xavp(case2[1]=>sub2) : cnt (expect: 2) = $cnt($xavp(case2[1]=>sub2))\n");
xlog("L_INFO", "xavp(case2[66]=>sub1) : cnt (expect: 0) = $cnt($xavp(case2[66]=>sub1))\n");
xlog("L_INFO", "xavp(case2[66]=>sub1[0]) : cnt (expect: 0) = $cnt($xavp(case2[66]=>sub1[0]))\n");
xlog("L_INFO", "----------- TESTS $$cnt($$xavp...) - root XAVPs using all index\n");
xlog("L_INFO", "xavp(case2=>sub1) : cnt (expect: 5) = $cnt($xavp(case2=>sub1))\n");
xlog("L_INFO", "xavp(case2=>sub1[*]) : cnt (expect: 5) = $cnt($xavp(case2=>sub1[*]))\n");
xlog("L_INFO", "xavp(case2[*]=>sub1) : cnt (expect: 5) = $cnt($xavp(case2[*]=>sub1))\n");
xlog("L_INFO", "xavp(case2[*]=>sub1[*]) : cnt (expect: 5) = $cnt($xavp(case2[*]=>sub1[*]))\n");
xlog("L_INFO", "xavp(case2[*]=>sub1) : cnt (expect: 5) = $cnt($xavp(case2[*]=>sub1))\n");
xlog("L_INFO", "xavp(case2[*]=>sub1[0]) : cnt (expect: 2) = $cnt($xavp(case2[*]=>sub1[0]))\n");
xlog("L_INFO", "xavp(case2[*]=>sub1[2]) : cnt (expect: 1) = $cnt($xavp(case2[*]=>sub1[2]))\n");
xlog("L_INFO", "xavp(case2[*]=>sub1[-1]) : cnt (expect: 2) = $cnt($xavp(case2[*]=>sub1[-1]))\n");
xlog("L_INFO", "xavp(case2[*]=>sub2) : cnt (expect: 2) = $cnt($xavp(case2[*]=>sub2))\n");
xlog("L_INFO", "xavp(case2[*]=>sub2[*]) : cnt (expect: 2) = $cnt($xavp(case2[*]=>sub2[*]))\n");
#xlog("L_INFO", "----------- TESTS $$cnt(...) - parsing errors\n");
# parsing error => server fails to start
#xlog("L_INFO", "PARSE ERROR: $cnt($xavu(test))\n");
#xlog("L_INFO", "PARSE ERROR: $cnt($var(test))\n");
#xlog("L_INFO", "PARSE ERROR: $cnt($unknown(test))\n");
"><pre><code>$xavp(case1) = "case1.value1";
$xavp(case1) = "case1.value2";
$xavp(case1) = "case1.value3";
$xavp(case2=>sub1) = "case2.value1 sub1.value1";
$xavp(case2[0]=>sub1) = "case2.value1 sub1.value2";
$xavp(case2[0]=>sub1) = "case2.value1 sub1.value3";
$xavp(case2[0]=>sub2) = "case2.value1 sub2.value1";
$xavp(case2[0]=>sub2) = "case2.value1 sub2.value2";
# => will become case2[1] after adding the next entry
$xavp(case2=>sub1) = "case2.value2 sub1.value1";
$xavp(case2[0]=>sub1) = "case2.value2 sub1.value2";
xlog("L_INFO", "----------- TESTS $$cnt($$xavp...) - root XAVPs with no sub XAVPs\n");
xlog("L_INFO", "xavp(case1[0]) : value = $xavp(case1[0]) : cnt (expect: 1) = $cnt($xavp(case1[0]))\n");
xlog("L_INFO", "xavp(case1[1]) : value = $xavp(case1[1]) : cnt (expect: 1) = $cnt($xavp(case1[1]))\n");
xlog("L_INFO", "xavp(case1[-1]) : value = $xavp(case1[-1]) : cnt (expect: 1) = $cnt($xavp(case1[-1]))\n");
xlog("L_INFO", "xavp(case1[-2]) : value = $xavp(case1[-2]) : cnt (expect: 1) = $cnt($xavp(case1[-2]))\n");
xlog("L_INFO", "xavp(case1[-66]) : value = $xavp(case1[-66]) : cnt (expect: 0) = $cnt($xavp(case1[-66]))\n");
xlog("L_INFO", "xavp(case1[66]) : value = $xavp(case1[66]) : cnt (expect: 0) = $cnt($xavp(case1[66]))\n");
xlog("L_INFO", "xavp(case1[*]) : cnt (expect: 3) = $cnt($xavp(case1[*]))\n");
xlog("L_INFO", "xavp(case1) : cnt (expect: 3) = $cnt($xavp(case1))\n");
xlog("L_INFO", "xavp(case1=>sub) : cnt (expect: 0) = $cnt($xavp(case1=>sub))\n");
xlog("L_INFO", "xavp(case1[0]=>sub) : cnt (expect: 0) = $cnt($xavp(case1[0]=>sub))\n");
xlog("L_INFO", "xavp(case1[0]=>sub[0]) : value = $xavp(case1[0]=>sub[0]) : cnt (expect: 0) = $cnt($xavp(case1[0]=>sub[0]))\n");
xlog("L_INFO", "----------- TESTS $$cnt($$xavp...) - root XAVPs using single index\n");
xlog("L_INFO", "xavp(case2[1]=>sub1[0]) : value = $xavp(case2[1]=>sub1[0]) : cnt (expect: 1) = $cnt($xavp(case2[1]=>sub1[0]))\n");
xlog("L_INFO", "xavp(case2[1]=>sub1[-1]) : value = $xavp(case2[1]=>sub1[-1]) : cnt (expect: 1) = $cnt($xavp(case2[1]=>sub1[-1]))\n");
xlog("L_INFO", "xavp(case2[1]=>sub1[3]) : value = $xavp(case2[1]=>sub1[3]) : cnt (expect: 0) = $cnt($xavp(case2[1]=>sub1[3]))\n");
xlog("L_INFO", "xavp(case2[1]=>sub1[*]) : cnt (expect: 3) = $cnt($xavp(case2[1]=>sub1[*]))\n");
xlog("L_INFO", "xavp(case2[1]=>sub1) : cnt (expect: 3) = $cnt($xavp(case2[1]=>sub1))\n");
xlog("L_INFO", "xavp(case2[1]=>sub2[*]) : cnt (expect: 2) = $cnt($xavp(case2[1]=>sub2[*]))\n");
xlog("L_INFO", "xavp(case2[1]=>sub2) : cnt (expect: 2) = $cnt($xavp(case2[1]=>sub2))\n");
xlog("L_INFO", "xavp(case2[66]=>sub1) : cnt (expect: 0) = $cnt($xavp(case2[66]=>sub1))\n");
xlog("L_INFO", "xavp(case2[66]=>sub1[0]) : cnt (expect: 0) = $cnt($xavp(case2[66]=>sub1[0]))\n");
xlog("L_INFO", "----------- TESTS $$cnt($$xavp...) - root XAVPs using all index\n");
xlog("L_INFO", "xavp(case2=>sub1) : cnt (expect: 5) = $cnt($xavp(case2=>sub1))\n");
xlog("L_INFO", "xavp(case2=>sub1[*]) : cnt (expect: 5) = $cnt($xavp(case2=>sub1[*]))\n");
xlog("L_INFO", "xavp(case2[*]=>sub1) : cnt (expect: 5) = $cnt($xavp(case2[*]=>sub1))\n");
xlog("L_INFO", "xavp(case2[*]=>sub1[*]) : cnt (expect: 5) = $cnt($xavp(case2[*]=>sub1[*]))\n");
xlog("L_INFO", "xavp(case2[*]=>sub1) : cnt (expect: 5) = $cnt($xavp(case2[*]=>sub1))\n");
xlog("L_INFO", "xavp(case2[*]=>sub1[0]) : cnt (expect: 2) = $cnt($xavp(case2[*]=>sub1[0]))\n");
xlog("L_INFO", "xavp(case2[*]=>sub1[2]) : cnt (expect: 1) = $cnt($xavp(case2[*]=>sub1[2]))\n");
xlog("L_INFO", "xavp(case2[*]=>sub1[-1]) : cnt (expect: 2) = $cnt($xavp(case2[*]=>sub1[-1]))\n");
xlog("L_INFO", "xavp(case2[*]=>sub2) : cnt (expect: 2) = $cnt($xavp(case2[*]=>sub2))\n");
xlog("L_INFO", "xavp(case2[*]=>sub2[*]) : cnt (expect: 2) = $cnt($xavp(case2[*]=>sub2[*]))\n");
#xlog("L_INFO", "----------- TESTS $$cnt(...) - parsing errors\n");
# parsing error => server fails to start
#xlog("L_INFO", "PARSE ERROR: $cnt($xavu(test))\n");
#xlog("L_INFO", "PARSE ERROR: $cnt($var(test))\n");
#xlog("L_INFO", "PARSE ERROR: $cnt($unknown(test))\n");
</code></pre></div>
<hr>
<h4>You can view, comment on, or merge this pull request online at:</h4>
<p> <a href='https://github.com/kamailio/kamailio/pull/2940'>https://github.com/kamailio/kamailio/pull/2940</a></p>
<h4>Commit Summary</h4>
<ul>
<li><a href="https://github.com/kamailio/kamailio/pull/2940/commits/b948fb3669b361d1dde51889bd1a5ec097a1e634" class="commit-link">b948fb3</a> pv: extend $cnt to work with $xavp</li>
</ul>
<h4 style="display: inline-block">File Changes </h4> <p style="display: inline-block">(<a href="https://github.com/kamailio/kamailio/pull/2940/files">1 file</a>)</p>
<ul>
<li>
<strong>M</strong>
<a href="https://github.com/kamailio/kamailio/pull/2940/files#diff-bd743b2d32f6e43734c7b42c3fa71fd6929a782e6de1d8396a7d3f31204fccc2">src/modules/pv/pv_core.c</a>
(156)
</li>
</ul>
<h4>Patch Links:</h4>
<ul>
<li><a href='https://github.com/kamailio/kamailio/pull/2940.patch'>https://github.com/kamailio/kamailio/pull/2940.patch</a></li>
<li><a href='https://github.com/kamailio/kamailio/pull/2940.diff'>https://github.com/kamailio/kamailio/pull/2940.diff</a></li>
</ul>
<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />You are receiving this because you are subscribed to this thread.<br />Reply to this email directly, <a href="https://github.com/kamailio/kamailio/pull/2940">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/ABO7UZNUJH7M4ZNLGG55OK3UMTZXRANCNFSM5IJRNXFA">unsubscribe</a>.<br />Triage notifications on the go with GitHub Mobile for <a href="https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675">iOS</a> or <a href="https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub">Android</a>.
<img src="https://github.com/notifications/beacon/ABO7UZOTYC6NI7IGGPEQTDTUMTZXRA5CNFSM5IJRNXFKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4PYFQENQ.gif" height="1" width="1" alt="" /></p>
<script type="application/ld+json">[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/kamailio/kamailio/pull/2940",
"url": "https://github.com/kamailio/kamailio/pull/2940",
"name": "View Pull Request"
},
"description": "View this Pull Request on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
}
]</script>