-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Please allow types re-definition (custom mapping) #33
Comments
Thanks for the input, I'll look into it ✌️ |
So the generator is still a bit experimental, so i think the following things are missing right now (you pointed out most of them):
IMHO it would make sense to have doc comments to be able to define them,
In terms of types, i think unions are not properly supported yet, so i will enhance that. Additionally, i agree that it would be nice if one could use an own Parser that complies with a newly introduced |
I primarily use generators. And honestly, I need generation schema by a particular object, not for all classes. |
Good proposal. That may be useful. Meantime my intention was to provide some schema generation configuration, instead of changing classes. In my use case, I need to generate AVRO schema for PHP generated classes (PIMCore). So, generally, I have no control over classes themselves but want to change generated schema. E.g. take PHP doc field comment into AVRO doc. |
Thx for the insight, i will factor this in ✌️ |
So i've created a pre-release for this new functionality. I still need to do some more testing, but if you want you can already check it out. |
@nick-zh, thank you very much! It looks very promising! |
The concept itself looks good. Thank you!
|
I would like to suggest making the default value null by default. |
|
|
if (0 !== count($convertedUnionType) && [] !== $arrayType) {
$convertedUnionType[] = $arrayType;
} elseif (0 === count($convertedUnionType) && [] !== $arrayType) {
return $arrayType;
} Why if |
said by
Also there in spec note what if there present default value, that must match the first type:
So, that is logically incorrect to exclude I would like to propose:
diff --git a/src/Generator/SchemaGenerator.php b/src/Generator/SchemaGenerator.php
index 9e99bc6..81fbaee 100644
--- a/src/Generator/SchemaGenerator.php
+++ b/src/Generator/SchemaGenerator.php
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace PhpKafka\PhpAvroSchemaGenerator\Generator;
use PhpKafka\PhpAvroSchemaGenerator\Avro\Avro;
+use PhpKafka\PhpAvroSchemaGenerator\Exception\SchemaGeneratorException;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassInterface;
use PhpKafka\PhpAvroSchemaGenerator\PhpClass\PhpClassPropertyInterface;
use PhpKafka\PhpAvroSchemaGenerator\Registry\ClassRegistryInterface;
@@ -106,6 +107,9 @@ final class SchemaGenerator implements SchemaGeneratorInterface
if (PhpClassPropertyInterface::NO_DEFAULT !== $property->getPropertyDefault()) {
$field['default'] = $property->getPropertyDefault();
+ if (null === $field['default'] && (!is_array($field['type']) or empty($field['type']) or 'null' != @$field['type'][0])){
+ throw new SchemaGeneratorException('Provided default value "null", but that type is not provided as first possible in union (see https://avro.apache.org/docs/current/spec.html#Unions)!');
+ }
}
if (null !== $property->getPropertyDoc() && '' !== $property->getPropertyDoc()) { |
Hey @Hubbitus
Many thanks for having a look at the new implementation and challenging it 🙏 i appreciate this a lot |
FYI: Null is now allowed (alpha3), i will add the order check separately. I am using a different lib for schema check / deployment, but i agree, it should be a part of this project as well |
In that PR I also adjust most of the autotests, but not all. Some of them failed because of the step to reflection which we discussed separately (#38). But I could continue work if you like the overall approach. Please look. P.S. And @nick-zh, really big thank you for the fast responses and spent time! I've appreciate that. |
This seems very specific and i don't think it is something the default implementation should cover or be able to extend upon.
This seems again very specific The spirit of the generator is, you have some properties and some comments / types defined for these properties, and we generate an avro schema for that. Any logic beyond that will be hard to cover in a general way, since projects differ a lot in the real world and how people tend to write their classes. If there is anything i can do to make extension easier though, i am totally open to that as long as it is not too specific, e.g. see proposal in #49 I moved the other questions to discussions, it is getting a bit overwhelming otherwise and these are good points, but more on a general level for this project 😄 |
Sure such logic is very project-specific!!! I did not ask to add parsing types by seeing in getters in core implementation! One note, please, src/Parser/AvroClassPropertyParser.php is just like demonstration how that may be used for override. That is not intended to be part of real PR. |
…p (that should act as example of extending and override)
@nick-zh, please look I've fixed almost all tests, but there still 2 failed like:
But that related to the public int|string $blaaaaaaaa; That use Union type declaration which is available since PHP 8.0.0. |
…alizing compare
…lassPropertyTypeItem::jsonSerialize in tests
…lassPropertyTypeItem::jsonSerialize in tests
Now most interesting for me to define optional (nullable fields), so, instead of just:
Allow to set something like:
But there are also more interesting cases when e.g. instead of just
int
I would like to use logicalType date.Please allow providing some functional interface to override fixed mechanism by providing an array of mappers, or just
callable
.The text was updated successfully, but these errors were encountered: