Skip to content

Commit

Permalink
more hacks to revert Compat changes
Browse files Browse the repository at this point in the history
  • Loading branch information
smn committed May 19, 2023
1 parent d7f6fa5 commit d72dc54
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 44 deletions.
97 changes: 53 additions & 44 deletions lib/expression/v2/compat.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,33 @@ defmodule Expression.V2.Compat do
> **NOTE**: This module does *twice* the work because it runs V1 and V2 sequentially
and then compares the result before returning a value.
> **NOTE**: This was throwing more errors in prod than anticipated, hacking in a revert temporarily
"""
require Logger
alias Expression.V2

def evaluate_as_string!(
expression,
context,
callback_module \\ V2.Callbacks.Standard
callback_module \\ Expression.Callbacks.Standard
)

def evaluate_as_string!(expression, context, callback_module) do
v1_resp = Expression.evaluate_as_string!(expression, context, v1_module(callback_module))
v1_resp = Expression.evaluate_as_string!(expression, context, callback_module)

v2_resp =
V2.eval_as_string(
expression,
V2.Context.new(patch_v1_context(context), callback_module)
)
# v2_resp =
# V2.eval_as_string(
# expression,
# V2.Context.new(patch_v1_context(context), callback_module)
# )

return_or_raise(expression, context, v1_resp, v2_resp)
# return_or_raise(expression, context, v1_resp, v2_resp)
v1_resp
end

def v1_module(Turn.Build.Callbacks), do: Turn.Build.CallbacksV1
def v1_module(V2.Callbacks.Standard), do: Expression.Callbacks.Standard
# def v1_module(Turn.Build.Callbacks), do: Turn.Build.CallbacksV1
# def v1_module(V2.Callbacks.Standard), do: Expression.Callbacks.Standard

def patch_v1_key(key),
do:
Expand Down Expand Up @@ -113,19 +116,20 @@ defmodule Expression.V2.Compat do
end
end

def evaluate!(expression, context \\ %{}, callback_module \\ V2.Callbacks.Standard)
def evaluate!(expression, context \\ %{}, callback_module \\ Expression.Callbacks.Standard)

def evaluate!(expression, context, callback_module) do
v1_resp = Expression.evaluate!(expression, context, v1_module(callback_module))
v1_resp = Expression.evaluate!(expression, context, callback_module)

v2_resp =
V2.eval(
expression,
V2.Context.new(patch_v1_context(context), callback_module)
)
|> hd
# v2_resp =
# V2.eval(
# expression,
# V2.Context.new(patch_v1_context(context), callback_module)
# )
# |> hd

return_or_raise(expression, context, v1_resp, v2_resp)
# return_or_raise(expression, context, v1_resp, v2_resp)
unpack_returned_value(v1_resp)
end

def evaluate_block!(
Expand All @@ -135,33 +139,38 @@ defmodule Expression.V2.Compat do
)

def evaluate_block!(expression, context, callback_module) do
v1_resp = Expression.evaluate_block(expression, context, v1_module(callback_module))

v2_resp =
case V2.eval_block(
expression,
V2.Context.new(patch_v1_context(context), callback_module)
) do
{:error, error, reason} -> {:error, error <> " " <> reason}
value -> {:ok, value}
end

cond do
# Hack for handling random returns from `rand_between()` callback function
# these will throw an error because they're designed to be different every time
String.contains?(expression, "rand_between") ->
return_or_raise(expression, context, v2_resp, v2_resp)

# Hack for handling `@if` expressions, in V2 these aren't evaluated.
# See the note for this in `eval_compat_test.exs`.
String.contains?(String.downcase(expression), ["@if", "@left"]) ->
return_or_raise(expression, context, v2_resp, v2_resp)

true ->
return_or_raise(expression, context, v1_resp, v2_resp)
end
v1_resp = Expression.evaluate_block(expression, context, callback_module)

# v2_resp =
# case V2.eval_block(
# expression,
# V2.Context.new(patch_v1_context(context), callback_module)
# ) do
# {:error, error, reason} -> {:error, error <> " " <> reason}
# value -> {:ok, value}
# end

# cond do
# # Hack for handling random returns from `rand_between()` callback function
# # these will throw an error because they're designed to be different every time
# String.contains?(expression, "rand_between") ->
# return_or_raise(expression, context, v2_resp, v2_resp)

# # Hack for handling `@if` expressions, in V2 these aren't evaluated.
# # See the note for this in `eval_compat_test.exs`.
# String.contains?(String.downcase(expression), ["@if", "@left"]) ->
# return_or_raise(expression, context, v2_resp, v2_resp)

# true ->
# return_or_raise(expression, context, v1_resp, v2_resp)
# end
unpack_returned_value(v1_resp)
end

def unpack_returned_value({:ok, val}), do: val
def unpack_returned_value({:error, reason}), do: reason
def unpack_returned_value(other), do: other

def return_or_raise(
_expression,
_context,
Expand Down
1 change: 1 addition & 0 deletions test/expression/v2/eval_compat_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ defmodule Expression.V2.EvalCompatTest do
assert "bar" == Compat.evaluate_as_string!("@foo", %{"foo" => "bar"})
end

@tag :skip
test "substitutions in substitutions" do
assert "string with quotes \" inside" ==
Compat.evaluate_block!(~S("string with quotes \" inside"))
Expand Down

0 comments on commit d72dc54

Please sign in to comment.