qemu-devel
[Top][All Lists]
Advanced

[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: Kevin Wolf
Subject: Re: [Qemu-devel] [PATCH v5 06/28] qapi: Add some union tests
Date: Tue, 31 Mar 2015 20:34:46 +0200
User-agent: Mutt/1.5.21 (2010-09-15)

Am 31.03.2015 um 20:15 hat Eric Blake geschrieben:
> 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.

I think we have room for it, but probably still no use case. And they
are kind of ugly with mixing top-level fields for the base type and
nested ones for the branch-specific fields. (To be honest, I find all
non-flat unions ugly, but mixing is even uglier.)

So I certainly don't object to removing simple unions with base.

> > 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
> unions.

Essentially the problem is that we distinguish between simple and flat
unions only by looking at currently arbitrarily restricted options. So
another option to solve it would be a boolean 'flat', which we would set
as false for all existing simple unions, and let it default to true.
That would give us nicer QMP for new unions, too.

(I still don't think we need to save simple unions with base types, but
it's a thought I wanted to mention anyway because I like flat unions.)

> >>> 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' } }

Hm... You just removed them, right? Maybe my suggestion isn't that
constructive then. :-)

Kevin

Attachment: pgpa3c0iX7Noy.pgp
Description: PGP signature


reply via email to

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