Skip to content

Commit

Permalink
perf: Added code to convert config value to different types
Browse files Browse the repository at this point in the history
Signed-off-by: divyagar <divyagarg2601@gmail.com>
  • Loading branch information
divyagar committed Mar 16, 2021
1 parent 819a78a commit c87fdfa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
23 changes: 23 additions & 0 deletions casbin/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,29 @@ def _write(self, section, line_num, b):

del b[:]

def get_bool(self, key):
"""lookups up the value using the provided key and converts the value to a bool."""
return self.get(key).capitalize() == "True"

def get_int(self, key):
"""lookups up the value using the provided key and converts the value to a int"""
return int(self.get(key))

def get_float(self, key):
"""lookups up the value using the provided key and converts the value to a float"""
return float(self.get(key))

def get_string(self, key):
"""lookups up the value using the provided key and converts the value to a string"""
return self.get(key)

def get_strings(self, key):
"""lookups up the value using the provided key and converts the value to an array of string"""
value = self.get(key)
if value == "":
return None
return value.split(",")

def set(self, key, value):
if len(key) == 0:
raise RuntimeError("key is empty")
Expand Down
8 changes: 8 additions & 0 deletions tests/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,11 @@ def test_new_config(self):
self.assertEqual(config.get("multi3::name"), "r.sub==p.sub && r.obj==p.obj")
self.assertEqual(config.get("multi4::name"), "")
self.assertEqual(config.get("multi5::name"), "r.sub==p.sub && r.obj==p.obj")

self.assertEqual(config.get_bool("multi5::name"), False)
self.assertEqual(config.get_string("multi5::name"), "r.sub==p.sub && r.obj==p.obj")
self.assertEqual(config.get_strings("multi5::name"), ['r.sub==p.sub && r.obj==p.obj'])
with self.assertRaises(ValueError):
config.get_int("multi5::name")
with self.assertRaises(ValueError):
config.get_float("multi5::name")

0 comments on commit c87fdfa

Please sign in to comment.