@@ -1477,6 +1477,229 @@ void cb_add_root_key_val()
14771477 cfl_variant_destroy (vobj );
14781478}
14791479
1480+ void cb_direct_array_access ()
1481+ {
1482+ struct cfl_kvlist * kvlist = NULL ;
1483+ struct cfl_array * array = NULL ;
1484+ struct cfl_variant * vobj = NULL ;
1485+ char * fmt ;
1486+ char * fmt_out ;
1487+ flb_sds_t str ;
1488+ struct flb_cfl_record_accessor * cra ;
1489+
1490+ /* Sample kvlist with direct array */
1491+ kvlist = cfl_kvlist_create ();
1492+ if (kvlist == NULL ) {
1493+ exit (EXIT_FAILURE );
1494+ }
1495+
1496+ /* Create array with elements a, b, c */
1497+ array = cfl_array_create (3 );
1498+ if (array == NULL ) {
1499+ exit (EXIT_FAILURE );
1500+ }
1501+
1502+ cfl_array_append_string (array , "a" );
1503+ cfl_array_append_string (array , "b" );
1504+ cfl_array_append_string (array , "c" );
1505+
1506+ /* Add array to kvlist */
1507+ cfl_kvlist_insert_array (kvlist , "array" , array );
1508+
1509+ /* Set up CFL variant(vobj) */
1510+ vobj = cfl_variant_create_from_kvlist (kvlist );
1511+ if (vobj == NULL ) {
1512+ exit (EXIT_FAILURE );
1513+ }
1514+
1515+ /* Formatter for direct array access */
1516+ fmt = flb_sds_create ("$array[0]" );
1517+ fmt_out = "a" ;
1518+
1519+ cra = flb_cfl_ra_create (fmt , FLB_FALSE );
1520+ TEST_CHECK (cra != NULL );
1521+ if (!cra ) {
1522+ exit (EXIT_FAILURE );
1523+ }
1524+
1525+ /* Do translation */
1526+ str = flb_cfl_ra_translate (cra , NULL , -1 , * vobj , NULL );
1527+ TEST_CHECK (str != NULL );
1528+ if (!str ) {
1529+ exit (EXIT_FAILURE );
1530+ }
1531+
1532+ TEST_CHECK (flb_sds_len (str ) == strlen (fmt_out ));
1533+ TEST_CHECK (memcmp (str , fmt_out , strlen (fmt_out )) == 0 );
1534+ printf ("== direct array access test ==\n== input ==\n%s\n== output ==\n%s\n" , str , fmt_out );
1535+
1536+ flb_sds_destroy (str );
1537+ flb_sds_destroy (fmt );
1538+ flb_cfl_ra_destroy (cra );
1539+ cfl_variant_destroy (vobj );
1540+ }
1541+
1542+ void cb_nested_array_access ()
1543+ {
1544+ struct cfl_kvlist * kvlist = NULL ;
1545+ struct cfl_array * matrix = NULL ;
1546+ struct cfl_array * row1 = NULL ;
1547+ struct cfl_array * row2 = NULL ;
1548+ struct cfl_array * row3 = NULL ;
1549+ struct cfl_variant * vobj = NULL ;
1550+ char * fmt ;
1551+ char * fmt_out ;
1552+ flb_sds_t str ;
1553+ struct flb_cfl_record_accessor * cra ;
1554+
1555+ /* Sample kvlist with nested arrays */
1556+ kvlist = cfl_kvlist_create ();
1557+ if (kvlist == NULL ) {
1558+ exit (EXIT_FAILURE );
1559+ }
1560+
1561+ /* Create 3x3 matrix */
1562+ matrix = cfl_array_create (3 );
1563+ row1 = cfl_array_create (3 );
1564+ row2 = cfl_array_create (3 );
1565+ row3 = cfl_array_create (3 );
1566+
1567+ if (matrix == NULL || row1 == NULL || row2 == NULL || row3 == NULL ) {
1568+ exit (EXIT_FAILURE );
1569+ }
1570+
1571+ /* Fill the rows */
1572+ cfl_array_append_int64 (row1 , 1 );
1573+ cfl_array_append_int64 (row1 , 2 );
1574+ cfl_array_append_int64 (row1 , 3 );
1575+
1576+ cfl_array_append_int64 (row2 , 4 );
1577+ cfl_array_append_int64 (row2 , 5 );
1578+ cfl_array_append_int64 (row2 , 6 );
1579+
1580+ cfl_array_append_int64 (row3 , 7 );
1581+ cfl_array_append_int64 (row3 , 8 );
1582+ cfl_array_append_int64 (row3 , 9 );
1583+
1584+ /* Add rows to matrix */
1585+ cfl_array_append_array (matrix , row1 );
1586+ cfl_array_append_array (matrix , row2 );
1587+ cfl_array_append_array (matrix , row3 );
1588+
1589+ /* Add matrix to kvlist */
1590+ cfl_kvlist_insert_array (kvlist , "matrix" , matrix );
1591+
1592+ /* Set up CFL variant(vobj) */
1593+ vobj = cfl_variant_create_from_kvlist (kvlist );
1594+ if (vobj == NULL ) {
1595+ exit (EXIT_FAILURE );
1596+ }
1597+
1598+ /* Formatter for nested array access */
1599+ fmt = flb_sds_create ("$matrix[1][2]" ); /* Should access the value 6 */
1600+ fmt_out = "6" ;
1601+
1602+ cra = flb_cfl_ra_create (fmt , FLB_FALSE );
1603+ TEST_CHECK (cra != NULL );
1604+ if (!cra ) {
1605+ exit (EXIT_FAILURE );
1606+ }
1607+
1608+ /* Do translation */
1609+ str = flb_cfl_ra_translate (cra , NULL , -1 , * vobj , NULL );
1610+ TEST_CHECK (str != NULL );
1611+ if (!str ) {
1612+ exit (EXIT_FAILURE );
1613+ }
1614+
1615+ TEST_CHECK (flb_sds_len (str ) == strlen (fmt_out ));
1616+ TEST_CHECK (memcmp (str , fmt_out , strlen (fmt_out )) == 0 );
1617+ printf ("== nested array access test ==\n== input ==\n%s\n== output ==\n%s\n" , str , fmt_out );
1618+
1619+ flb_sds_destroy (str );
1620+ flb_sds_destroy (fmt );
1621+ flb_cfl_ra_destroy (cra );
1622+ cfl_variant_destroy (vobj );
1623+ }
1624+
1625+ void cb_mixed_array_map_access ()
1626+ {
1627+ struct cfl_kvlist * kvlist = NULL ;
1628+ struct cfl_kvlist * person1 = NULL ;
1629+ struct cfl_kvlist * person2 = NULL ;
1630+ struct cfl_array * records = NULL ;
1631+ struct cfl_variant * vobj = NULL ;
1632+ char * fmt ;
1633+ char * fmt_out ;
1634+ flb_sds_t str ;
1635+ struct flb_cfl_record_accessor * cra ;
1636+
1637+ /* Sample kvlist with array containing maps */
1638+ kvlist = cfl_kvlist_create ();
1639+ if (kvlist == NULL ) {
1640+ exit (EXIT_FAILURE );
1641+ }
1642+
1643+ /* Create records array */
1644+ records = cfl_array_create (2 );
1645+ if (records == NULL ) {
1646+ exit (EXIT_FAILURE );
1647+ }
1648+
1649+ /* Create person records */
1650+ person1 = cfl_kvlist_create ();
1651+ person2 = cfl_kvlist_create ();
1652+ if (person1 == NULL || person2 == NULL ) {
1653+ exit (EXIT_FAILURE );
1654+ }
1655+
1656+ /* Add properties to persons */
1657+ cfl_kvlist_insert_string (person1 , "name" , "John" );
1658+ cfl_kvlist_insert_int64 (person1 , "age" , 30 );
1659+
1660+ cfl_kvlist_insert_string (person2 , "name" , "Jane" );
1661+ cfl_kvlist_insert_int64 (person2 , "age" , 25 );
1662+
1663+ /* Add persons to records */
1664+ cfl_array_append_kvlist (records , person1 );
1665+ cfl_array_append_kvlist (records , person2 );
1666+
1667+ /* Add records to kvlist */
1668+ cfl_kvlist_insert_array (kvlist , "records" , records );
1669+
1670+ /* Set up CFL variant(vobj) */
1671+ vobj = cfl_variant_create_from_kvlist (kvlist );
1672+ if (vobj == NULL ) {
1673+ exit (EXIT_FAILURE );
1674+ }
1675+
1676+ /* Formatter for mixed array+map access */
1677+ fmt = flb_sds_create ("$records[1]['name']" ); /* Should access "Jane" */
1678+ fmt_out = "Jane" ;
1679+
1680+ cra = flb_cfl_ra_create (fmt , FLB_FALSE );
1681+ TEST_CHECK (cra != NULL );
1682+ if (!cra ) {
1683+ exit (EXIT_FAILURE );
1684+ }
1685+
1686+ /* Do translation */
1687+ str = flb_cfl_ra_translate (cra , NULL , -1 , * vobj , NULL );
1688+ TEST_CHECK (str != NULL );
1689+ if (!str ) {
1690+ exit (EXIT_FAILURE );
1691+ }
1692+
1693+ TEST_CHECK (flb_sds_len (str ) == strlen (fmt_out ));
1694+ TEST_CHECK (memcmp (str , fmt_out , strlen (fmt_out )) == 0 );
1695+ printf ("== mixed array+map access test ==\n== input ==\n%s\n== output ==\n%s\n" , str , fmt_out );
1696+
1697+ flb_sds_destroy (str );
1698+ flb_sds_destroy (fmt );
1699+ flb_cfl_ra_destroy (cra );
1700+ cfl_variant_destroy (vobj );
1701+ }
1702+
14801703TEST_LIST = {
14811704 { "keys" , cb_keys },
14821705 { "dash_key" , cb_dash_key },
@@ -1495,5 +1718,8 @@ TEST_LIST = {
14951718 { "add_root_key_val" , cb_add_root_key_val },
14961719 { "flb_ra_translate_check" , cb_ra_translate_check },
14971720 { "ra_create_str_from_list" , cb_ra_create_str_from_list },
1721+ { "direct_array_access" , cb_direct_array_access },
1722+ { "nested_array_access" , cb_nested_array_access },
1723+ { "mixed_array_map_access" , cb_mixed_array_map_access },
14981724 { NULL }
1499- };
1725+ };
0 commit comments