Skip to content

Commit 72f9c0f

Browse files
authored
Replace-boolean-strings-by-their-values (#2054)
* Improve type conversion function * Convert boolean strings to their values
1 parent 8a40096 commit 72f9c0f

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

sonar/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,11 @@ def reload(self, data: types.ApiPayload) -> None:
207207
elif self.key == COMPONENT_VISIBILITY:
208208
self.value = data.get("visibility", None)
209209
elif self.key == "sonar.login.message":
210-
self.value: Optional[Any] = None
210+
self.value = None
211211
if "values" in data and isinstance(data["values"], list) and len(data["values"]) > 0:
212212
self.value = data["values"][0]
213213
else:
214-
self.value = next((data[key] for key in ("fieldValues", "values", "value") if key in data), None)
214+
self.value = util.convert_to_type(next((data[key] for key in ("fieldValues", "values", "value") if key in data), None))
215215
if not self.value and "defaultValue" in data:
216216
self.value = util.DEFAULT
217217
self.__reload_inheritance(data)

sonar/utilities.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,10 @@ def redacted_token(token: str) -> str:
159159
return re.sub(r"(..).*(..)", r"\1***\2", token)
160160

161161

162-
def convert_to_type(value: Any) -> Any:
162+
def convert_to_type(value: str) -> Any:
163163
"""Converts a potentially string value to the corresponding int or float"""
164+
if not isinstance(value, str):
165+
return value
164166
try:
165167
return int(value)
166168
except ValueError:
@@ -169,6 +171,10 @@ def convert_to_type(value: Any) -> Any:
169171
return float(value)
170172
except ValueError:
171173
pass
174+
try:
175+
return bool(value)
176+
except ValueError:
177+
pass
172178
return value
173179

174180

0 commit comments

Comments
 (0)