Skip to content

Commit

Permalink
refactor and README edits
Browse files Browse the repository at this point in the history
  • Loading branch information
attipaci committed Dec 9, 2024
1 parent 8b80edc commit cd98828
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,23 @@ Before destroying a RESP structure, the caller may want to dereference values wi
}
```

Note, that you can convert a RESP to an `XField`, and/or to JSON representation using the `redisxRESP2XField()` and
`redisxRESP2JSON()` functions, e.g.:


```c
Redis redis = ...

// Obtain a copy of the response received from HELLO upcon connecting...
RESP *resp = redisxGetHelloData(redis);

// Print the response from HELLO to the standard output in JSON format
printf("%s", redisxRESP2JSON("hello_response", resp));

// Destroy our copy of the RESP
redisxDestroyRESP(resp);
```c


-----------------------------------------------------------------------------

Expand Down
4 changes: 2 additions & 2 deletions include/redisx.h
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ boolean redisxIsMapType(const RESP *r);
boolean redisxHasComponents(const RESP *r);
boolean redisxIsEqualRESP(const RESP *a, const RESP *b);
int redisxSplitText(RESP *resp, char **text);
XField *resp2XField(const char *name, const RESP *resp);
char *resp2json(const char *name, const RESP *resp);
XField *redisxRESP2XField(const char *name, const RESP *resp);
char *redisxRESP2JSON(const char *name, const RESP *resp);

RedisMapEntry *redisxGetMapEntry(const RESP *map, const RESP *key);
RedisMapEntry *redisxGetKeywordEntry(const RESP *map, const char *key);
Expand Down
12 changes: 6 additions & 6 deletions src/resp.c
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ static XField *respArrayToXField(const char *name, const RESP **component, int n
array = (XField *) f->value;

for(i = 0; i < n; i++) {
XField *e = resp2XField(array[i].name, component[i]);
XField *e = redisxRESP2XField(array[i].name, component[i]);
if(e) {
array[i] = *e;
free(e);
Expand All @@ -612,7 +612,7 @@ static XField *respArrayToXField(const char *name, const RESP **component, int n
if(!array) return x_trace_null(fn, "field array");

for(i = 0; i < n; i++) {
XField *e = resp2XField("<element>", component[i]);
XField *e = redisxRESP2XField("<element>", component[i]);
if(e) {
memcpy(&array[i * eSize], e, sizeof(XField));
free(e);
Expand All @@ -630,7 +630,7 @@ static XField *respMap2XField(const char *name, const RedisMapEntry *map, int n)
while(--n >= 0) {
const RedisMapEntry *e = &map[n];
if(redisxIsStringType(e->key)) {
XField *fi = resp2XField((char *) e->key->value, e->value);
XField *fi = redisxRESP2XField((char *) e->key->value, e->value);
if(fi) {
fi->next = s->firstField;
s->firstField = fi;
Expand Down Expand Up @@ -668,7 +668,7 @@ static XField *respMap2XField(const char *name, const RedisMapEntry *map, int n)
*
* @sa resp2json()
*/
XField *resp2XField(const char *name, const RESP *resp) {
XField *redisxRESP2XField(const char *name, const RESP *resp) {
static const char *fn = "resp2XField";

errno = 0;
Expand Down Expand Up @@ -729,8 +729,8 @@ XField *resp2XField(const char *name, const RESP *resp) {
*
* @sa resp2XField()
*/
char *resp2json(const char *name, const RESP *resp) {
return xjsonFieldToString(resp2XField(name, resp));
char *redisxRESP2JSON(const char *name, const RESP *resp) {
return xjsonFieldToString(redisxRESP2XField(name, resp));
}


2 changes: 1 addition & 1 deletion test/src/test-hello.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int main() {
}

resp = redisxGetHelloData(redis);
json = resp2json("server_properties", resp);
json = redisxRESP2JSON("server_properties", resp);
printf("%s", json ? json : "<null>");
redisxDestroyRESP(resp);

Expand Down

0 comments on commit cd98828

Please sign in to comment.