Skip to content

Commit b73d32c

Browse files
committed
enforce logical type for boolean opts
1 parent ce02bd7 commit b73d32c

File tree

4 files changed

+31
-27
lines changed

4 files changed

+31
-27
lines changed

R/opts.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ getopt <- function(object, opt)
139139
#' @export
140140
#'
141141
subscribe <- function(con, topic = NULL)
142-
invisible(.Call(rnng_subscribe, con, 1L, topic))
142+
invisible(.Call(rnng_subscribe, con, topic, TRUE))
143143

144144
#' Unsubscribe Topic
145145
#'
@@ -186,7 +186,7 @@ subscribe <- function(con, topic = NULL)
186186
#' @export
187187
#'
188188
unsubscribe <- function(con, topic = NULL)
189-
invisible(.Call(rnng_subscribe, con, 0L, topic))
189+
invisible(.Call(rnng_subscribe, con, topic, FALSE))
190190

191191
#' Set Survey Time
192192
#'

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ aio
365365
#> < recvAio >
366366
#> - $data for message data
367367
aio$data |> str()
368-
#> num [1:100000000] -0.309 -1.345 -0.456 0.545 -1.052 ...
368+
#> num [1:100000000] -1.2309 1.3604 0.0492 1.9256 -0.5256 ...
369369
```
370370

371371
As `call_aio()` is blocking and will wait for completion, an alternative
@@ -526,11 +526,11 @@ ncurl("https://httpbin.org/headers")
526526
#> [1] 7b 0a 20 20 22 68 65 61 64 65 72 73 22 3a 20 7b 0a 20 20 20 20 22 48 6f 73
527527
#> [26] 74 22 3a 20 22 68 74 74 70 62 69 6e 2e 6f 72 67 22 2c 20 0a 20 20 20 20 22
528528
#> [51] 58 2d 41 6d 7a 6e 2d 54 72 61 63 65 2d 49 64 22 3a 20 22 52 6f 6f 74 3d 31
529-
#> [76] 2d 36 33 36 65 34 39 34 34 2d 32 39 66 37 31 31 34 63 36 63 33 38 66 63 30
530-
#> [101] 32 32 61 30 37 35 37 65 36 22 0a 20 20 7d 0a 7d 0a
529+
#> [76] 2d 36 33 36 65 35 62 37 39 2d 34 32 35 61 30 35 66 33 37 37 33 32 62 30 30
530+
#> [101] 66 37 62 39 64 66 62 62 31 22 0a 20 20 7d 0a 7d 0a
531531
#>
532532
#> $data
533-
#> [1] "{\n \"headers\": {\n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-636e4944-29f7114c6c38fc022a0757e6\"\n }\n}\n"
533+
#> [1] "{\n \"headers\": {\n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-636e5b79-425a05f37732b00f7b9dfbb1\"\n }\n}\n"
534534
```
535535

536536
For advanced use, supports additional HTTP methods such as POST or PUT.
@@ -551,13 +551,13 @@ res
551551

552552
call_aio(res)$headers
553553
#> $Date
554-
#> [1] "Fri, 11 Nov 2022 13:08:21 GMT"
554+
#> [1] "Fri, 11 Nov 2022 14:26:02 GMT"
555555
#>
556556
#> $Server
557557
#> [1] "gunicorn/19.9.0"
558558

559559
res$data
560-
#> [1] "{\n \"args\": {}, \n \"data\": \"{\\\"key\\\": \\\"value\\\"}\", \n \"files\": {}, \n \"form\": {}, \n \"headers\": {\n \"Authorization\": \"Bearer APIKEY\", \n \"Content-Length\": \"16\", \n \"Content-Type\": \"application/json\", \n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-636e4945-382ea8cc7800989d0f59eeb6\"\n }, \n \"json\": {\n \"key\": \"value\"\n }, \n \"origin\": \"213.86.169.34\", \n \"url\": \"http://httpbin.org/post\"\n}\n"
560+
#> [1] "{\n \"args\": {}, \n \"data\": \"{\\\"key\\\": \\\"value\\\"}\", \n \"files\": {}, \n \"form\": {}, \n \"headers\": {\n \"Authorization\": \"Bearer APIKEY\", \n \"Content-Length\": \"16\", \n \"Content-Type\": \"application/json\", \n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-636e5b7a-1d51a9d01df10adc716a0ac5\"\n }, \n \"json\": {\n \"key\": \"value\"\n }, \n \"origin\": \"213.86.169.34\", \n \"url\": \"http://httpbin.org/post\"\n}\n"
561561
```
562562

563563
In this respect, it may be used as a performant and lightweight method
@@ -599,10 +599,10 @@ s |> send('{"action": "subscribe", "symbols": "EURUSD"}')
599599
#> [1] 0
600600

601601
s |> recv()
602-
#> [1] "{\"s\":\"EURUSD\",\"a\":1.03098,\"b\":1.03096,\"dc\":\"1.1484\",\"dd\":\"0.0118\",\"ppms\":false,\"t\":1668172102000}"
602+
#> [1] "{\"s\":\"EURUSD\",\"a\":1.03024,\"b\":1.03017,\"dc\":\"1.0774\",\"dd\":\"0.0111\",\"ppms\":false,\"t\":1668176764000}"
603603

604604
s |> recv()
605-
#> [1] "{\"s\":\"EURUSD\",\"a\":1.03098,\"b\":1.03096,\"dc\":\"1.1484\",\"dd\":\"0.0118\",\"ppms\":false,\"t\":1668172102000}"
605+
#> [1] "{\"s\":\"EURUSD\",\"a\":1.03019,\"b\":1.03017,\"dc\":\"1.0726\",\"dd\":\"0.0111\",\"ppms\":false,\"t\":1668176764000}"
606606

607607
close(s)
608608
```

