qapi/introspect.py: add _gen_features helper

_make_tree might receive a dict (a SchemaInfo object) or some other type
(usually, a string) for its obj parameter. Adding features information
should arguably be performed by the caller at such a time when we know
the type of the object and don't have to re-interrogate it.

Signed-off-by: John Snow <jsnow@redhat.com>
Message-Id: <20210216021809.134886-5-jsnow@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
This commit is contained in:
John Snow 2021-02-15 21:17:54 -05:00 committed by Markus Armbruster
parent d70f5130f6
commit 84bece7dd4

View File

@ -24,15 +24,11 @@ from .schema import (
)
def _make_tree(obj, ifcond, features, extra=None):
def _make_tree(obj, ifcond, extra=None):
if extra is None:
extra = {}
if ifcond:
extra['if'] = ifcond
if features:
obj['features'] = [
_make_tree(f.name, f.ifcond, None) for f in features
]
if extra:
return (obj, extra)
return obj
@ -169,6 +165,10 @@ const QLitObject %(c_name)s = %(c_string)s;
return '[' + self._use_type(typ.element_type) + ']'
return self._name(typ.name)
@staticmethod
def _gen_features(features):
return [_make_tree(f.name, f.ifcond) for f in features]
def _gen_tree(self, name, mtype, obj, ifcond, features):
extra = None
if mtype not in ('command', 'event', 'builtin', 'array'):
@ -179,13 +179,17 @@ const QLitObject %(c_name)s = %(c_string)s;
name = self._name(name)
obj['name'] = name
obj['meta-type'] = mtype
self._trees.append(_make_tree(obj, ifcond, features, extra))
if features:
obj['features'] = self._gen_features(features)
self._trees.append(_make_tree(obj, ifcond, extra))
def _gen_member(self, member):
obj = {'name': member.name, 'type': self._use_type(member.type)}
if member.optional:
obj['default'] = None
return _make_tree(obj, member.ifcond, member.features)
if member.features:
obj['features'] = self._gen_features(member.features)
return _make_tree(obj, member.ifcond)
def _gen_variants(self, tag_name, variants):
return {'tag': tag_name,
@ -193,7 +197,7 @@ const QLitObject %(c_name)s = %(c_string)s;
def _gen_variant(self, variant):
obj = {'case': variant.name, 'type': self._use_type(variant.type)}
return _make_tree(obj, variant.ifcond, None)
return _make_tree(obj, variant.ifcond)
def visit_builtin_type(self, name, info, json_type):
self._gen_tree(name, 'builtin', {'json-type': json_type}, [], None)