Skip to content

Commit

Permalink
parser: support empty list as DTB qeury
Browse files Browse the repository at this point in the history
This is for convenience reasons, as it allows using `vm0.dtb = dtb([])`
instead of `dtb([{}]);` with a dummy element.

Signed-off-by: Axel Heider <[email protected]>
  • Loading branch information
Axel Heider committed Jan 29, 2024
1 parent 7936ec8 commit e16f01a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
25 changes: 13 additions & 12 deletions camkes/parser/fdtQueryEngine.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,20 +596,21 @@ def resolve_fdt_node(cls, node):
resolved['this-node-path'] = node_path
return resolved

# An empty query is supported for convenience reasons, it produces an empty
# result. Any real query must find a match, otherwise an error is raised.
def resolve(self, args):
result = self.engine.query(args)
if not len(result):
raise ParseError(f'DTB query failed: {args}')

query_results = []
for entry in result:
if not len(entry):
query_results.append({})
else:
node = entry[0]
node_resolved = self.resolve_fdt_node(node)
query_results.append(node_resolved)

if (len(args) > 0):
result = self.engine.query(args)
if not len(result):
raise ParseError(f'DTB query failed: {args}')
for entry in result:
if not len(entry):
query_results.append({})
else:
node = entry[0]
node_resolved = self.resolve_fdt_node(node)
query_results.append(node_resolved)
return {
'query': query_results,
'dtb-size': [self.dtb_file_size]
Expand Down
8 changes: 7 additions & 1 deletion camkes/parser/tests/testdtbmatchquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,13 @@ def test_multiple_queries(self):
self.assertEquals(node['dtb-size'], [self.dtbSize])

def test_blank(self):
self.assertRaises(ParseError, self.dtbQuery.resolve, [])
# For convenience reasons an empty query list is allowed and does not
# raise a ParseError. It returns an empty result list instead.
node = self.dtbQuery.resolve([])
self.assertIn('query', node)
self.assertEquals(len(node['query']), 0)
self.assertIn('dtb-size', node)
self.assertEquals(node['dtb-size'], [self.dtbSize])

def test_blank_query(self):
node = self.dtbQuery.resolve([{}])
Expand Down

0 comments on commit e16f01a

Please sign in to comment.