Skip to content

Commit 884ba3e

Browse files
committed
#97 - done
1 parent 1292307 commit 884ba3e

File tree

8 files changed

+85
-17
lines changed

8 files changed

+85
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ When this option is on:
809809

810810
[Back to content](#content)
811811

812-
tnt_multireturn_skip_count - DEPRECATED in 2.4.0+
812+
tnt_multireturn_skip_count - DEPRECATED in 2.4.0+, RETURNED IN 2.4.6-rc3+
813813
-------------------------------------------------
814814
**syntax:** *tnt_multireturn_skip_count [0|1|2]*
815815

src/ngx_http_tnt_handlers.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ typedef struct {
119119
*/
120120
ngx_uint_t pure_result;
121121

122+
/** Tarantool returns array of array as the result set,
123+
* this option will help avoid "array of array" behavior.
124+
* For instance.
125+
* If this option is set to 2, then the result will: result:{}.
126+
* If this option is set to 0, then the result will: result:[[{}]].
127+
*/
128+
ngx_uint_t multireturn_skip_count;
129+
122130
ngx_array_t *headers;
123131

124132
} ngx_http_tnt_loc_conf_t;

src/ngx_http_tnt_module.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,14 @@ static ngx_command_t ngx_http_tnt_commands[] = {
209209
offsetof(ngx_http_tnt_loc_conf_t, pure_result),
210210
&ngx_http_tnt_pass_http_request_masks },
211211

212+
{ ngx_string("tnt_multireturn_skip_count"),
213+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF
214+
|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
215+
ngx_conf_set_size_slot,
216+
NGX_HTTP_LOC_CONF_OFFSET,
217+
offsetof(ngx_http_tnt_loc_conf_t, multireturn_skip_count),
218+
NULL },
219+
212220
{ ngx_string("tnt_http_methods"),
213221
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
214222
ngx_conf_set_bitmask_slot,
@@ -379,6 +387,7 @@ ngx_http_tnt_create_loc_conf(ngx_conf_t *cf)
379387
conf->upstream.buffer_size =
380388
conf->in_multiplier =
381389
conf->out_multiplier =
390+
conf->multireturn_skip_count =
382391
conf->pass_http_request_buffer_size = NGX_CONF_UNSET_SIZE;
383392

384393
/*
@@ -467,6 +476,9 @@ ngx_http_tnt_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
467476
ngx_conf_merge_bitmask_value(conf->pure_result, prev->pure_result,
468477
NGX_TNT_CONF_OFF);
469478

479+
ngx_conf_merge_size_value(conf->multireturn_skip_count,
480+
prev->multireturn_skip_count, 0);
481+
470482
if (conf->headers == NULL) {
471483
conf->headers = prev->headers;
472484
}
@@ -714,7 +726,8 @@ ngx_http_tnt_send_reply(ngx_http_request_t *r,
714726
return NGX_ERROR;
715727
}
716728

717-
tp_reply_to_json_set_options(&tc, tlcf->pure_result == NGX_TNT_CONF_ON);
729+
tp_reply_to_json_set_options(&tc, tlcf->pure_result == NGX_TNT_CONF_ON,
730+
tlcf->multireturn_skip_count);
718731

719732
rc = tp_transcode(&tc, (char *)ctx->tp_cache->start,
720733
ctx->tp_cache->end - ctx->tp_cache->start);

src/ngx_http_tnt_version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@
3333
#ifndef NGX_HTTP_TNT_VERSION_H
3434
#define NGX_HTTP_TNT_VERSION_H 1
3535

36-
#define NGX_HTTP_TNT_MODULE_VERSION_STRING "v2.4.6-rc2"
36+
#define NGX_HTTP_TNT_MODULE_VERSION_STRING "v2.5-rc2"
3737

3838
#endif

src/tp_transcode.c

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,9 @@ typedef struct tp2json {
823823
*/
824824
bool pure_result;
825825

826+
size_t multireturn_skip_count;
827+
size_t multireturn_skiped;
828+
826829
} tp2json_t;
827830

828831
static inline int
@@ -957,20 +960,29 @@ tp2json_transcode_internal(tp2json_t *ctx, const char **beg, const char *end)
957960
{
958961
const uint32_t size = mp_decode_array(beg);
959962

960-
if (unlikely(len < size + 2/*,[]*/))
961-
OOM_TP2JSON;
963+
if (ctx->multireturn_skiped > 0) {
962964

963-
APPEND_CH('[');
964-
uint32_t i = 0;
965-
for (i = 0; i < size; i++) {
966-
if (i)
967-
APPEND_CH(',');
968-
rc = tp2json_transcode_internal(ctx, beg, end);
969-
if (rc != TP_TRANSCODE_OK)
970-
return rc;
971-
}
972-
APPEND_CH(']');
965+
--ctx->multireturn_skiped;
966+
rc = tp2json_transcode_internal(ctx, beg, end);
967+
if (rc != TP_TRANSCODE_OK)
968+
return rc;
973969

970+
} else {
971+
972+
if (unlikely(len < size + 2 /*,[]*/))
973+
OOM_TP2JSON;
974+
975+
APPEND_CH('[');
976+
uint32_t i = 0;
977+
for (i = 0; i < size; i++) {
978+
if (i)
979+
APPEND_CH(',');
980+
rc = tp2json_transcode_internal(ctx, beg, end);
981+
if (rc != TP_TRANSCODE_OK)
982+
return rc;
983+
}
984+
APPEND_CH(']');
985+
}
974986
break;
975987
}
976988
case MP_MAP:
@@ -1050,6 +1062,7 @@ tp_reply2json_transcode(void *ctx_, const char *in, size_t in_size)
10501062
}
10511063

