Skip to content

Commit 484b184

Browse files
committed
Add NULL checks
1 parent f957638 commit 484b184

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

src/jsonata/functions.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ def substring(string: Optional[str], start: Optional[float], length: Optional[fl
226226
if string is None:
227227
return None
228228

229+
if string is utils.Utils.NULL_VALUE:
230+
raise jexception.JException("T0410", -1)
231+
229232
start = int(start) if start is not None else None
230233
length = int(length) if length is not None else None
231234

@@ -307,6 +310,9 @@ def substring_before(string: Optional[str], chars: Optional[str]) -> Optional[st
307310
if string is None:
308311
return None
309312

313+
if string is utils.Utils.NULL_VALUE:
314+
raise jexception.JException("T0410", -1)
315+
310316
if chars is None:
311317
return string
312318

@@ -328,6 +334,9 @@ def substring_after(string: Optional[str], chars: Optional[str]) -> Optional[str
328334
if string is None:
329335
return None
330336

337+
if string is utils.Utils.NULL_VALUE:
338+
raise jexception.JException("T0410", -1)
339+
331340
pos = string.find(chars)
332341
if pos > -1:
333342
return string[pos + len(chars):]
@@ -345,6 +354,9 @@ def lowercase(string: Optional[str]) -> Optional[str]:
345354
if string is None:
346355
return None
347356

357+
if string is utils.Utils.NULL_VALUE:
358+
raise jexception.JException("T0410", -1)
359+
348360
return string.casefold()
349361

350362
#
@@ -358,6 +370,9 @@ def uppercase(string: Optional[str]) -> Optional[str]:
358370
if string is None:
359371
return None
360372

373+
if string is utils.Utils.NULL_VALUE:
374+
raise jexception.JException("T0410", -1)
375+
361376
return string.upper()
362377

363378
#
@@ -371,6 +386,9 @@ def length(string: Optional[str]) -> Optional[int]:
371386
if string is None:
372387
return None
373388

389+
if string is utils.Utils.NULL_VALUE:
390+
raise jexception.JException("T0410", -1)
391+
374392
return len(string)
375393

376394
#
@@ -384,6 +402,9 @@ def trim(string: Optional[str]) -> Optional[str]:
384402
if string is None:
385403
return None
386404

405+
if string is utils.Utils.NULL_VALUE:
406+
raise jexception.JException("T0410", -1)
407+
387408
if not string:
388409
return ""
389410

@@ -414,6 +435,9 @@ def pad(string: Optional[str], width: Optional[int], char: Optional[str]) -> Opt
414435
if string is None:
415436
return None
416437

438+
if string is utils.Utils.NULL_VALUE:
439+
raise jexception.JException("T0410", -1)
440+
417441
if char is None or not char:
418442
char = " "
419443

@@ -428,6 +452,10 @@ def pad(string: Optional[str], width: Optional[int], char: Optional[str]) -> Opt
428452
def left_pad(string: Optional[str], size: Optional[int], pad_str: Optional[str]) -> Optional[str]:
429453
if string is None:
430454
return None
455+
456+
if string is utils.Utils.NULL_VALUE:
457+
raise jexception.JException("T0410", -1)
458+
431459
if pad_str is None:
432460
pad_str = " "
433461

@@ -451,6 +479,10 @@ def left_pad(string: Optional[str], size: Optional[int], pad_str: Optional[str])
451479
def right_pad(string: Optional[str], size: Optional[int], pad_str: Optional[str]) -> Optional[str]:
452480
if string is None:
453481
return None
482+
483+
if string is utils.Utils.NULL_VALUE:
484+
raise jexception.JException("T0410", -1)
485+
454486
if pad_str is None:
455487
pad_str = " "
456488

@@ -511,6 +543,9 @@ def contains(string: Optional[str], token: Union[None, str, re.Pattern]) -> Opti
511543
if string is None:
512544
return None
513545

546+
if string is utils.Utils.NULL_VALUE:
547+
return None
548+
514549
result = False
515550

516551
if isinstance(token, str):
@@ -539,6 +574,9 @@ def match_(string: Optional[str], regex: Optional[re.Pattern], limit: Optional[i
539574
if string is None:
540575
return None
541576

577+
if string is utils.Utils.NULL_VALUE:
578+
raise jexception.JException("T0410", -1)
579+
542580
# limit, if specified, must be a non-negative number
543581
if limit is not None and limit < 0:
544582
raise jexception.JException("D3040", -1, limit)
@@ -710,6 +748,10 @@ def safe_replace_first(s: Optional[str], pattern: re.Pattern, replacement: str)
710748
def replace(string: Optional[str], pattern: Union[str, re.Pattern], replacement: Optional[Any], limit: Optional[int]) -> Optional[str]:
711749
if string is None:
712750
return None
751+
752+
if string is utils.Utils.NULL_VALUE:
753+
raise jexception.JException("T0410", -1)
754+
713755
if isinstance(pattern, str):
714756
if not pattern:
715757
raise jexception.JException("Second argument of replace function cannot be an empty string", 0)
@@ -740,6 +782,10 @@ def base64encode(string: Optional[str]) -> Optional[str]:
740782
# undefined inputs always return undefined
741783
if string is None:
742784
return None
785+
786+
if string is utils.Utils.NULL_VALUE:
787+
raise jexception.JException("T0410", -1)
788+
743789
try:
744790
return base64.b64encode(string.encode("utf-8")).decode("utf-8")
745791
except Exception as e:
@@ -755,6 +801,10 @@ def base64decode(string: Optional[str]) -> Optional[str]:
755801
# undefined inputs always return undefined
756802
if string is None:
757803
return None
804+
805+
if string is utils.Utils.NULL_VALUE:
806+
raise jexception.JException("T0410", -1)
807+
758808
try:
759809
return base64.b64decode(string.encode("utf-8")).decode("utf-8")
760810
except Exception as e:
@@ -771,6 +821,9 @@ def encode_url_component(string: Optional[str]) -> Optional[str]:
771821
if string is None:
772822
return None
773823

824+
if string is utils.Utils.NULL_VALUE:
825+
raise jexception.JException("T0410", -1)
826+
774827
# See https://stackoverflow.com/questions/946170/equivalent-javascript-functions-for-pythons-urllib-parse-quote-and-urllib-par
775828
return urllib.parse.quote(string, safe="~()*!.'")
776829

@@ -785,6 +838,9 @@ def encode_url(string: Optional[str]) -> Optional[str]:
785838
if string is None:
786839
return None
787840

841+
if string is utils.Utils.NULL_VALUE:
842+
raise jexception.JException("T0410", -1)
843+
788844
# See https://stackoverflow.com/questions/946170/equivalent-javascript-functions-for-pythons-urllib-parse-quote-and-urllib-par
789845
return urllib.parse.quote(string, safe="~@#$&()*!+=:;,.?/'")
790846

@@ -799,6 +855,9 @@ def decode_url_component(string: Optional[str]) -> Optional[str]:
799855
if string is None:
800856
return None
801857

858+
if string is utils.Utils.NULL_VALUE:
859+
raise jexception.JException("T0410", -1)
860+
802861
# See https://stackoverflow.com/questions/946170/equivalent-javascript-functions-for-pythons-urllib-parse-quote-and-urllib-par
803862
return urllib.parse.unquote(string, errors="strict")
804863

@@ -813,6 +872,9 @@ def decode_url(string: Optional[str]) -> Optional[str]:
813872
if string is None:
814873
return None
815874

875+
if string is utils.Utils.NULL_VALUE:
876+
raise jexception.JException("T0410", -1)
877+
816878
# See https://stackoverflow.com/questions/946170/equivalent-javascript-functions-for-pythons-urllib-parse-quote-and-urllib-par
817879
return urllib.parse.unquote(string, errors="strict")
818880

@@ -821,6 +883,9 @@ def split(string: Optional[str], pattern: Union[str, Optional[re.Pattern]], limi
821883
if string is None:
822884
return None
823885

886+
if string is utils.Utils.NULL_VALUE:
887+
raise jexception.JException("T0410", -1)
888+
824889
if limit is not None and int(limit) < 0:
825890
raise jexception.JException("D3020", -1, string)
826891

0 commit comments

Comments
 (0)