[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH v5 06/28] qapi: Add some union tests

From: Eric Blake
Subject: Re: [Qemu-devel] [PATCH v5 06/28] qapi: Add some union tests
Date: Tue, 31 Mar 2015 12:15:59 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0

On 03/31/2015 11:13 AM, Kevin Wolf wrote:

>> [3]
>> { 'union':'SimpleAndSafe', 'discriminator':'MyEnum',
>>   'data':{ 'one':'str', 'two':'int' }}
>> {"execute":"foo", "arguments":{'type':'one', 'data':'hello'}}
> This would overload 'discriminator' with two different meanings:
> * In case [1] it refers to one key in the JSON object which contains the
>   name of the union branch to select. That is, it is the _name_ of the
>   key used as a discriminator.
> * In case [3], the 'type' key is used in the JSON object to select a
>   union branch. 'discriminator' describes the _valid values_ of it, i.e.
>   the branch names.
> We shouldn't mix these meanings. If you need [3], we could call it
> 'discriminator-type' or something like that. If both options are
> consistently used for simple and flat unions, you end up with these
> rules:

Nice idea. Good thing I haven't actually implemented the extension yet.

> * 'discriminator' is the key that is used to select the union branch.
>   - For flat unions it is required and must refer to an explicitly
>     declared key in the base type.
>   - For simple unions it is optional and defaults to 'type'. The key is
>     implicitly created in the union type.
> * 'discrimiator-type' describes the valid values of 'discriminator',
>   either by referring to an enum type or to 'str'.

Referring to 'str' should only be allowed for implicit enums.  We
already require it to resolve to an enum for flat unions.

>   - For flat unions, this must match the type of the explicit
>     declaration of the discriminator field. It is optional and defauls
>     to the only valid value.
>   - For simple unions it is optional, too, and defaults to 'str'.
>   - If it is the name of an enum type, that enum type is reused and the
>     declared union branches must match the valid values of the enum.
>   - If it is 'str', a new enum is generated, and all the declared union
>     branches are used as valid values.

Sounds reasonable to me.  But even with this approach, I still don't
know that we have room for a 'base' type on a simple union.  I guess the
joy of future extensions is that we can make legal whatever we want, and
that this series is adding enough tests to make it obvious whether those
new additions break any existing test cases.

> There's quite some duplication in it where we need to make sure that the
> schema matches in all places, but without an explicit declaration of the
> disriminator field in simple unions, this seems to be the best I can
> come up with.

Yeah, it's a bit more verbose than the overloaded version, but may be
worth it.

>> But the existing, unused-except-in-testsuite, notion of a simple union
>> with a base class looks like:
>> [4]
>> { 'type':'Shared', 'data':{'common':'int'}}
>> { 'union':'SimpleWithBase', 'base':'Shared',
>>   'data':{ 'one':'str', 'two':'int' }}
>> {"execute":"foo", "arguments":{'common':1, 'type':'one', 'data':'hello'}}
>> If we were to allow the addition of 'discriminator':'EnumType' to a
>> simple union [3], but then add that discriminator to an existing case of
>> a simple union with base [4], it would look like:
>> { 'type':'Shared', 'data':{'common':'int'}}
>> { 'union':'SimpleWithBaseAndDiscriminator', 'base':'Shared',
>>   'discriminator':'MyEnum',
>>   'data':{ 'one':'str', 'two':'int' }}
>> Yuck.  That is indistinguishable from flat unions [1], except by whether
>> discriminator names an enum type or a member of the base class.
> Which could even be ambiguous, couldn't it?

My whole argument for why I _don't_ want to allow base types for simple

>>> In particular, I can define simple unions in terms of flat ones by
>>> restricting all union cases to a single member named 'data'.  They're
>>> not implemented that way, but that's a historical accident.  Simple
>>> unions are a redundant concept.
>> Cool.  Or more concretely,
>> { 'union': 'Simple', 'data': { 'one': 'str', 'two': 'int' } }
>> is identical on the wire to:
>> { 'enum': 'MyEnum', 'data': ['one', 'two'] }
>> { 'type': 'Base', 'data': { 'type': 'MyEnum' } }
>> { 'type': 'Branch1', 'data': { 'data': 'str' } }
>> { 'type': 'Branch2', 'data': { 'data': 'int' } }
>> { 'union': 'Flat', 'base': 'Base', 'discriminator': 'type',
>>   'data': { 'one': 'Branch1', 'two': 'Branch2' } }
> Perhaps we should expose all unions for schema introspection in a way
> similar to this (possibly not splitting out Branch1 and Branch2 as
> independent types, though). We would just have to give a name to
> implicitly generated enums and base types.

So maybe we update the schema to allow anonymous types.  That is:

{ 'union': 'Simple',
  'data': { 'one': { 'name': 'str', 'value': 'int' } } }

would be nicer than the current requirement of:

{ 'type': 'Branch1', 'data': { 'name': 'str', 'value': 'int' } }
{ 'union': 'Simple',
  'data': { 'one': 'Branch1' } }

Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]