Esempio n. 1
0
 def get_field(self, field_name: str, schema: dict, **options) -> list:
     """
     Returns a list of a) the passed-in field, and b) inner fields represented by passed-in-fieldname.inner-feldname:
     - reference: the typeName of a data_relation
     - type: the type of the field
     - attr: dictionary of attributes. Nonexisting values are set as None, all keys exist.
     - name: the name of the field
     """
     result_field = {'type': schema['type'], 'name': field_name}
     if self.special_cases(result_field, schema, **options):
         return [result_field] + result_field.pop('_inner_fields', [])
     if 'data_relation' in schema:
         result_field['reference'] = Naming.type(schema['data_relation']['resource'])
     elif schema['type'] == 'list' and 'schema' in schema:
         subschema = schema['schema']
         if 'data_relation' in subschema:
             result_field['reference'] = Naming.type(subschema['data_relation']['resource'])
         elif subschema['type'] == 'dict':
             self.get_dict(result_field, subschema['schema'], **options)
     elif schema['type'] == 'dict' and 'schema' in schema:
         self.get_dict(result_field, schema['schema'], **options)
     result_field['attr'] = {
         'Unique': schema.get('unique'),
         'Default': schema.get('default'),
         'Allowed': schema.get('allowed'),
         'Required': schema.get('required'),
         'Description': schema.get('description'),
         'Write only': schema.get('writeonly'),
         'Read only': schema.get('readonly'),
         'Modifiable': schema.get('modifiable'),
         'Sink': schema.get('sink', 0),
         'Unit Code': schema.get('unitCode'),
         'Doc': schema.get('doc'),
         'Roles with writing permission': schema.get(ALLOWED_WRITE_ROLES),
         'OR': schema.get('or'),
         'Excludes': schema.get('excludes')
     }
     if 'unitCode' in schema:
         result_field['attr']['Unit Code'] = UnitCodes.humanize(schema['unitCode']) + ' ({})'.format(schema['unitCode'])
     return [result_field] + result_field.pop('_inner_fields', [])