10521064
ctx->first_entry = false;
1065+
ctx->multireturn_skiped = ctx->multireturn_skip_count;
10531066
}
10541067

10551068
if (ctx->r.error) {
@@ -1275,12 +1288,15 @@ tp_transcode_bind_data(tp_transcode_t *t,
12751288
}
12761289

12771290
void
1278-
tp_reply_to_json_set_options(tp_transcode_t *t, bool pure_result)
1291+
tp_reply_to_json_set_options(tp_transcode_t *t,
1292+
bool pure_result,
1293+
size_t multireturn_skip_count)
12791294
{
12801295
assert(t);
12811296
assert(t->codec.ctx);
12821297
tp2json_t *ctx = t->codec.ctx;
12831298
ctx->pure_result = pure_result;
1299+
ctx->multireturn_skip_count = multireturn_skip_count;
12841300
}
12851301

12861302
bool

src/tp_transcode.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ void tp_transcode_bind_data(tp_transcode_t *t,
177177
/**
178178
*/
179179
void
180-
tp_reply_to_json_set_options(tp_transcode_t *t, bool pure_result);
180+
tp_reply_to_json_set_options(tp_transcode_t *t, bool pure_result,
181+
size_t multireturn_skip_count);
181182

182183
/**
183184
* WARNING! tp_dump() is for debug!

t/ngx_confs/tnt_server_test.conf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,5 +294,17 @@ http {
294294
tnt_pass tnt;
295295
}
296296

297+
location /skip_count_1 {
298+
tnt_method echo_2;
299+
tnt_multireturn_skip_count 1;
300+
tnt_pass tnt;
301+
}
302+
303+
location /skip_count_2 {
304+
tnt_method echo_2;
305+
tnt_multireturn_skip_count 2;
306+
tnt_pass tnt;
307+
}
308+
297309
}
298310
}

t/v25_features.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,24 @@
8888
assert len(out['result'][0]['body']) == 4, 'expected 4'
8989
for k in out['result'][0]['body']:
9090
assert k['a'] == 'b', 'not expected'
91+
print ('[+] OK')
92+
9193

94+
print ('[+] Post - skip_count_1')
95+
preset_method_location = BASE_URL + '/skip_count_1'
96+
rc, out = post(preset_method_location, {'params': [{"arg1": 1}], 'id': 1},
97+
default_headers)
98+
assert rc == 200, "rc != 200"
99+
assert len(out['result']) == 1, 'result should be an array with 2 elems'
100+
assert out['result'][0]['arg1'] == 1, 'arg1 != 1'
92101
print ('[+] OK')
93102

103+
104+
print ('[+] Post - skip_count_2')
105+
preset_method_location = BASE_URL + '/skip_count_2'
106+
rc, out = post(preset_method_location, {'params': [{"arg1": 1}], 'id': 1},
107+
default_headers)
108+
assert rc == 200, "rc != 200"
109+
assert isinstance(out['result'], dict), 'not {}'
110+
assert out['result']['arg1'] == 1, 'arg1 != 1'
111+
print ('[+] OK')

0 commit comments

Comments
 (0)