Hi, I the best way is to have the type have a field where its value is an
enum with only one choice.
I don't remember the avro syntax perfectly, but it would be something like
this
{ "type": "enum",
"name": "NameType",
"symbols" : ["First"]
}
Then you get the type by looking at the value of NameType. And for validation,
it will only validate if the valueof NameType is the the single allowed one.
It's a bit hacky, but works, and gets rid of the requirement to ahve
horrible nesting.
On Wed, Apr 25, 2018 at 7:27 AM, Marcelo Valle <[email protected]> wrote:
> I am writing a python program using the official avro library for python,
> version 1.8.2.
>
> This is a simple schema to show my problem:
>
> {
> "type": "record",
> "namespace": "com.example",
> "name": "NameUnion",
> "fields": [
> {
> "name": "name",
> "type": [
> {
> "type": "record",
> "namespace": "com.example",
> "name": "FullName",
> "fields": [
> {
> "name": "first",
> "type": "string"
> },
> {
> "name": "last",
> "type": "string"
> }
> ]
> },
> {
> "type": "record",
> "namespace": "com.example",
> "name": "ConcatenatedFullName",
> "fields": [
> {
> "name": "entireName",
> "type": "string"
> }
> ]
> }
> ]
> }
> ]
> }
>
> Possible datums for this schema would be `{"name": {"first": "Hakuna",
> "last": "Matata"}}` and `{"name": {"entireName": "Hakuna Matata"}}`.
>
> However, this gives margin to ambiguity, as not always avro will be able
> to detect the right schema specified in the union. In this case, either
> datum will correspond to 1 and only 1 valid schema, but there might be a
> case where more than 1 schema in the union would be valid.
>
> I wonder whether it would be possible to use a datum like `{"name":
> {"FullName": {"first": "Hakuna", "last": "Matata"}}}`, where the specific
> union schema name is specified in the datum.
>
> Is it possible? How to do it?
>
> --
> Marcelo Valle
> http://mvalle.com - @mvallebr
>
--
Sent using my Mnemonic Memory Circuit