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

[Xen-devel] [PATCH v6 11/18] libxl/gentypes.py: don't generate default values



If a type has init_val defined and a field of the type has the value of
init_val, there's no need to generate output for that field in JSON
output. When the parser consumes that generated JSON object, all default
values should be automatically filled in.

Also define init_val for enumeration type, string type and libxl_defbool
type.

Signed-off-by: Wei Liu <wei.liu2@xxxxxxxxxx>
---
 tools/libxl/gentypes.py     |   27 ++++++++++++++++++++++++---
 tools/libxl/idl.py          |    3 ++-
 tools/libxl/libxl_types.idl |    3 ++-
 3 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/tools/libxl/gentypes.py b/tools/libxl/gentypes.py
index 01416e7..611c8af 100644
--- a/tools/libxl/gentypes.py
+++ b/tools/libxl/gentypes.py
@@ -135,7 +135,7 @@ def _libxl_C_type_init(ty, v, indent = "    ", parent = 
None, subinit=False):
             else:
                 s += _libxl_C_type_init(f.type, fexpr, "", nparent)
     else:
-        if ty.init_val is not None:
+        if ty.init_val is not None and ty.typename != "libxl_defbool":
             s += "%s = %s;\n" % (ty.pass_arg(v, parent is None), ty.init_val)
         elif ty.init_fn is not None:
             s += "%s(%s);\n" % (ty.init_fn, ty.pass_arg(v, parent is None))
@@ -206,6 +206,13 @@ def libxl_C_type_gen_map_key(f, parent, indent = ""):
         s = indent + s
     return s.replace("\n", "\n%s" % indent).rstrip(indent)
 
+def get_init_val(f):
+    if f.init_val is not None:
+        return f.init_val
+    elif f.type.init_val is not None:
+        return f.type.init_val
+    return None
+
 def libxl_C_type_gen_json(ty, v, indent = "    ", parent = None):
     s = ""
     if parent is None:
@@ -255,8 +262,22 @@ def libxl_C_type_gen_json(ty, v, indent = "    ", parent = 
None):
         s += "    goto out;\n"
         for f in [f for f in ty.fields if not f.const and not f.type.private]:
             (nparent,fexpr) = ty.member(v, f, parent is None)
-            s += libxl_C_type_gen_map_key(f, nparent)
-            s += libxl_C_type_gen_json(f.type, fexpr, "", nparent)
+            init_val = get_init_val(f)
+            if init_val:
+                if f.type.typename != "libxl_defbool":
+                    s += "if (%s != %s) {\n" % (fexpr, init_val)
+                else:
+                    s += "if (%s.val != %s) {\n" % (fexpr, init_val)
+                indent1 = "    "
+            else:
+                indent1 = ""
+
+            s += libxl_C_type_gen_map_key(f, nparent, indent1)
+            s += libxl_C_type_gen_json(f.type, fexpr, indent1, nparent)
+
+            if init_val:
+                s += "}\n"
+
         s += "s = yajl_gen_map_close(hand);\n"
         s += "if (s != yajl_gen_status_ok)\n"
         s += "    goto out;\n"
diff --git a/tools/libxl/idl.py b/tools/libxl/idl.py
index 8b118dd..14ca165 100644
--- a/tools/libxl/idl.py
+++ b/tools/libxl/idl.py
@@ -142,6 +142,7 @@ class EnumerationValue(object):
 class Enumeration(Type):
     def __init__(self, typename, values, **kwargs):
         kwargs.setdefault('dispose_fn', None)
+        kwargs.setdefault('init_val', '0')
         Type.__init__(self, typename, **kwargs)
 
         self.value_namespace = kwargs.setdefault('value_namespace',
@@ -270,7 +271,7 @@ uint64 = UInt(64, json_gen_fn = "libxl__uint64_gen_json")
 
 string = Builtin("char *", namespace = None, dispose_fn = "free",
                  json_gen_fn = "libxl__string_gen_json",
-                 autogenerate_json = False)
+                 autogenerate_json = False, init_val = "NULL")
 
 class Array(Type):
     """An array of the same type"""
diff --git a/tools/libxl/libxl_types.idl b/tools/libxl/libxl_types.idl
index 95d450f..d510e2d 100644
--- a/tools/libxl/libxl_types.idl
+++ b/tools/libxl/libxl_types.idl
@@ -5,7 +5,8 @@
 
 namespace("libxl_")
 
-libxl_defbool = Builtin("defbool", passby=PASS_BY_REFERENCE)
+libxl_defbool = Builtin("defbool", passby=PASS_BY_REFERENCE,
+                        init_val="LIBXL__DEFBOOL_DEFAULT")
 
 libxl_domid = Builtin("domid", json_gen_fn = "yajl_gen_integer", 
autogenerate_json = False)
 libxl_devid = Builtin("devid", json_gen_fn = "yajl_gen_integer", 
autogenerate_json = False, signed = True, init_val="-1")
-- 
1.7.10.4


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.