Skip to content

Commit

Permalink
dtschema: Rework int array to matrix fixups
Browse files Browse the repository at this point in the history
Using _is_int_schema to match int arrays doesn't work if we have a list
of 'items' with just descriptions as that looks the same as a string or
phandle list. To fix this, let's also look for a $ref to a 'uint*-array'
type in addition to integer keywords. As part of this, _fixup_int_array
becomes redundant and can be removed.

The items size fixup needs to be done after all the other fixups and after
all the recursion. Otherwise, we miss some instances.

Signed-off-by: Rob Herring <[email protected]>
  • Loading branch information
robherring committed Dec 12, 2019
1 parent 6463dcb commit 9643764
Showing 1 changed file with 28 additions and 32 deletions.
60 changes: 28 additions & 32 deletions dtschema/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,48 +146,45 @@ def _fixup_string_to_array(subschema):
subschema['items'] = [ _extract_single_schemas(subschema) ]

def _fixup_int_array_to_matrix(subschema):
is_int = False
if 'allOf' in subschema.keys() and '$ref' in subschema['allOf'][0].keys():
if not re.match('.*uint(8|16|32)-array', subschema['allOf'][0]['$ref']):
return

if not 'items' in subschema.keys():
return
# Find 'items'. It may be under the 'allOf' or at the same level
for item in subschema['allOf']:
if isinstance(item, dict) and 'items' in item.keys():
subschema = item
break

if (not isinstance(subschema['items'],dict)) or 'items' in subschema['items'].keys():
return
if not 'items' in subschema.keys():
return

if not _is_int_schema(subschema['items']):
elif not 'items' in subschema.keys() or \
(isinstance(subschema['items'],list) and not _is_int_schema(subschema['items'][0])) or \
(isinstance(subschema['items'],dict) and not _is_int_schema(subschema['items'])):
return

subschema['items'] = copy.deepcopy(subschema)
# Don't copy 'allOf'
subschema['items'].pop('allOf', None)
for k in list(subschema.keys()):
if k == 'items' or k == 'allOf':
continue
subschema.pop(k)

if isinstance(subschema['items'],dict) and not 'items' in subschema['items'].keys():
subschema['items'] = copy.deepcopy(subschema)
# Don't copy 'allOf'
subschema['items'].pop('allOf', None)
for k in list(subschema.keys()):
if k == 'items' or k == 'allOf':
continue
subschema.pop(k)

if isinstance(subschema['items'],list) and not \
('items' in subschema['items'][0].keys() or \
'minItems' in subschema['items'][0].keys() or \
'maxitems' in subschema['items'][0].keys()):
subschema['items'] = [ {'items': subschema['items']} ]

def _fixup_scalar_to_array(subschema):
if not _is_int_schema(subschema):
return

subschema['items'] = { 'items': _extract_single_schemas(subschema) }

def _fixup_int_array(subschema):

if not 'items' in subschema.keys():
return

# A string list or already a matrix?
for l in subschema['items']:
if isinstance(l, dict) and 'items' in l.keys():
return
if _is_int_schema(l):
break
else:
return

subschema['items'] = [ {'items': subschema['items']} ]

def _fixup_items_size(schema):
# Make items list fixed size-spec
if isinstance(schema, list):
Expand Down Expand Up @@ -218,10 +215,8 @@ def fixup_vals(schema):
# Now we should be a the schema level to do actual fixups
# print(schema)
_fixup_int_array_to_matrix(schema)
_fixup_int_array(schema)
_fixup_string_to_array(schema)
_fixup_scalar_to_array(schema)
_fixup_items_size(schema)
# print(schema)

def walk_conditionals(schema):
Expand Down Expand Up @@ -456,6 +451,7 @@ def process_schema(filename):

# Add any implicit properties
fixup_node_props(schema)
_fixup_items_size(schema)

add_select_schema(schema)
if not 'select' in schema.keys():
Expand Down

0 comments on commit 9643764

Please sign in to comment.