CamilleTeruel commented on code in PR #4976: URL: https://github.com/apache/incubator-devlake/pull/4976#discussion_r1173670305
########## backend/python/pydevlake/pydevlake/extractor.py: ########## @@ -15,15 +15,45 @@ from typing import Type + +from jsonpointer import resolve_pointer, JsonPointerException + from pydevlake import ToolModel def autoextract(json: dict, model_cls: Type[ToolModel]) -> ToolModel: - annotations = dict(model_cls.__annotations__) - for key, value in json.items(): - if key in annotations: - expected_type = annotations[key] - if isinstance(expected_type, type) and issubclass(expected_type, ToolModel): - # TODO: replace with actual foreign key - json[key] = value["id"] - return model_cls(**json) + """ + Automatically extract a tool model from a json object. + The tool model class can define fields with a source argument to specify the JSON pointer (RFC 6901) to the value. + + Example: + class DummyModel(ToolModel): + name: str + version: str = Field(source='/version/number') Review Comment: A JSON pointer points to a single element. You can point to a specific array element by index (e.g. /a/b/c/3/e). You can't project a nested property to each array member, like `/a/b/c/*/e` with JSON pointer. See RFC6901 for details. Note that a list attribute of a ToolModel would have to be implemented as a Relation to another table, not as simple column Field. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
