Tarantool development patches archive
 help / color / mirror / Atom feed
From: Kirill Shcherbatov <kshcherbatov@tarantool.org>
To: tarantool-patches@freelists.org
Cc: "v.shpilevoy@tarantool.org" <v.shpilevoy@tarantool.org>
Subject: [tarantool-patches] Re: [PATCH v7 2/7] box: introduce OPT_ARRAY opt_type to decode arrays
Date: Fri, 25 May 2018 14:54:14 +0300	[thread overview]
Message-ID: <126d3d80-f249-6216-7974-c31cb49ead8f@tarantool.org> (raw)
In-Reply-To: <73780eb4-fbd6-68cd-fdcf-bec1e6c354a3@tarantool.org>

> 1. If to_array is failed, the error is reset in opts_parse_key:
> 
> snprintf(errmsg, sizeof(errmsg), "'%.*s' must be %s",
> 	 key_len, key, opt_type_strs[def->type]);
> diag_set(ClientError, errcode, field_no, errmsg);
> 
> So the original error is lost. You should refactor opt_set
> so that it sets diag on type mismatch instead of doing it in
> opts_parse_key. And opts_parse_key does nothing on error in opt_set -
> just return -1.

diff --git a/src/box/opt_def.c b/src/box/opt_def.c
index c8440c9..f4dd05b 100644
--- a/src/box/opt_def.c
+++ b/src/box/opt_def.c
@@ -49,10 +49,11 @@ const char *opt_type_strs[] = {
 
 static int
 opt_set(void *opts, const struct opt_def *def, const char **val,
-	struct region *region)
+	struct region *region, uint32_t errcode, uint32_t field_no)
 {
 	int64_t ival;
 	uint64_t uval;
+	char errmsg[DIAG_ERRMSG_MAX];
 	double dval;
 	uint32_t str_len;
 	const char *str;
@@ -61,30 +62,30 @@ opt_set(void *opts, const struct opt_def *def, const char **val,
 	switch (def->type) {
 	case OPT_BOOL:
 		if (mp_typeof(**val) != MP_BOOL)
-			return -1;
+			goto type_mismatch_err;
 		store_bool(opt, mp_decode_bool(val));
 		break;
 	case OPT_UINT32:
 		if (mp_typeof(**val) != MP_UINT)
-			return -1;
+			goto type_mismatch_err;
 		uval = mp_decode_uint(val);
 		if (uval > UINT32_MAX)
-			return -1;
+			goto type_mismatch_err;
 		store_u32(opt, uval);
 		break;
 	case OPT_INT64:
 		if (mp_read_int64(val, &ival) != 0)
-			return -1;
+			goto type_mismatch_err;
 		store_u64(opt, ival);
 		break;
 	case OPT_FLOAT:
 		if (mp_read_double(val, &dval) != 0)
-			return -1;
+			goto type_mismatch_err;
 		store_double(opt, dval);
 		break;
 	case OPT_STR:
 		if (mp_typeof(**val) != MP_STR)
-			return -1;
+			goto type_mismatch_err;
 		str = mp_decode_str(val, &str_len);
 		str_len = MIN(str_len, def->len - 1);
 		memcpy(opt, str, str_len);
@@ -92,7 +93,7 @@ opt_set(void *opts, const struct opt_def *def, const char **val,
 		break;
 	case OPT_STRPTR:
 		if (mp_typeof(**val) != MP_STR)
-			return -1;
+			goto type_mismatch_err;
 		str = mp_decode_str(val, &str_len);
 		if (str_len > 0) {
 			ptr = (char *) region_alloc(region, str_len + 1);
@@ -111,7 +112,7 @@ opt_set(void *opts, const struct opt_def *def, const char **val,
 		break;
 	case OPT_ENUM:
 		if (mp_typeof(**val) != MP_STR)
-			return -1;
+			goto type_mismatch_err;
 		str = mp_decode_str(val, &str_len);
 		if (def->to_enum == NULL) {
 			ival = strnindex(def->enum_strs, str, str_len,
@@ -138,7 +139,7 @@ opt_set(void *opts, const struct opt_def *def, const char **val,
 		break;
 	case OPT_ARRAY:
 		if (mp_typeof(**val) != MP_ARRAY)
-			return -1;
+			goto type_mismatch_err;
 		ival = mp_decode_array(val);
 		assert(def->to_array != NULL);
 		if (def->to_array(val, ival, opt) != 0)
@@ -148,6 +149,12 @@ opt_set(void *opts, const struct opt_def *def, const char **val,
 		unreachable();
 	}
 	return 0;
+
+type_mismatch_err:
+	snprintf(errmsg, sizeof(errmsg), "'%s' must be %s",
+		 def->name, opt_type_strs[def->type]);
+	diag_set(ClientError, errcode, field_no, errmsg);
+	return -1;
 }
 
 int
@@ -163,12 +170,8 @@ opts_parse_key(void *opts, const struct opt_def *reg, const char *key,
 		    memcmp(key, def->name, key_len) != 0)
 			continue;
 
-		if (opt_set(opts, def, data, region) != 0) {
-			snprintf(errmsg, sizeof(errmsg), "'%.*s' must be %s",
-				 key_len, key, opt_type_strs[def->type]);
-			diag_set(ClientError, errcode, field_no, errmsg);
+		if (opt_set(opts, def, data, region, errcode, field_no) != 0)
 			return -1;
-		}
 		found = true;
 		break;
 	}

And some other changes

+/**
+ * Decode MsgPack array callback.
+ * All memory allocations returned by opt_def_to_array_cb with opt
+ * [out] argument should be managed manually.
+ * @param str encoded data pointer (next to MsgPack ARRAY header).
+ * @param len array length (items count).
+ * @param [out] opt pointer to store resulting value.
+ * @param errcode Code of error to set if something is wrong.
+ * @param field_no Field number of an option in a parent element.
+ * @retval 0 on success.
+ * @retval -1 on error.
+ */
+typedef int (*opt_def_to_array_cb)(const char **str, uint32_t len, char *opt,
+                                  uint32_t errcode, uint32_t field_no);


+       case OPT_ARRAY:
+               if (mp_typeof(**val) != MP_ARRAY)
+                       goto type_mismatch_err;
+               ival = mp_decode_array(val);
+               assert(def->to_array != NULL);
+               if (def->to_array(val, ival, opt, errcode, field_no) != 0)
+                       return -1;
+               break;

  reply	other threads:[~2018-05-25 11:54 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-23 14:05 [tarantool-patches] [PATCH v7 0/7] sql: remove Checks to server Kirill Shcherbatov
2018-05-23 14:05 ` [tarantool-patches] [PATCH v7 1/7] sql: remove parser construct, destruct to sql.h Kirill Shcherbatov
2018-05-23 17:46   ` [tarantool-patches] " Konstantin Osipov
2018-05-24 19:26   ` Vladislav Shpilevoy
2018-05-25 12:05     ` Kirill Shcherbatov
2018-05-23 14:05 ` [tarantool-patches] [PATCH v7 2/7] box: introduce OPT_ARRAY opt_type to decode arrays Kirill Shcherbatov
2018-05-23 17:53   ` [tarantool-patches] " Konstantin Osipov
2018-05-24  7:32     ` Kirill Shcherbatov
2018-05-24 19:26   ` Vladislav Shpilevoy
2018-05-25 11:54     ` Kirill Shcherbatov [this message]
2018-05-23 14:05 ` [tarantool-patches] [PATCH v7 3/7] sql: introduce expr_len for sql_expr_compile Kirill Shcherbatov
2018-05-24 19:26   ` [tarantool-patches] " Vladislav Shpilevoy
2018-05-25 11:54     ` Kirill Shcherbatov
2018-05-23 14:05 ` [tarantool-patches] [PATCH v7 4/7] sql: rename sql_expr_free to sql_expr_delete Kirill Shcherbatov
2018-05-23 18:00   ` [tarantool-patches] " Konstantin Osipov
2018-05-24 19:26   ` Vladislav Shpilevoy
2018-05-25 11:54     ` Kirill Shcherbatov
2018-05-23 14:05 ` [tarantool-patches] [PATCH v7 5/7] sql: change sqlite3AddCheckConstraint signature Kirill Shcherbatov
2018-05-23 18:01   ` [tarantool-patches] " Konstantin Osipov
2018-05-24 19:26   ` Vladislav Shpilevoy
2018-05-25 11:53     ` Kirill Shcherbatov
2018-05-29 11:51   ` n.pettik
2018-05-30  8:32     ` Kirill Shcherbatov
2018-05-23 14:05 ` [tarantool-patches] [PATCH v7 6/7] sql: export funcs defined on Expr, ExprList to sql.h Kirill Shcherbatov
2018-05-23 18:15   ` [tarantool-patches] " Konstantin Osipov
2018-05-24  7:33     ` Kirill Shcherbatov
2018-05-24 19:26   ` Vladislav Shpilevoy
2018-05-25 11:53     ` Kirill Shcherbatov
2018-05-28 11:19       ` Vladislav Shpilevoy
2018-05-28 14:59         ` Kirill Shcherbatov
2018-05-23 14:05 ` [tarantool-patches] [PATCH v7 7/7] sql: remove Checks to server Kirill Shcherbatov
2018-05-24 19:26   ` [tarantool-patches] " Vladislav Shpilevoy
2018-05-25 11:53     ` Kirill Shcherbatov
2018-05-28 11:19       ` Vladislav Shpilevoy
2018-05-28 14:59         ` Kirill Shcherbatov
2018-05-28 18:50           ` Vladislav Shpilevoy
2018-05-29 11:49   ` n.pettik
2018-05-30  8:32     ` Kirill Shcherbatov
2018-05-30 10:42       ` n.pettik
2018-05-25 12:04 ` [tarantool-patches] Re: [PATCH v7 0/7] " Kirill Shcherbatov
2018-05-28 11:19   ` Vladislav Shpilevoy
2018-05-30 11:03 ` n.pettik
2018-05-31 17:44   ` Kirill Yukhin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=126d3d80-f249-6216-7974-c31cb49ead8f@tarantool.org \
    --to=kshcherbatov@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='[tarantool-patches] Re: [PATCH v7 2/7] box: introduce OPT_ARRAY opt_type to decode arrays' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox