Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/kafka mapping support #420

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

djklim87
Copy link
Contributor

No description provided.

$row[$fieldName] = $this->morphValuesByFieldType($message[$fieldName], $fieldType);
$inputKeyName = $fieldName;
if ($this->customMapping !== []
&& isset($this->customMapping[$fieldName])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isset should be enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case isset is enough let's remove $this->customMapping !== [] cuz it does not do anything, isset is enough

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because during the construct, we use json_decode. If the JSON is corrupted, it can return false. That’s why we check if it’s an array.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have declaration

private array $customMapping;

In case it returns false and we try to assign, we will fail already before this code, so I guess [] can be removed

	$decodedMapping = json_decode($instance['custom_mapping'], true);
	$this->customMapping = $decodedMapping;

What if we will do this thing:

	$decodedMapping = json_decode($instance['custom_mapping'], true) ?: []

So it will make sure we have array even if it returns false and avoid strict type fatal error

@@ -104,6 +104,7 @@ public function runPool(): void {
* buffer_table:string,
* destination_name:string,
* query:string,
* custom_mapping: string,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We request string here but in parseMapping method returns false in case json failed to encode, is it normal?

Copy link
Contributor Author

@djklim87 djklim87 Dec 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have never encountered issues with json_encode. In my opinion, it’s nearly impossible to encode simple array with an error

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSON easy to fail in case we accept user data. Does the encoded array get information from the outside of the world? User provide it? in case easy to trigger false so with unsupported Unicodes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that adding some additional checks to the mapping provided by the user is a good idea. However, after these checks, we should assume the data is correct and that there is no possibility of encoding/decoding errors.

src/Plugin/Queue/Handlers/Source/DropSourceHandler.php Outdated Show resolved Hide resolved
src/Plugin/Queue/Handlers/Source/CreateKafka.php Outdated Show resolved Hide resolved
@donhardman donhardman assigned donhardman and djklim87 and unassigned donhardman Dec 26, 2024
@djklim87 djklim87 requested a review from donhardman December 26, 2024 14:09
$row[$fieldName] = $this->morphValuesByFieldType($message[$fieldName], $fieldType);
$inputKeyName = $fieldName;
if ($this->customMapping !== []
&& isset($this->customMapping[$fieldName])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case isset is enough let's remove $this->customMapping !== [] cuz it does not do anything, isset is enough

@@ -104,6 +104,7 @@ public function runPool(): void {
* buffer_table:string,
* destination_name:string,
* query:string,
* custom_mapping: string,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSON easy to fail in case we accept user data. Does the encoded array get information from the outside of the world? User provide it? in case easy to trigger false so with unsupported Unicodes

@djklim87 djklim87 requested a review from donhardman January 3, 2025 16:18
Copy link
Contributor

@donhardman donhardman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but still have one issue with strict type checking when json decode returns false and we try to assign it to property declared as array, also we are hiding this checker with annotation, that probably not very good idea, cuz phpstan shows this issue

$row[$fieldName] = $this->morphValuesByFieldType($message[$fieldName], $fieldType);
$inputKeyName = $fieldName;
if ($this->customMapping !== []
&& isset($this->customMapping[$fieldName])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have declaration

private array $customMapping;

In case it returns false and we try to assign, we will fail already before this code, so I guess [] can be removed

	$decodedMapping = json_decode($instance['custom_mapping'], true);
	$this->customMapping = $decodedMapping;

What if we will do this thing:

	$decodedMapping = json_decode($instance['custom_mapping'], true) ?: []

So it will make sure we have array even if it returns false and avoid strict type fatal error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants