@@ -1804,21 +1804,24 @@ lysc_node_when(const struct lysc_node *node)
1804
1804
}
1805
1805
}
1806
1806
1807
- LIBYANG_API_DEF const struct lysc_node *
1808
- lysc_node_lref_target (const struct lysc_node * node )
1807
+ /**
1808
+ * @brief Get the target node of a leafref.
1809
+ *
1810
+ * @param[in] node Context node for the leafref.
1811
+ * @param[in] type Leafref type to resolve.
1812
+ * @return Target schema node;
1813
+ * @return NULL if the tearget is not found.
1814
+ */
1815
+ static const struct lysc_node *
1816
+ lysc_type_lref_target (const struct lysc_node * node , const struct lysc_type * type )
1809
1817
{
1810
1818
struct lysc_type_leafref * lref ;
1811
1819
struct ly_path * p ;
1812
1820
const struct lysc_node * target ;
1813
1821
1814
- if (!node || !(node -> nodetype & LYD_NODE_TERM )) {
1815
- return NULL ;
1816
- }
1822
+ assert (type -> basetype == LY_TYPE_LEAFREF );
1817
1823
1818
- lref = (struct lysc_type_leafref * )((struct lysc_node_leaf * )node )-> type ;
1819
- if (lref -> basetype != LY_TYPE_LEAFREF ) {
1820
- return NULL ;
1821
- }
1824
+ lref = (struct lysc_type_leafref * )type ;
1822
1825
1823
1826
/* compile the path */
1824
1827
if (ly_path_compile_leafref (node -> module -> ctx , node , NULL , lref -> path ,
@@ -1834,6 +1837,61 @@ lysc_node_lref_target(const struct lysc_node *node)
1834
1837
return target ;
1835
1838
}
1836
1839
1840
+ LIBYANG_API_DEF const struct lysc_node *
1841
+ lysc_node_lref_target (const struct lysc_node * node )
1842
+ {
1843
+ if (!node || !(node -> nodetype & LYD_NODE_TERM ) || (((struct lysc_node_leaf * )node )-> type -> basetype != LY_TYPE_LEAFREF )) {
1844
+ return NULL ;
1845
+ }
1846
+
1847
+ return lysc_type_lref_target (node , ((struct lysc_node_leaf * )node )-> type );
1848
+ }
1849
+
1850
+ LIBYANG_API_DEF LY_ERR
1851
+ lysc_node_lref_targets (const struct lysc_node * node , struct ly_set * * set )
1852
+ {
1853
+ LY_ERR rc = LY_SUCCESS ;
1854
+ struct lysc_type * type ;
1855
+ struct lysc_type_union * type_un ;
1856
+ const struct lysc_node * target ;
1857
+ LY_ARRAY_COUNT_TYPE u ;
1858
+
1859
+ LY_CHECK_ARG_RET (NULL , node , (node -> nodetype & LYD_NODE_TERM ), LY_EINVAL );
1860
+
1861
+ /* allocate return set */
1862
+ LY_CHECK_RET (ly_set_new (set ));
1863
+
1864
+ type = ((struct lysc_node_leaf * )node )-> type ;
1865
+ if (type -> basetype == LY_TYPE_UNION ) {
1866
+ /* union with possible leafrefs */
1867
+ type_un = (struct lysc_type_union * )type ;
1868
+
1869
+ LY_ARRAY_FOR (type_un -> types , u ) {
1870
+ if (type_un -> types [u ]-> basetype != LY_TYPE_LEAFREF ) {
1871
+ continue ;
1872
+ }
1873
+
1874
+ target = lysc_type_lref_target (node , type_un -> types [u ]);
1875
+ if (target ) {
1876
+ LY_CHECK_GOTO (rc = ly_set_add (* set , target , 1 , NULL ), cleanup );
1877
+ }
1878
+ }
1879
+ } else if (type -> basetype == LY_TYPE_LEAFREF ) {
1880
+ /* leafref */
1881
+ target = lysc_type_lref_target (node , type );
1882
+ if (target ) {
1883
+ LY_CHECK_GOTO (rc = ly_set_add (* set , target , 1 , NULL ), cleanup );
1884
+ }
1885
+ }
1886
+
1887
+ cleanup :
1888
+ if (rc ) {
1889
+ ly_set_free (* set , NULL );
1890
+ * set = NULL ;
1891
+ }
1892
+ return rc ;
1893
+ }
1894
+
1837
1895
enum ly_stmt
1838
1896
lysp_match_kw (struct ly_in * in , uint64_t * indent )
1839
1897
{
0 commit comments