Browse Source

started work on query response pretty printer

boB Rudis 9 months ago
parent
commit
d3eadcdc78
No known key found for this signature in database
5 changed files with 72 additions and 6 deletions
  1. 1
    0
      NAMESPACE
  2. 5
    1
      R/aaa.R
  3. 39
    1
      R/resolver.R
  4. 17
    0
      man/print.gdns_response.Rd
  5. 10
    4
      src/resolver.cpp

+ 1
- 0
NAMESPACE View File

@@ -1,6 +1,7 @@
1 1
 # Generated by roxygen2: do not edit by hand
2 2
 
3 3
 S3method(print,gctx)
4
+S3method(print,gdns_response)
4 5
 export("%>%")
5 6
 export(gdns_get_address)
6 7
 export(gdns_lib_version)

+ 5
- 1
R/aaa.R View File

@@ -1,3 +1,5 @@
1
+set_names <- function (object = nm, nm) { names(object) <- nm ; object }
2
+
1 3
 rr_types <- c(
2 4
 a = 1,
3 5
 ns = 2,
@@ -85,4 +87,6 @@ avc = 258,
85 87
 doa = 259,
86 88
 ta = 32768,
87 89
 dlv = 32769
88
-)
90
+)
91
+
92
+rr_types_rev <- set_names(names(rr_types), as.character(rr_types))

+ 39
- 1
R/resolver.R View File

@@ -123,9 +123,47 @@ gdns_query <- function(gctx, name, rr_type = "txt") {
123 123
   res <- int_gdns_query(gctx, name, unname(as.integer(rr_types[rr_type])))
124 124
   if (length(res)) {
125 125
     out <- jsonlite::fromJSON(res)
126
+    class(out) <- c("gdns_response", "list")
126 127
     out
127 128
   } else {
128 129
     NULL
129 130
   }
130 131
 
131
-}
132
+}
133
+
134
+list(`1` = "ipv4_address", `2` = "nsdname", `6` = c("expire",
135
+"minimum", "mname", "refresh", "retry", "rname", "serial"), `16` = "txt_strings",
136
+    `28` = "ipv6_address", `43` = c("algorithm", "digest", "digest_type",
137
+    "key_tag"), `46` = c("algorithm", "key_tag", "labels", "original_ttl",
138
+    "signature", "signature_expiration", "signature_inception",
139
+    "signers_name", "type_covered"), `47` = c("next_domain_name",
140
+    "type_bit_maps"), `48` = c("algorithm", "flags", "protocol",
141
+    "public_key")) -> rr_fields
142
+
143
+#' Printer for gdns_response objects
144
+#'
145
+#' @param x a `gdns_response` object
146
+#' @param ... ignored
147
+#' @keywords internal
148
+#' @export
149
+print.gdns_response <- function(x, ...) {
150
+
151
+  cat(
152
+    "Query: ",
153
+    x$replies_tree$question$qname[1], " ",
154
+    toupper(rr_types_rev[x$replies_tree$question$qtype[1]]),
155
+    "\n", sep=""
156
+  )
157
+
158
+  print(str(
159
+    x$replies_tree$answer[[1]]$rdata[
160
+      rr_fields[[as.character(unique(x$replies_tree$answer[[1]]$type))]]
161
+    ], 1
162
+  ))
163
+
164
+}
165
+
166
+
167
+
168
+
169
+

+ 17
- 0
man/print.gdns_response.Rd View File

@@ -0,0 +1,17 @@
1
+% Generated by roxygen2: do not edit by hand
2
+% Please edit documentation in R/resolver.R
3
+\name{print.gdns_response}
4
+\alias{print.gdns_response}
5
+\title{Printer for gdns_response objects}
6
+\usage{
7
+\method{print}{gdns_response}(x, ...)
8
+}
9
+\arguments{
10
+\item{x}{a \code{gdns_response} object}
11
+
12
+\item{...}{ignored}
13
+}
14
+\description{
15
+Printer for gdns_response objects
16
+}
17
+\keyword{internal}

+ 10
- 4
src/resolver.cpp View File

@@ -22,7 +22,8 @@ void check_is_xptr(SEXP s) {
22 22
 // [[Rcpp::export]]
23 23
 SEXP is_null_xptr_(SEXP s) {
24 24
   check_is_xptr(s);
25
-  return Rf_ScalarLogical(R_ExternalPtrAddr(s) == NULL);
25
+  void *r = (void *)R_ExternalPtrAddr(s);
26
+  return wrap(r == NULL);
26 27
 }
27 28
 
28 29
 static void gctx_finalizer(SEXP ptr) {
@@ -92,6 +93,8 @@ CharacterVector gdns_get_address(SEXP gctx, std::string host) {
92 93
   std::vector< std::string > out;
93 94
   bool ok = false;
94 95
 
96
+  check_is_xptr(gctx);
97
+
95 98
   getdns_context *ctxt = (getdns_context *)R_ExternalPtrAddr(gctx);
96 99
 
97 100
   if (gctx == NULL) return(CharacterVector());
@@ -145,6 +148,8 @@ CharacterVector int_get_resolvers(SEXP gctx) {
145 148
   getdns_list *addrs;
146 149
   std::vector< std::string > out;
147 150
 
151
+  check_is_xptr(gctx);
152
+
148 153
   getdns_context *ctxt = (getdns_context *)R_ExternalPtrAddr(gctx);
149 154
   if (gctx == NULL) return(CharacterVector());
150 155
 
@@ -193,15 +198,16 @@ CharacterVector int_get_resolvers(SEXP gctx) {
193 198
 CharacterVector int_gdns_query(SEXP gctx, std::string name, uint16_t rr) {
194 199
 
195 200
   uint32_t err;
196
-  size_t sz;
201
+  // size_t sz;
197 202
   getdns_return_t r;
198 203
   getdns_dict *resp = NULL;
199
-  getdns_list *results;
204
+  // getdns_list *results;
200 205
   std::string out;
201 206
   bool ok = false;
202 207
 
203
-  getdns_context *ctxt = (getdns_context *)R_ExternalPtrAddr(gctx);
208
+  check_is_xptr(gctx);
204 209
 
210
+  getdns_context *ctxt = (getdns_context *)R_ExternalPtrAddr(gctx);
205 211
   if (gctx == NULL) return(CharacterVector());
206 212
 
207 213
   if ((r = getdns_general_sync(ctxt, name.c_str(), rr, NULL, &resp))) {

Loading…
Cancel
Save