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

AVRO-4090: [php] Avoid repeating data validation #3241

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
23 changes: 18 additions & 5 deletions lang/php/lib/Datum/AvroIODatumWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function write($datum, $encoder)
* @param AvroSchema $writers_schema
* @param $datum
* @param AvroIOBinaryEncoder $encoder
* @returns mixed
* @return mixed
*
* @throws AvroIOTypeException if $datum is invalid for $writers_schema
*/
Expand All @@ -69,6 +69,19 @@ public function writeData($writers_schema, $datum, $encoder)
throw new AvroIOTypeException($writers_schema, $datum);
}

return $this->writeValidatedData($writers_schema, $datum, $encoder);
}

/**
* @param AvroSchema $writers_schema
* @param $datum
* @param AvroIOBinaryEncoder $encoder
* @return mixed
*
* @throws AvroIOTypeException if $datum is invalid for $writers_schema
*/
private function writeValidatedData($writers_schema, $datum, $encoder)
{
switch ($writers_schema->type()) {
case AvroSchema::NULL_TYPE:
return $encoder->writeNull($datum);
Expand Down Expand Up @@ -120,7 +133,7 @@ private function writeArray($writers_schema, $datum, $encoder)
$encoder->writeLong($datum_count);
$items = $writers_schema->items();
foreach ($datum as $item) {
$this->writeData($items, $item, $encoder);
$this->writeValidatedData($items, $item, $encoder);
}
}
return $encoder->writeLong(0);
Expand All @@ -139,7 +152,7 @@ private function writeMap($writers_schema, $datum, $encoder)
$encoder->writeLong($datum_count);
foreach ($datum as $k => $v) {
$encoder->writeString($k);
$this->writeData($writers_schema->values(), $v, $encoder);
$this->writeValidatedData($writers_schema->values(), $v, $encoder);
}
}
$encoder->writeLong(0);
Expand All @@ -163,7 +176,7 @@ private function writeEnum($writers_schema, $datum, $encoder)
private function writeRecord($writers_schema, $datum, $encoder)
{
foreach ($writers_schema->fields() as $field) {
$this->writeData($field->type(), $datum[$field->name()] ?? null, $encoder);
$this->writeValidatedData($field->type(), $datum[$field->name()] ?? null, $encoder);
}
}

Expand All @@ -184,6 +197,6 @@ private function writeUnion($writers_schema, $datum, $encoder)
}

$encoder->writeLong($datum_schema_index);
$this->writeData($datum_schema, $datum, $encoder);
$this->writeValidatedData($datum_schema, $datum, $encoder);
}
}