def wsdl_type_to_schema(inherited_types, wsdl_type): if wsdl_type.root.name == 'simpleType': return get_json_schema(wsdl_type) elements = get_complex_type_elements(inherited_types, wsdl_type) properties = {} for element in elements: if element.root.name == 'enumeration': properties[element.name] = get_json_schema(element) elif element.type is None and element.ref: properties[element.name] = element.ref[ 0] ## set to service type name for now elif element.type[ 1] != 'http://www.w3.org/2001/XMLSchema': ## not a built-in XML type _type = element.type[0] if 'ArrayOf' in _type: properties[element.name] = get_array_type(_type) else: properties[ element.name] = _type ## set to service type name for now else: properties[element.name] = get_json_schema(element) return singer.Schema(type=['null', 'object'], additionalProperties=False, properties=properties)
def get_array_type(array_type): xml_type = re.match(ARRAY_TYPE_REGEX, array_type).groups()[0] json_type = xml_to_json_type(xml_type) if json_type == 'string' and xml_type != 'string': # complex type items = xml_type # will be filled in fill_in_nested_types else: items = json_type items = singer.Schema(type=items) array_obj = singer.Schema(type=['null', 'object'], properties={}) array_obj.properties[xml_type] = singer.Schema(type=['null', 'array'], items=items) return array_obj
def get_json_schema(element): types = [] _format = None if element.nillable: types.append('null') if element.root.name == 'simpleType': types.append('null') types.append('string') else: xml_type = element.type[0] _type = xml_to_json_type(xml_type) types.append(_type) if xml_type in ['dateTime', 'date']: _format = 'date-time' schema = singer.Schema(type=types) if _format: schema.format = _format return schema
def fill_in_nested_types(type_map, schema): if hasattr(schema, 'properties') and schema.properties: for prop, descriptor in schema.properties.items(): schema.properties[prop] = fill_in_nested_types( type_map, descriptor) elif hasattr(schema, 'items') and schema.items: items = fill_in_nested_types(type_map, schema.items) schema = singer.Schema(type=schema.type, items=items) else: if isinstance(schema, str) and schema in type_map: return type_map[schema] return schema
def normalize_abstract_types(inherited_types, type_map): for base_type, types in inherited_types.items(): if base_type in type_map: schemas = [] for inherited_type in types: if inherited_type in type_map: schemas.append(type_map[inherited_type]) schemas.append(type_map[base_type]) if base_type in TOP_LEVEL_CORE_OBJECTS: type_map[base_type] = combine_object_schemas(schemas) else: type_map[base_type] = singer.Schema(anyOf=schemas)
def combine_object_schemas(schemas): properties = {} for schema in schemas: for prop, prop_schema in schema.properties.items(): properties[prop] = prop_schema return singer.Schema(type=['object'], properties=properties)