Skip to content

Commit

Permalink
ADD nullSafe transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
fgalan committed Apr 8, 2024
1 parent 6233201 commit 527aa0b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ Result:

- `typeOf`: returns type representation of the data (e.g. `str`, `int`, `float`, etc.)
- `strToLocation`: given a string with a comma separated list of decimal numbers, returns an array of such numbers. Example: `"value1, value2"|strToLocation`. Example: `"value1, value2"|strToLocation` returns `[value1, value2]`. It's name (maybe not very good :) is due to it can be useful to generate a [GeoJSON](https://geojson.org/) representing a point which coordinates are provided in a string, eg: `{coordinates:(point|strToLocation),type: "Point"}`
- `nullSafe`: return the input value, if not `None`, or a failsafe value. Example: `a|safeNull(23)` return the value of `a` if `a` is not None or `23` if `a` is None.

## Packaging

Expand Down Expand Up @@ -137,6 +138,8 @@ upload process you will be prompted to provide your user and password.

## Changelog

- Add: nullSafe transformation ([#11](https://github.com/telefonicasc/tcjexl/issues/11))

0.2.0 (March 11th, 2024)

- Add: allow to use context for transformations arguments (using a patched version of pyjexl)
Expand Down
3 changes: 3 additions & 0 deletions tcjexl/jexl.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,6 @@ def __init__(self, context=None, now=datetime.datetime.now(timezone.utc)):
# Tested by test_transforms_misc.py
super().add_transform("typeOf", lambda x: f'{type(x)}'[8:-2])
super().add_transform("strToLocation", lambda str: [float(x) for x in str.split(",")])

# Tested by test_null.py
super().add_transform("nullSafe", lambda x, y: x if x is not None else y)
67 changes: 67 additions & 0 deletions tests/test_null.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright 2024 Telefónica Soluciones de Informática y Comunicaciones de España, S.A.U.
#
# This file is part of tcjexl
#
# tcjexl is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# tcjexl is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with IoT orchestrator. If not, see http://www.gnu.org/licenses/.


import unittest
from tcjexl import JEXL


class TestNull(unittest.TestCase):

def setUp(self) -> None:
self.jexl = JEXL()

def test_null_fail(self):
context = {"a": None}

self.assertRaises(TypeError, self.jexl.evaluate, 'a*10', context)

def test_nullsafe_number(self):
expr = 'a|nullSafe(0)*10'
context1 = {"a": None}
context2 = {"a": 23}
result1 = self.jexl.evaluate(expr, context1)
result2 = self.jexl.evaluate(expr, context2)
self.assertEqual(result1, 0)
self.assertEqual(result2, 230)

def test_nullsafe_string(self):
expr = "a|nullSafe('foo')+'bar'"
context1 = {"a": None}
context2 = {"a": 'zzz'}
result1 = self.jexl.evaluate(expr, context1)
result2 = self.jexl.evaluate(expr, context2)
self.assertEqual(result1, 'foobar')
self.assertEqual(result2, 'zzzbar')

def test_nullsafe_boolean(self):
expr = "a|nullSafe(false)"
context1 = {"a": None}
context2 = {"a": True}
result1 = self.jexl.evaluate(expr, context1)
result2 = self.jexl.evaluate(expr, context2)
self.assertEqual(result1, False)
self.assertEqual(result2, True)

def test_nullsafe_variable(self):
expr = "a|nullSafe(b)"
context1 = {"a": None, "b": 32}
context2 = {"a": 4, "b": 32}
result1 = self.jexl.evaluate(expr, context1)
result2 = self.jexl.evaluate(expr, context2)
self.assertEqual(result1, 32)
self.assertEqual(result2, 4)

0 comments on commit 527aa0b

Please sign in to comment.