From 9643764f33d27b77ced07e7edeb4fa5ad5d6f011 Mon Sep 17 00:00:00 2001 From: Rob Herring Date: Wed, 11 Dec 2019 19:58:10 -0600 Subject: [PATCH] dtschema: Rework int array to matrix fixups 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 --- dtschema/lib.py | 60 +++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/dtschema/lib.py b/dtschema/lib.py index af1f8926..26dd16d5 100644 --- a/dtschema/lib.py +++ b/dtschema/lib.py @@ -146,25 +146,38 @@ 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): @@ -172,22 +185,6 @@ def _fixup_scalar_to_array(subschema): 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): @@ -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): @@ -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():