Skip to content

Commit

Permalink
Added option to serialize type attribute for primitive schema (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
bajdzun authored Jan 12, 2022
1 parent eb860a5 commit d566139
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
21 changes: 15 additions & 6 deletions lib/avro/schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

/**
* Avro Schema and and Avro Schema support classes.
* Avro Schema and Avro Schema support classes.
* @package Avro
*/

Expand Down Expand Up @@ -49,7 +49,7 @@
*/

/**
* Exceptions associated with parsing JSON schema represenations
* Exceptions associated with parsing JSON schema representations
* @package Avro
*/
class AvroSchemaParseException extends AvroException {};
Expand Down Expand Up @@ -330,7 +330,7 @@ static function real_parse($avro, $default_namespace=null, &$schemata=null)
$extra_attributes = array_diff_key($avro, array_flip(self::$reserved_attrs));

if (self::is_primitive_type($type))
return new AvroPrimitiveSchema($type, $logical_type, $extra_attributes);
return new AvroPrimitiveSchema($type, $logical_type, $extra_attributes, true);

elseif (self::is_named_type($type))
{
Expand Down Expand Up @@ -562,17 +562,22 @@ public function attribute($attribute) { return $this->$attribute(); }
class AvroPrimitiveSchema extends AvroSchema
{

private $serialize_type_attribute;

/**
* @param string $type the primitive schema type name
* @param string $logical_type a schema logical type name
* @param array $extra_attributes extra attributes defined on the schema
* @param boolean $serialize_type_attribute serialize type attribute in primitive schema
* @throws AvroSchemaParseException if the given $type is not a
* primitive schema type name
*/
public function __construct($type, $logical_type=null, $extra_attributes=array())
public function __construct($type, $logical_type=null, $extra_attributes=array(), $serialize_type_attribute=false)
{
if (self::is_primitive_type($type))
if (self::is_primitive_type($type)) {
$this->serialize_type_attribute = $serialize_type_attribute;
return parent::__construct($type, $logical_type, $extra_attributes);
}
throw new AvroSchemaParseException(
sprintf('%s is not a valid primitive type.', $type));
}
Expand All @@ -588,6 +593,10 @@ public function to_avro()
), $this->extra_attributes ?: array());
}

if (true === $this->serialize_type_attribute) {
return array(self::TYPE_ATTR => $this->type);
}

return $this->type;
}
}
Expand Down Expand Up @@ -1073,7 +1082,7 @@ public function __construct($schemata=array())

public function list_schemas() {
var_export($this->schemata);
foreach($this->schemata as $sch)
foreach($this->schemata as $sch)
print('Schema '.$sch->__toString()."\n");
}

Expand Down
2 changes: 1 addition & 1 deletion test/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected static function make_primitive_examples()
as $type)
{
$examples []= new SchemaExample(sprintf('"%s"', $type), true);
$examples []= new SchemaExample(sprintf('{"type": "%s"}', $type), true, sprintf('"%s"', $type));
$examples []= new SchemaExample(sprintf('{"type": "%s"}', $type), true, sprintf('{"type":"%s"}', $type));
}
return $examples;
}
Expand Down

0 comments on commit d566139

Please sign in to comment.