Skip to content

Commit

Permalink
Merge pull request #71 from lkearney-arista/boolean-values
Browse files Browse the repository at this point in the history
BSC-19829: correctly handle boolean values in predicate
  • Loading branch information
andi-bigswitch committed May 31, 2024
2 parents 89d871e + 9e20569 commit ff55b12
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pybsn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ def match(self, **kwargs):
"""
for k, v in kwargs.items():
self = self.filter("%s=$x" % k.replace('_', '-'), x=v)

return self

def filter(self, template, *args, **kwargs):
Expand All @@ -209,7 +210,7 @@ def filter(self, template, *args, **kwargs):
.../segment[member-vlan<1000]
"""

kwargs = {k: repr(v) for k, v in kwargs.items()}
kwargs = {k: _normalize(v) for k, v in kwargs.items()}
predicate = '[' + Template(template).substitute(**kwargs) + ']'
return Node(self._path + predicate, self._connection)

Expand Down Expand Up @@ -467,6 +468,16 @@ def __repr__(self):
return "BigDbClient(%s)" % self.url


def _normalize(v):
""" Helper method to normalize query values """
if type(v) == bool:
# replace to use booleans to use strings in JSON-boolean style
if v:
return "'true'"
else:
return "'false'"
return repr(v)

def logged_request(session, request, timeout):
""" Helper method that logs HTTP requests made by this library, if configured. """
prepared = session.prepare_request(request)
Expand Down
2 changes: 2 additions & 0 deletions test/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def test_match(self):
node = self.root.node
self.assertEqual(node.match()._path, "controller/node")
self.assertEqual(node.match(a="foo")._path, "controller/node[a='foo']")
self.assertEqual(node.match(a=True)._path, "controller/node[a='true']")
self.assertIn(
node.match(a="foo", b=2)._path,
("controller/node[a='foo'][b=2]",
Expand All @@ -144,6 +145,7 @@ def test_filter(self):
self.assertEqual(node.filter("a='foo'")._path, "controller/node[a='foo']")
self.assertEqual(node.filter("a=$x", x="foo")._path, "controller/node[a='foo']")
self.assertEqual(node.filter("b=$x", x=1)._path, "controller/node[b=1]")
self.assertEqual(node.filter("a=$x", x=True)._path, "controller/node[a='true']")

def test_root_call(self):
self.client.get.return_value = dict(foo="bar")
Expand Down

0 comments on commit ff55b12

Please sign in to comment.