Skip to content

Commit

Permalink
Add support for logicalType in schema export (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
smalot authored Jul 29, 2020
1 parent f031a4d commit 93ffb3b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea/
vendor/
composer.lock
bin/composer.phar
!bin/php
!build/.gitkeep
52 changes: 49 additions & 3 deletions lib/avro/schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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');
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

Expand Down

0 comments on commit 93ffb3b

Please sign in to comment.