def generate_iri_mappings( path: str, template: str, skip_nested: bool = False, template_mapping: List[IriTemplateMapping] = [], parent_prop_name: str = None) -> Tuple[str, List[IriTemplateMapping]]: """ Generate iri mappings to add to IriTemplate :param path: Path of the collection or non-collection class. :param template: IriTemplate string. :param skip_nested: To only add properties of the class_type class or its immediate children. :param template_mapping: List of template mappings. :param parent_prop_name: Property name according to parent object (only applies for nested properties) :return: Template string, list of template mappings and boolean showing whether to keep adding delimiter or not. """ expanded_base_url = DocUrl.doc_url for supportedProp in get_doc( ).parsed_classes[path]["class"].supportedProperty: prop_class = supportedProp.prop nested_class_prop = False if isinstance(supportedProp.prop, HydraLink): hydra_link = supportedProp.prop prop_class = hydra_link.range.split(expanded_base_url)[1] nested_class_prop = True elif expanded_base_url in supportedProp.prop: prop_class = supportedProp.prop.split(expanded_base_url)[1] nested_class_prop = True if nested_class_prop and skip_nested is False: template, template_mapping = generate_iri_mappings( prop_class, template, skip_nested=True, parent_prop_name=supportedProp.title, template_mapping=template_mapping) continue if skip_nested is True: var = f"{parent_prop_name}[{supportedProp.title}]" mapping = IriTemplateMapping(variable=var, prop=prop_class) else: var = supportedProp.title mapping = IriTemplateMapping(variable=var, prop=prop_class) template_mapping.append(mapping) template = template + f"{var}, " return template, template_mapping
def add_pagination_iri_mappings( template: str, template_mapping: List[IriTemplateMapping] ) -> Tuple[str, List[IriTemplateMapping]]: """Add various pagination related to variable to the IRI template and also adds mappings for them. :param template: IriTemplate string. :param template_mapping: List of template mappings. :return: Final IriTemplate string and related list of mappings. """ paginate_variables = ["pageIndex", "limit", "offset"] for i in range(len(paginate_variables)): # If final variable then do not add space and comma and add the final parentheses if i == len(paginate_variables) - 1: template += "{})".format(paginate_variables[i]) else: template += "{}, ".format(paginate_variables[i]) mapping = IriTemplateMapping(variable=paginate_variables[i], prop=paginate_variables[i]) template_mapping.append(mapping) return template, template_mapping