diff --git a/src/main/conversions.c b/src/main/conversions.c index 248368819..fadcf3a21 100644 --- a/src/main/conversions.c +++ b/src/main/conversions.c @@ -1662,12 +1662,24 @@ as_status map_to_pyobject(AerospikeClient *self, as_error *err, const as_map *map, PyObject **py_map) { *py_map = PyDict_New(); - if (!*py_map) { return as_error_update(err, AEROSPIKE_ERR_CLIENT, "Failed to allocate memory for dictionary."); } + // as_orderedmap has flags set to 1 + if (map->flags == 1) { + PyObject *key_ordered_dict_class = AerospikeKeyOrderedDict_Get_Type(); + PyObject *py_keyordereddict = + PyObject_CallFunctionObjArgs(key_ordered_dict_class, *py_map, NULL); + Py_DECREF(*py_map); + if (py_keyordereddict == NULL) { + return as_error_update(err, AEROSPIKE_ERR_CLIENT, + "Failed to create KeyOrderedDict instance."); + } + *py_map = py_keyordereddict; + } + conversion_data convd = { .err = err, .count = 0, .client = self, .udata = *py_map}; diff --git a/src/main/key_ordered_dict/type.c b/src/main/key_ordered_dict/type.c index c58df9b5d..66dc4807e 100644 --- a/src/main/key_ordered_dict/type.c +++ b/src/main/key_ordered_dict/type.c @@ -29,10 +29,10 @@ static PyMethodDef AerospikeKeyOrderedDict_Type_Methods[] = {{NULL}}; * PYTHON TYPE HOOKS ******************************************************************************/ -static int AerospikeKeyOrderedDict_Type_Init(AerospikeQuery *self, - PyObject *args, PyObject *kwds) +static int AerospikeKeyOrderedDict_Type_Init(PyObject *self, PyObject *args, + PyObject *kwds) { - return PyDict_Type.tp_init((PyObject *)self, args, kwds); + return PyDict_Type.tp_init(self, args, kwds); } /******************************************************************************* diff --git a/test/new_tests/test_get_put_keyordereddict.py b/test/new_tests/test_get_put_keyordereddict.py new file mode 100644 index 000000000..f75d06107 --- /dev/null +++ b/test/new_tests/test_get_put_keyordereddict.py @@ -0,0 +1,41 @@ +from aerospike import KeyOrderedDict +import pytest +from aerospike import exception as e + + +class TestGetPutOrderedDict: + @pytest.fixture(autouse=True) + def setup(self, request, as_connection): + yield + try: + self.as_connection.remove(self.key) + except e.AerospikeError: + pass + + @pytest.mark.parametrize( + "bin_value", + [ + KeyOrderedDict({"f": 6, "e": 5, "d": 4}), + [ + KeyOrderedDict({"f": 6, "e": 5, "d": 4}) + ] + ], + ids=[ + "bin-level", + "nested" + ] + ) + def test_get_put_keyordereddict(self, bin_value): + bins = { + "dict": bin_value + } + self.key = ("test", "demo", 1) + self.as_connection.put(self.key, bins) + + _, _, res = self.as_connection.get(self.key) + + assert res["dict"] == bin_value + if type(res["dict"]) == list: + assert type(res["dict"][0]) == KeyOrderedDict + else: + assert type(res["dict"]) == KeyOrderedDict