diff options
Diffstat (limited to 'php')
27 files changed, 947 insertions, 249 deletions
diff --git a/php/README.md b/php/README.md index da7884ea..e307768d 100644 --- a/php/README.md +++ b/php/README.md @@ -17,7 +17,7 @@ generation functionality. To use PHP runtime library requires: - C extension: PHP 5.5, 5.6, or 7. -- PHP package: PHP 5.5, 5.6 or 7. +- [PHP package](http://php.net/downloads.php): PHP 5.5, 5.6 or 7. ## Installation @@ -36,7 +36,7 @@ To install the c extension, the following tools are needed: On Ubuntu, you can install them with: ``` -sudo apt-get install php-pear php5-dev autoconf automake libtool make gcc +sudo apt-get install -y php-pear php5-dev autoconf automake libtool make gcc ``` On other platforms, please use the corresponding package managing tool to install them before proceeding. diff --git a/php/composer.json b/php/composer.json index 34e0447c..20f6a45e 100644 --- a/php/composer.json +++ b/php/composer.json @@ -9,13 +9,20 @@ "php": ">=5.5.0" }, "require-dev": { - "phpunit/phpunit": ">=4.8.0" + "phpunit/phpunit": "^5|^4.8.0" }, "autoload": { "psr-4": { "Google\\Protobuf\\": "src/Google/Protobuf", - "GPBMetadata\\Google\\Protobuf\\": "src/GPBMetadata/Google/Protobuf", + "GPBMetadata\\Google\\Protobuf\\": "src/GPBMetadata/Google/Protobuf" + } + }, + "autoload-dev": { + "psr-4": { "": "tests/generated" } + }, + "scripts": { + "test": "(cd tests && rm -rf generated && mkdir -p generated && ../../src/protoc --php_out=generated proto/empty/echo.proto proto/test.proto proto/test_include.proto proto/test_no_namespace.proto proto/test_prefix.proto proto/test_php_namespace.proto proto/test_empty_php_namespace.proto proto/test_reserved_enum_lower.proto proto/test_reserved_enum_upper.proto proto/test_reserved_enum_value_lower.proto proto/test_reserved_enum_value_upper.proto proto/test_reserved_message_lower.proto proto/test_reserved_message_upper.proto proto/test_service.proto proto/test_service_namespace.proto proto/test_descriptors.proto) && (cd ../src && ./protoc --php_out=../php/tests/generated -I../php/tests -I. ../php/tests/proto/test_import_descriptor_proto.proto) && vendor/bin/phpunit" } } diff --git a/php/ext/google/protobuf/encode_decode.c b/php/ext/google/protobuf/encode_decode.c index de13dfa8..899b99f0 100644 --- a/php/ext/google/protobuf/encode_decode.c +++ b/php/ext/google/protobuf/encode_decode.c @@ -1587,8 +1587,11 @@ PHP_METHOD(Message, mergeFromJsonString) { char *data = NULL; PHP_PROTO_SIZE data_len; + zend_bool ignore_json_unknown = false; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len) == + if (zend_parse_parameters( + ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &data, &data_len, + &ignore_json_unknown) == FAILURE) { return; } @@ -1607,7 +1610,7 @@ PHP_METHOD(Message, mergeFromJsonString) { stackenv_init(&se, "Error occurred during parsing: %s"); upb_sink_reset(&sink, get_fill_handlers(desc), msg); - parser = upb_json_parser_create(&se.env, method, &sink); + parser = upb_json_parser_create(&se.env, method, &sink, ignore_json_unknown); upb_bufsrc_putbuf(data, data_len, upb_json_parser_input(parser)); stackenv_uninit(&se); diff --git a/php/ext/google/protobuf/message.c b/php/ext/google/protobuf/message.c index 76b97eef..0efe090d 100644 --- a/php/ext/google/protobuf/message.c +++ b/php/ext/google/protobuf/message.c @@ -30,6 +30,7 @@ #include <php.h> #include <stdlib.h> +#include <inttypes.h> #include "protobuf.h" #include "utf8.h" @@ -1251,28 +1252,62 @@ PHP_METHOD(Timestamp, fromDateTime) { return; } - // Get timestamp from Datetime object. - zval retval; - zval function_name; - int64_t timestamp; + int64_t timestamp_seconds; + { + zval retval; + zval function_name; #if PHP_MAJOR_VERSION < 7 - INIT_ZVAL(retval); - INIT_ZVAL(function_name); + INIT_ZVAL(retval); + INIT_ZVAL(function_name); #endif - PHP_PROTO_ZVAL_STRING(&function_name, "date_timestamp_get", 1); + PHP_PROTO_ZVAL_STRING(&function_name, "date_timestamp_get", 1); - if (call_user_function(EG(function_table), NULL, &function_name, &retval, 1, - ZVAL_PTR_TO_CACHED_PTR(datetime) TSRMLS_CC) == FAILURE) { - zend_error(E_ERROR, "Cannot get timestamp from DateTime."); - return; + if (call_user_function(EG(function_table), NULL, &function_name, &retval, 1, + ZVAL_PTR_TO_CACHED_PTR(datetime) TSRMLS_CC) == FAILURE) { + zend_error(E_ERROR, "Cannot get timestamp from DateTime."); + return; + } + + protobuf_convert_to_int64(&retval, ×tamp_seconds); + + zval_dtor(&retval); + zval_dtor(&function_name); } - protobuf_convert_to_int64(&retval, ×tamp); + int64_t timestamp_micros; + { + zval retval; + zval function_name; + zval format_string; - zval_dtor(&retval); - zval_dtor(&function_name); +#if PHP_MAJOR_VERSION < 7 + INIT_ZVAL(retval); + INIT_ZVAL(function_name); + INIT_ZVAL(format_string); +#endif + + PHP_PROTO_ZVAL_STRING(&function_name, "date_format", 1); + PHP_PROTO_ZVAL_STRING(&format_string, "u", 1); + + CACHED_VALUE params[2] = { + ZVAL_PTR_TO_CACHED_VALUE(datetime), + ZVAL_TO_CACHED_VALUE(format_string), + }; + + if (call_user_function(EG(function_table), NULL, &function_name, &retval, + ARRAY_SIZE(params), params TSRMLS_CC) == FAILURE) { + zend_error(E_ERROR, "Cannot format DateTime."); + return; + } + + protobuf_convert_to_int64(&retval, ×tamp_micros); + + zval_dtor(&retval); + zval_dtor(&function_name); + zval_dtor(&format_string); + } // Set seconds MessageHeader* self = UNBOX(MessageHeader, getThis()); @@ -1280,13 +1315,13 @@ PHP_METHOD(Timestamp, fromDateTime) { upb_msgdef_ntofz(self->descriptor->msgdef, "seconds"); void* storage = message_data(self); void* memory = slot_memory(self->descriptor->layout, storage, field); - *(int64_t*)memory = timestamp; + *(int64_t*)memory = timestamp_seconds; // Set nanos field = upb_msgdef_ntofz(self->descriptor->msgdef, "nanos"); storage = message_data(self); memory = slot_memory(self->descriptor->layout, storage, field); - *(int32_t*)memory = 0; + *(int32_t*)memory = timestamp_micros * 1000; RETURN_NULL(); } @@ -1305,38 +1340,41 @@ PHP_METHOD(Timestamp, toDateTime) { memory = slot_memory(self->descriptor->layout, storage, field); int32_t nanos = *(int32_t*)memory; - // Get formated time string. - char formated_time[50]; - time_t raw_time = seconds; - struct tm *utc_time = gmtime(&raw_time); - strftime(formated_time, sizeof(formated_time), "%Y-%m-%dT%H:%M:%SUTC", - utc_time); + // Get formatted time string. + char formatted_time[32]; + snprintf(formatted_time, sizeof(formatted_time), "%" PRId64 ".%06" PRId32, + seconds, nanos / 1000); // Create Datetime object. zval datetime; - zval formated_time_php; zval function_name; - int64_t timestamp = 0; + zval format_string; + zval formatted_time_php; #if PHP_MAJOR_VERSION < 7 INIT_ZVAL(function_name); - INIT_ZVAL(formated_time_php); + INIT_ZVAL(format_string); + INIT_ZVAL(formatted_time_php); #endif - PHP_PROTO_ZVAL_STRING(&function_name, "date_create", 1); - PHP_PROTO_ZVAL_STRING(&formated_time_php, formated_time, 1); + PHP_PROTO_ZVAL_STRING(&function_name, "date_create_from_format", 1); + PHP_PROTO_ZVAL_STRING(&format_string, "U.u", 1); + PHP_PROTO_ZVAL_STRING(&formatted_time_php, formatted_time, 1); - CACHED_VALUE params[1] = {ZVAL_TO_CACHED_VALUE(formated_time_php)}; + CACHED_VALUE params[2] = { + ZVAL_TO_CACHED_VALUE(format_string), + ZVAL_TO_CACHED_VALUE(formatted_time_php), + }; - if (call_user_function(EG(function_table), NULL, - &function_name, &datetime, 1, - params TSRMLS_CC) == FAILURE) { + if (call_user_function(EG(function_table), NULL, &function_name, &datetime, + ARRAY_SIZE(params), params TSRMLS_CC) == FAILURE) { zend_error(E_ERROR, "Cannot create DateTime."); return; } - zval_dtor(&formated_time_php); zval_dtor(&function_name); + zval_dtor(&format_string); + zval_dtor(&formatted_time_php); #if PHP_MAJOR_VERSION < 7 zval* datetime_ptr = &datetime; diff --git a/php/ext/google/protobuf/protobuf.h b/php/ext/google/protobuf/protobuf.h index 48072a4f..a84feec6 100644 --- a/php/ext/google/protobuf/protobuf.h +++ b/php/ext/google/protobuf/protobuf.h @@ -42,6 +42,10 @@ #define MAX_LENGTH_OF_INT64 20 #define SIZEOF_INT64 8 +/* From Chromium. */ +#define ARRAY_SIZE(x) \ + ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x]))))) + // ----------------------------------------------------------------------------- // PHP7 Wrappers // ---------------------------------------------------------------------------- @@ -134,7 +138,8 @@ INIT_CLASS_ENTRY_EX(class_type, CLASSNAME, strlen(CLASSNAME), \ LOWWERNAME##_methods); \ LOWWERNAME##_type = zend_register_internal_class(&class_type TSRMLS_CC); \ - LOWWERNAME##_type->create_object = message_create; + LOWWERNAME##_type->create_object = message_create; \ + zend_do_inheritance(LOWWERNAME##_type, message_type TSRMLS_CC); #define PHP_PROTO_INIT_SUBMSGCLASS_END \ } @@ -400,7 +405,7 @@ static inline int php_proto_zend_hash_get_current_data_ex(HashTable* ht, INIT_CLASS_ENTRY_EX(class_type, CLASSNAME, strlen(CLASSNAME), \ LOWWERNAME##_methods); \ LOWWERNAME##_type = zend_register_internal_class(&class_type TSRMLS_CC); \ - LOWWERNAME##_type->create_object = message_create; + zend_do_inheritance(LOWWERNAME##_type, message_type TSRMLS_CC); #define PHP_PROTO_INIT_SUBMSGCLASS_END \ } diff --git a/php/ext/google/protobuf/upb.c b/php/ext/google/protobuf/upb.c index e01f3bfd..90c52aa8 100644 --- a/php/ext/google/protobuf/upb.c +++ b/php/ext/google/protobuf/upb.c @@ -14280,6 +14280,39 @@ done: #define UPB_JSON_MAX_DEPTH 64 +static const char *kDoubleValueFullMessageName = "google.protobuf.DoubleValue"; +static const char *kFloatValueFullMessageName = "google.protobuf.FloatValue"; +static const char *kInt64ValueFullMessageName = "google.protobuf.Int64Value"; +static const char *kUInt64ValueFullMessageName = "google.protobuf.UInt64Value"; +static const char *kInt32ValueFullMessageName = "google.protobuf.Int32Value"; +static const char *kUInt32ValueFullMessageName = "google.protobuf.UInt32Value"; +static const char *kBoolValueFullMessageName = "google.protobuf.BoolValue"; +static const char *kStringValueFullMessageName = "google.protobuf.StringValue"; +static const char *kBytesValueFullMessageName = "google.protobuf.BytesValue"; + +/* Forward declare */ +static bool is_top_level(upb_json_parser *p); + +static bool is_number_wrapper_object(upb_json_parser *p); +static bool does_number_wrapper_start(upb_json_parser *p); +static bool does_number_wrapper_end(upb_json_parser *p); + +static bool is_string_wrapper_object(upb_json_parser *p); +static bool does_string_wrapper_start(upb_json_parser *p); +static bool does_string_wrapper_end(upb_json_parser *p); + +static bool is_boolean_wrapper_object(upb_json_parser *p); +static bool does_boolean_wrapper_start(upb_json_parser *p); +static bool does_boolean_wrapper_end(upb_json_parser *p); + +static void start_wrapper_object(upb_json_parser *p); +static void end_wrapper_object(upb_json_parser *p); + +static bool start_subobject(upb_json_parser *p); +static void end_subobject(upb_json_parser *p); + +static const char eof_ch = 'e'; + typedef struct { upb_sink sink; @@ -14344,6 +14377,9 @@ struct upb_json_parser { /* Intermediate result of parsing a unicode escape sequence. */ uint32_t digit; + + /* Whether to proceed if unknown field is met. */ + bool ignore_json_unknown; }; struct upb_json_parsermethod { @@ -14852,21 +14888,54 @@ static bool end_text(upb_json_parser *p, const char *ptr) { return capture_end(p, ptr); } -static void start_number(upb_json_parser *p, const char *ptr) { +static bool start_number(upb_json_parser *p, const char *ptr) { + if (is_top_level(p)) { + if (!is_number_wrapper_object(p)) { + return false; + } + start_wrapper_object(p); + } else if (does_number_wrapper_start(p)) { + if (!start_subobject(p)) { + return false; + } + start_wrapper_object(p); + } + multipart_startaccum(p); capture_begin(p, ptr); + return true; } static bool parse_number(upb_json_parser *p, bool is_quoted); -static bool end_number(upb_json_parser *p, const char *ptr) { +static bool end_number_nontop(upb_json_parser *p, const char *ptr) { if (!capture_end(p, ptr)) { return false; } + if (p->top->f == NULL) { + multipart_end(p); + return true; + } + return parse_number(p, false); } +static bool end_number(upb_json_parser *p, const char *ptr) { + if (!end_number_nontop(p, ptr)) { + return false; + } + + if (does_number_wrapper_end(p)) { + end_wrapper_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + } + + return true; +} + /* |buf| is NULL-terminated. |buf| itself will never include quotes; * |is_quoted| tells us whether this text originally appeared inside quotes. */ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, @@ -15016,6 +15085,10 @@ static bool parse_number(upb_json_parser *p, bool is_quoted) { static bool parser_putbool(upb_json_parser *p, bool val) { bool ok; + if (p->top->f == NULL) { + return true; + } + if (upb_fielddef_type(p->top->f) != UPB_TYPE_BOOL) { upb_status_seterrf(&p->status, "Boolean value specified for non-bool field: %s", @@ -15030,8 +15103,50 @@ static bool parser_putbool(upb_json_parser *p, bool val) { return true; } +static bool end_bool(upb_json_parser *p, bool val) { + if (is_top_level(p)) { + if (!is_boolean_wrapper_object(p)) { + return false; + } + start_wrapper_object(p); + } else if (does_boolean_wrapper_start(p)) { + if (!start_subobject(p)) { + return false; + } + start_wrapper_object(p); + } + + if (!parser_putbool(p, val)) { + return false; + } + + if (does_boolean_wrapper_end(p)) { + end_wrapper_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + } + + return true; +} + static bool start_stringval(upb_json_parser *p) { - UPB_ASSERT(p->top->f); + if (is_top_level(p)) { + if (!is_string_wrapper_object(p)) { + return false; + } + start_wrapper_object(p); + } else if (does_string_wrapper_start(p)) { + if (!start_subobject(p)) { + return false; + } + start_wrapper_object(p); + } + + if (p->top->f == NULL) { + multipart_startaccum(p); + return true; + } if (upb_fielddef_isstring(p->top->f)) { upb_jsonparser_frame *inner; @@ -15079,9 +15194,14 @@ static bool start_stringval(upb_json_parser *p) { } } -static bool end_stringval(upb_json_parser *p) { +static bool end_stringval_nontop(upb_json_parser *p) { bool ok = true; + if (p->top->f == NULL) { + multipart_end(p); + return true; + } + switch (upb_fielddef_type(p->top->f)) { case UPB_TYPE_BYTES: if (!base64_push(p, getsel_for_handlertype(p, UPB_HANDLER_STRING), @@ -15141,6 +15261,21 @@ static bool end_stringval(upb_json_parser *p) { return ok; } +static bool end_stringval(upb_json_parser *p) { + if (!end_stringval_nontop(p)) { + return false; + } + + if (does_string_wrapper_end(p)) { + end_wrapper_object(p); + if (!is_top_level(p)) { + end_subobject(p); + } + } + + return true; +} + static void start_member(upb_json_parser *p) { UPB_ASSERT(!p->top->f); multipart_startaccum(p); @@ -15273,6 +15408,10 @@ static bool handle_mapentry(upb_json_parser *p) { static bool end_membername(upb_json_parser *p) { UPB_ASSERT(!p->top->f); + if (!p->top->m) { + return true; + } + if (p->top->is_map) { return handle_mapentry(p); } else { @@ -15285,9 +15424,10 @@ static bool end_membername(upb_json_parser *p) { multipart_end(p); return true; + } else if (p->ignore_json_unknown) { + multipart_end(p); + return true; } else { - /* TODO(haberman): Ignore unknown fields if requested/configured to do - * so. */ upb_status_seterrf(&p->status, "No such field: %.*s\n", (int)len, buf); upb_env_reporterror(p->env, &p->status); return false; @@ -15319,7 +15459,22 @@ static void end_member(upb_json_parser *p) { } static bool start_subobject(upb_json_parser *p) { - UPB_ASSERT(p->top->f); + if (is_top_level(p)) { + return true; + } + + if (p->top->f == NULL) { + upb_jsonparser_frame *inner; + if (!check_stack(p)) return false; + + inner = p->top + 1; + inner->m = NULL; + inner->f = NULL; + inner->is_map = false; + inner->is_mapentry = false; + p->top = inner; + return true; + } if (upb_fielddef_ismap(p->top->f)) { upb_jsonparser_frame *inner; @@ -15371,6 +15526,10 @@ static bool start_subobject(upb_json_parser *p) { } static void end_subobject(upb_json_parser *p) { + if (is_top_level(p)) { + return; + } + if (p->top->is_map) { upb_selector_t sel; p->top--; @@ -15378,9 +15537,12 @@ static void end_subobject(upb_json_parser *p) { upb_sink_endseq(&p->top->sink, sel); } else { upb_selector_t sel; + bool is_unknown = p->top->m == NULL; p->top--; - sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSUBMSG); - upb_sink_endsubmsg(&p->top->sink, sel); + if (!is_unknown) { + sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSUBMSG); + upb_sink_endsubmsg(&p->top->sink, sel); + } } } @@ -15440,6 +15602,118 @@ static void end_object(upb_json_parser *p) { } } +static bool is_double_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kDoubleValueFullMessageName) == 0; +} + +static bool is_float_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kFloatValueFullMessageName) == 0; +} + +static bool is_int64_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kInt64ValueFullMessageName) == 0; +} + +static bool is_uint64_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kUInt64ValueFullMessageName) == 0; +} + +static bool is_int32_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kInt32ValueFullMessageName) == 0; +} + +static bool is_uint32_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kUInt32ValueFullMessageName) == 0; +} + +static bool is_bool_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kBoolValueFullMessageName) == 0; +} + +static bool is_string_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kStringValueFullMessageName) == 0; +} + +static bool is_bytes_value(const upb_msgdef *m) { + return strcmp(upb_msgdef_fullname(m), kBytesValueFullMessageName) == 0; +} + +static bool is_number_wrapper(const upb_msgdef *m) { + return is_double_value(m) || + is_float_value(m) || + is_int64_value(m) || + is_uint64_value(m) || + is_int32_value(m) || + is_uint32_value(m); +} + +static bool is_string_wrapper(const upb_msgdef *m) { + return is_string_value(m) || + is_bytes_value(m); +} + +static void start_wrapper_object(upb_json_parser *p) { + const char *membername = "value"; + + start_object(p); + + /* Set up context for parsing value */ + start_member(p); + capture_begin(p, membername); + capture_end(p, membername + 5); + end_membername(p); +} + +static void end_wrapper_object(upb_json_parser *p) { + end_member(p); + end_object(p); +} + +static bool is_top_level(upb_json_parser *p) { + return p->top == p->stack && p->top->f == NULL; +} + +static bool does_number_wrapper_start(upb_json_parser *p) { + return p->top->f != NULL && + upb_fielddef_issubmsg(p->top->f) && + is_number_wrapper(upb_fielddef_msgsubdef(p->top->f)); +} + +static bool does_number_wrapper_end(upb_json_parser *p) { + return p->top->m != NULL && is_number_wrapper(p->top->m); +} + +static bool is_number_wrapper_object(upb_json_parser *p) { + return p->top->m != NULL && is_number_wrapper(p->top->m); +} + +static bool does_string_wrapper_start(upb_json_parser *p) { + return p->top->f != NULL && + upb_fielddef_issubmsg(p->top->f) && + is_string_wrapper(upb_fielddef_msgsubdef(p->top->f)); +} + +static bool does_string_wrapper_end(upb_json_parser *p) { + return p->top->m != NULL && is_string_wrapper(p->top->m); +} + +static bool is_string_wrapper_object(upb_json_parser *p) { + return p->top->m != NULL && is_string_wrapper(p->top->m); +} + +static bool does_boolean_wrapper_start(upb_json_parser *p) { + return p->top->f != NULL && + upb_fielddef_issubmsg(p->top->f) && + is_bool_value(upb_fielddef_msgsubdef(p->top->f)); +} + +static bool does_boolean_wrapper_end(upb_json_parser *p) { + return p->top->m != NULL && is_bool_value(p->top->m); +} + +static bool is_boolean_wrapper_object(upb_json_parser *p) { + return p->top->m != NULL && is_bool_value(p->top->m); +} #define CHECK_RETURN_TOP(x) if (!(x)) goto error @@ -15462,160 +15736,204 @@ static void end_object(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -#line 1310 "upb/json/parser.rl" +#line 1589 "upb/json/parser.rl" -#line 1222 "upb/json/parser.c" +#line 1496 "upb/json/parser.c" static const char _json_actions[] = { - 0, 1, 0, 1, 2, 1, 3, 1, - 5, 1, 6, 1, 7, 1, 8, 1, - 10, 1, 12, 1, 13, 1, 14, 1, - 15, 1, 16, 1, 17, 1, 21, 1, - 25, 1, 27, 2, 3, 8, 2, 4, - 5, 2, 6, 2, 2, 6, 8, 2, - 11, 9, 2, 13, 15, 2, 14, 15, - 2, 18, 1, 2, 19, 27, 2, 20, - 9, 2, 22, 27, 2, 23, 27, 2, - 24, 27, 2, 26, 27, 3, 14, 11, - 9 + 0, 1, 0, 1, 1, 1, 3, 1, + 4, 1, 6, 1, 7, 1, 8, 1, + 9, 1, 11, 1, 13, 1, 14, 1, + 15, 1, 16, 1, 17, 1, 18, 1, + 20, 1, 22, 1, 23, 1, 24, 1, + 25, 1, 26, 1, 27, 1, 28, 2, + 4, 9, 2, 5, 6, 2, 7, 3, + 2, 7, 9, 2, 12, 10, 2, 14, + 16, 2, 15, 16, 2, 19, 2, 2, + 20, 28, 2, 21, 10, 2, 23, 28, + 2, 24, 28, 2, 25, 28, 2, 27, + 28, 3, 15, 12, 10 }; static const unsigned char _json_key_offsets[] = { - 0, 0, 4, 9, 14, 15, 19, 24, - 29, 34, 38, 42, 45, 48, 50, 54, - 58, 60, 62, 67, 69, 71, 80, 86, - 92, 98, 104, 106, 115, 116, 116, 116, - 121, 126, 131, 132, 133, 134, 135, 135, - 136, 137, 138, 138, 139, 140, 141, 141, - 146, 151, 152, 156, 161, 166, 171, 175, - 175, 178, 178, 178 + 0, 0, 12, 13, 18, 23, 28, 29, + 30, 31, 32, 33, 34, 35, 36, 37, + 38, 43, 48, 49, 53, 58, 63, 68, + 72, 76, 79, 82, 84, 88, 92, 94, + 96, 101, 103, 105, 114, 120, 126, 132, + 138, 140, 149, 150, 150, 150, 155, 160, + 165, 166, 167, 168, 169, 169, 170, 171, + 172, 172, 173, 174, 175, 175, 180, 185, + 186, 190, 195, 200, 205, 209, 209, 212, + 215, 218, 221, 224, 227, 227, 227 }; static const char _json_trans_keys[] = { - 32, 123, 9, 13, 32, 34, 125, 9, - 13, 32, 34, 125, 9, 13, 34, 32, - 58, 9, 13, 32, 93, 125, 9, 13, - 32, 44, 125, 9, 13, 32, 44, 125, - 9, 13, 32, 34, 9, 13, 45, 48, - 49, 57, 48, 49, 57, 46, 69, 101, - 48, 57, 69, 101, 48, 57, 43, 45, - 48, 57, 48, 57, 48, 57, 46, 69, - 101, 48, 57, 34, 92, 34, 92, 34, - 47, 92, 98, 102, 110, 114, 116, 117, + 32, 34, 45, 91, 102, 110, 116, 123, + 9, 13, 48, 57, 34, 32, 93, 125, + 9, 13, 32, 44, 93, 9, 13, 32, + 93, 125, 9, 13, 97, 108, 115, 101, + 117, 108, 108, 114, 117, 101, 32, 34, + 125, 9, 13, 32, 34, 125, 9, 13, + 34, 32, 58, 9, 13, 32, 93, 125, + 9, 13, 32, 44, 125, 9, 13, 32, + 44, 125, 9, 13, 32, 34, 9, 13, + 45, 48, 49, 57, 48, 49, 57, 46, + 69, 101, 48, 57, 69, 101, 48, 57, + 43, 45, 48, 57, 48, 57, 48, 57, + 46, 69, 101, 48, 57, 34, 92, 34, + 92, 34, 47, 92, 98, 102, 110, 114, + 116, 117, 48, 57, 65, 70, 97, 102, 48, 57, 65, 70, 97, 102, 48, 57, 65, 70, 97, 102, 48, 57, 65, 70, - 97, 102, 48, 57, 65, 70, 97, 102, - 34, 92, 34, 45, 91, 102, 110, 116, - 123, 48, 57, 34, 32, 93, 125, 9, - 13, 32, 44, 93, 9, 13, 32, 93, - 125, 9, 13, 97, 108, 115, 101, 117, - 108, 108, 114, 117, 101, 32, 34, 125, - 9, 13, 32, 34, 125, 9, 13, 34, - 32, 58, 9, 13, 32, 93, 125, 9, - 13, 32, 44, 125, 9, 13, 32, 44, - 125, 9, 13, 32, 34, 9, 13, 32, - 9, 13, 0 + 97, 102, 34, 92, 34, 45, 91, 102, + 110, 116, 123, 48, 57, 34, 32, 93, + 125, 9, 13, 32, 44, 93, 9, 13, + 32, 93, 125, 9, 13, 97, 108, 115, + 101, 117, 108, 108, 114, 117, 101, 32, + 34, 125, 9, 13, 32, 34, 125, 9, + 13, 34, 32, 58, 9, 13, 32, 93, + 125, 9, 13, 32, 44, 125, 9, 13, + 32, 44, 125, 9, 13, 32, 34, 9, + 13, 32, 9, 13, 32, 9, 13, 32, + 9, 13, 32, 9, 13, 32, 9, 13, + 32, 9, 13, 0 }; static const char _json_single_lengths[] = { - 0, 2, 3, 3, 1, 2, 3, 3, - 3, 2, 2, 1, 3, 0, 2, 2, - 0, 0, 3, 2, 2, 9, 0, 0, - 0, 0, 2, 7, 1, 0, 0, 3, - 3, 3, 1, 1, 1, 1, 0, 1, - 1, 1, 0, 1, 1, 1, 0, 3, - 3, 1, 2, 3, 3, 3, 2, 0, - 1, 0, 0, 0 + 0, 8, 1, 3, 3, 3, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, + 3, 3, 1, 2, 3, 3, 3, 2, + 2, 1, 3, 0, 2, 2, 0, 0, + 3, 2, 2, 9, 0, 0, 0, 0, + 2, 7, 1, 0, 0, 3, 3, 3, + 1, 1, 1, 1, 0, 1, 1, 1, + 0, 1, 1, 1, 0, 3, 3, 1, + 2, 3, 3, 3, 2, 0, 1, 1, + 1, 1, 1, 1, 0, 0, 0 }; static const char _json_range_lengths[] = { - 0, 1, 1, 1, 0, 1, 1, 1, - 1, 1, 1, 1, 0, 1, 1, 1, - 1, 1, 1, 0, 0, 0, 3, 3, - 3, 3, 0, 1, 0, 0, 0, 1, - 1, 1, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 1, - 1, 0, 1, 1, 1, 1, 1, 0, - 1, 0, 0, 0 + 0, 2, 0, 1, 1, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 0, 1, 1, 1, 1, 1, + 1, 1, 0, 1, 1, 1, 1, 1, + 1, 0, 0, 0, 3, 3, 3, 3, + 0, 1, 0, 0, 0, 1, 1, 1, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 1, 1, 0, + 1, 1, 1, 1, 1, 0, 1, 1, + 1, 1, 1, 1, 0, 0, 0 }; static const short _json_index_offsets[] = { - 0, 0, 4, 9, 14, 16, 20, 25, - 30, 35, 39, 43, 46, 50, 52, 56, - 60, 62, 64, 69, 72, 75, 85, 89, - 93, 97, 101, 104, 113, 115, 116, 117, - 122, 127, 132, 134, 136, 138, 140, 141, - 143, 145, 147, 148, 150, 152, 154, 155, - 160, 165, 167, 171, 176, 181, 186, 190, - 191, 194, 195, 196 + 0, 0, 11, 13, 18, 23, 28, 30, + 32, 34, 36, 38, 40, 42, 44, 46, + 48, 53, 58, 60, 64, 69, 74, 79, + 83, 87, 90, 94, 96, 100, 104, 106, + 108, 113, 116, 119, 129, 133, 137, 141, + 145, 148, 157, 159, 160, 161, 166, 171, + 176, 178, 180, 182, 184, 185, 187, 189, + 191, 192, 194, 196, 198, 199, 204, 209, + 211, 215, 220, 225, 230, 234, 235, 238, + 241, 244, 247, 250, 253, 254, 255 }; static const char _json_indicies[] = { - 0, 2, 0, 1, 3, 4, 5, 3, - 1, 6, 7, 8, 6, 1, 9, 1, - 10, 11, 10, 1, 11, 1, 1, 11, - 12, 13, 14, 15, 13, 1, 16, 17, - 8, 16, 1, 17, 7, 17, 1, 18, - 19, 20, 1, 19, 20, 1, 22, 23, - 23, 21, 24, 1, 23, 23, 24, 21, - 25, 25, 26, 1, 26, 1, 26, 21, - 22, 23, 23, 20, 21, 28, 29, 27, - 31, 32, 30, 33, 33, 33, 33, 33, - 33, 33, 33, 34, 1, 35, 35, 35, - 1, 36, 36, 36, 1, 37, 37, 37, - 1, 38, 38, 38, 1, 40, 41, 39, - 42, 43, 44, 45, 46, 47, 48, 43, - 1, 49, 1, 50, 51, 53, 54, 1, - 53, 52, 55, 56, 54, 55, 1, 56, - 1, 1, 56, 52, 57, 1, 58, 1, - 59, 1, 60, 1, 61, 62, 1, 63, - 1, 64, 1, 65, 66, 1, 67, 1, - 68, 1, 69, 70, 71, 72, 70, 1, - 73, 74, 75, 73, 1, 76, 1, 77, - 78, 77, 1, 78, 1, 1, 78, 79, - 80, 81, 82, 80, 1, 83, 84, 75, - 83, 1, 84, 74, 84, 1, 85, 86, - 86, 1, 1, 1, 1, 0 + 0, 2, 3, 4, 5, 6, 7, 8, + 0, 3, 1, 9, 1, 11, 12, 1, + 11, 10, 13, 14, 12, 13, 1, 14, + 1, 1, 14, 10, 15, 1, 16, 1, + 17, 1, 18, 1, 19, 1, 20, 1, + 21, 1, 22, 1, 23, 1, 24, 1, + 25, 26, 27, 25, 1, 28, 29, 30, + 28, 1, 31, 1, 32, 33, 32, 1, + 33, 1, 1, 33, 34, 35, 36, 37, + 35, 1, 38, 39, 30, 38, 1, 39, + 29, 39, 1, 40, 41, 42, 1, 41, + 42, 1, 44, 45, 45, 43, 46, 1, + 45, 45, 46, 43, 47, 47, 48, 1, + 48, 1, 48, 43, 44, 45, 45, 42, + 43, 50, 51, 49, 53, 54, 52, 55, + 55, 55, 55, 55, 55, 55, 55, 56, + 1, 57, 57, 57, 1, 58, 58, 58, + 1, 59, 59, 59, 1, 60, 60, 60, + 1, 62, 63, 61, 64, 65, 66, 67, + 68, 69, 70, 65, 1, 71, 1, 72, + 73, 75, 76, 1, 75, 74, 77, 78, + 76, 77, 1, 78, 1, 1, 78, 74, + 79, 1, 80, 1, 81, 1, 82, 1, + 83, 84, 1, 85, 1, 86, 1, 87, + 88, 1, 89, 1, 90, 1, 91, 92, + 93, 94, 92, 1, 95, 96, 97, 95, + 1, 98, 1, 99, 100, 99, 1, 100, + 1, 1, 100, 101, 102, 103, 104, 102, + 1, 105, 106, 97, 105, 1, 106, 96, + 106, 1, 107, 108, 108, 1, 109, 109, + 1, 110, 110, 1, 111, 111, 1, 112, + 112, 1, 113, 113, 1, 1, 1, 1, + 0 }; static const char _json_trans_targs[] = { - 1, 0, 2, 3, 4, 56, 3, 4, - 56, 5, 5, 6, 7, 8, 9, 56, - 8, 9, 11, 12, 18, 57, 13, 15, - 14, 16, 17, 20, 58, 21, 20, 58, - 21, 19, 22, 23, 24, 25, 26, 20, - 58, 21, 28, 30, 31, 34, 39, 43, - 47, 29, 59, 59, 32, 31, 29, 32, - 33, 35, 36, 37, 38, 59, 40, 41, - 42, 59, 44, 45, 46, 59, 48, 49, - 55, 48, 49, 55, 50, 50, 51, 52, - 53, 54, 55, 53, 54, 59, 56 + 1, 0, 2, 71, 3, 6, 10, 13, + 16, 70, 4, 3, 70, 4, 5, 7, + 8, 9, 72, 11, 12, 73, 14, 15, + 74, 17, 18, 75, 17, 18, 75, 19, + 19, 20, 21, 22, 23, 75, 22, 23, + 25, 26, 32, 76, 27, 29, 28, 30, + 31, 34, 77, 35, 34, 77, 35, 33, + 36, 37, 38, 39, 40, 34, 77, 35, + 42, 44, 45, 48, 53, 57, 61, 43, + 78, 78, 46, 45, 43, 46, 47, 49, + 50, 51, 52, 78, 54, 55, 56, 78, + 58, 59, 60, 78, 62, 63, 69, 62, + 63, 69, 64, 64, 65, 66, 67, 68, + 69, 67, 68, 78, 70, 70, 70, 70, + 70, 70 }; static const char _json_trans_actions[] = { - 0, 0, 0, 21, 77, 53, 0, 47, - 23, 17, 0, 0, 15, 19, 19, 50, - 0, 0, 0, 0, 0, 1, 0, 0, - 0, 0, 0, 3, 13, 0, 0, 35, - 5, 11, 0, 38, 7, 7, 7, 41, - 44, 9, 62, 56, 25, 0, 0, 0, - 31, 29, 33, 59, 15, 0, 27, 0, - 0, 0, 0, 0, 0, 68, 0, 0, - 0, 71, 0, 0, 0, 65, 21, 77, - 53, 0, 47, 23, 17, 0, 0, 15, - 19, 19, 50, 0, 0, 74, 0 + 0, 0, 74, 68, 27, 0, 0, 0, + 41, 33, 17, 0, 29, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 23, 89, 65, 0, 59, 25, 19, + 0, 0, 17, 21, 21, 62, 0, 0, + 0, 0, 0, 3, 0, 0, 0, 0, + 0, 5, 15, 0, 0, 47, 7, 13, + 0, 50, 9, 9, 9, 53, 56, 11, + 74, 68, 27, 0, 0, 0, 41, 33, + 45, 71, 17, 0, 29, 0, 0, 0, + 0, 0, 0, 80, 0, 0, 0, 83, + 0, 0, 0, 77, 23, 89, 65, 0, + 59, 25, 19, 0, 0, 17, 21, 21, + 62, 0, 0, 86, 0, 31, 37, 39, + 35, 43 +}; + +static const char _json_eof_actions[] = { + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 0, 1, 0, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 31, + 37, 39, 35, 43, 0, 0, 0 }; static const int json_start = 1; -static const int json_en_number_machine = 10; -static const int json_en_string_machine = 19; -static const int json_en_value_machine = 27; +static const int json_en_number_machine = 24; +static const int json_en_string_machine = 33; +static const int json_en_value_machine = 41; static const int json_en_main = 1; -#line 1313 "upb/json/parser.rl" +#line 1592 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -15628,6 +15946,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, const char *p = buf; const char *pe = buf + size; + const char *eof = &eof_ch; parser->handle = handle; @@ -15637,7 +15956,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -#line 1393 "upb/json/parser.c" +#line 1712 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -15711,119 +16030,119 @@ _match: { switch ( *_acts++ ) { - case 0: -#line 1225 "upb/json/parser.rl" - { p--; {cs = stack[--top]; goto _again;} } - break; case 1: -#line 1226 "upb/json/parser.rl" - { p--; {stack[top++] = cs; cs = 10; goto _again;} } +#line 1503 "upb/json/parser.rl" + { p--; {cs = stack[--top]; goto _again;} } break; case 2: -#line 1230 "upb/json/parser.rl" - { start_text(parser, p); } +#line 1505 "upb/json/parser.rl" + { p--; {stack[top++] = cs; cs = 24; goto _again;} } break; case 3: -#line 1231 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_text(parser, p)); } +#line 1509 "upb/json/parser.rl" + { start_text(parser, p); } break; case 4: -#line 1237 "upb/json/parser.rl" - { start_hex(parser); } +#line 1510 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_text(parser, p)); } break; case 5: -#line 1238 "upb/json/parser.rl" - { hexdigit(parser, p); } +#line 1516 "upb/json/parser.rl" + { start_hex(parser); } break; case 6: -#line 1239 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_hex(parser)); } +#line 1517 "upb/json/parser.rl" + { hexdigit(parser, p); } break; case 7: -#line 1245 "upb/json/parser.rl" - { CHECK_RETURN_TOP(escape(parser, p)); } +#line 1518 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_hex(parser)); } break; case 8: -#line 1251 "upb/json/parser.rl" - { p--; {cs = stack[--top]; goto _again;} } +#line 1524 "upb/json/parser.rl" + { CHECK_RETURN_TOP(escape(parser, p)); } break; case 9: -#line 1254 "upb/json/parser.rl" - { {stack[top++] = cs; cs = 19; goto _again;} } +#line 1530 "upb/json/parser.rl" + { p--; {cs = stack[--top]; goto _again;} } break; case 10: -#line 1256 "upb/json/parser.rl" - { p--; {stack[top++] = cs; cs = 27; goto _again;} } +#line 1533 "upb/json/parser.rl" + { {stack[top++] = cs; cs = 33; goto _again;} } break; case 11: -#line 1261 "upb/json/parser.rl" - { start_member(parser); } +#line 1535 "upb/json/parser.rl" + { p--; {stack[top++] = cs; cs = 41; goto _again;} } break; case 12: -#line 1262 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_membername(parser)); } +#line 1540 "upb/json/parser.rl" + { start_member(parser); } break; case 13: -#line 1265 "upb/json/parser.rl" - { end_member(parser); } +#line 1541 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_membername(parser)); } break; case 14: -#line 1271 "upb/json/parser.rl" - { start_object(parser); } +#line 1544 "upb/json/parser.rl" + { end_member(parser); } break; case 15: -#line 1274 "upb/json/parser.rl" - { end_object(parser); } +#line 1550 "upb/json/parser.rl" + { start_object(parser); } break; case 16: -#line 1280 "upb/json/parser.rl" - { CHECK_RETURN_TOP(start_array(parser)); } +#line 1553 "upb/json/parser.rl" + { end_object(parser); } break; case 17: -#line 1284 "upb/json/parser.rl" - { end_array(parser); } +#line 1559 "upb/json/parser.rl" + { CHECK_RETURN_TOP(start_array(parser)); } break; case 18: -#line 1289 "upb/json/parser.rl" - { start_number(parser, p); } +#line 1563 "upb/json/parser.rl" + { end_array(parser); } break; case 19: -#line 1290 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_number(parser, p)); } +#line 1568 "upb/json/parser.rl" + { CHECK_RETURN_TOP(start_number(parser, p)); } break; case 20: -#line 1292 "upb/json/parser.rl" - { CHECK_RETURN_TOP(start_stringval(parser)); } +#line 1569 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 21: -#line 1293 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_stringval(parser)); } +#line 1571 "upb/json/parser.rl" + { CHECK_RETURN_TOP(start_stringval(parser)); } break; case 22: -#line 1295 "upb/json/parser.rl" - { CHECK_RETURN_TOP(parser_putbool(parser, true)); } +#line 1572 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_stringval(parser)); } break; case 23: -#line 1297 "upb/json/parser.rl" - { CHECK_RETURN_TOP(parser_putbool(parser, false)); } +#line 1574 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 24: -#line 1299 "upb/json/parser.rl" - { /* null value */ } +#line 1576 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 25: -#line 1301 "upb/json/parser.rl" - { CHECK_RETURN_TOP(start_subobject(parser)); } +#line 1578 "upb/json/parser.rl" + { /* null value */ } break; case 26: -#line 1302 "upb/json/parser.rl" - { end_subobject(parser); } +#line 1580 "upb/json/parser.rl" + { CHECK_RETURN_TOP(start_subobject(parser)); } break; case 27: -#line 1307 "upb/json/parser.rl" +#line 1581 "upb/json/parser.rl" + { end_subobject(parser); } + break; + case 28: +#line 1586 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -#line 1579 "upb/json/parser.c" +#line 1898 "upb/json/parser.c" } } @@ -15833,10 +16152,47 @@ _again: if ( ++p != pe ) goto _resume; _test_eof: {} + if ( p == eof ) + { + const char *__acts = _json_actions + _json_eof_actions[cs]; + unsigned int __nacts = (unsigned int) *__acts++; + while ( __nacts-- > 0 ) { + switch ( *__acts++ ) { + case 0: +#line 1499 "upb/json/parser.rl" + { + p--; {cs = stack[--top]; goto _again;} + } + break; + case 20: +#line 1569 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_number(parser, p)); } + break; + case 23: +#line 1574 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_bool(parser, true)); } + break; + case 24: +#line 1576 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_bool(parser, false)); } + break; + case 25: +#line 1578 "upb/json/parser.rl" + { /* null value */ } + break; + case 27: +#line 1581 "upb/json/parser.rl" + { end_subobject(parser); } + break; +#line 1940 "upb/json/parser.c" + } + } + } + _out: {} } -#line 1334 "upb/json/parser.rl" +#line 1614 "upb/json/parser.rl" if (p != pe) { upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p); @@ -15854,8 +16210,7 @@ error: } bool end(void *closure, const void *hd) { - UPB_UNUSED(closure); - UPB_UNUSED(hd); + upb_json_parser *parser = closure; /* Prevent compile warning on unused static constants. */ UPB_UNUSED(json_start); @@ -15863,7 +16218,14 @@ bool end(void *closure, const void *hd) { UPB_UNUSED(json_en_string_machine); UPB_UNUSED(json_en_value_machine); UPB_UNUSED(json_en_main); - return true; + + parse(parser, hd, &eof_ch, 0, NULL); + + return parser->current_state >= +#line 1978 "upb/json/parser.c" +70 +#line 1642 "upb/json/parser.rl" +; } static void json_parser_reset(upb_json_parser *p) { @@ -15877,13 +16239,13 @@ static void json_parser_reset(upb_json_parser *p) { /* Emit Ragel initialization of the parser. */ -#line 1633 "upb/json/parser.c" +#line 1995 "upb/json/parser.c" { cs = json_start; top = 0; } -#line 1374 "upb/json/parser.rl" +#line 1656 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); @@ -15970,7 +16332,8 @@ static void add_jsonname_table(upb_json_parsermethod *m, const upb_msgdef* md) { upb_json_parser *upb_json_parser_create(upb_env *env, const upb_json_parsermethod *method, - upb_sink *output) { + upb_sink *output, + bool ignore_json_unknown) { #ifndef NDEBUG const size_t size_before = upb_env_bytesallocated(env); #endif @@ -15989,6 +16352,8 @@ upb_json_parser *upb_json_parser_create(upb_env *env, p->top->m = upb_handlers_msgdef(output->handlers); set_name_table(p, p->top); + p->ignore_json_unknown = ignore_json_unknown; + /* If this fails, uncomment and increase the value in parser.h. */ /* fprintf(stderr, "%zd\n", upb_env_bytesallocated(env) - size_before); */ UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(env) - size_before <= diff --git a/php/ext/google/protobuf/upb.h b/php/ext/google/protobuf/upb.h index a263db30..180f4715 100644 --- a/php/ext/google/protobuf/upb.h +++ b/php/ext/google/protobuf/upb.h @@ -9457,7 +9457,7 @@ UPB_DECLARE_DERIVED_TYPE(upb::json::ParserMethod, upb::RefCounted, class upb::json::Parser { public: static Parser* Create(Environment* env, const ParserMethod* method, - Sink* output); + Sink* output, bool ignore_json_unknown); BytesSink* input(); @@ -9491,7 +9491,8 @@ UPB_BEGIN_EXTERN_C upb_json_parser* upb_json_parser_create(upb_env* e, const upb_json_parsermethod* m, - upb_sink* output); + upb_sink* output, + bool ignore_json_unknown); upb_bytessink *upb_json_parser_input(upb_json_parser *p); upb_json_parsermethod* upb_json_parsermethod_new(const upb_msgdef* md, @@ -9511,8 +9512,8 @@ UPB_END_EXTERN_C namespace upb { namespace json { inline Parser* Parser::Create(Environment* env, const ParserMethod* method, - Sink* output) { - return upb_json_parser_create(env, method, output); + Sink* output, bool ignore_json_unknown) { + return upb_json_parser_create(env, method, output, ignore_json_unknown); } inline BytesSink* Parser::input() { return upb_json_parser_input(this); diff --git a/php/src/Google/Protobuf/Internal/DescriptorProto_ExtensionRange.php b/php/src/Google/Protobuf/Internal/DescriptorProto_ExtensionRange.php new file mode 100644 index 00000000..c928fbe5 --- /dev/null +++ b/php/src/Google/Protobuf/Internal/DescriptorProto_ExtensionRange.php @@ -0,0 +1,16 @@ +<?php +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: google/protobuf/descriptor.proto + +namespace Google\Protobuf\Internal; + +if (false) { + /** + * This class is deprecated. Use Google\Protobuf\Internal\DescriptorProto\ExtensionRange instead. + * @deprecated + */ + class DescriptorProto_ExtensionRange {} +} +class_exists(DescriptorProto\ExtensionRange::class); +@trigger_error('Google\Protobuf\Internal\DescriptorProto_ExtensionRange is deprecated and will be removed in the next major release. Use Google\Protobuf\Internal\DescriptorProto\ExtensionRange instead', E_USER_DEPRECATED); + diff --git a/php/src/Google/Protobuf/Internal/DescriptorProto_ReservedRange.php b/php/src/Google/Protobuf/Internal/DescriptorProto_ReservedRange.php new file mode 100644 index 00000000..e49e945f --- /dev/null +++ b/php/src/Google/Protobuf/Internal/DescriptorProto_ReservedRange.php @@ -0,0 +1,16 @@ +<?php +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: google/protobuf/descriptor.proto + +namespace Google\Protobuf\Internal; + +if (false) { + /** + * This class is deprecated. Use Google\Protobuf\Internal\DescriptorProto\ReservedRange instead. + * @deprecated + */ + class DescriptorProto_ReservedRange {} +} +class_exists(DescriptorProto\ReservedRange::class); +@trigger_error('Google\Protobuf\Internal\DescriptorProto_ReservedRange is deprecated and will be removed in the next major release. Use Google\Protobuf\Internal\DescriptorProto\ReservedRange instead', E_USER_DEPRECATED); + diff --git a/php/src/Google/Protobuf/Internal/EnumDescriptorProto_EnumReservedRange.php b/php/src/Google/Protobuf/Internal/EnumDescriptorProto_EnumReservedRange.php new file mode 100644 index 00000000..b1b59ed9 --- /dev/null +++ b/php/src/Google/Protobuf/Internal/EnumDescriptorProto_EnumReservedRange.php @@ -0,0 +1,16 @@ +<?php +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: google/protobuf/descriptor.proto + +namespace Google\Protobuf\Internal; + +if (false) { + /** + * This class is deprecated. Use Google\Protobuf\Internal\EnumDescriptorProto\EnumReservedRange instead. + * @deprecated + */ + class EnumDescriptorProto_EnumReservedRange {} +} +class_exists(EnumDescriptorProto\EnumReservedRange::class); +@trigger_error('Google\Protobuf\Internal\EnumDescriptorProto_EnumReservedRange is deprecated and will be removed in the next major release. Use Google\Protobuf\Internal\EnumDescriptorProto\EnumReservedRange instead', E_USER_DEPRECATED); + diff --git a/php/src/Google/Protobuf/Internal/FieldDescriptorProto_Label.php b/php/src/Google/Protobuf/Internal/FieldDescriptorProto_Label.php new file mode 100644 index 00000000..218a846e --- /dev/null +++ b/php/src/Google/Protobuf/Internal/FieldDescriptorProto_Label.php @@ -0,0 +1,16 @@ +<?php +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: google/protobuf/descriptor.proto + +namespace Google\Protobuf\Internal; + +if (false) { + /** + * This class is deprecated. Use Google\Protobuf\Internal\FieldDescriptorProto\Label instead. + * @deprecated + */ + class FieldDescriptorProto_Label {} +} +class_exists(FieldDescriptorProto\Label::class); +@trigger_error('Google\Protobuf\Internal\FieldDescriptorProto_Label is deprecated and will be removed in the next major release. Use Google\Protobuf\Internal\FieldDescriptorProto\Label instead', E_USER_DEPRECATED); + diff --git a/php/src/Google/Protobuf/Internal/FieldDescriptorProto_Type.php b/php/src/Google/Protobuf/Internal/FieldDescriptorProto_Type.php new file mode 100644 index 00000000..fd8d449a --- /dev/null +++ b/php/src/Google/Protobuf/Internal/FieldDescriptorProto_Type.php @@ -0,0 +1,16 @@ +<?php +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: google/protobuf/descriptor.proto + +namespace Google\Protobuf\Internal; + +if (false) { + /** + * This class is deprecated. Use Google\Protobuf\Internal\FieldDescriptorProto\Type instead. + * @deprecated + */ + class FieldDescriptorProto_Type {} +} +class_exists(FieldDescriptorProto\Type::class); +@trigger_error('Google\Protobuf\Internal\FieldDescriptorProto_Type is deprecated and will be removed in the next major release. Use Google\Protobuf\Internal\FieldDescriptorProto\Type instead', E_USER_DEPRECATED); + diff --git a/php/src/Google/Protobuf/Internal/FieldOptions_CType.php b/php/src/Google/Protobuf/Internal/FieldOptions_CType.php new file mode 100644 index 00000000..4d18783e --- /dev/null +++ b/php/src/Google/Protobuf/Internal/FieldOptions_CType.php @@ -0,0 +1,16 @@ +<?php +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: google/protobuf/descriptor.proto + +namespace Google\Protobuf\Internal; + +if (false) { + /** + * This class is deprecated. Use Google\Protobuf\Internal\FieldOptions\CType instead. + * @deprecated + */ + class FieldOptions_CType {} +} +class_exists(FieldOptions\CType::class); +@trigger_error('Google\Protobuf\Internal\FieldOptions_CType is deprecated and will be removed in the next major release. Use Google\Protobuf\Internal\FieldOptions\CType instead', E_USER_DEPRECATED); + diff --git a/php/src/Google/Protobuf/Internal/FieldOptions_JSType.php b/php/src/Google/Protobuf/Internal/FieldOptions_JSType.php new file mode 100644 index 00000000..9db07822 --- /dev/null +++ b/php/src/Google/Protobuf/Internal/FieldOptions_JSType.php @@ -0,0 +1,16 @@ +<?php +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: google/protobuf/descriptor.proto + +namespace Google\Protobuf\Internal; + +if (false) { + /** + * This class is deprecated. Use Google\Protobuf\Internal\FieldOptions\JSType instead. + * @deprecated + */ + class FieldOptions_JSType {} +} +class_exists(FieldOptions\JSType::class); +@trigger_error('Google\Protobuf\Internal\FieldOptions_JSType is deprecated and will be removed in the next major release. Use Google\Protobuf\Internal\FieldOptions\JSType instead', E_USER_DEPRECATED); + diff --git a/php/src/Google/Protobuf/Internal/FileOptions_OptimizeMode.php b/php/src/Google/Protobuf/Internal/FileOptions_OptimizeMode.php new file mode 100644 index 00000000..8926e63b --- /dev/null +++ b/php/src/Google/Protobuf/Internal/FileOptions_OptimizeMode.php @@ -0,0 +1,16 @@ +<?php +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: google/protobuf/descriptor.proto + +namespace Google\Protobuf\Internal; + +if (false) { + /** + * This class is deprecated. Use Google\Protobuf\Internal\FileOptions\OptimizeMode instead. + * @deprecated + */ + class FileOptions_OptimizeMode {} +} +class_exists(FileOptions\OptimizeMode::class); +@trigger_error('Google\Protobuf\Internal\FileOptions_OptimizeMode is deprecated and will be removed in the next major release. Use Google\Protobuf\Internal\FileOptions\OptimizeMode instead', E_USER_DEPRECATED); + diff --git a/php/src/Google/Protobuf/Internal/GPBJsonWire.php b/php/src/Google/Protobuf/Internal/GPBJsonWire.php index 9ae57ab3..43f4745d 100644 --- a/php/src/Google/Protobuf/Internal/GPBJsonWire.php +++ b/php/src/Google/Protobuf/Internal/GPBJsonWire.php @@ -226,7 +226,7 @@ class GPBJsonWire $output->writeRaw("\"", 1); break; case GPBType::STRING: - $value = json_encode($value); + $value = json_encode($value, JSON_UNESCAPED_UNICODE); $output->writeRaw($value, strlen($value)); break; // case GPBType::GROUP: diff --git a/php/src/Google/Protobuf/Internal/GeneratedCodeInfo_Annotation.php b/php/src/Google/Protobuf/Internal/GeneratedCodeInfo_Annotation.php new file mode 100644 index 00000000..e36f1e57 --- /dev/null +++ b/php/src/Google/Protobuf/Internal/GeneratedCodeInfo_Annotation.php @@ -0,0 +1,16 @@ +<?php +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: google/protobuf/descriptor.proto + +namespace Google\Protobuf\Internal; + +if (false) { + /** + * This class is deprecated. Use Google\Protobuf\Internal\GeneratedCodeInfo\Annotation instead. + * @deprecated + */ + class GeneratedCodeInfo_Annotation {} +} +class_exists(GeneratedCodeInfo\Annotation::class); +@trigger_error('Google\Protobuf\Internal\GeneratedCodeInfo_Annotation is deprecated and will be removed in the next major release. Use Google\Protobuf\Internal\GeneratedCodeInfo\Annotation instead', E_USER_DEPRECATED); + diff --git a/php/src/Google/Protobuf/Internal/Message.php b/php/src/Google/Protobuf/Internal/Message.php index 73ac375e..8ff141ca 100644 --- a/php/src/Google/Protobuf/Internal/Message.php +++ b/php/src/Google/Protobuf/Internal/Message.php @@ -1563,7 +1563,7 @@ class Message } break; case GPBType::STRING: - $value = json_encode($value); + $value = json_encode($value, JSON_UNESCAPED_UNICODE); $size += strlen($value); break; case GPBType::BYTES: diff --git a/php/src/Google/Protobuf/Internal/MethodOptions_IdempotencyLevel.php b/php/src/Google/Protobuf/Internal/MethodOptions_IdempotencyLevel.php new file mode 100644 index 00000000..a2913114 --- /dev/null +++ b/php/src/Google/Protobuf/Internal/MethodOptions_IdempotencyLevel.php @@ -0,0 +1,16 @@ +<?php +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: google/protobuf/descriptor.proto + +namespace Google\Protobuf\Internal; + +if (false) { + /** + * This class is deprecated. Use Google\Protobuf\Internal\MethodOptions\IdempotencyLevel instead. + * @deprecated + */ + class MethodOptions_IdempotencyLevel {} +} +class_exists(MethodOptions\IdempotencyLevel::class); +@trigger_error('Google\Protobuf\Internal\MethodOptions_IdempotencyLevel is deprecated and will be removed in the next major release. Use Google\Protobuf\Internal\MethodOptions\IdempotencyLevel instead', E_USER_DEPRECATED); + diff --git a/php/src/Google/Protobuf/Internal/SourceCodeInfo_Location.php b/php/src/Google/Protobuf/Internal/SourceCodeInfo_Location.php new file mode 100644 index 00000000..1346492d --- /dev/null +++ b/php/src/Google/Protobuf/Internal/SourceCodeInfo_Location.php @@ -0,0 +1,16 @@ +<?php +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: google/protobuf/descriptor.proto + +namespace Google\Protobuf\Internal; + +if (false) { + /** + * This class is deprecated. Use Google\Protobuf\Internal\SourceCodeInfo\Location instead. + * @deprecated + */ + class SourceCodeInfo_Location {} +} +class_exists(SourceCodeInfo\Location::class); +@trigger_error('Google\Protobuf\Internal\SourceCodeInfo_Location is deprecated and will be removed in the next major release. Use Google\Protobuf\Internal\SourceCodeInfo\Location instead', E_USER_DEPRECATED); + diff --git a/php/src/Google/Protobuf/Internal/UninterpretedOption_NamePart.php b/php/src/Google/Protobuf/Internal/UninterpretedOption_NamePart.php new file mode 100644 index 00000000..9750eb01 --- /dev/null +++ b/php/src/Google/Protobuf/Internal/UninterpretedOption_NamePart.php @@ -0,0 +1,16 @@ +<?php +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: google/protobuf/descriptor.proto + +namespace Google\Protobuf\Internal; + +if (false) { + /** + * This class is deprecated. Use Google\Protobuf\Internal\UninterpretedOption\NamePart instead. + * @deprecated + */ + class UninterpretedOption_NamePart {} +} +class_exists(UninterpretedOption\NamePart::class); +@trigger_error('Google\Protobuf\Internal\UninterpretedOption_NamePart is deprecated and will be removed in the next major release. Use Google\Protobuf\Internal\UninterpretedOption\NamePart instead', E_USER_DEPRECATED); + diff --git a/php/src/Google/Protobuf/Timestamp.php b/php/src/Google/Protobuf/Timestamp.php index e21f3231..6d26f6c5 100644 --- a/php/src/Google/Protobuf/Timestamp.php +++ b/php/src/Google/Protobuf/Timestamp.php @@ -55,7 +55,9 @@ use Google\Protobuf\Internal\GPBUtil; * {hour}, {min}, and {sec} are zero-padded to two digits each. The fractional * seconds, which can go up to 9 digits (i.e. up to 1 nanosecond resolution), * are optional. The "Z" suffix indicates the timezone ("UTC"); the timezone - * is required, though only UTC (as indicated by "Z") is presently supported. + * is required. A proto3 JSON serializer should always use UTC (as indicated by + * "Z") when printing the Timestamp type and a proto3 JSON parser should be + * able to accept both UTC and other timezones (as indicated by an offset). * For example, "2017-01-15T01:30:15.01Z" encodes 15.01 seconds past * 01:30 UTC on January 15, 2017. * In JavaScript, one can convert a Date object to this format using the @@ -64,8 +66,8 @@ use Google\Protobuf\Internal\GPBUtil; * to this format using [`strftime`](https://docs.python.org/2/library/time.html#time.strftime) * with the time format spec '%Y-%m-%dT%H:%M:%S.%fZ'. Likewise, in Java, one * can use the Joda Time's [`ISODateTimeFormat.dateTime()`]( - * http://www.joda.org/joda-time/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTime--) - * to obtain a formatter capable of generating timestamps in this format. + * http://www.joda.org/joda-time/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTime-- + * ) to obtain a formatter capable of generating timestamps in this format. * * Generated from protobuf message <code>google.protobuf.Timestamp</code> */ @@ -180,18 +182,19 @@ class Timestamp extends \Google\Protobuf\Internal\Message */ public function fromDateTime(\DateTime $datetime) { - $this->seconds = $datetime->format('U'); - $this->nanos = 0; + $this->seconds = $datetime->getTimestamp(); + $this->nanos = 1000 * $datetime->format('u'); } /** - * Converts Timestamp to PHP DateTime. Nano second is ignored. + * Converts Timestamp to PHP DateTime. * * @return \DateTime $datetime */ public function toDateTime() { - return \DateTime::createFromFormat('U', $this->seconds); + $time = sprintf('%s.%06d', $this->seconds, $this->nanos / 1000); + return \DateTime::createFromFormat('U.u', $time); } } diff --git a/php/tests/compatibility_test.sh b/php/tests/compatibility_test.sh index b377d85c..dc8de5dc 100755 --- a/php/tests/compatibility_test.sh +++ b/php/tests/compatibility_test.sh @@ -92,7 +92,7 @@ echo "Running compatibility tests between $VERSION_NUMBER and $OLD_VERSION" # Download old test. rm -rf protobuf -git clone https://github.com/google/protobuf.git +git clone https://github.com/protocolbuffers/protobuf.git pushd protobuf git checkout v$OLD_VERSION popd @@ -124,6 +124,7 @@ sed -i.bak '/php_implementation_test.php/d' phpunit.xml sed -i.bak '/generated_phpdoc_test.php/d' phpunit.xml sed -i.bak 's/generated_phpdoc_test.php//g' tests/test.sh sed -i.bak '/memory_leak_test.php/d' tests/test.sh +sed -i.bak '/^ public function testTimestamp()$/,/^ }$/d' tests/well_known_test.php for t in "${tests[@]}" do remove_error_test tests/$t diff --git a/php/tests/encode_decode_test.php b/php/tests/encode_decode_test.php index d36b883e..870dacab 100644 --- a/php/tests/encode_decode_test.php +++ b/php/tests/encode_decode_test.php @@ -11,9 +11,90 @@ use Foo\TestMessage\Sub; use Foo\TestPackedMessage; use Foo\TestRandomFieldOrder; use Foo\TestUnpackedMessage; +use Google\Protobuf\DoubleValue; +use Google\Protobuf\FloatValue; +use Google\Protobuf\Int32Value; +use Google\Protobuf\UInt32Value; +use Google\Protobuf\Int64Value; +use Google\Protobuf\UInt64Value; +use Google\Protobuf\BoolValue; +use Google\Protobuf\StringValue; +use Google\Protobuf\BytesValue; class EncodeDecodeTest extends TestBase { + public function testDecodeJsonSimple() + { + $m = new TestMessage(); + $m->mergeFromJsonString("{\"optionalInt32\":1}"); + } + + public function testDecodeTopLevelBoolValue() + { + $m = new BoolValue(); + + $m->mergeFromJsonString("true"); + $this->assertEquals(true, $m->getValue()); + + $m->mergeFromJsonString("false"); + $this->assertEquals(false, $m->getValue()); + } + + public function testDecodeTopLevelDoubleValue() + { + $m = new DoubleValue(); + $m->mergeFromJsonString("1.5"); + $this->assertEquals(1.5, $m->getValue()); + } + + public function testDecodeTopLevelFloatValue() + { + $m = new FloatValue(); + $m->mergeFromJsonString("1.5"); + $this->assertEquals(1.5, $m->getValue()); + } + + public function testDecodeTopLevelInt32Value() + { + $m = new Int32Value(); + $m->mergeFromJsonString("1"); + $this->assertEquals(1, $m->getValue()); + } + + public function testDecodeTopLevelUInt32Value() + { + $m = new UInt32Value(); + $m->mergeFromJsonString("1"); + $this->assertEquals(1, $m->getValue()); + } + + public function testDecodeTopLevelInt64Value() + { + $m = new Int64Value(); + $m->mergeFromJsonString("1"); + $this->assertEquals(1, $m->getValue()); + } + + public function testDecodeTopLevelUInt64Value() + { + $m = new UInt64Value(); + $m->mergeFromJsonString("1"); + $this->assertEquals(1, $m->getValue()); + } + + public function testDecodeTopLevelStringValue() + { + $m = new StringValue(); + $m->mergeFromJsonString("\"a\""); + $this->assertSame("a", $m->getValue()); + } + + public function testDecodeTopLevelBytesValue() + { + $m = new BytesValue(); + $m->mergeFromJsonString("\"YQ==\""); + $this->assertSame("a", $m->getValue()); + } public function testEncode() { diff --git a/php/tests/memory_leak_test.php b/php/tests/memory_leak_test.php index 4e3874b7..f3bcb963 100644 --- a/php/tests/memory_leak_test.php +++ b/php/tests/memory_leak_test.php @@ -152,7 +152,7 @@ date_default_timezone_set('UTC'); $from = new DateTime('2011-01-01T15:03:01.012345UTC'); $timestamp->fromDateTime($from); assert($from->format('U') == $timestamp->getSeconds()); -assert(0 == $timestamp->getNanos()); +assert(1000 * $from->format('u') == $timestamp->getNanos()); $to = $timestamp->toDateTime(); assert(\DateTime::class == get_class($to)); diff --git a/php/tests/undefined_test.php b/php/tests/undefined_test.php index f8444571..935d8be7 100644 --- a/php/tests/undefined_test.php +++ b/php/tests/undefined_test.php @@ -909,7 +909,7 @@ class UndefinedTest extends PHPUnit_Framework_TestCase /** * @expectedException PHPUnit_Framework_Error */ - public function testMessageSetNullFail() + public function testMessageSetNullFailMap() { $arr = new MapField(GPBType::INT32, GPBType::MESSAGE, TestMessage::class); diff --git a/php/tests/well_known_test.php b/php/tests/well_known_test.php index 1e8c4f42..6a788df2 100644 --- a/php/tests/well_known_test.php +++ b/php/tests/well_known_test.php @@ -42,6 +42,7 @@ class WellKnownTest extends TestBase { public function testEmpty() { $msg = new GPBEmpty(); + $this->assertTrue($msg instanceof \Google\Protobuf\Internal\Message); } public function testImportDescriptorProto() @@ -312,11 +313,12 @@ class WellKnownTest extends TestBase { $from = new DateTime('2011-01-01T15:03:01.012345UTC'); $timestamp->fromDateTime($from); $this->assertEquals($from->format('U'), $timestamp->getSeconds()); - $this->assertSame(0, $timestamp->getNanos()); + $this->assertEquals(1000 * $from->format('u'), $timestamp->getNanos()); $to = $timestamp->toDateTime(); $this->assertSame(\DateTime::class, get_class($to)); $this->assertSame($from->format('U'), $to->format('U')); + $this->assertSame($from->format('u'), $to->format('u')); } public function testType() |