This is an automated email from the ASF dual-hosted git repository. opwvhk pushed a commit to branch branch-1.12 in repository https://gitbox.apache.org/repos/asf/avro.git
commit 5324d94ebe2ca7145ef5d81aa01590cedbc46fb2 Author: Thiago Romão Barcala <[email protected]> AuthorDate: Fri Apr 18 22:31:41 2025 +0200 AVRO-4090: Avoid repeating data validation (#3241) (cherry picked from commit d5d5466d8d8a36fcbecbc924515174638f7ad515) --- lang/php/lib/Datum/AvroIODatumWriter.php | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/lang/php/lib/Datum/AvroIODatumWriter.php b/lang/php/lib/Datum/AvroIODatumWriter.php index 000f070690..ef8031f763 100644 --- a/lang/php/lib/Datum/AvroIODatumWriter.php +++ b/lang/php/lib/Datum/AvroIODatumWriter.php @@ -59,7 +59,7 @@ class AvroIODatumWriter * @param AvroSchema $writers_schema * @param $datum * @param AvroIOBinaryEncoder $encoder - * @returns mixed + * @return mixed * * @throws AvroIOTypeException if $datum is invalid for $writers_schema */ @@ -69,6 +69,19 @@ class AvroIODatumWriter 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); @@ -120,7 +133,7 @@ class AvroIODatumWriter $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); @@ -139,7 +152,7 @@ class AvroIODatumWriter $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); @@ -163,7 +176,7 @@ class AvroIODatumWriter 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); } } @@ -184,6 +197,6 @@ class AvroIODatumWriter } $encoder->writeLong($datum_schema_index); - $this->writeData($datum_schema, $datum, $encoder); + $this->writeValidatedData($datum_schema, $datum, $encoder); } }
