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