src/core.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -910,18 +910,18 @@ SEXP rnng_set_opt(SEXP object, SEXP opt, SEXP value) {
910910
break;
911911
case REALSXP:
912912
case INTSXP:
913-
case LGLSXP:
914913
val = Rf_asInteger(value);
915914
xc = nng_socket_set_ms(*sock, op, (nng_duration) val);
916915
if (xc == 0) break;
917916
xc = nng_socket_set_size(*sock, op, (size_t) val);
918917
if (xc == 0) break;
919918
xc = nng_socket_set_int(*sock, op, val);
920919
if (xc == 0) break;
921-
xc = nng_socket_set_bool(*sock, op, (bool) val);
922-
if (xc == 0) break;
923920
xc = nng_socket_set_uint64(*sock, op, (uint64_t) val);
924921
break;
922+
case LGLSXP:
923+
xc = nng_socket_set_bool(*sock, op, (bool) LOGICAL(value)[0]);
924+
break;
925925
default:
926926
Rf_error("type of 'value' not supported");
927927
}
@@ -938,18 +938,18 @@ SEXP rnng_set_opt(SEXP object, SEXP opt, SEXP value) {
938938
break;
939939
case REALSXP:
940940
case INTSXP:
941-
case LGLSXP:
942941
val = Rf_asInteger(value);
943942
xc = nng_ctx_set_ms(*ctx, op, (nng_duration) val);
944943
if (xc == 0) break;
945944
xc = nng_ctx_set_size(*ctx, op, (size_t) val);
946945
if (xc == 0) break;
947946
xc = nng_ctx_set_int(*ctx, op, val);
948947
if (xc == 0) break;
949-
xc = nng_ctx_set_bool(*ctx, op, (bool) val);
950-
if (xc == 0) break;
951948
xc = nng_ctx_set_uint64(*ctx, op, (uint64_t) val);
952949
break;
950+
case LGLSXP:
951+
xc = nng_ctx_set_bool(*ctx, op, (bool) LOGICAL(value)[0]);
952+
break;
953953
default:
954954
Rf_error("type of 'value' not supported");
955955
}
@@ -966,18 +966,18 @@ SEXP rnng_set_opt(SEXP object, SEXP opt, SEXP value) {
966966
break;
967967
case REALSXP:
968968
case INTSXP:
969-
case LGLSXP:
970969
val = Rf_asInteger(value);
971970
xc = nng_stream_set_ms(st, op, (nng_duration) val);
972971
if (xc == 0) break;
973972
xc = nng_stream_set_size(st, op, (size_t) val);
974973
if (xc == 0) break;
975974
xc = nng_stream_set_int(st, op, val);
976975
if (xc == 0) break;
977-
xc = nng_stream_set_bool(st, op, (bool) val);
978-
if (xc == 0) break;
979976
xc = nng_stream_set_uint64(st, op, (uint64_t) val);
980977
break;
978+
case LGLSXP:
979+
xc = nng_stream_set_bool(st, op, (bool) LOGICAL(value)[0]);
980+
break;
981981
default:
982982
Rf_error("type of 'value' not supported");
983983
}
@@ -994,18 +994,18 @@ SEXP rnng_set_opt(SEXP object, SEXP opt, SEXP value) {
994994
break;
995995
case REALSXP:
996996
case INTSXP:
997-
case LGLSXP:
998997
val = Rf_asInteger(value);
999998
xc = nng_listener_set_ms(*list, op, (nng_duration) val);
1000999
if (xc == 0) break;
10011000
xc = nng_listener_set_size(*list, op, (size_t) val);
10021001
if (xc == 0) break;
10031002
xc = nng_listener_set_int(*list, op, val);
10041003
if (xc == 0) break;
1005-
xc = nng_listener_set_bool(*list, op, (bool) val);
1006-
if (xc == 0) break;
10071004
xc = nng_listener_set_uint64(*list, op, (uint64_t) val);
10081005
break;
1006+
case LGLSXP:
1007+
xc = nng_listener_set_bool(*list, op, (bool) LOGICAL(value)[0]);
1008+
break;
10091009
default:
10101010
Rf_error("type of 'value' not supported");
10111011
}
@@ -1022,18 +1022,18 @@ SEXP rnng_set_opt(SEXP object, SEXP opt, SEXP value) {
10221022
break;
10231023
case REALSXP:
10241024
case INTSXP:
1025-
case LGLSXP:
10261025
val = Rf_asInteger(value);
10271026
xc = nng_dialer_set_ms(*dial, op, (nng_duration) val);
10281027
if (xc == 0) break;
10291028
xc = nng_dialer_set_size(*dial, op, (size_t) val);
10301029
if (xc == 0) break;
10311030
xc = nng_dialer_set_int(*dial, op, val);
10321031
if (xc == 0) break;
1033-
xc = nng_dialer_set_bool(*dial, op, (bool) val);
1034-
if (xc == 0) break;
10351032
xc = nng_dialer_set_uint64(*dial, op, (uint64_t) val);
10361033
break;
1034+
case LGLSXP:
1035+
xc = nng_dialer_set_bool(*dial, op, (bool) LOGICAL(value)[0]);
1036+
break;
10371037
default:
10381038
Rf_error("type of 'value' not supported");
10391039
}
@@ -1049,12 +1049,12 @@ SEXP rnng_set_opt(SEXP object, SEXP opt, SEXP value) {
10491049

10501050
}
10511051

