MCPcopy Index your code
hub / github.com/saltstack/salt / enforce_types

Function enforce_types

salt/utils/gitfs.py:184–235  ·  view source on GitHub ↗

Force params to be strings unless they should remain a different type

(key, val)

Source from the content-addressed store, hash-verified

182
183
184def enforce_types(key, val):
185 """
186 Force params to be strings unless they should remain a different type
187 """
188 non_string_params = {
189 "ssl_verify": bool,
190 "insecure_auth": bool,
191 "disable_saltenv_mapping": bool,
192 "saltenv_whitelist": "stringlist",
193 "saltenv_blacklist": "stringlist",
194 "refspecs": "stringlist",
195 "ref_types": "stringlist",
196 "update_interval": int,
197 }
198
199 def _find_global(key):
200 for item in non_string_params:
201 try:
202 if key.endswith("_" + item):
203 ret = item
204 break
205 except TypeError:
206 if key.endswith("_" + str(item)):
207 ret = item
208 break
209 else:
210 ret = None
211 return ret
212
213 if key not in non_string_params:
214 key = _find_global(key)
215 if key is None:
216 return str(val)
217
218 expected = non_string_params[key]
219 if expected == "stringlist":
220 if not isinstance(val, ((str,), list)):
221 val = str(val)
222 if isinstance(val, str):
223 return [x.strip() for x in val.split(",")]
224 return [str(x) for x in val]
225 else:
226 try:
227 return expected(val)
228 except Exception: # pylint: disable=broad-except
229 log.error(
230 "Failed to enforce type for key=%s with val=%s, falling back "
231 "to a string",
232 key,
233 val,
234 )
235 return str(val)
236
237
238def failhard(role):

Callers 1

init_remotesMethod · 0.85

Calls 3

_find_globalFunction · 0.85
expectedFunction · 0.85
errorMethod · 0.45

Tested by

no test coverage detected