diff options
author | ncteisen <ncteisen@gmail.com> | 2017-12-28 15:04:48 -0800 |
---|---|---|
committer | ncteisen <ncteisen@gmail.com> | 2017-12-28 15:09:17 -0800 |
commit | 7d9b6358b507aef4188739c46db1ad3f39a5bf98 (patch) | |
tree | 84991fb473351e484207e5dcd31d406f95c01d0f /src/core/lib/json | |
parent | 2e04b2dd0c9aed66a14e6810b18e8c5b9b4f8a04 (diff) |
Initial commit
Picks up work from https://github.com/grpc/grpc/pull/10259.
A merge was impossible due to the many sweeping changed that
have occured since I last touched that PR (c++-ization, exec_ctx,
reorganitation of filters, etc).
Diffstat (limited to 'src/core/lib/json')
-rw-r--r-- | src/core/lib/json/json.cc | 36 | ||||
-rw-r--r-- | src/core/lib/json/json.h | 21 |
2 files changed, 56 insertions, 1 deletions
diff --git a/src/core/lib/json/json.cc b/src/core/lib/json/json.cc index 4ad51f662a..2171e69455 100644 --- a/src/core/lib/json/json.cc +++ b/src/core/lib/json/json.cc @@ -19,6 +19,7 @@ #include <string.h> #include <grpc/support/alloc.h> +#include <grpc/support/log.h> #include "src/core/lib/json/json.h" @@ -44,5 +45,40 @@ void grpc_json_destroy(grpc_json* json) { json->parent->child = json->next; } + if (json->owns_value) { + gpr_free((void*)json->value); + } + gpr_free(json); } + +grpc_json* grpc_json_link_child(grpc_json* parent, grpc_json* child, + grpc_json* sibling) { + // first child case. + if (parent->child == NULL) { + GPR_ASSERT(sibling == NULL); + parent->child = child; + return child; + } + if (sibling == NULL) { + sibling = parent->child; + } + // always find the right most sibling. + while (sibling->next != NULL) { + sibling = sibling->next; + } + sibling->next = child; + return child; +} + +grpc_json* grpc_json_create_child(grpc_json* sibling, grpc_json* parent, + const char* key, const char* value, + grpc_json_type type, bool owns_value) { + grpc_json* child = grpc_json_create(type); + grpc_json_link_child(parent, child, sibling); + child->owns_value = owns_value; + child->parent = parent; + child->value = value; + child->key = key; + return child; +} diff --git a/src/core/lib/json/json.h b/src/core/lib/json/json.h index bbd43025eb..d88a79271f 100644 --- a/src/core/lib/json/json.h +++ b/src/core/lib/json/json.h @@ -19,6 +19,7 @@ #ifndef GRPC_CORE_LIB_JSON_JSON_H #define GRPC_CORE_LIB_JSON_JSON_H +#include <stdbool.h> #include <stdlib.h> #include "src/core/lib/json/json_common.h" @@ -35,6 +36,9 @@ typedef struct grpc_json { grpc_json_type type; const char* key; const char* value; + + /* if set, destructor will free value */ + bool owns_value; } grpc_json; /* The next two functions are going to parse the input string, and @@ -65,9 +69,24 @@ char* grpc_json_dump_to_string(grpc_json* json, int indent); /* Use these to create or delete a grpc_json object. * Deletion is recursive. We will not attempt to free any of the strings - * in any of the objects of that tree. + * in any of the objects of that tree, unless the boolean, owns_value, + * is true. */ grpc_json* grpc_json_create(grpc_json_type type); void grpc_json_destroy(grpc_json* json); +/* Links the child json object into the parent's json tree. If the parent + * already has children, then passing in the most recently added child as the + * sibling parameter is an optimization. For if sibling is NULL, this function + * will manually traverse the tree in order to find the right most sibling. + */ +grpc_json* grpc_json_link_child(grpc_json* parent, grpc_json* child, + grpc_json* sibling); + +/* Creates a child json object into the parent's json tree then links it in + * as described above. */ +grpc_json* grpc_json_create_child(grpc_json* sibling, grpc_json* parent, + const char* key, const char* value, + grpc_json_type type, bool owns_value); + #endif /* GRPC_CORE_LIB_JSON_JSON_H */ |