Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(excel2json): new user error (InputError) implementation in prope…
- Loading branch information
1 parent
286abf8
commit b1703e1
Showing
8 changed files
with
500 additions
and
259 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
from dataclasses import dataclass | ||
from typing import Protocol | ||
|
||
# pylint: disable=too-few-public-methods | ||
|
||
|
||
separator = "\n " | ||
list_separator = "\n - " | ||
|
||
|
||
class Problem(Protocol): | ||
"""Information about input errors.""" | ||
|
||
def execute_error_protocol(self) -> str: | ||
""" | ||
This function initiates all the steps for successful problem communication with the user. | ||
""" | ||
|
||
|
||
@dataclass(frozen=True) | ||
class RequiredColumnMissingProblem: | ||
"""This class contains information if a required column is missing.""" | ||
|
||
columns: list[str] | ||
|
||
def execute_error_protocol(self) -> str: | ||
""" | ||
This function initiates all the steps for successful problem communication with the user. | ||
Returns: | ||
message for the error | ||
""" | ||
return f"The following required column(s) are missing:{list_separator}{list_separator.join(self.columns)}" | ||
|
||
|
||
@dataclass(frozen=True) | ||
class DuplicatesInColumnProblem: | ||
"""This class contains information if a required column is missing.""" | ||
|
||
column: str | ||
duplicate_values: list[str] | ||
|
||
def execute_error_protocol(self) -> str: | ||
""" | ||
This function initiates all the steps for successful problem communication with the user. | ||
Returns: | ||
message for the error | ||
""" | ||
return ( | ||
f"No duplicates are allowed in the column '{self.column}'\n" | ||
f"The following values appear several times:{list_separator}" | ||
f"{list_separator.join(self.duplicate_values)}" | ||
) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class MissingValuesInRowProblem: | ||
"""This class contains information if a required column is missing.""" | ||
|
||
column: str | ||
row_numbers: list[int] | ||
|
||
def execute_error_protocol(self) -> str: | ||
""" | ||
This function initiates all the steps for successful problem communication with the user. | ||
Returns: | ||
message for the error | ||
""" | ||
nums = [str(x) for x in self.row_numbers] | ||
return f"The column '{self.column}' must have values in the row(s):{list_separator}{list_separator.join(nums)}" | ||
|
||
|
||
@dataclass(frozen=True) | ||
class InvalidExcelContentProblem: | ||
"""This class contains information if a required column is missing.""" | ||
|
||
expected_content: str | ||
actual_content: str | ||
column: str | ||
row: int | ||
|
||
def execute_error_protocol(self) -> str: | ||
""" | ||
This function initiates all the steps for successful problem communication with the user. | ||
Returns: | ||
message for the error | ||
""" | ||
return ( | ||
f"There is invalid content in the column: '{self.column}', row: {self.row}{separator}" | ||
f"Expected Content: {self.expected_content}{separator}" | ||
f"Actual Content: {self.actual_content}" | ||
) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class JsonValidationPropertyProblem: | ||
"""This class contains information about a JSON property section that fails its validation against the schema.""" | ||
|
||
problematic_value: str | None = None | ||
original_msg: str | None = None | ||
message_path: str | None = None | ||
excel_column: str | None = None | ||
excel_row: int | None = None | ||
|
||
def execute_error_protocol(self) -> str: | ||
""" | ||
This function initiates all the steps for successful problem communication with the user. | ||
Returns: | ||
message for the error | ||
""" | ||
msg = [ | ||
f"{separator}Section of the problem: 'Properties'", | ||
] | ||
if self.problematic_value: | ||
msg.append(f"Problematic value: '{self.problematic_value}'") | ||
if self.excel_row: | ||
msg.append(f"The problem is caused by the value in the Excel row {self.excel_row}") | ||
if self.excel_column: | ||
msg.append(f"The problem is caused by the value in the Excel column '{self.excel_column}'") | ||
if self.original_msg: | ||
msg.append(f"Original Error Message:\n{self.original_msg}") | ||
if self.message_path: | ||
msg.append(f"The error occurred at {self.message_path}") | ||
return separator.join(msg) |
Oops, something went wrong.