Skip to content

Commit

Permalink
Better handling of None during function apply
Browse files Browse the repository at this point in the history
Fixes #3
  • Loading branch information
rayokota committed Aug 3, 2024
1 parent 0d1b69b commit 15b41ad
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
9 changes: 2 additions & 7 deletions src/jsonata/jsonata.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def _eval(self, expr: Optional[parser.Parser.Symbol], input: Optional[Any], envi
elif expr.type == "regex":
result = self.evaluate_regex(expr) # , input, environment);
elif expr.type == "function":
result = self.evaluate_function(expr, input, environment, None)
result = self.evaluate_function(expr, input, environment, utils.Utils.NONE)
elif expr.type == "variable":
result = self.evaluate_variable(expr, input, environment)
elif expr.type == "lambda":
Expand Down Expand Up @@ -1274,11 +1274,6 @@ def evaluate_apply_expression(self, expr: Optional[parser.Parser.Symbol], input:

lhs = self.eval(expr.lhs, input, environment)

# Map null to NULL_VALUE before applying to functions
# TODO: fix more generically!
if lhs is None:
lhs = utils.Utils.NULL_VALUE

if expr.rhs.type == "function":
# Symbol applyTo = new Symbol(); applyTo.context = lhs
# this is a Object _invocation_; invoke it with lhs expression as the first argument
Expand Down Expand Up @@ -1348,7 +1343,7 @@ def evaluate_function(self, expr: Optional[parser.Parser.Symbol], input: Optiona

evaluated_args = []

if applyto_context is not None:
if applyto_context is not utils.Utils.NONE:
evaluated_args.append(applyto_context)
# eager evaluation - evaluate the arguments
args = expr.arguments if expr.arguments is not None else []
Expand Down
8 changes: 8 additions & 0 deletions tests/string_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ def test_escape(self):
# Additional $split tests
#
def test_split(self):
# Splitting on an undefined value
res = jsonata.Jsonata("$split(a, '-')").evaluate({})
assert res == None

# Splitting on an undefined value, equivalent to above
res = jsonata.Jsonata("a ~> $split('-')").evaluate({})
assert res == None

# Splitting empty string with empty separator must return empty list
res = jsonata.Jsonata("$split('', '')").evaluate(None)
assert res == []
Expand Down

0 comments on commit 15b41ad

Please sign in to comment.