Skip to content

Commit 93ffb3b

Browse files
authored
Add support for logicalType in schema export (#7)
1 parent f031a4d commit 93ffb3b

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.idea/
22
vendor/
33
composer.lock
4+
bin/composer.phar
45
!bin/php
56
!build/.gitkeep

lib/avro/schema.php

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,9 @@ static function parse_fields($field_data, $default_namespace, &$schemata)
12861286
$type = AvroUtil::array_value($field, AvroSchema::TYPE_ATTR);
12871287
$order = AvroUtil::array_value($field, AvroField::ORDER_ATTR);
12881288
$doc = AvroUtil::array_value($field, AvroSchema::DOC_ATTR);
1289+
$logical_type = AvroUtil::array_value($field, AvroSchema::LOGICAL_TYPE_ATTR);
1290+
$precision = AvroUtil::array_value($field, AvroField::PRECISION_ATTR);
1291+
$scale = AvroUtil::array_value($field, AvroField::SCALE_ATTR);
12891292

12901293
$default = null;
12911294
$has_default = false;
@@ -1309,7 +1312,8 @@ static function parse_fields($field_data, $default_namespace, &$schemata)
13091312
$field_schema = self::subparse($type, $default_namespace, $schemata);
13101313

13111314
$new_field = new AvroField($name, $field_schema, $is_schema_from_schemata,
1312-
$has_default, $default, $order, $doc);
1315+
$has_default, $default, $order, $doc,
1316+
$logical_type, $precision, $scale);
13131317
$field_names []= $name;
13141318
$fields []= $new_field;
13151319
}
@@ -1419,6 +1423,16 @@ class AvroField extends AvroSchema
14191423
*/
14201424
const ORDER_ATTR = 'order';
14211425

1426+
/**
1427+
* @var string
1428+
*/
1429+
const PRECISION_ATTR = 'precision';
1430+
1431+
/**
1432+
* @var string
1433+
*/
1434+
const SCALE_ATTR = 'scale';
1435+
14221436
/**
14231437
* @var string
14241438
*/
@@ -1494,20 +1508,40 @@ private static function check_order_value($order)
14941508
*/
14951509
private $doc;
14961510

1511+
/**
1512+
* @var string logical type of this field
1513+
*/
1514+
private $logical_type;
1515+
1516+
/**
1517+
* @var int precision of the logical type
1518+
*/
1519+
private $precision;
1520+
1521+
/**
1522+
* @var int scale of the logical type
1523+
*/
1524+
private $scale;
1525+
14971526
/**
14981527
* @param string $name
14991528
* @param AvroSchema $schema
15001529
* @param boolean $is_type_from_schemata
1501-
* @param $has_default
1530+
* @param boolean $has_default
15021531
* @param string $default
15031532
* @param string $order
1533+
* @param string $doc
1534+
* @param string $logical_type
1535+
* @param int $precision
1536+
* @param int $scale
15041537
* @throws AvroSchemaParseException
15051538
* @internal param string $type
15061539
* @todo Check validity of $default value
15071540
* @todo Check validity of $order value
15081541
*/
15091542
public function __construct($name, $schema, $is_type_from_schemata,
1510-
$has_default, $default, $order=null, $doc=null)
1543+
$has_default, $default, $order=null, $doc=null,
1544+
$logical_type=null, $precision=null, $scale=null)
15111545
{
15121546
if (!AvroName::is_well_formed_name($name))
15131547
throw new AvroSchemaParseException('Field requires a "name" attribute');
@@ -1521,6 +1555,9 @@ public function __construct($name, $schema, $is_type_from_schemata,
15211555
$this->check_order_value($order);
15221556
$this->order = $order;
15231557
$this->doc = $doc;
1558+
$this->logical_type = $logical_type;
1559+
$this->precision = $precision;
1560+
$this->scale = $scale;
15241561
}
15251562

15261563
/**
@@ -1542,6 +1579,15 @@ public function to_avro()
15421579
if ($this->doc)
15431580
$avro[AvroSchema::DOC_ATTR] = $this->doc;
15441581

1582+
if ($this->logical_type)
1583+
$avro[AvroSchema::LOGICAL_TYPE_ATTR] = $this->logical_type;
1584+
1585+
if ($this->precision)
1586+
$avro[AvroField::PRECISION_ATTR] = $this->precision;
1587+
1588+
if ($this->scale)
1589+
$avro[AvroField::SCALE_ATTR] = $this->scale;
1590+
15451591
return $avro;
15461592
}
15471593

0 commit comments

Comments
 (0)