-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: main
Are you sure you want to change the base?
Changes from all commits
cd72cc4
21db304
d2d5970
2c57c80
749451c
afcfa1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,9 @@ class KafkaWorker implements WorkerRunnerInterface | |
|
||
private Client $client; | ||
private string $brokerList; | ||
/** @var array|string[] */ | ||
/** @var array<string, string> */ | ||
private array $customMapping; | ||
djklim87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** @var array<int, string> */ | ||
private array $topicList; | ||
private string $consumerGroup; | ||
private string $bufferTable; | ||
|
@@ -45,6 +47,7 @@ class KafkaWorker implements WorkerRunnerInterface | |
* full_name:string, | ||
* buffer_table:string, | ||
* destination_name:string, | ||
* custom_mapping: string, | ||
* query:string, | ||
* attrs:string } $instance | ||
* | ||
|
@@ -61,6 +64,10 @@ public function __construct( | |
$this->client = $client; | ||
$this->consumerGroup = $attrs['group']; | ||
$this->brokerList = $attrs['broker']; | ||
|
||
/** @var array<string,string> $decodedMapping */ | ||
$decodedMapping = json_decode($instance['custom_mapping'], true); | ||
$this->customMapping = $decodedMapping; | ||
$this->bufferTable = $instance['buffer_table']; | ||
$this->topicList = array_map(fn($item) => trim($item), explode(',', $attrs['topic'])); | ||
$this->batchSize = (int)$attrs['batch']; | ||
|
@@ -208,8 +215,13 @@ private function mapMessages(array $batch): array { | |
private function handleRow(array $message): array { | ||
$row = []; | ||
foreach ($this->fields as $fieldName => $fieldType) { | ||
if (isset($message[$fieldName])) { | ||
$row[$fieldName] = $this->morphValuesByFieldType($message[$fieldName], $fieldType); | ||
$inputKeyName = $fieldName; | ||
if ($this->customMapping !== [] | ||
&& isset($this->customMapping[$fieldName])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isset should be enough? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In case isset is enough let's remove There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have declaration
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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’m not sure this is a good idea: |
||
$inputKeyName = $this->customMapping[$fieldName]; | ||
} | ||
if (isset($message[$inputKeyName])) { | ||
$row[$fieldName] = $this->morphValuesByFieldType($message[$inputKeyName], $fieldType); | ||
} else { | ||
if (in_array( | ||
$fieldType, [Fields::TYPE_INT, | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.