| 
									
										
										
										
											2022-11-11 16:35:09 +05:30
										 |  |  | #  Copyright 2021 Collate | 
					
						
							|  |  |  | #  Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  | #  you may not use this file except in compliance with the License. | 
					
						
							|  |  |  | #  You may obtain a copy of the License at | 
					
						
							|  |  |  | #  http://www.apache.org/licenses/LICENSE-2.0 | 
					
						
							|  |  |  | #  Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  | #  distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  | #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  | #  See the License for the specific language governing permissions and | 
					
						
							|  |  |  | #  limitations under the License. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | Utils module to parse the jsonschema | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import json | 
					
						
							|  |  |  | import traceback | 
					
						
							| 
									
										
										
										
											2022-12-15 16:54:55 +05:30
										 |  |  | from enum import Enum | 
					
						
							|  |  |  | from typing import List, Optional | 
					
						
							| 
									
										
										
										
											2022-11-11 16:35:09 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-26 10:03:21 +05:30
										 |  |  | from pydantic.main import ModelMetaclass | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-15 16:54:55 +05:30
										 |  |  | from metadata.generated.schema.type.schema import FieldModel | 
					
						
							| 
									
										
										
										
											2022-11-11 16:35:09 +05:30
										 |  |  | from metadata.utils.logger import ingestion_logger | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | logger = ingestion_logger() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-15 16:54:55 +05:30
										 |  |  | class JsonSchemaDataTypes(Enum): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Enum for Json Schema Datatypes | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     STRING = "string" | 
					
						
							|  |  |  |     FLOAT = "number" | 
					
						
							|  |  |  |     INT = "integer" | 
					
						
							|  |  |  |     BOOLEAN = "boolean" | 
					
						
							|  |  |  |     NULL = "null" | 
					
						
							|  |  |  |     RECORD = "object" | 
					
						
							|  |  |  |     ARRAY = "array" | 
					
						
							| 
									
										
										
										
											2024-03-26 10:03:21 +05:30
										 |  |  |     UNKNOWN = "unknown" | 
					
						
							| 
									
										
										
										
											2022-12-15 16:54:55 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-26 10:03:21 +05:30
										 |  |  | def parse_json_schema( | 
					
						
							|  |  |  |     schema_text: str, cls: ModelMetaclass = FieldModel | 
					
						
							|  |  |  | ) -> Optional[List[FieldModel]]: | 
					
						
							| 
									
										
										
										
											2022-11-11 16:35:09 +05:30
										 |  |  |     """
 | 
					
						
							|  |  |  |     Method to parse the jsonschema | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         json_schema_data = json.loads(schema_text) | 
					
						
							| 
									
										
										
										
											2023-03-07 06:40:04 -08:00
										 |  |  |         field_models = [ | 
					
						
							| 
									
										
										
										
											2024-03-26 10:03:21 +05:30
										 |  |  |             cls( | 
					
						
							| 
									
										
										
										
											2023-03-07 06:40:04 -08:00
										 |  |  |                 name=json_schema_data.get("title", "default"), | 
					
						
							|  |  |  |                 dataType=JsonSchemaDataTypes(json_schema_data.get("type")).name, | 
					
						
							|  |  |  |                 description=json_schema_data.get("description"), | 
					
						
							| 
									
										
										
										
											2024-03-26 10:03:21 +05:30
										 |  |  |                 children=get_json_schema_fields( | 
					
						
							|  |  |  |                     json_schema_data.get("properties", {}), cls=cls | 
					
						
							|  |  |  |                 ), | 
					
						
							| 
									
										
										
										
											2023-03-07 06:40:04 -08:00
										 |  |  |             ) | 
					
						
							|  |  |  |         ] | 
					
						
							| 
									
										
										
										
											2022-12-15 16:54:55 +05:30
										 |  |  |         return field_models | 
					
						
							| 
									
										
										
										
											2022-11-11 16:35:09 +05:30
										 |  |  |     except Exception as exc:  # pylint: disable=broad-except | 
					
						
							|  |  |  |         logger.debug(traceback.format_exc()) | 
					
						
							|  |  |  |         logger.warning(f"Unable to parse the jsonschema: {exc}") | 
					
						
							|  |  |  |     return None | 
					
						
							| 
									
										
										
										
											2022-12-15 16:54:55 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-26 10:03:21 +05:30
										 |  |  | def get_json_schema_fields( | 
					
						
							|  |  |  |     properties, cls: ModelMetaclass = FieldModel | 
					
						
							|  |  |  | ) -> Optional[List[FieldModel]]: | 
					
						
							| 
									
										
										
										
											2022-12-15 16:54:55 +05:30
										 |  |  |     """
 | 
					
						
							|  |  |  |     Recursively convert the parsed schema into required models | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     field_models = [] | 
					
						
							|  |  |  |     for key, value in properties.items(): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             field_models.append( | 
					
						
							| 
									
										
										
										
											2024-03-26 10:03:21 +05:30
										 |  |  |                 cls( | 
					
						
							| 
									
										
										
										
											2024-03-12 23:02:41 +05:30
										 |  |  |                     name=key, | 
					
						
							|  |  |  |                     displayName=value.get("title"), | 
					
						
							| 
									
										
										
										
											2024-03-26 10:03:21 +05:30
										 |  |  |                     dataType=JsonSchemaDataTypes(value.get("type", "unknown")).name, | 
					
						
							| 
									
										
										
										
											2022-12-15 16:54:55 +05:30
										 |  |  |                     description=value.get("description"), | 
					
						
							|  |  |  |                     children=get_json_schema_fields(value.get("properties")) | 
					
						
							|  |  |  |                     if value.get("type") == "object" | 
					
						
							|  |  |  |                     else None, | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         except Exception as exc:  # pylint: disable=broad-except | 
					
						
							|  |  |  |             logger.debug(traceback.format_exc()) | 
					
						
							|  |  |  |             logger.warning(f"Unable to parse the json schema into models: {exc}") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return field_models |