def test_field_simple_cleanup(self): my_field = { 'field_details': { 'name': "my_field\t", 'type': 'keyword', 'level': 'core', 'short': " a really short description\n\n", 'description': "\ta long\n\nmultiline description ", 'allowed_values': [{ 'name': "authentication\t", 'description': "when can auth be used?\n\n", }] } } cleaner.field_cleanup(my_field) self.assertEqual(my_field['field_details']['name'], 'my_field') self.assertEqual(my_field['field_details']['short'], 'a really short description') self.assertEqual(my_field['field_details']['description'], "a long\n\nmultiline description") self.assertEqual( my_field['field_details']['allowed_values'][0]['name'], 'authentication') self.assertEqual( my_field['field_details']['allowed_values'][0]['description'], 'when can auth be used?')
def extract_matching_fields( fields: Dict[str, FieldEntry], subset_definitions: Dict[str, Any] ) -> Dict[str, FieldEntry]: """Removes fields that are not in the subset definition. Returns a copy without modifying the input fields dict.""" retained_fields: Dict[str, FieldEntry] = {x: fields[x].copy() for x in subset_definitions} for key, val in subset_definitions.items(): retained_fields[key]['field_details'] = fields[key]['field_details'].copy() for option in val: if option != 'fields': if 'intermediate' in retained_fields[key]['field_details']: retained_fields[key]['field_details']['intermediate'] = False retained_fields[key]['field_details'].setdefault( 'description', 'Intermediate field included by adding option with subset') retained_fields[key]['field_details']['level'] = 'custom' cleaner.field_cleanup(retained_fields[key]) retained_fields[key]['field_details'][option] = val[option] # If the field in the schema has a 'fields' key, we expect a 'fields' key in the subset if 'fields' in fields[key]: if 'fields' not in val: raise ValueError("'fields' key expected, not found in subset for {}".format(key)) elif isinstance(val['fields'], dict): retained_fields[key]['fields'] = extract_matching_fields(fields[key]['fields'], val['fields']) elif val['fields'] != "*": raise ValueError("Unexpected value '{}' found in 'fields' key".format(val['fields'])) # If the field in the schema does not have a 'fields' key, there should not be a 'fields' key in the subset elif 'fields' in val: raise ValueError("'fields' key not expected, found in subset for {}".format(key)) return retained_fields