[Tarantool-patches] [PATCH 4/5] error: provide MP_ERROR extension serializer

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Tue May 19 23:48:28 MSK 2020


Thanks for the review!

On 19/05/2020 13:51, Alexander Turenko wrote:
>> +	const char *field_to_key[MP_ERROR_MAX] = {
>> +		/* MP_ERROR_TYPE = */ "\"type\": ",
>> +		/* MP_ERROR_FILE = */ "\"file\": ",
>> +		/* MP_ERROR_LINE = */ "\"line\": ",
>> +		/* MP_ERROR_MESSAGE = */ "\"message\": ",
>> +		/* MP_ERROR_ERRNO = */ "\"errno\": ",
>> +		/* MP_ERROR_CODE = */ "\"code\": ",
>> +		/* MP_ERROR_FIELDS = */ "\"fields\": ",
>> +	};
> 
> Should be static?

I can do it static, but only outside of the function and the
of the whole template then. Otherwise we will have 2 copies
of this array, because mp_print_error_one() is turned into 2
functions: mp_sprint_error_one() and mp_fprint_error_one().

====================
diff --git a/src/box/mp_error.cc b/src/box/mp_error.cc
index 23a74d0a9..badf7ee92 100644
--- a/src/box/mp_error.cc
+++ b/src/box/mp_error.cc
@@ -561,6 +561,16 @@ error_unpack_unsafe(const char **data)
 	return err;
 }
 
+static const char *const mp_error_field_to_json_key[MP_ERROR_MAX] = {
+	[MP_ERROR_TYPE] =	"\"type\": ",
+	[MP_ERROR_FILE] =	"\"file\": ",
+	[MP_ERROR_LINE] =	"\"line\": ",
+	[MP_ERROR_MESSAGE] =	"\"message\": ",
+	[MP_ERROR_ERRNO] =	"\"errno\": ",
+	[MP_ERROR_CODE] =	"\"code\": ",
+	[MP_ERROR_FIELDS] =	"\"fields\": ",
+};
+
 /**
  * Include this file into self with a few template parameters
  * to create mp_snprint_error() and mp_fprint_error() functions
@@ -634,15 +644,6 @@ mp_print_error_one(MP_PRINT_ARGS_DECL, const char **data, int depth)
 		MP_PRINT(total, "...}");
 		return total;
 	}
-	const char *field_to_key[MP_ERROR_MAX] = {
-		[MP_ERROR_TYPE] =	"\"type\": ",
-		[MP_ERROR_FILE] =	"\"file\": ",
-		[MP_ERROR_LINE] =	"\"line\": ",
-		[MP_ERROR_MESSAGE] =	"\"message\": ",
-		[MP_ERROR_ERRNO] =	"\"errno\": ",
-		[MP_ERROR_CODE] =	"\"code\": ",
-		[MP_ERROR_FIELDS] =	"\"fields\": ",
-	};
 	--depth;
 	if (mp_typeof(**data) != MP_MAP)
 		return -1;
@@ -654,7 +655,7 @@ mp_print_error_one(MP_PRINT_ARGS_DECL, const char **data, int depth)
 			return -1;
 		uint64_t key = mp_decode_uint(data);
 		if (key < MP_ERROR_MAX)
-			MP_PRINT(total, "%s", field_to_key[key]);
+			MP_PRINT(total, "%s", mp_error_field_to_json_key[key]);
 		else
 			MP_PRINT(total, "%llu: ", key);
 		MP_PRINT_2(total, mp_print_common, data, depth);


More information about the Tarantool-patches mailing list