diff --git a/pysmartdatamodels/README.md b/pysmartdatamodels/README.md index 487573e204..17e0b35a7a 100644 --- a/pysmartdatamodels/README.md +++ b/pysmartdatamodels/README.md @@ -356,6 +356,19 @@ print(sdm.update_broker(dataModel, subject, attribute, value, serverUrl=serverUr if there's any problem related to input parameter and json schema: False +Function ngsi_ld_example_generator_str(schema: str, dataModel: str, subject: str) + + It returns a fake normalized ngsi-ld format example based on the given json schema + Parameters: + schema: schema.json contents + dataModel: repo name + subject: model name + + Returns: + if the input parameter exists and the json schema is a valide json: + a fake normalized ngsi-ld format example stored in dictionary format + if there's any problem related to input parameter and json schema: + False 18- Return a fake key value ngsi-ld format example. Function ngsi_ld_keyvalue_example_generator(schemaUrl) diff --git a/pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py b/pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py index 387c03c3a6..ff86eead56 100644 --- a/pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py +++ b/pysmartdatamodels/pysmartdatamodels/pysmartdatamodels.py @@ -1120,10 +1120,45 @@ def ngsi_ld_example_generator(schema_url: str): payload = open_jsonref(schema_url) if payload == "": return False - # print(payload["allOf"]) + return ngsi_ld_example_generator_str(payload, dataModel, subject) + +def ngsi_ld_example_generator_str(schema: str, dataModel: str, subject: str): + """It returns a fake normalized ngsi-ld format example based on the given json schema + Parameters: + schema: schema.json contents + dataModel: repo name + subject: model name + + Returns: + if the input parameter exists and the json schema is a valide json: + a fake normalized ngsi-ld format example stored in dictionary format + if there's any problem related to input parameter and json schema: + False + """ + + if dataModel == "" or subject == "": + return False + output = {} + tz = pytz.timezone("Europe/Madrid") + + try: + payload = json.loads(schema) + except ValueError: + output["result"] = False + output["cause"] = "Schema parameter value is not a valid json" + output["time"] = str(datetime.datetime.now(tz=tz)) + # output["parameters"] = {"schema_url: ": schema_url} + print(json.dumps(output)) + sys.exit() + + if payload == "": + return False + + # print(payload["allOf"]) fullDict = {} # echo("payload", payload) + fullDict['id'] = {} if "allOf" in payload: for index in range(len(payload["allOf"])): if "properties" in payload["allOf"][index]: @@ -1159,7 +1194,7 @@ def ngsi_ld_example_generator(schema_url: str): else: output = {**output, **{prop: parsedProperty}} # echo("output", output) - output["@context"] = [create_context(subject)] + output["@context"] = [create_context('dataModel.' + dataModel)] # print("======================") # print(json.dumps(output)) @@ -1504,4 +1539,4 @@ def generate_sql_schema(model_yaml: str) -> str: result = sql_data_types + "\n" + table_create_statement print(result) - return result \ No newline at end of file + return result