Skip to content

Commit 25f5148

Browse files
samuel-gauthierrjarry
authored andcommitted
schema: fix type patterns return value
The libyang lysp_type struct arg.str fields contains the pattern string + a character in the beginning telling if the match is inverted or not. Since the libyang2 port, the patterns function do not take this into account anymore, and yields this string directly, which is not correct. The patterns function is supposed to yield Iterator[Tuple[str, bool], containing the pattern string and a boolean telling if the match is inverted or not, restore it. Add a test for it as well. While there, use ly_array_iter. Signed-off-by: Samuel Gauthier <samuel.gauthier@6wind.com>
1 parent 8966c0d commit 25f5148

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

libyang/schema.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -557,9 +557,16 @@ def patterns(self) -> Iterator[Tuple[str, bool]]:
557557
return
558558
if self.cdata_parsed.patterns == ffi.NULL:
559559
return
560-
arr_length = ffi.cast("uint64_t *", self.cdata_parsed.patterns)[-1]
561-
for i in range(arr_length):
562-
yield c2str(self.cdata_parsed.patterns[i].arg.str)
560+
for p in ly_array_iter(self.cdata_parsed.patterns):
561+
if not p:
562+
continue
563+
# in case of pattern restriction, the first byte has a special meaning:
564+
# 0x06 (ACK) for regular match and 0x15 (NACK) for invert-match
565+
invert_match = p.arg.str[0] == b"\x15"
566+
# yield tuples like:
567+
# ('[a-zA-Z_][a-zA-Z0-9\-_.]*', False)
568+
# ('[xX][mM][lL].*', True)
569+
yield c2str(p.arg.str + 1), invert_match
563570

564571
def all_patterns(self) -> Iterator[Tuple[str, bool]]:
565572
if self.cdata.basetype == lib.LY_TYPE_UNION:

tests/test_schema.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,15 @@ def test_leaf_type_status(self):
389389
self.assertEqual(leaf.deprecated(), False)
390390
self.assertEqual(leaf.obsolete(), True)
391391

392+
def test_leaf_type_pattern(self):
393+
leaf = next(
394+
self.ctx.find_path("/yolo-system:conf/yolo-system:url/yolo-system:host")
395+
)
396+
self.assertIsInstance(leaf, SLeaf)
397+
t = leaf.type()
398+
self.assertIsInstance(t, Type)
399+
self.assertEqual(list(t.patterns()), [("[a-z.]+", False), ("1", True)])
400+
392401
def test_leaf_type_union(self):
393402
leaf = next(self.ctx.find_path("/yolo-system:conf/yolo-system:number"))
394403
self.assertIsInstance(leaf, SLeafList)

tests/yang-old/yolo/yolo-system.yang

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ module yolo-system {
6060
type types:protocol;
6161
}
6262
leaf host {
63-
type string;
63+
type string {
64+
pattern "[a-z.]+";
65+
pattern "1" {
66+
modifier "invert-match";
67+
}
68+
}
6469
}
6570
leaf port {
6671
type uint16;

tests/yang/yolo/yolo-system.yang

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ module yolo-system {
7070
}
7171
}
7272
leaf host {
73-
type string;
73+
type string {
74+
pattern "[a-z.]+";
75+
pattern "1" {
76+
modifier "invert-match";
77+
}
78+
}
7479
}
7580
leaf port {
7681
type uint16;

0 commit comments

Comments
 (0)