From 93ffb3b19bbfa08ba81eff07bd4ec5ef0b3bdf4c Mon Sep 17 00:00:00 2001 From: Sebastien MALOT Date: Wed, 29 Jul 2020 14:23:17 +0200 Subject: [PATCH] Add support for logicalType in schema export (#7) --- .gitignore | 1 + lib/avro/schema.php | 52 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 2ae1915..da80e3c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .idea/ vendor/ composer.lock +bin/composer.phar !bin/php !build/.gitkeep diff --git a/lib/avro/schema.php b/lib/avro/schema.php index 3b4bbcd..a8853d4 100644 --- a/lib/avro/schema.php +++ b/lib/avro/schema.php @@ -1286,6 +1286,9 @@ static function parse_fields($field_data, $default_namespace, &$schemata) $type = AvroUtil::array_value($field, AvroSchema::TYPE_ATTR); $order = AvroUtil::array_value($field, AvroField::ORDER_ATTR); $doc = AvroUtil::array_value($field, AvroSchema::DOC_ATTR); + $logical_type = AvroUtil::array_value($field, AvroSchema::LOGICAL_TYPE_ATTR); + $precision = AvroUtil::array_value($field, AvroField::PRECISION_ATTR); + $scale = AvroUtil::array_value($field, AvroField::SCALE_ATTR); $default = null; $has_default = false; @@ -1309,7 +1312,8 @@ static function parse_fields($field_data, $default_namespace, &$schemata) $field_schema = self::subparse($type, $default_namespace, $schemata); $new_field = new AvroField($name, $field_schema, $is_schema_from_schemata, - $has_default, $default, $order, $doc); + $has_default, $default, $order, $doc, + $logical_type, $precision, $scale); $field_names []= $name; $fields []= $new_field; } @@ -1419,6 +1423,16 @@ class AvroField extends AvroSchema */ const ORDER_ATTR = 'order'; + /** + * @var string + */ + const PRECISION_ATTR = 'precision'; + + /** + * @var string + */ + const SCALE_ATTR = 'scale'; + /** * @var string */ @@ -1494,20 +1508,40 @@ private static function check_order_value($order) */ private $doc; + /** + * @var string logical type of this field + */ + private $logical_type; + + /** + * @var int precision of the logical type + */ + private $precision; + + /** + * @var int scale of the logical type + */ + private $scale; + /** * @param string $name * @param AvroSchema $schema * @param boolean $is_type_from_schemata - * @param $has_default + * @param boolean $has_default * @param string $default * @param string $order + * @param string $doc + * @param string $logical_type + * @param int $precision + * @param int $scale * @throws AvroSchemaParseException * @internal param string $type * @todo Check validity of $default value * @todo Check validity of $order value */ public function __construct($name, $schema, $is_type_from_schemata, - $has_default, $default, $order=null, $doc=null) + $has_default, $default, $order=null, $doc=null, + $logical_type=null, $precision=null, $scale=null) { if (!AvroName::is_well_formed_name($name)) throw new AvroSchemaParseException('Field requires a "name" attribute'); @@ -1521,6 +1555,9 @@ public function __construct($name, $schema, $is_type_from_schemata, $this->check_order_value($order); $this->order = $order; $this->doc = $doc; + $this->logical_type = $logical_type; + $this->precision = $precision; + $this->scale = $scale; } /** @@ -1542,6 +1579,15 @@ public function to_avro() if ($this->doc) $avro[AvroSchema::DOC_ATTR] = $this->doc; + if ($this->logical_type) + $avro[AvroSchema::LOGICAL_TYPE_ATTR] = $this->logical_type; + + if ($this->precision) + $avro[AvroField::PRECISION_ATTR] = $this->precision; + + if ($this->scale) + $avro[AvroField::SCALE_ATTR] = $this->scale; + return $avro; }