Commit 5223070c authored by Wenchao Xia's avatar Wenchao Xia Committed by Luiz Capitulino
Browse files

qapi script: do not allow string discriminator



Since enum based discriminators provide better type-safety and
ensure that future qapi additions do not forget to adjust dependent
unions, forbid using string as discriminator from now on.

Signed-off-by: default avatarWenchao Xia <wenchaoqemu@gmail.com>
Reviewed-by: default avatarEric Blake <eblake@redhat.com>
Reviewed-by: default avatarMarkus Armbruster <armbru@redhat.com>
Signed-off-by: default avatarLuiz Capitulino <lcapitulino@redhat.com>
parent 59ca664e
Loading
Loading
Loading
Loading
+1 −4
Original line number Diff line number Diff line
@@ -123,10 +123,7 @@ And it looks like this on the wire:

Flat union types avoid the nesting on the wire. They are used whenever a
specific field of the base type is declared as the discriminator ('type' is
then no longer generated). The discriminator can be a string field or a
predefined enum field. If it is a string field, a hidden enum type will be
generated as "[UNION_NAME]Kind". If it is an enum field, a compile time check
will be done to verify the correctness. It is recommended to use an enum field.
then no longer generated). The discriminator must be of enumeration type.
The above example can then be modified as follows:

 { 'enum': 'BlockdevDriver', 'data': [ 'raw', 'qcow2' ] }
+5 −0
Original line number Diff line number Diff line
@@ -239,6 +239,11 @@ def check_union(expr, expr_info):
                                "type '%s'"
                                % (discriminator, base))
        enum_define = find_enum(discriminator_type)
        # Do not allow string discriminator
        if not enum_define:
            raise QAPIExprError(expr_info,
                                "Discriminator '%s' must be of enumeration "
                                "type" % discriminator)

    # Check every branch
    for (key, value) in members.items():
+2 −1
Original line number Diff line number Diff line
@@ -145,7 +145,8 @@ check-qapi-schema-y := $(addprefix tests/qapi-schema/, \
        unclosed-list.json unclosed-object.json unclosed-string.json \
        duplicate-key.json union-invalid-base.json flat-union-no-base.json \
        flat-union-invalid-discriminator.json \
        flat-union-invalid-branch-key.json flat-union-reverse-define.json)
        flat-union-invalid-branch-key.json flat-union-reverse-define.json \
        flat-union-string-discriminator.json)

GENERATED_HEADERS += tests/test-qapi-types.h tests/test-qapi-visit.h tests/test-qmp-commands.h

+1 −0
Original line number Diff line number Diff line
<stdin>:13: Discriminator 'kind' must be of enumeration type
+1 −0
Original line number Diff line number Diff line
1
Loading