qapi: add 'not' condition operation

For the sake of completeness, introduce the 'not' condition.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <20210804083105.97531-10-marcandre.lureau@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Long line broken in tests/qapi-schema/qapi-schema-test.json]
Signed-off-by: Markus Armbruster <armbru@redhat.com>
This commit is contained in:
Marc-André Lureau 2021-08-04 12:31:04 +04:00 committed by Markus Armbruster
parent 8a156d89d1
commit 2b7d214536
10 changed files with 25 additions and 9 deletions

View File

@ -207,6 +207,8 @@ def cgen_ifcond(ifcond: Union[str, Dict[str, Any]]) -> str:
return ifcond return ifcond
oper, operands = next(iter(ifcond.items())) oper, operands = next(iter(ifcond.items()))
if oper == 'not':
return '!' + cgen_ifcond(operands)
oper = {'all': '&&', 'any': '||'}[oper] oper = {'all': '&&', 'any': '||'}[oper]
operands = [cgen_ifcond(o) for o in operands] operands = [cgen_ifcond(o) for o in operands]
return '(' + (') ' + oper + ' (').join(operands) + ')' return '(' + (') ' + oper + ' (').join(operands) + ')'
@ -220,6 +222,8 @@ def docgen_ifcond(ifcond: Union[str, Dict[str, Any]]) -> str:
return ifcond return ifcond
oper, operands = next(iter(ifcond.items())) oper, operands = next(iter(ifcond.items()))
if oper == 'not':
return '!' + docgen_ifcond(operands)
oper = {'all': ' and ', 'any': ' or '}[oper] oper = {'all': ' and ', 'any': ' or '}[oper]
operands = [docgen_ifcond(o) for o in operands] operands = [docgen_ifcond(o) for o in operands]
return '(' + oper.join(operands) + ')' return '(' + oper.join(operands) + ')'

View File

@ -290,15 +290,18 @@ def check_if(expr: _JSONObject, info: QAPISourceInfo, source: str) -> None:
raise QAPISemError( raise QAPISemError(
info, info,
"'if' condition dict of %s must have one key: " "'if' condition dict of %s must have one key: "
"'all' or 'any'" % source) "'all', 'any' or 'not'" % source)
check_keys(cond, info, "'if' condition", [], check_keys(cond, info, "'if' condition", [],
["all", "any"]) ["all", "any", "not"])
oper, operands = next(iter(cond.items())) oper, operands = next(iter(cond.items()))
if not operands: if not operands:
raise QAPISemError( raise QAPISemError(
info, "'if' condition [] of %s is useless" % source) info, "'if' condition [] of %s is useless" % source)
if oper == "not":
_check_if(operands)
return
if oper in ("all", "any") and not isinstance(operands, list): if oper in ("all", "any") and not isinstance(operands, list):
raise QAPISemError( raise QAPISemError(
info, "'%s' condition of %s must be an array" % (oper, source)) info, "'%s' condition of %s must be an array" % (oper, source))

View File

@ -1,3 +1,3 @@
bad-if-key.json: In struct 'TestIfStruct': bad-if-key.json: In struct 'TestIfStruct':
bad-if-key.json:2: 'if' condition has unknown key 'value' bad-if-key.json:2: 'if' condition has unknown key 'value'
Valid keys are 'all', 'any'. Valid keys are 'all', 'any', 'not'.

View File

@ -1,2 +1,2 @@
bad-if-keys.json: In struct 'TestIfStruct': bad-if-keys.json: In struct 'TestIfStruct':
bad-if-keys.json:2: 'if' condition dict of struct must have one key: 'all' or 'any' bad-if-keys.json:2: 'if' condition dict of struct must have one key: 'all', 'any' or 'not'

View File

@ -126,7 +126,8 @@
## ##
{ 'alternate': 'Alternate', { 'alternate': 'Alternate',
'features': [ 'alt-feat' ], 'features': [ 'alt-feat' ],
'data': { 'i': 'int', 'b': 'bool' } } 'data': { 'i': 'int', 'b': 'bool' },
'if': { 'not': 'IFNOT' } }
## ##
# == Another subsection # == Another subsection

View File

@ -51,6 +51,7 @@ alternate Alternate
tag type tag type
case i: int case i: int
case b: bool case b: bool
if OrderedDict([('not', 'IFNOT')])
feature alt-feat feature alt-feat
object q_obj_cmd-arg object q_obj_cmd-arg
member arg1: int optional=False member arg1: int optional=False

View File

@ -171,6 +171,12 @@ Features
a feature a feature
If
~~
"!IFNOT"
Another subsection Another subsection
================== ==================

View File

@ -1,3 +1,3 @@
enum-if-invalid.json: In enum 'TestIfEnum': enum-if-invalid.json: In enum 'TestIfEnum':
enum-if-invalid.json:2: 'if' condition has unknown key 'val' enum-if-invalid.json:2: 'if' condition has unknown key 'val'
Valid keys are 'all', 'any'. Valid keys are 'all', 'any', 'not'.

View File

@ -245,7 +245,8 @@
{ 'command': 'test-if-alternate-cmd', { 'command': 'test-if-alternate-cmd',
'data': { 'alt-cmd-arg': 'TestIfAlternate' }, 'data': { 'alt-cmd-arg': 'TestIfAlternate' },
'if': { 'all': ['defined(TEST_IF_ALT)'] } } 'if': { 'all': ['defined(TEST_IF_ALT)',
{'not': 'defined(TEST_IF_NOT_ALT)'}] } }
{ 'command': 'test-if-cmd', { 'command': 'test-if-cmd',
'data': { 'data': {

View File

@ -333,10 +333,10 @@ alternate TestIfAlternate
if OrderedDict([('all', ['defined(TEST_IF_ALT)', 'defined(TEST_IF_STRUCT)'])]) if OrderedDict([('all', ['defined(TEST_IF_ALT)', 'defined(TEST_IF_STRUCT)'])])
object q_obj_test-if-alternate-cmd-arg object q_obj_test-if-alternate-cmd-arg
member alt-cmd-arg: TestIfAlternate optional=False member alt-cmd-arg: TestIfAlternate optional=False
if OrderedDict([('all', ['defined(TEST_IF_ALT)'])]) if OrderedDict([('all', ['defined(TEST_IF_ALT)', OrderedDict([('not', 'defined(TEST_IF_NOT_ALT)')])])])
command test-if-alternate-cmd q_obj_test-if-alternate-cmd-arg -> None command test-if-alternate-cmd q_obj_test-if-alternate-cmd-arg -> None
gen=True success_response=True boxed=False oob=False preconfig=False gen=True success_response=True boxed=False oob=False preconfig=False
if OrderedDict([('all', ['defined(TEST_IF_ALT)'])]) if OrderedDict([('all', ['defined(TEST_IF_ALT)', OrderedDict([('not', 'defined(TEST_IF_NOT_ALT)')])])])
object q_obj_test-if-cmd-arg object q_obj_test-if-cmd-arg
member foo: TestIfStruct optional=False member foo: TestIfStruct optional=False
member bar: TestIfEnum optional=False member bar: TestIfEnum optional=False