-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add base implementation for encode-decode intrinsic functions (#7709)
- Loading branch information
Showing
12 changed files
with
497 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
59 changes: 59 additions & 0 deletions
59
...tions/asl/component/intrinsic/function/statesfunction/encoding_decoding/base_64_decode.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import base64 | ||
from typing import Final | ||
|
||
from localstack.services.stepfunctions.asl.component.intrinsic.argument.function_argument_list import ( | ||
FunctionArgumentList, | ||
) | ||
from localstack.services.stepfunctions.asl.component.intrinsic.function.statesfunction.states_function import ( | ||
StatesFunction, | ||
) | ||
from localstack.services.stepfunctions.asl.component.intrinsic.functionname.state_fuinction_name_types import ( | ||
StatesFunctionNameType, | ||
) | ||
from localstack.services.stepfunctions.asl.component.intrinsic.functionname.states_function_name import ( | ||
StatesFunctionName, | ||
) | ||
from localstack.services.stepfunctions.asl.eval.environment import Environment | ||
|
||
|
||
class Base64Decode(StatesFunction): | ||
# Encodes data based on MIME Base64 encoding scheme. | ||
# | ||
# For example: | ||
# With input | ||
# { | ||
# "input": "Data to encode" | ||
# } | ||
# | ||
# The call | ||
# "base64.$": "States.Base64Encode($.input)" | ||
# | ||
# Returns | ||
# {"base64": "RGF0YSB0byBlbmNvZGU="} | ||
|
||
MAX_INPUT_CHAR_LEN: Final[int] = 10_000 | ||
|
||
def __init__(self, arg_list: FunctionArgumentList): | ||
super().__init__( | ||
states_name=StatesFunctionName(function_type=StatesFunctionNameType.Base64Decode), | ||
arg_list=arg_list, | ||
) | ||
if arg_list.size != 1: | ||
raise ValueError( | ||
f"Expected 1 argument for function type '{type(self)}', but got: '{arg_list}'." | ||
) | ||
|
||
def _eval_body(self, env: Environment) -> None: | ||
self.arg_list.eval(env=env) | ||
|
||
base64_string: str = env.stack.pop() | ||
if len(base64_string) > self.MAX_INPUT_CHAR_LEN: | ||
raise ValueError( | ||
f"Maximum input string for function type '{type(self)}' " | ||
f"is '{self.MAX_INPUT_CHAR_LEN}', but got '{len(base64_string)}'." | ||
) | ||
|
||
base64_string_bytes = base64_string.encode("ascii") | ||
string_bytes = base64.b64decode(base64_string_bytes) | ||
string = string_bytes.decode("ascii") | ||
env.stack.append(string) |
59 changes: 59 additions & 0 deletions
59
...tions/asl/component/intrinsic/function/statesfunction/encoding_decoding/base_64_encode.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import base64 | ||
from typing import Final | ||
|
||
from localstack.services.stepfunctions.asl.component.intrinsic.argument.function_argument_list import ( | ||
FunctionArgumentList, | ||
) | ||
from localstack.services.stepfunctions.asl.component.intrinsic.function.statesfunction.states_function import ( | ||
StatesFunction, | ||
) | ||
from localstack.services.stepfunctions.asl.component.intrinsic.functionname.state_fuinction_name_types import ( | ||
StatesFunctionNameType, | ||
) | ||
from localstack.services.stepfunctions.asl.component.intrinsic.functionname.states_function_name import ( | ||
StatesFunctionName, | ||
) | ||
from localstack.services.stepfunctions.asl.eval.environment import Environment | ||
|
||
|
||
class Base64Encode(StatesFunction): | ||
# Decodes data based on MIME Base64 encoding scheme. | ||
# | ||
# For example: | ||
# With input | ||
# { | ||
# "base64": "RGF0YSB0byBlbmNvZGU=" | ||
# } | ||
# | ||
# The call | ||
# "data.$": "States.Base64Decode($.base64)" | ||
# | ||
# Returns | ||
# {"data": "Decoded data"} | ||
|
||
MAX_INPUT_CHAR_LEN: Final[int] = 10_000 | ||
|
||
def __init__(self, arg_list: FunctionArgumentList): | ||
super().__init__( | ||
states_name=StatesFunctionName(function_type=StatesFunctionNameType.Base64Encode), | ||
arg_list=arg_list, | ||
) | ||
if arg_list.size != 1: | ||
raise ValueError( | ||
f"Expected 1 argument for function type '{type(self)}', but got: '{arg_list}'." | ||
) | ||
|
||
def _eval_body(self, env: Environment) -> None: | ||
self.arg_list.eval(env=env) | ||
|
||
string: str = env.stack.pop() | ||
if len(string) > self.MAX_INPUT_CHAR_LEN: | ||
raise ValueError( | ||
f"Maximum input string for function type '{type(self)}' " | ||
f"is '{self.MAX_INPUT_CHAR_LEN}', but got '{len(string)}'." | ||
) | ||
|
||
string_bytes = string.encode("ascii") | ||
string_base64_bytes = base64.b64encode(string_bytes) | ||
base64_string = string_base64_bytes.decode("ascii") | ||
env.stack.append(base64_string) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...stepfunctions/templates/intrinsicfunctions/statemachines/encode_decode/base64decode.json5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"Comment": "BASE_64_DECODE", | ||
"StartAt": "State_0", | ||
"States": { | ||
"State_0": { | ||
"Type": "Pass", | ||
"Parameters": { | ||
"FunctionResult.$": "States.Base64Decode($.FunctionInput)", | ||
}, | ||
"End": true, | ||
}, | ||
}, | ||
} |
13 changes: 13 additions & 0 deletions
13
...stepfunctions/templates/intrinsicfunctions/statemachines/encode_decode/base64encode.json5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"Comment": "BASE_64_ENCODE", | ||
"StartAt": "State_0", | ||
"States": { | ||
"State_0": { | ||
"Type": "Pass", | ||
"Parameters": { | ||
"FunctionResult.$": "States.Base64Encode($.FunctionInput)", | ||
}, | ||
"End": true, | ||
}, | ||
}, | ||
} |
53 changes: 53 additions & 0 deletions
53
tests/integration/stepfunctions/v2/intrinsic_functions/test_encode_decode.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import pytest | ||
|
||
from tests.integration.stepfunctions.templates.intrinsicfunctions.intrinsic_functions_templates import ( | ||
IntrinsicFunctionTemplate as IFT, | ||
) | ||
from tests.integration.stepfunctions.utils import is_old_provider | ||
from tests.integration.stepfunctions.v2.intrinsic_functions.utils import create_and_test_on_inputs | ||
|
||
pytestmark = pytest.mark.skipif( | ||
condition=is_old_provider(), reason="Test suite for v2 provider only." | ||
) | ||
|
||
|
||
# TODO: test for validation errors, and boundary testing. | ||
|
||
|
||
@pytest.mark.skip_snapshot_verify( | ||
paths=["$..loggingConfiguration", "$..tracingConfiguration", "$..previousEventId"] | ||
) | ||
class TestEncodeDecode: | ||
def test_base_64_encode( | ||
self, | ||
stepfunctions_client, | ||
create_iam_role_for_sfn, | ||
create_state_machine, | ||
snapshot, | ||
): | ||
input_values = ["", "Data to encode"] | ||
create_and_test_on_inputs( | ||
stepfunctions_client, | ||
create_iam_role_for_sfn, | ||
create_state_machine, | ||
snapshot, | ||
IFT.BASE_64_ENCODE, | ||
input_values, | ||
) | ||
|
||
def test_base_64_decode( | ||
self, | ||
stepfunctions_client, | ||
create_iam_role_for_sfn, | ||
create_state_machine, | ||
snapshot, | ||
): | ||
input_values = ["", "RGF0YSB0byBlbmNvZGU="] | ||
create_and_test_on_inputs( | ||
stepfunctions_client, | ||
create_iam_role_for_sfn, | ||
create_state_machine, | ||
snapshot, | ||
IFT.BASE_64_DECODE, | ||
input_values, | ||
) |
Oops, something went wrong.