Skip to content

Commit

Permalink
AVRO-4090: Avoid repeating data validation
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagorb committed Nov 15, 2024
1 parent 1a2f167 commit 108cf5e
Showing 1 changed file with 18 additions and 5 deletions.
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);
}
}

0 comments on commit 108cf5e

Please sign in to comment.