Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions samcli/cli/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ class CfnParameterOverridesType(click.ParamType):

ordered_pattern_match = [_pattern_1, _pattern_2]

# NOTE(TheSriram): name needs to be added to click.ParamType requires it.
name = ""
name = "string,list"

def convert(self, value, param, ctx):
result = {}
Expand Down Expand Up @@ -140,8 +139,7 @@ class CfnMetadataType(click.ParamType):

_pattern = r"(?:{key}={value})".format(key=PARAM_AND_METADATA_KEY_REGEX, value=VALUE_REGEX_COMMA_DELIM)

# NOTE(TheSriram): name needs to be added to click.ParamType requires it.
name = ""
name = "string"

def convert(self, value, param, ctx):
result = {}
Expand Down Expand Up @@ -196,8 +194,7 @@ def __init__(self, multiple_values_per_key=False):

_pattern = r"{tag}={tag}".format(tag=_generate_match_regex(match_pattern=TAG_REGEX, delim=" "))

# NOTE(TheSriram): name needs to be added to click.ParamType requires it.
name = ""
name = "list"

def convert(self, value, param, ctx):
result = {}
Expand Down Expand Up @@ -301,8 +298,7 @@ class SigningProfilesOptionType(click.ParamType):

pattern = r"(?:(?: )([A-Za-z0-9\"]+)=(\"(?:\\.|[^\"\\]+)*\"|(?:\\.|[^ \"\\]+)+))"

# Note: this is required, otherwise it is failing when running help
name = ""
name = "string"

def convert(self, value, param, ctx):
"""
Expand Down Expand Up @@ -393,7 +389,7 @@ def transform(self, *args, **kwargs):

# Transformation callback function checks if the received option value is a valid ECR url.
transformer = Transformer(converter=click.STRING, transformation=is_ecr_url)
name = ""
name = "string"

def convert(self, value, param, ctx):
"""
Expand All @@ -410,7 +406,7 @@ class ImageRepositoriesType(click.ParamType):
Custom Parameter Type for Multi valued Image Repositories option.
"""

name = ""
name = "list"
KEY_VALUE_PAIR_LENGTH = 2

def convert(self, value, param, ctx):
Expand All @@ -431,7 +427,7 @@ class RemoteInvokeBotoApiParameterType(click.ParamType):
Custom Parameter Type for Multi valued Boto API parameter option of remote invoke command.
"""

name = ""
name = "list"
MIN_KEY_VALUE_PAIR_LENGTH = 2

def convert(self, value, param, ctx):
Expand Down
34 changes: 15 additions & 19 deletions schema/make_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@
PARAMS_TO_OMIT_DEFAULT_FIELD = [
"layer_cache_basedir" # sets default to root directory to that of machine the schema is generated on
]
EXCEPTION_OPTION_CLASS_MAPPER = { # for params without proper class names, map them uniquely
"parameter_overrides": ["array", "string"],
"image_repository": ["string"],
"image_repositories": ["array"],
"metadata": ["string"],
"signing_profiles": ["string"],
"tags": ["array"],
"parameter": ["array"],
}
CHARS_TO_CLEAN = [
"\b", # backspaces
"\u001b[0m", # ANSI start bold
Expand Down Expand Up @@ -156,21 +147,26 @@ def format_param(param: click.core.Option) -> SamCliParameterSchema:
"""
if not param:
raise SchemaGenerationException("Expected to format a parameter that doesn't exist")
if not param.type.name and param.name not in EXCEPTION_OPTION_CLASS_MAPPER.keys():
if not param.type.name:
raise SchemaGenerationException(f"Parameter {param} passed without a type:\n{param.type}")

param_type = param.type.name.lower()
param_type = []
if "," in param.type.name: # custom type with support for various input values
param_type = [x.lower() for x in param.type.name.split(",")]
else:
param_type.append(param.type.name.lower())

formatted_param_types = []
# NOTE: Params do not have explicit "string" type; either "text" or "path".
# All choice options are from a set of strings.
if param.name in EXCEPTION_OPTION_CLASS_MAPPER.keys():
formatted_param_types = EXCEPTION_OPTION_CLASS_MAPPER[param.name]
elif param_type in ["text", "path", "choice", "filename", "directory"]:
formatted_param_types = ["string"]
elif param_type == "list":
formatted_param_types = ["array"]
else:
formatted_param_types = [param_type] or ["string"]
for param_name in param_type:
if param_name in ["text", "path", "choice", "filename", "directory"]:
formatted_param_types.append("string")
elif param_name == "list":
formatted_param_types.append("array")
else:
formatted_param_types.append(param_name or "string")
formatted_param_types = sorted(list(set(formatted_param_types))) # deduplicate

formatted_param: SamCliParameterSchema = SamCliParameterSchema(
param.name or "",
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/schema/test_schema_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ def test_parameter_to_schema(self, property_name, property_value, added_property
expected_schema.update(added_property_field)
self.assertEqual(expected_schema, param_schema)

def test_parameter_to_schema_with_multiple_type(self):
param = SamCliParameterSchema("param name", ["type1", "type2"], "param description")

param_schema = param.to_schema()

expected_schema = {"title": "param name", "type": ["type1", "type2"], "description": "param description"}
self.assertEqual(expected_schema, param_schema)


class TestCommandSchema(TestCase):
def test_command_to_schema(self):
Expand Down Expand Up @@ -87,6 +95,9 @@ class TestSchemaLogic(TestCase):
("filename", "string"),
("directory", "string"),
("LIST", "array"),
("type1,type2", ["type1", "type2"]),
("list,type1", ["array", "type1"]),
("string,path,choice,filename,directory", "string"),
]
)
def test_param_formatted_correctly(self, param_type, expected_type):
Expand Down