218e99
From 9ff6403c20232b826766d34cbb66caea8c650bf1 Mon Sep 17 00:00:00 2001
218e99
From: Kevin Wolf <kwolf@redhat.com>
218e99
Date: Mon, 9 Sep 2013 14:28:10 +0200
218e99
Subject: [PATCH 19/38] docs: Document QAPI union types
218e99
218e99
RH-Author: Kevin Wolf <kwolf@redhat.com>
218e99
Message-id: <1378736903-18489-20-git-send-email-kwolf@redhat.com>
218e99
Patchwork-id: 54206
218e99
O-Subject: [RHEL-7.0 qemu-kvm PATCH 19/32] docs: Document QAPI union types
218e99
Bugzilla: 1005818
218e99
RH-Acked-by: Fam Zheng <famz@redhat.com>
218e99
RH-Acked-by: Max Reitz <mreitz@redhat.com>
218e99
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
218e99
218e99
Bugzilla: 1005818
218e99
218e99
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
218e99
Reviewed-by: Eric Blake <eblake@redhat.com>
218e99
(cherry picked from commit 51631493e4876081ae27078b50bd95bd4418bf37)
218e99
218e99
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
218e99
---
218e99
 docs/qapi-code-gen.txt | 62 ++++++++++++++++++++++++++++++++++++++++++++------
218e99
 1 file changed, 55 insertions(+), 7 deletions(-)
218e99
218e99
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
218e99
---
218e99
 docs/qapi-code-gen.txt |   62 ++++++++++++++++++++++++++++++++++++++++++-----
218e99
 1 files changed, 55 insertions(+), 7 deletions(-)
218e99
218e99
diff --git a/docs/qapi-code-gen.txt b/docs/qapi-code-gen.txt
218e99
index cccb11e..f6f8d33 100644
218e99
--- a/docs/qapi-code-gen.txt
218e99
+++ b/docs/qapi-code-gen.txt
218e99
@@ -34,9 +34,15 @@ OrderedDicts so that ordering is preserved.
218e99
 There are two basic syntaxes used, type definitions and command definitions.
218e99
 
218e99
 The first syntax defines a type and is represented by a dictionary.  There are
218e99
-two kinds of types that are supported: complex user-defined types, and enums.
218e99
+three kinds of user-defined types that are supported: complex types,
218e99
+enumeration types and union types.
218e99
 
218e99
-A complex type is a dictionary containing a single key who's value is a
218e99
+Generally speaking, types definitions should always use CamelCase for the type
218e99
+names. Command names should be all lower case with words separated by a hyphen.
218e99
+
218e99
+=== Complex types ===
218e99
+
218e99
+A complex type is a dictionary containing a single key whose value is a
218e99
 dictionary.  This corresponds to a struct in C or an Object in JSON.  An
218e99
 example of a complex type is:
218e99
 
218e99
@@ -47,13 +53,57 @@ The use of '*' as a prefix to the name means the member is optional.  Optional
218e99
 members should always be added to the end of the dictionary to preserve
218e99
 backwards compatibility.
218e99
 
218e99
-An enumeration type is a dictionary containing a single key who's value is a
218e99
+=== Enumeration types ===
218e99
+
218e99
+An enumeration type is a dictionary containing a single key whose value is a
218e99
 list of strings.  An example enumeration is:
218e99
 
218e99
  { 'enum': 'MyEnum', 'data': [ 'value1', 'value2', 'value3' ] }
218e99
 
218e99
-Generally speaking, complex types and enums should always use CamelCase for
218e99
-the type names.
218e99
+=== Union types ===
218e99
+
218e99
+Union types are used to let the user choose between several different data
218e99
+types.  A union type is defined using a dictionary as explained in the
218e99
+following paragraphs.
218e99
+
218e99
+
218e99
+A simple union type defines a mapping from discriminator values to data types
218e99
+like in this example:
218e99
+
218e99
+ { 'type': 'FileOptions', 'data': { 'filename': 'str' } }
218e99
+ { 'type': 'Qcow2Options',
218e99
+   'data': { 'backing-file': 'str', 'lazy-refcounts': 'bool' } }
218e99
+
218e99
+ { 'union': 'BlockdevOptions',
218e99
+   'data': { 'file': 'FileOptions',
218e99
+             'qcow2': 'Qcow2Options' } }
218e99
+
218e99
+In the QMP wire format, a simple union is represented by a dictionary that
218e99
+contains the 'type' field as a discriminator, and a 'data' field that is of the
218e99
+specified data type corresponding to the discriminator value:
218e99
+
218e99
+ { "type": "qcow2", "data" : { "backing-file": "/some/place/my-image",
218e99
+                               "lazy-refcounts": true } }
218e99
+
218e99
+
218e99
+A union definition can specify a complex type as its base. In this case, the
218e99
+fields of the complex type are included as top-level fields of the union
218e99
+dictionary in the QMP wire format. An example definition is:
218e99
+
218e99
+ { 'type': 'BlockdevCommonOptions', 'data': { 'readonly': 'bool' } }
218e99
+ { 'union': 'BlockdevOptions',
218e99
+   'base': 'BlockdevCommonOptions',
218e99
+   'data': { 'raw': 'RawOptions',
218e99
+             'qcow2': 'Qcow2Options' } }
218e99
+
218e99
+And it looks like this on the wire:
218e99
+
218e99
+ { "type": "qcow2",
218e99
+   "readonly": false,
218e99
+   "data" : { "backing-file": "/some/place/my-image",
218e99
+              "lazy-refcounts": true } }
218e99
+
218e99
+=== Commands ===
218e99
 
218e99
 Commands are defined by using a list containing three members.  The first
218e99
 member is the command name, the second member is a dictionary containing
218e99
@@ -65,8 +115,6 @@ An example command is:
218e99
    'data': { 'arg1': 'str', '*arg2': 'str' },
218e99
    'returns': 'str' }
218e99
 
218e99
-Command names should be all lower case with words separated by a hyphen.
218e99
-
218e99
 
218e99
 == Code generation ==
218e99
 
218e99
-- 
218e99
1.7.1
218e99