1052-
SEXP rnng_subscribe(SEXP object, SEXP type, SEXP value) {
1052+
SEXP rnng_subscribe(SEXP object, SEXP value, SEXP sub) {
10531053

10541054
if (TYPEOF(object) != EXTPTRSXP)
10551055
Rf_error("'object' is not a valid Socket, Context, Stream, Listener or Dialer");
10561056

1057-
const char *op = INTEGER(type)[0] ? "sub:subscribe" : "sub:unsubscribe";
1057+
const char *op = LOGICAL(sub)[0] ? "sub:subscribe" : "sub:unsubscribe";
10581058
int xc;
10591059

10601060
const SEXP ptrtag = R_ExternalPtrTag(object);

tests/tests.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ nanotestw(n$listener_setopt("false", 100) == 9L)
3939
nanotesterr(n$listener_getopt("false"), 9)
4040
nanotestw(n$listener_setopt("false", "false") == 9L)
4141
nanotestw(n$listener_setopt("false", NULL) == 9L)
42+
nanotestw(n$listener_setopt("false", TRUE) == 9L)
4243
nanotesterr(n$listener_setopt("false", list()), "type")
4344
nanotestz(n$listener_start())
4445
nanotest(n$listener[[1]]$state == "started")
@@ -55,6 +56,7 @@ nanotestw(n1$dialer_setopt("false", 100) == 9L)
5556
nanotesterr(n1$dialer_getopt("false"), 9)
5657
nanotestw(n1$dialer_setopt("false", "false") == 9L)
5758
nanotestw(n1$dialer_setopt("false", NULL) == 9L)
59+
nanotestw(n1$dialer_setopt("false", TRUE) == 9L)
5860
nanotesterr(n1$dialer_setopt("false", list()), "type")
5961
nanotestz(n1$dialer_start())
6062
nanotest(n1$dialer[[1]]$state == "started")
@@ -131,6 +133,7 @@ assign("aio", value = "test", envir = sraio)
131133
nanotesterr(sraio$result, "valid")
132134
rraio <- n1$recv_aio(mode = "characte", keep.raw = TRUE, timeout = 500)
133135
nanotesterr(setopt(rraio[["aio"]], "false", 0L), "valid")
136+
nanotesterr(subscribe(rraio[["aio"]], "false"), "valid")
134137
nanotesterr(getopt(rraio[["aio"]], "false"), "valid")
135138
assign("aio", value = "test", envir = rraio)
136139
nanotesterr(rraio$raw, "valid")
@@ -171,6 +174,7 @@ nanotestw(req$setopt("false", 100) == 9L)
171174
nanotesterr(req$getopt("false"), 9)
172175
nanotestw(req$setopt("false", "false") == 9L)
173176
nanotestw(req$setopt("false", NULL) == 9L)
177+
nanotestw(req$setopt("false", TRUE) == 9L)
174178
nanotesterr(req$setopt("false", list()), "type")
175179
ctx <- context(rep)
176180
ctx

0 commit comments

Comments
 (0)