aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar Mark D. Roth <roth@google.com>2016-11-10 09:39:34 -0800
committerGravatar Mark D. Roth <roth@google.com>2016-11-10 09:39:34 -0800
commit84c8a027d3b9427982bc31be52e587ecd3462b9d (patch)
tree669ec04da70fa2c57f097ba4a2f3a22b1cea6b95 /src
parentdb4b4590444c57c048e708b1cc25fde101e1bd69 (diff)
Change JSON format to use the standard proto3 mapping for each type.
Diffstat (limited to 'src')
-rw-r--r--src/core/ext/client_channel/client_channel.c49
-rw-r--r--src/core/lib/channel/message_size_filter.c12
-rw-r--r--src/core/lib/support/string.c2
-rw-r--r--src/core/lib/support/string.h2
-rw-r--r--src/core/lib/transport/service_config.c24
5 files changed, 50 insertions, 39 deletions
diff --git a/src/core/ext/client_channel/client_channel.c b/src/core/ext/client_channel/client_channel.c
index 139912e4ba..a92a220c74 100644
--- a/src/core/ext/client_channel/client_channel.c
+++ b/src/core/ext/client_channel/client_channel.c
@@ -90,7 +90,7 @@ static void *method_parameters_create_from_json(const grpc_json *json) {
gpr_timespec timeout = {0, 0, GPR_TIMESPAN};
for (grpc_json *field = json->child; field != NULL; field = field->next) {
if (field->key == NULL) continue;
- if (strcmp(field->key, "wait_for_ready") == 0) {
+ if (strcmp(field->key, "waitForReady") == 0) {
if (wait_for_ready != WAIT_FOR_READY_UNSET) return NULL; // Duplicate.
if (field->type != GRPC_JSON_TRUE && field->type != GRPC_JSON_FALSE) {
return NULL;
@@ -99,26 +99,39 @@ static void *method_parameters_create_from_json(const grpc_json *json) {
: WAIT_FOR_READY_FALSE;
} else if (strcmp(field->key, "timeout") == 0) {
if (timeout.tv_sec > 0 || timeout.tv_nsec > 0) return NULL; // Duplicate.
- if (field->type != GRPC_JSON_OBJECT) return NULL;
- if (field->child == NULL) return NULL;
- for (grpc_json *subfield = field->child; subfield != NULL;
- subfield = subfield->next) {
- if (subfield->key == NULL) return NULL;
- if (strcmp(subfield->key, "seconds") == 0) {
- if (timeout.tv_sec > 0) return NULL; // Duplicate.
- if (subfield->type != GRPC_JSON_NUMBER) return NULL;
- timeout.tv_sec = gpr_parse_nonnegative_number(subfield->value);
- if (timeout.tv_sec == -1) return NULL;
- } else if (strcmp(subfield->key, "nanos") == 0) {
- if (timeout.tv_nsec > 0) return NULL; // Duplicate.
- if (subfield->type != GRPC_JSON_NUMBER) return NULL;
- timeout.tv_nsec = gpr_parse_nonnegative_number(subfield->value);
- if (timeout.tv_nsec == -1) return NULL;
- } else {
- // Unknown key.
+ if (field->type != GRPC_JSON_STRING) return NULL;
+ size_t len = strlen(field->value);
+ if (field->value[len - 1] != 's') return NULL;
+ char* buf = gpr_strdup(field->value);
+ buf[len - 1] = '\0'; // Remove trailing 's'.
+ char* decimal_point = strchr(buf, '.');
+ if (decimal_point != NULL) {
+ *decimal_point = '\0';
+ timeout.tv_nsec = gpr_parse_nonnegative_int(decimal_point + 1);
+ if (timeout.tv_nsec == -1) {
+ gpr_free(buf);
return NULL;
}
+ // There should always be exactly 3, 6, or 9 fractional digits.
+ int multiplier = 1;
+ switch (strlen(decimal_point + 1)) {
+ case 9:
+ break;
+ case 6:
+ multiplier *= 1000;
+ break;
+ case 3:
+ multiplier *= 1000000;
+ break;
+ default: // Unsupported number of digits.
+ gpr_free(buf);
+ return NULL;
+ }
+ timeout.tv_nsec *= multiplier;
}
+ timeout.tv_sec = gpr_parse_nonnegative_int(buf);
+ if (timeout.tv_sec == -1) return NULL;
+ gpr_free(buf);
}
}
method_parameters *value = gpr_malloc(sizeof(method_parameters));
diff --git a/src/core/lib/channel/message_size_filter.c b/src/core/lib/channel/message_size_filter.c
index 507ea26c05..28ad587c0e 100644
--- a/src/core/lib/channel/message_size_filter.c
+++ b/src/core/lib/channel/message_size_filter.c
@@ -65,15 +65,15 @@ static void* message_size_limits_create_from_json(const grpc_json* json) {
int max_response_message_bytes = -1;
for (grpc_json* field = json->child; field != NULL; field = field->next) {
if (field->key == NULL) continue;
- if (strcmp(field->key, "max_request_message_bytes") == 0) {
+ if (strcmp(field->key, "maxRequestMessageBytes") == 0) {
if (max_request_message_bytes >= 0) return NULL; // Duplicate.
- if (field->type != GRPC_JSON_NUMBER) return NULL;
- max_request_message_bytes = gpr_parse_nonnegative_number(field->value);
+ if (field->type != GRPC_JSON_STRING) return NULL;
+ max_request_message_bytes = gpr_parse_nonnegative_int(field->value);
if (max_request_message_bytes == -1) return NULL;
- } else if (strcmp(field->key, "max_response_message_bytes") == 0) {
+ } else if (strcmp(field->key, "maxResponseMessageBytes") == 0) {
if (max_response_message_bytes >= 0) return NULL; // Duplicate.
- if (field->type != GRPC_JSON_NUMBER) return NULL;
- max_response_message_bytes = gpr_parse_nonnegative_number(field->value);
+ if (field->type != GRPC_JSON_STRING) return NULL;
+ max_response_message_bytes = gpr_parse_nonnegative_int(field->value);
if (max_response_message_bytes == -1) return NULL;
}
}
diff --git a/src/core/lib/support/string.c b/src/core/lib/support/string.c
index aa58dd15e3..f10a30f0fd 100644
--- a/src/core/lib/support/string.c
+++ b/src/core/lib/support/string.c
@@ -191,7 +191,7 @@ int int64_ttoa(int64_t value, char *string) {
return i;
}
-int gpr_parse_nonnegative_number(const char *value) {
+int gpr_parse_nonnegative_int(const char *value) {
char *end;
long result = strtol(value, &end, 0);
if (*end != '\0' || result < 0 || result > INT_MAX) return -1;
diff --git a/src/core/lib/support/string.h b/src/core/lib/support/string.h
index a691ebb2a6..e933e2eb46 100644
--- a/src/core/lib/support/string.h
+++ b/src/core/lib/support/string.h
@@ -78,7 +78,7 @@ where long is 32bit is size.*/
int int64_ttoa(int64_t value, char *output);
// Parses a non-negative number from a value string. Returns -1 on error.
-int gpr_parse_nonnegative_number(const char *value);
+int gpr_parse_nonnegative_int(const char *value);
/* Reverse a run of bytes */
void gpr_reverse_bytes(char *str, int len);
diff --git a/src/core/lib/transport/service_config.c b/src/core/lib/transport/service_config.c
index a8494cbe8b..2e2b59e3f7 100644
--- a/src/core/lib/transport/service_config.c
+++ b/src/core/lib/transport/service_config.c
@@ -46,8 +46,8 @@
// JSON form, which will look like this:
//
// {
-// "lb_policy_name": "string", // optional
-// "method_config": [ // array of one or more method_config objects
+// "loadBalancingPolicy": "string", // optional
+// "methodConfig": [ // array of one or more method_config objects
// {
// "name": [ // array of one or more name objects
// {
@@ -55,15 +55,13 @@
// "method": "string", // optional
// }
// ],
-// // remaining fields are optional
-// "wait_for_ready": bool,
-// "timeout": {
-// // one or both of these fields may be specified
-// "seconds": number,
-// "nanos": number,
-// },
-// "max_request_message_bytes": number,
-// "max_response_message_bytes": number
+// // remaining fields are optional.
+// // see https://developers.google.com/protocol-buffers/docs/proto3#json
+// // for format details.
+// "waitForReady": bool,
+// "timeout": "duration_string",
+// "maxRequestMessageBytes": "int64_string",
+// "maxResponseMessageBytes": "int64_string",
// }
// ]
// }
@@ -100,7 +98,7 @@ const char* grpc_service_config_get_lb_policy_name(
const char* lb_policy_name = NULL;
for (grpc_json* field = json->child; field != NULL; field = field->next) {
if (field->key == NULL) return NULL;
- if (strcmp(field->key, "lb_policy_name") == 0) {
+ if (strcmp(field->key, "loadBalancingPolicy") == 0) {
if (lb_policy_name != NULL) return NULL; // Duplicate.
if (field->type != GRPC_JSON_STRING) return NULL;
lb_policy_name = field->value;
@@ -194,7 +192,7 @@ grpc_mdstr_hash_table* grpc_service_config_create_method_config_table(
grpc_mdstr_hash_table_entry* entries = NULL;
for (grpc_json* field = json->child; field != NULL; field = field->next) {
if (field->key == NULL) return NULL;
- if (strcmp(field->key, "method_config") == 0) {
+ if (strcmp(field->key, "methodConfig") == 0) {
if (entries != NULL) return NULL; // Duplicate.
if (field->type != GRPC_JSON_ARRAY) return NULL;
// Find number of entries.