From 434f311cd57f04ae0316197e8d58ee549f48e68f Mon Sep 17 00:00:00 2001 From: tjoubert Date: Tue, 20 Sep 2022 16:28:37 +0400 Subject: [PATCH 1/2] Added computedValues property to collection --- arango/database.py | 9 +++++++++ arango/formatter.py | 48 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/arango/database.py b/arango/database.py index 5e9dcfa8..249374c5 100644 --- a/arango/database.py +++ b/arango/database.py @@ -933,6 +933,7 @@ def create_collection( smart_join_attribute: Optional[str] = None, write_concern: Optional[int] = None, schema: Optional[Json] = None, + computedValues: Optional[Jsons] = None, ) -> Result[StandardCollection]: """Create a new collection. @@ -1010,6 +1011,12 @@ def create_collection( for documents. See ArangoDB documentation for more information on document schema validation. :type schema: dict + :param computedValues: Array of computed values for the new collection + enabling default values to new documents or the maintenance of + auxiliary attributes for search queries. Available in ArangoDB + version 3.10 or greater. See ArangoDB documentation for more + information on computed values. + :type computedValues: list :return: Standard collection API wrapper. :rtype: arango.collection.StandardCollection :raise arango.exceptions.CollectionCreateError: If create fails. @@ -1043,6 +1050,8 @@ def create_collection( data["writeConcern"] = write_concern if schema is not None: data["schema"] = schema + if computedValues is not None: + data["computedValues"] = computedValues params: Params = {} if sync_replication is not None: diff --git a/arango/formatter.py b/arango/formatter.py index 38a2baf7..78590bce 100644 --- a/arango/formatter.py +++ b/arango/formatter.py @@ -1,6 +1,6 @@ from typing import Any -from arango.typings import Headers, Json +from arango.typings import Headers, Json, Jsons def verify_format(_: Any, res: Json) -> Json: @@ -208,6 +208,52 @@ def format_collection(body: Json) -> Json: if "schema" in body: result["schema"] = body["schema"] + # New in 3.10 + if "computedValues" in body: + result["computedValues"] = format_collection_computed_values(body["computedValues"]) + + return verify_format(body, result) + + +def format_collection_computed_values(body: Jsons) -> Jsons: + """Format collection computed values data. + + :param body: Input body. + :type body: dict + :return: Formatted body. + :rtype: dict + """ + result: Jsons = [] + + for item in body: + result.append(format_collection_computed_value_item(item)) + + return verify_format(body, result) + + +def format_collection_computed_value_item(body: Json) -> Json: + """Format a computed value item for a collection. + + :param body: Input body. + :type body: dict + :return: Formatted body. + :rtype: dict + """ + result: Json = {} + + if "name" in body: + result["name"] = body["name"] + if "expression" in body: + result["expression"] = body["expression"] + if "overwrite" in body: + result["overwrite"] = body["overwrite"] + if "computedOn" in body: + result["computedOn"] = body["computedOn"] + if "keepNull" in body: + result["keepNull"] = body["keepNull"] + if "failOnWarning" in body: + result["failOnWarning"] = body["failOnWarning"] + return verify_format(body, result) From 6b457f3262aaad0e60aa7ee7c4dcd927298e0286 Mon Sep 17 00:00:00 2001 From: tjoubert Date: Tue, 20 Sep 2022 16:58:32 +0400 Subject: [PATCH 2/2] Fixed computedValues formatting --- arango/database.py | 6 ++--- arango/formatter.py | 56 ++++++++++----------------------------------- 2 files changed, 15 insertions(+), 47 deletions(-) diff --git a/arango/database.py b/arango/database.py index 249374c5..289bfdc9 100644 --- a/arango/database.py +++ b/arango/database.py @@ -1012,9 +1012,9 @@ def create_collection( document schema validation. :type schema: dict :param computedValues: Array of computed values for the new collection - enabling default values to new documents or the maintenance of - auxiliary attributes for search queries. Available in ArangoDB - version 3.10 or greater. See ArangoDB documentation for more + enabling default values to new documents or the maintenance of + auxiliary attributes for search queries. Available in ArangoDB + version 3.10 or greater. See ArangoDB documentation for more information on computed values. :type computedValues: list :return: Standard collection API wrapper. diff --git a/arango/formatter.py b/arango/formatter.py index 78590bce..f2e68692 100644 --- a/arango/formatter.py +++ b/arango/formatter.py @@ -1,6 +1,6 @@ from typing import Any -from arango.typings import Headers, Json, Jsons +from arango.typings import Headers, Json def verify_format(_: Any, res: Json) -> Json: @@ -210,49 +210,17 @@ def format_collection(body: Json) -> Json: # New in 3.10 if "computedValues" in body: - result["computedValues"] = format_collection_computed_values(body["computedValues"]) - - return verify_format(body, result) - - -def format_collection_computed_values(body: Jsons) -> Jsons: - """Format collection computed values data. - - :param body: Input body. - :type body: dict - :return: Formatted body. - :rtype: dict - """ - result: Jsons = [] - - for item in body: - result.append(format_collection_computed_value_item(item)) - - return verify_format(body, result) - - -def format_collection_computed_value_item(body: Json) -> Json: - """Format a computed value item for a collection. - - :param body: Input body. - :type body: dict - :return: Formatted body. - :rtype: dict - """ - result: Json = {} - - if "name" in body: - result["name"] = body["name"] - if "expression" in body: - result["expression"] = body["expression"] - if "overwrite" in body: - result["overwrite"] = body["overwrite"] - if "computedOn" in body: - result["computedOn"] = body["computedOn"] - if "keepNull" in body: - result["keepNull"] = body["keepNull"] - if "failOnWarning" in body: - result["failOnWarning"] = body["failOnWarning"] + result["computedValues"] = [ + { + "name": cv["name"], + "expression": cv["expression"], + "overwrite": cv["overwrite"], + "computedOn": cv["computedOn"], + "keepNull": cv["keepNull"], + "failOnWarning": cv["failOnWarning"], + } + for cv in body["computedValues"] + ] return verify_format(body, result)