diff --git a/conformance/failure_list_php_c.txt b/conformance/failure_list_php_c.txt index 8053d91ac..dd69f5eaf 100644 --- a/conformance/failure_list_php_c.txt +++ b/conformance/failure_list_php_c.txt @@ -22,26 +22,8 @@ Required.DurationProtoInputTooLarge.JsonOutput Required.DurationProtoInputTooSmall.JsonOutput Required.TimestampProtoInputTooLarge.JsonOutput Required.TimestampProtoInputTooSmall.JsonOutput -Required.Proto3.JsonInput.Any.JsonOutput -Required.Proto3.JsonInput.Any.ProtobufOutput -Required.Proto3.JsonInput.AnyNested.JsonOutput -Required.Proto3.JsonInput.AnyNested.ProtobufOutput -Required.Proto3.JsonInput.AnyUnorderedTypeTag.JsonOutput -Required.Proto3.JsonInput.AnyUnorderedTypeTag.ProtobufOutput -Required.Proto3.JsonInput.AnyWithDuration.JsonOutput -Required.Proto3.JsonInput.AnyWithDuration.ProtobufOutput Required.Proto3.JsonInput.AnyWithFieldMask.JsonOutput Required.Proto3.JsonInput.AnyWithFieldMask.ProtobufOutput -Required.Proto3.JsonInput.AnyWithInt32ValueWrapper.JsonOutput -Required.Proto3.JsonInput.AnyWithInt32ValueWrapper.ProtobufOutput -Required.Proto3.JsonInput.AnyWithStruct.JsonOutput -Required.Proto3.JsonInput.AnyWithStruct.ProtobufOutput -Required.Proto3.JsonInput.AnyWithTimestamp.JsonOutput -Required.Proto3.JsonInput.AnyWithTimestamp.ProtobufOutput -Required.Proto3.JsonInput.AnyWithValueForInteger.JsonOutput -Required.Proto3.JsonInput.AnyWithValueForInteger.ProtobufOutput -Required.Proto3.JsonInput.AnyWithValueForJsonObject.JsonOutput -Required.Proto3.JsonInput.AnyWithValueForJsonObject.ProtobufOutput Required.Proto3.JsonInput.BoolMapField.JsonOutput Required.Proto3.JsonInput.DoubleFieldMaxNegativeValue.JsonOutput Required.Proto3.JsonInput.DoubleFieldMaxNegativeValue.ProtobufOutput diff --git a/php/ext/google/protobuf/encode_decode.c b/php/ext/google/protobuf/encode_decode.c index 2bb5f5217..131367662 100644 --- a/php/ext/google/protobuf/encode_decode.c +++ b/php/ext/google/protobuf/encode_decode.c @@ -1095,7 +1095,10 @@ static const upb_json_parsermethod *msgdef_jsonparsermethod(Descriptor* desc) { static void putmsg(zval* msg, const Descriptor* desc, upb_sink* sink, int depth, bool is_json TSRMLS_DC); static void putrawmsg(MessageHeader* msg, const Descriptor* desc, - upb_sink* sink, int depth, bool is_json TSRMLS_DC); + upb_sink* sink, int depth, bool is_json, + bool open_msg TSRMLS_DC); +static void putjsonany(MessageHeader* msg, const Descriptor* desc, + upb_sink* sink, int depth TSRMLS_DC); static void putstr(zval* str, const upb_fielddef* f, upb_sink* sink, bool force_default); @@ -1245,15 +1248,114 @@ static void putmap(zval* map, const upb_fielddef* f, upb_sink* sink, static void putmsg(zval* msg_php, const Descriptor* desc, upb_sink* sink, int depth, bool is_json TSRMLS_DC) { MessageHeader* msg = UNBOX(MessageHeader, msg_php); - putrawmsg(msg, desc, sink, depth, is_json TSRMLS_CC); + putrawmsg(msg, desc, sink, depth, is_json, true TSRMLS_CC); +} + +static const upb_handlers* msgdef_json_serialize_handlers( + Descriptor* desc, bool preserve_proto_fieldnames); + +static void putjsonany(MessageHeader* msg, const Descriptor* desc, + upb_sink* sink, int depth TSRMLS_DC) { + upb_status status; + const upb_fielddef* type_field = upb_msgdef_itof(desc->msgdef, UPB_ANY_TYPE); + const upb_fielddef* value_field = upb_msgdef_itof(desc->msgdef, UPB_ANY_VALUE); + + uint32_t type_url_offset; + zval* type_url_php_str; + const upb_msgdef *payload_type = NULL; + + upb_sink_startmsg(sink); + + /* Handle type url */ + type_url_offset = desc->layout->fields[upb_fielddef_index(type_field)].offset; + type_url_php_str = CACHED_PTR_TO_ZVAL_PTR( + DEREF(message_data(msg), type_url_offset, CACHED_VALUE*)); + if (Z_STRLEN_P(type_url_php_str) > 0) { + putstr(type_url_php_str, type_field, sink, false); + } + + { + const char* type_url_str = Z_STRVAL_P(type_url_php_str); + size_t type_url_len = Z_STRLEN_P(type_url_php_str); + if (type_url_len <= 20 || + strncmp(type_url_str, "type.googleapis.com/", 20) != 0) { + zend_error(E_ERROR, "Invalid type url: %s", type_url_str); + } + + /* Resolve type url */ + type_url_str += 20; + type_url_len -= 20; + + payload_type = upb_symtab_lookupmsg2( + generated_pool->symtab, type_url_str, type_url_len); + if (payload_type == NULL) { + zend_error(E_ERROR, "Unknown type: %s", type_url_str); + return; + } + } + + { + uint32_t value_offset; + zval* value_php_str; + const char* value_str; + size_t value_len; + + value_offset = desc->layout->fields[upb_fielddef_index(value_field)].offset; + value_php_str = CACHED_PTR_TO_ZVAL_PTR( + DEREF(message_data(msg), value_offset, CACHED_VALUE*)); + value_str = Z_STRVAL_P(value_php_str); + value_len = Z_STRLEN_P(value_php_str); + + if (value_len > 0) { + Descriptor* payload_desc = + UNBOX_HASHTABLE_VALUE(Descriptor, get_def_obj((void*)payload_type)); + zend_class_entry* payload_klass = payload_desc->klass; + zval val; + upb_sink subsink; + bool is_wellknown; + + /* Create message of the payload type. */ + ZVAL_OBJ(&val, payload_klass->create_object(payload_klass TSRMLS_CC)); + MessageHeader* intern = UNBOX(MessageHeader, &val); + custom_data_init(payload_klass, intern PHP_PROTO_TSRMLS_CC); + + merge_from_string(value_str, value_len, payload_desc, intern); + + is_wellknown = + upb_msgdef_wellknowntype(payload_desc->msgdef) != + UPB_WELLKNOWN_UNSPECIFIED; + if (is_wellknown) { + upb_sink_startstr(sink, getsel(value_field, UPB_HANDLER_STARTSTR), 0, + &subsink); + } + + subsink.handlers = + msgdef_json_serialize_handlers(payload_desc, true); + subsink.closure = sink->closure; + putrawmsg(intern, payload_desc, &subsink, depth, true, + is_wellknown TSRMLS_CC); + + zval_dtor(&val); + } + } + + upb_sink_endmsg(sink, &status); } static void putrawmsg(MessageHeader* msg, const Descriptor* desc, - upb_sink* sink, int depth, bool is_json TSRMLS_DC) { + upb_sink* sink, int depth, bool is_json, + bool open_msg TSRMLS_DC) { upb_msg_field_iter i; upb_status status; - upb_sink_startmsg(sink); + if (is_json && upb_msgdef_wellknowntype(desc->msgdef) == UPB_WELLKNOWN_ANY) { + putjsonany(msg, desc, sink, depth TSRMLS_CC); + return; + } + + if (open_msg) { + upb_sink_startmsg(sink); + } // Protect against cycles (possible because users may freely reassign message // and repeated fields) by imposing a maximum recursion depth. @@ -1343,7 +1445,9 @@ static void putrawmsg(MessageHeader* msg, const Descriptor* desc, upb_sink_putunknown(sink, unknown->ptr, unknown->len); } - upb_sink_endmsg(sink, &status); + if (open_msg) { + upb_sink_endmsg(sink, &status); + } } static void putstr(zval* str, const upb_fielddef *f, @@ -1409,7 +1513,7 @@ static void putrawsubmsg(MessageHeader* submsg, const upb_fielddef* f, UNBOX_HASHTABLE_VALUE(Descriptor, get_def_obj(upb_fielddef_msgsubdef(f))); upb_sink_startsubmsg(sink, getsel(f, UPB_HANDLER_STARTSUBMSG), &subsink); - putrawmsg(submsg, subdesc, &subsink, depth + 1, is_json TSRMLS_CC); + putrawmsg(submsg, subdesc, &subsink, depth + 1, is_json, true TSRMLS_CC); upb_sink_endsubmsg(sink, getsel(f, UPB_HANDLER_ENDSUBMSG)); } @@ -1633,7 +1737,8 @@ 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, ignore_json_unknown); + parser = upb_json_parser_create(&se.env, method, generated_pool->symtab, + &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/upb.c b/php/ext/google/protobuf/upb.c index 049e2bb06..6b576d856 100644 --- a/php/ext/google/protobuf/upb.c +++ b/php/ext/google/protobuf/upb.c @@ -1458,7 +1458,9 @@ static void assign_msg_wellknowntype(upb_msgdef *m) { m->well_known_type = UPB_WELLKNOWN_UNSPECIFIED; return; } - if (!strcmp(name, "google.protobuf.Duration")) { + if (!strcmp(name, "google.protobuf.Any")) { + m->well_known_type = UPB_WELLKNOWN_ANY; + } else if (!strcmp(name, "google.protobuf.Duration")) { m->well_known_type = UPB_WELLKNOWN_DURATION; } else if (!strcmp(name, "google.protobuf.Timestamp")) { m->well_known_type = UPB_WELLKNOWN_TIMESTAMP; @@ -3233,6 +3235,14 @@ const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym) { return def ? upb_dyncast_msgdef(def) : NULL; } +const upb_msgdef *upb_symtab_lookupmsg2(const upb_symtab *s, const char *sym, + size_t len) { + upb_value v; + upb_def *def = upb_strtable_lookup2(&s->symtab, sym, len, &v) ? + upb_value_getptr(v) : NULL; + return def ? upb_dyncast_msgdef(def) : NULL; +} + const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym) { upb_value v; upb_def *def = upb_strtable_lookup(&s->symtab, sym, &v) ? @@ -12546,7 +12556,7 @@ done: return r; } -//#line 1 "upb/json/parser.rl" +#line 1 "upb/json/parser.rl" /* ** upb::json::Parser (upb_json_parser) ** @@ -12623,6 +12633,9 @@ static void end_structvalue_object(upb_json_parser *p); static void start_object(upb_json_parser *p); static void end_object(upb_json_parser *p); +static void start_any_object(upb_json_parser *p, const char *ptr); +static bool end_any_object(upb_json_parser *p, const char *ptr); + static bool start_subobject(upb_json_parser *p); static void end_subobject(upb_json_parser *p); @@ -12630,8 +12643,89 @@ static void start_member(upb_json_parser *p); static void end_member(upb_json_parser *p); static bool end_membername(upb_json_parser *p); +static void start_any_member(upb_json_parser *p, const char *ptr); +static void end_any_member(upb_json_parser *p, const char *ptr); +static bool end_any_membername(upb_json_parser *p); + +size_t parse(void *closure, const void *hd, const char *buf, size_t size, + const upb_bufhandle *handle); +static bool end(void *closure, const void *hd); + static const char eof_ch = 'e'; +/* stringsink */ +typedef struct { + upb_byteshandler handler; + upb_bytessink sink; + char *ptr; + size_t len, size; +} upb_stringsink; + + +static void *stringsink_start(void *_sink, const void *hd, size_t size_hint) { + upb_stringsink *sink = _sink; + sink->len = 0; + UPB_UNUSED(hd); + UPB_UNUSED(size_hint); + return sink; +} + +static size_t stringsink_string(void *_sink, const void *hd, const char *ptr, + size_t len, const upb_bufhandle *handle) { + upb_stringsink *sink = _sink; + size_t new_size = sink->size; + + UPB_UNUSED(hd); + UPB_UNUSED(handle); + + while (sink->len + len > new_size) { + new_size *= 2; + } + + if (new_size != sink->size) { + sink->ptr = realloc(sink->ptr, new_size); + sink->size = new_size; + } + + memcpy(sink->ptr + sink->len, ptr, len); + sink->len += len; + + return len; +} + +void upb_stringsink_init(upb_stringsink *sink) { + upb_byteshandler_init(&sink->handler); + upb_byteshandler_setstartstr(&sink->handler, stringsink_start, NULL); + upb_byteshandler_setstring(&sink->handler, stringsink_string, NULL); + + upb_bytessink_reset(&sink->sink, &sink->handler, sink); + + sink->size = 32; + sink->ptr = malloc(sink->size); + sink->len = 0; +} + +void upb_stringsink_uninit(upb_stringsink *sink) { free(sink->ptr); } + +typedef struct { + /* For encoding Any value field in binary format. */ + const upb_handlers *encoder_handlers; + upb_pb_encoder *encoder; + upb_stringsink stringsink; + + /* For decoding Any value field in json format. */ + upb_json_parsermethod *parser_method; + upb_json_parser* parser; + upb_sink sink; + + /* Mark the range of uninterpreted values in json input before type url. */ + const char *before_type_url_start; + const char *before_type_url_end; + + /* Mark the range of uninterpreted values in json input after type url. */ + const char *after_type_url_start; +} upb_jsonparser_any_frame; + typedef struct { upb_sink sink; @@ -12660,6 +12754,15 @@ typedef struct { * message itself), not the parent's field that leads to this map. */ const upb_fielddef *mapfield; + /* We are in an Any message context. This flag is set when parsing the Any + * message and indicates to all field parsers (subobjects, strings, numbers, + * and bools) that the parsed field should be serialized as binary data or + * cached (type url not found yet). */ + bool is_any; + + /* The type of packed message in Any. */ + upb_jsonparser_any_frame *any_frame; + /* True if the field to be parsed is unknown. */ bool is_unknown_field; } upb_jsonparser_frame; @@ -12700,6 +12803,9 @@ struct upb_json_parser { /* Intermediate result of parsing a unicode escape sequence. */ uint32_t digit; + /* For resolve type url in Any. */ + const upb_symtab *symtab; + /* Whether to proceed if unknown field is met. */ bool ignore_json_unknown; @@ -12723,6 +12829,84 @@ struct upb_json_parsermethod { #define PARSER_CHECK_RETURN(x) if (!(x)) return false +static void json_parser_any_frame_reset(upb_jsonparser_any_frame *frame) { + frame->encoder_handlers = NULL; + frame->encoder = NULL; + frame->parser_method = NULL; + frame->parser = NULL; + frame->before_type_url_start = NULL; + frame->before_type_url_end = NULL; + frame->after_type_url_start = NULL; +} + +static void json_parser_any_frame_set_payload_type( + upb_json_parser *p, + upb_jsonparser_any_frame *frame, + const upb_msgdef *payload_type) { + /* Initialize encoder. */ + frame->encoder_handlers = + upb_pb_encoder_newhandlers(payload_type, &frame->encoder_handlers); + upb_stringsink_init(&frame->stringsink); + frame->encoder = + upb_pb_encoder_create( + p->env, frame->encoder_handlers, + &frame->stringsink.sink); + + /* Initialize parser. */ + frame->parser_method = + upb_json_parsermethod_new(payload_type, &frame->parser_method); + upb_sink_reset(&frame->sink, frame->encoder_handlers, frame->encoder); + frame->parser = + upb_json_parser_create(p->env, frame->parser_method, p->symtab, + &frame->sink, p->ignore_json_unknown); +} + +static void json_parser_any_frame_free(upb_jsonparser_any_frame *frame) { + upb_handlers_unref(frame->encoder_handlers, + &frame->encoder_handlers); + upb_json_parsermethod_unref(frame->parser_method, + &frame->parser_method); + upb_stringsink_uninit(&frame->stringsink); +} + +static bool json_parser_any_frame_has_type_url( + upb_jsonparser_any_frame *frame) { + return frame->encoder != NULL; +} + +static bool json_parser_any_frame_has_value_before_type_url( + upb_jsonparser_any_frame *frame) { + return frame->before_type_url_start != frame->before_type_url_end; +} + +static bool json_parser_any_frame_has_value_after_type_url( + upb_jsonparser_any_frame *frame) { + return frame->after_type_url_start != NULL; +} + +static bool json_parser_any_frame_has_value( + upb_jsonparser_any_frame *frame) { + return json_parser_any_frame_has_value_before_type_url(frame) || + json_parser_any_frame_has_value_after_type_url(frame); +} + +static void json_parser_any_frame_set_before_type_url_end( + upb_jsonparser_any_frame *frame, + const char *ptr) { + if (frame->encoder == NULL) { + frame->before_type_url_end = ptr; + } +} + +static void json_parser_any_frame_set_after_type_url_start_once( + upb_jsonparser_any_frame *frame, + const char *ptr) { + if (json_parser_any_frame_has_type_url(frame) && + frame->after_type_url_start == NULL) { + frame->after_type_url_start = ptr; + } +} + /* Used to signal that a capture has been suspended. */ static char suspend_capture; @@ -13526,6 +13710,11 @@ static bool end_null(upb_json_parser *p) { return true; } +static bool start_any_stringval(upb_json_parser *p) { + multipart_startaccum(p); + return true; +} + static bool start_stringval(upb_json_parser *p) { if (is_top_level(p)) { if (is_string_wrapper_object(p)) { @@ -13561,6 +13750,10 @@ static bool start_stringval(upb_json_parser *p) { return true; } + if (p->top->is_any) { + return start_any_stringval(p); + } + if (upb_fielddef_isstring(p->top->f)) { upb_jsonparser_frame *inner; upb_selector_t sel; @@ -13577,6 +13770,8 @@ static bool start_stringval(upb_json_parser *p) { inner->name_table = NULL; inner->is_map = false; inner->is_mapentry = false; + inner->is_any = false; + inner->any_frame = NULL; inner->is_unknown_field = false; p->top = inner; @@ -13608,6 +13803,50 @@ static bool start_stringval(upb_json_parser *p) { } } +static bool end_any_stringval(upb_json_parser *p) { + size_t len; + const char *buf = accumulate_getptr(p, &len); + + /* Set type_url */ + upb_selector_t sel; + upb_jsonparser_frame *inner; + if (!check_stack(p)) return false; + inner = p->top + 1; + + sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); + upb_sink_startstr(&p->top->sink, sel, 0, &inner->sink); + sel = getsel_for_handlertype(p, UPB_HANDLER_STRING); + upb_sink_putstring(&inner->sink, sel, buf, len, NULL); + sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); + upb_sink_endstr(&inner->sink, sel); + + multipart_end(p); + + /* Resolve type url */ + if (strncmp(buf, "type.googleapis.com/", 20) == 0 && len > 20) { + const upb_msgdef *payload_type = NULL; + buf += 20; + len -= 20; + + payload_type = upb_symtab_lookupmsg2(p->symtab, buf, len); + if (payload_type == NULL) { + upb_status_seterrf( + &p->status, "Cannot find packed type: %.*s\n", (int)len, buf); + upb_env_reporterror(p->env, &p->status); + return false; + } + + json_parser_any_frame_set_payload_type(p, p->top->any_frame, payload_type); + + return true; + } else { + upb_status_seterrf( + &p->status, "Invalid type url: %.*s\n", (int)len, buf); + upb_env_reporterror(p->env, &p->status); + return false; + } +} + static bool end_stringval_nontop(upb_json_parser *p) { bool ok = true; @@ -13622,6 +13861,10 @@ static bool end_stringval_nontop(upb_json_parser *p) { return true; } + if (p->top->is_any) { + return end_any_stringval(p); + } + switch (upb_fielddef_type(p->top->f)) { case UPB_TYPE_BYTES: if (!base64_push(p, getsel_for_handlertype(p, UPB_HANDLER_STRING), @@ -14069,6 +14312,8 @@ static bool handle_mapentry(upb_json_parser *p) { inner->name_table = NULL; inner->mapfield = mapfield; inner->is_map = false; + inner->is_any = false; + inner->any_frame = NULL; inner->is_unknown_field = false; /* Don't set this to true *yet* -- we reuse parsing handlers below to push @@ -14105,7 +14350,9 @@ static bool end_membername(upb_json_parser *p) { return true; } - if (p->top->is_map) { + if (p->top->is_any) { + return end_any_membername(p); + } else if (p->top->is_map) { return handle_mapentry(p); } else { size_t len; @@ -14129,6 +14376,23 @@ static bool end_membername(upb_json_parser *p) { } } +static bool end_any_membername(upb_json_parser *p) { + size_t len; + const char *buf = accumulate_getptr(p, &len); + upb_value v; + + if (len == 5 && strncmp(buf, "@type", len) == 0) { + upb_strtable_lookup2(p->top->name_table, "type_url", 8, &v); + p->top->f = upb_value_getconstptr(v); + multipart_end(p); + return true; + } else { + p->top->is_unknown_field = true; + multipart_end(p); + return true; + } +} + static void end_member(upb_json_parser *p) { /* If we just parsed a map-entry value, end that frame too. */ if (p->top->is_mapentry) { @@ -14153,6 +14417,16 @@ static void end_member(upb_json_parser *p) { p->top->is_unknown_field = false; } +static void start_any_member(upb_json_parser *p, const char *ptr) { + start_member(p); + json_parser_any_frame_set_after_type_url_start_once(p->top->any_frame, ptr); +} + +static void end_any_member(upb_json_parser *p, const char *ptr) { + json_parser_any_frame_set_before_type_url_end(p->top->any_frame, ptr); + end_member(p); +} + static bool start_subobject(upb_json_parser *p) { if (p->top->is_unknown_field) { upb_jsonparser_frame *inner; @@ -14163,6 +14437,8 @@ static bool start_subobject(upb_json_parser *p) { inner->f = NULL; inner->is_map = false; inner->is_mapentry = false; + inner->is_any = false; + inner->any_frame = NULL; inner->is_unknown_field = false; p->top = inner; return true; @@ -14185,6 +14461,8 @@ static bool start_subobject(upb_json_parser *p) { inner->f = NULL; inner->is_map = true; inner->is_mapentry = false; + inner->is_any = false; + inner->any_frame = NULL; inner->is_unknown_field = false; p->top = inner; @@ -14209,6 +14487,16 @@ static bool start_subobject(upb_json_parser *p) { inner->is_unknown_field = false; p->top = inner; + if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) { + p->top->is_any = true; + p->top->any_frame = + upb_env_malloc(p->env, sizeof(upb_jsonparser_any_frame)); + json_parser_any_frame_reset(p->top->any_frame); + } else { + p->top->is_any = false; + p->top->any_frame = NULL; + } + return true; } else { upb_status_seterrf(&p->status, @@ -14313,6 +14601,8 @@ static bool start_array(upb_json_parser *p) { inner->f = NULL; inner->is_map = false; inner->is_mapentry = false; + inner->is_any = false; + inner->any_frame = NULL; inner->is_unknown_field = true; p->top = inner; @@ -14337,6 +14627,8 @@ static bool start_array(upb_json_parser *p) { inner->f = p->top->f; inner->is_map = false; inner->is_mapentry = false; + inner->is_any = false; + inner->any_frame = NULL; inner->is_unknown_field = false; p->top = inner; @@ -14389,6 +14681,129 @@ static void end_object(upb_json_parser *p) { } } +static void start_any_object(upb_json_parser *p, const char *ptr) { + start_object(p); + p->top->any_frame->before_type_url_start = ptr; + p->top->any_frame->before_type_url_end = ptr; +} + +static bool end_any_object(upb_json_parser *p, const char *ptr) { + const char *value_membername = "value"; + bool is_well_known_packed = false; + const char *packed_end = ptr + 1; + upb_selector_t sel; + upb_jsonparser_frame *inner; + + if (json_parser_any_frame_has_value(p->top->any_frame) && + !json_parser_any_frame_has_type_url(p->top->any_frame)) { + upb_status_seterrmsg(&p->status, "No valid type url"); + upb_env_reporterror(p->env, &p->status); + return false; + } + + /* Well known types data is represented as value field. */ + if (upb_msgdef_wellknowntype(p->top->any_frame->parser->top->m) != + UPB_WELLKNOWN_UNSPECIFIED) { + is_well_known_packed = true; + + if (json_parser_any_frame_has_value_before_type_url(p->top->any_frame)) { + p->top->any_frame->before_type_url_start = + memchr(p->top->any_frame->before_type_url_start, ':', + p->top->any_frame->before_type_url_end - + p->top->any_frame->before_type_url_start); + if (p->top->any_frame->before_type_url_start == NULL) { + upb_status_seterrmsg(&p->status, "invalid data for well known type."); + upb_env_reporterror(p->env, &p->status); + return false; + } + p->top->any_frame->before_type_url_start++; + } + + if (json_parser_any_frame_has_value_after_type_url(p->top->any_frame)) { + p->top->any_frame->after_type_url_start = + memchr(p->top->any_frame->after_type_url_start, ':', + (ptr + 1) - + p->top->any_frame->after_type_url_start); + if (p->top->any_frame->after_type_url_start == NULL) { + upb_status_seterrmsg(&p->status, "Invalid data for well known type."); + upb_env_reporterror(p->env, &p->status); + return false; + } + p->top->any_frame->after_type_url_start++; + packed_end = ptr; + } + } + + if (json_parser_any_frame_has_value_before_type_url(p->top->any_frame)) { + if (!parse(p->top->any_frame->parser, NULL, + p->top->any_frame->before_type_url_start, + p->top->any_frame->before_type_url_end - + p->top->any_frame->before_type_url_start, NULL)) { + return false; + } + } else { + if (!is_well_known_packed) { + if (!parse(p->top->any_frame->parser, NULL, "{", 1, NULL)) { + return false; + } + } + } + + if (json_parser_any_frame_has_value_before_type_url(p->top->any_frame) && + json_parser_any_frame_has_value_after_type_url(p->top->any_frame)) { + if (!parse(p->top->any_frame->parser, NULL, ",", 1, NULL)) { + return false; + } + } + + if (json_parser_any_frame_has_value_after_type_url(p->top->any_frame)) { + if (!parse(p->top->any_frame->parser, NULL, + p->top->any_frame->after_type_url_start, + packed_end - p->top->any_frame->after_type_url_start, NULL)) { + return false; + } + } else { + if (!is_well_known_packed) { + if (!parse(p->top->any_frame->parser, NULL, "}", 1, NULL)) { + return false; + } + } + } + + if (!end(p->top->any_frame->parser, NULL)) { + return false; + } + + p->top->is_any = false; + + /* Set value */ + start_member(p); + capture_begin(p, value_membername); + capture_end(p, value_membername + 5); + end_membername(p); + + if (!check_stack(p)) return false; + inner = p->top + 1; + + sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); + upb_sink_startstr(&p->top->sink, sel, 0, &inner->sink); + sel = getsel_for_handlertype(p, UPB_HANDLER_STRING); + upb_sink_putstring(&inner->sink, sel, p->top->any_frame->stringsink.ptr, + p->top->any_frame->stringsink.len, NULL); + sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); + upb_sink_endstr(&inner->sink, sel); + + end_member(p); + + end_object(p); + + /* Deallocate any parse frame. */ + json_parser_any_frame_free(p->top->any_frame); + upb_env_free(p->env, p->top->any_frame); + + return true; +} + static bool is_string_wrapper(const upb_msgdef *m) { upb_wellknowntype_t type = upb_msgdef_wellknowntype(m); return type == UPB_WELLKNOWN_STRINGVALUE || @@ -14554,44 +14969,44 @@ static bool is_string_wrapper_object(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -//#line 2147 "upb/json/parser.rl" +#line 2576 "upb/json/parser.rl" -//#line 2016 "upb/json/parser.c" +#line 2422 "upb/json/parser.c" static const char _json_actions[] = { 0, 1, 0, 1, 1, 1, 3, 1, 4, 1, 6, 1, 7, 1, 8, 1, 9, 1, 10, 1, 11, 1, 12, 1, 13, 1, 21, 1, 23, 1, 24, 1, - 25, 1, 27, 1, 28, 1, 30, 1, + 26, 1, 27, 1, 28, 1, 30, 1, 32, 1, 33, 1, 34, 1, 35, 1, - 36, 1, 38, 2, 4, 9, 2, 5, + 37, 1, 38, 2, 4, 9, 2, 5, 6, 2, 7, 3, 2, 7, 9, 2, 14, 15, 2, 16, 17, 2, 18, 19, - 2, 22, 20, 2, 26, 37, 2, 29, + 2, 22, 20, 2, 24, 26, 2, 29, 2, 2, 30, 38, 2, 31, 20, 2, 33, 38, 2, 34, 38, 2, 35, 38, - 3, 25, 22, 20, 3, 26, 37, 38, - 4, 14, 15, 16, 17 + 2, 36, 25, 2, 37, 38, 4, 14, + 15, 16, 17 }; static const short _json_key_offsets[] = { 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, 144, 147, 149, 151, 154, 155, - 159, 161, 163, 165, 167, 168, 170, 172, - 173, 175, 177, 178, 180, 182, 183, 185, - 187, 188, 190, 192, 196, 198, 200, 201, - 202, 203, 204, 206, 211, 220, 221, 221, - 221, 226, 231, 236, 237, 238, 239, 240, - 240, 241, 242, 243, 243, 244, 245, 246, - 246, 251, 256, 257, 261, 266, 271, 276, - 280, 280, 283, 286, 289, 292, 295, 298, - 298, 298, 298, 298 + 38, 43, 44, 48, 53, 58, 63, 67, + 71, 74, 77, 79, 83, 87, 89, 91, + 96, 98, 100, 109, 115, 121, 127, 133, + 135, 139, 142, 144, 146, 149, 150, 154, + 156, 158, 160, 162, 163, 165, 167, 168, + 170, 172, 173, 175, 177, 178, 180, 182, + 183, 185, 187, 191, 193, 195, 196, 197, + 198, 199, 201, 206, 215, 216, 216, 216, + 221, 226, 231, 232, 233, 234, 235, 235, + 236, 237, 238, 238, 239, 240, 241, 241, + 246, 247, 251, 256, 261, 266, 270, 270, + 273, 276, 279, 282, 285, 288, 288, 288, + 288, 288 }; static const char _json_trans_keys[] = { @@ -14600,93 +15015,92 @@ static const char _json_trans_keys[] = { 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, + 125, 9, 13, 34, 32, 58, 9, 13, + 32, 93, 125, 9, 13, 32, 44, 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, 34, 92, 45, 48, 49, 57, - 48, 49, 57, 46, 115, 48, 57, 115, - 48, 57, 34, 46, 115, 48, 57, 48, - 57, 48, 57, 48, 57, 48, 57, 45, - 48, 57, 48, 57, 45, 48, 57, 48, - 57, 84, 48, 57, 48, 57, 58, 48, - 57, 48, 57, 58, 48, 57, 48, 57, - 43, 45, 46, 90, 48, 57, 48, 57, - 58, 48, 48, 34, 48, 57, 43, 45, - 90, 48, 57, 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, + 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, 34, 92, 45, + 48, 49, 57, 48, 49, 57, 46, 115, + 48, 57, 115, 48, 57, 34, 46, 115, + 48, 57, 48, 57, 48, 57, 48, 57, + 48, 57, 45, 48, 57, 48, 57, 45, + 48, 57, 48, 57, 84, 48, 57, 48, + 57, 58, 48, 57, 48, 57, 58, 48, + 57, 48, 57, 43, 45, 46, 90, 48, + 57, 48, 57, 58, 48, 48, 34, 48, + 57, 43, 45, 90, 48, 57, 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, 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, 0 + 9, 13, 32, 9, 13, 32, 9, 13, + 0 }; static const char _json_single_lengths[] = { 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, 2, 1, 2, 0, 1, 1, 2, - 0, 0, 0, 0, 1, 0, 0, 1, - 0, 0, 1, 0, 0, 1, 0, 0, - 1, 0, 0, 4, 0, 0, 1, 1, - 1, 1, 0, 3, 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, 0, 0 + 3, 1, 2, 3, 3, 3, 2, 2, + 1, 3, 0, 2, 2, 0, 0, 3, + 2, 2, 9, 0, 0, 0, 0, 2, + 2, 1, 2, 0, 1, 1, 2, 0, + 0, 0, 0, 1, 0, 0, 1, 0, + 0, 1, 0, 0, 1, 0, 0, 1, + 0, 0, 4, 0, 0, 1, 1, 1, + 1, 0, 3, 7, 1, 0, 0, 3, + 3, 3, 1, 1, 1, 1, 0, 1, + 1, 1, 0, 1, 1, 1, 0, 3, + 1, 2, 3, 3, 3, 2, 0, 1, + 1, 1, 1, 1, 1, 0, 0, 0, + 0, 0 }; static const char _json_range_lengths[] = { 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, 1, 0, 1, 1, 0, 1, - 1, 1, 1, 1, 0, 1, 1, 0, + 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, 1, 0, 1, 1, 0, 1, 1, - 0, 1, 1, 0, 1, 1, 0, 0, - 0, 0, 1, 1, 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, 0, 0 + 1, 1, 1, 0, 1, 1, 0, 1, + 1, 0, 1, 1, 0, 1, 1, 0, + 1, 1, 0, 1, 1, 0, 0, 0, + 0, 1, 1, 1, 0, 0, 0, 1, + 1, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1, + 0, 1, 1, 1, 1, 1, 0, 1, + 1, 1, 1, 1, 1, 0, 0, 0, + 0, 0 }; static const short _json_index_offsets[] = { 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, 152, 155, 158, 160, 163, 165, - 169, 171, 173, 175, 177, 179, 181, 183, - 185, 187, 189, 191, 193, 195, 197, 199, - 201, 203, 205, 207, 212, 214, 216, 218, - 220, 222, 224, 226, 231, 240, 242, 243, - 244, 249, 254, 259, 261, 263, 265, 267, - 268, 270, 272, 274, 275, 277, 279, 281, - 282, 287, 292, 294, 298, 303, 308, 313, - 317, 318, 321, 324, 327, 330, 333, 336, - 337, 338, 339, 340 + 48, 53, 55, 59, 64, 69, 74, 78, + 82, 85, 89, 91, 95, 99, 101, 103, + 108, 111, 114, 124, 128, 132, 136, 140, + 143, 147, 150, 153, 155, 158, 160, 164, + 166, 168, 170, 172, 174, 176, 178, 180, + 182, 184, 186, 188, 190, 192, 194, 196, + 198, 200, 202, 207, 209, 211, 213, 215, + 217, 219, 221, 226, 235, 237, 238, 239, + 244, 249, 254, 256, 258, 260, 262, 263, + 265, 267, 269, 270, 272, 274, 276, 277, + 282, 284, 288, 293, 298, 303, 307, 308, + 311, 314, 317, 320, 323, 326, 327, 328, + 329, 330 }; static const unsigned char _json_indicies[] = { @@ -14696,97 +15110,93 @@ static const unsigned char _json_indicies[] = { 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, 1, - 67, 68, 1, 69, 70, 1, 71, 1, - 70, 71, 1, 72, 1, 69, 70, 68, - 1, 73, 1, 74, 1, 75, 1, 76, - 1, 77, 1, 78, 1, 79, 1, 80, - 1, 81, 1, 82, 1, 83, 1, 84, - 1, 85, 1, 86, 1, 87, 1, 88, - 1, 89, 1, 90, 1, 91, 1, 92, - 92, 93, 94, 1, 95, 1, 96, 1, - 97, 1, 98, 1, 99, 1, 100, 1, - 101, 1, 102, 102, 103, 101, 1, 104, - 105, 106, 107, 108, 109, 110, 105, 1, - 111, 1, 112, 113, 115, 116, 1, 115, - 114, 117, 118, 116, 117, 1, 118, 1, - 1, 118, 114, 119, 1, 120, 1, 121, - 1, 122, 1, 123, 124, 1, 125, 1, - 126, 1, 127, 128, 1, 129, 1, 130, - 1, 131, 132, 133, 134, 132, 1, 135, - 136, 137, 135, 1, 138, 1, 139, 140, - 139, 1, 140, 1, 1, 140, 141, 142, - 143, 144, 142, 1, 145, 146, 137, 145, - 1, 146, 136, 146, 1, 147, 148, 148, - 1, 149, 149, 1, 150, 150, 1, 151, - 151, 1, 152, 152, 1, 153, 153, 1, - 1, 1, 1, 1, 1, 0 + 25, 26, 27, 25, 1, 28, 1, 29, + 30, 29, 1, 30, 1, 1, 30, 31, + 32, 33, 34, 32, 1, 35, 36, 27, + 35, 1, 36, 26, 36, 1, 37, 38, + 39, 1, 38, 39, 1, 41, 42, 42, + 40, 43, 1, 42, 42, 43, 40, 44, + 44, 45, 1, 45, 1, 45, 40, 41, + 42, 42, 39, 40, 47, 48, 46, 50, + 51, 49, 52, 52, 52, 52, 52, 52, + 52, 52, 53, 1, 54, 54, 54, 1, + 55, 55, 55, 1, 56, 56, 56, 1, + 57, 57, 57, 1, 59, 60, 58, 61, + 62, 63, 1, 64, 65, 1, 66, 67, + 1, 68, 1, 67, 68, 1, 69, 1, + 66, 67, 65, 1, 70, 1, 71, 1, + 72, 1, 73, 1, 74, 1, 75, 1, + 76, 1, 77, 1, 78, 1, 79, 1, + 80, 1, 81, 1, 82, 1, 83, 1, + 84, 1, 85, 1, 86, 1, 87, 1, + 88, 1, 89, 89, 90, 91, 1, 92, + 1, 93, 1, 94, 1, 95, 1, 96, + 1, 97, 1, 98, 1, 99, 99, 100, + 98, 1, 101, 102, 103, 104, 105, 106, + 107, 102, 1, 108, 1, 109, 110, 112, + 113, 1, 112, 111, 114, 115, 113, 114, + 1, 115, 1, 1, 115, 111, 116, 1, + 117, 1, 118, 1, 119, 1, 120, 121, + 1, 122, 1, 123, 1, 124, 125, 1, + 126, 1, 127, 1, 128, 129, 130, 131, + 129, 1, 132, 1, 133, 134, 133, 1, + 134, 1, 1, 134, 135, 136, 137, 138, + 136, 1, 139, 140, 131, 139, 1, 140, + 130, 140, 1, 141, 142, 142, 1, 143, + 143, 1, 144, 144, 1, 145, 145, 1, + 146, 146, 1, 147, 147, 1, 1, 1, + 1, 1, 1, 0 }; static const char _json_trans_targs[] = { - 1, 0, 2, 106, 3, 6, 10, 13, - 16, 105, 4, 3, 105, 4, 5, 7, - 8, 9, 107, 11, 12, 108, 14, 15, - 109, 17, 18, 110, 17, 18, 110, 19, - 19, 20, 21, 22, 23, 110, 22, 23, - 25, 26, 32, 111, 27, 29, 28, 30, - 31, 34, 112, 35, 34, 112, 35, 33, - 36, 37, 38, 39, 40, 34, 112, 35, - 42, 43, 47, 43, 47, 44, 46, 45, - 113, 49, 50, 51, 52, 53, 54, 55, - 56, 57, 58, 59, 60, 61, 62, 63, - 64, 65, 66, 67, 68, 74, 73, 69, - 70, 71, 72, 73, 114, 75, 68, 73, - 77, 79, 80, 83, 88, 92, 96, 78, - 115, 115, 81, 80, 78, 81, 82, 84, - 85, 86, 87, 115, 89, 90, 91, 115, - 93, 94, 95, 115, 97, 98, 104, 97, - 98, 104, 99, 99, 100, 101, 102, 103, - 104, 102, 103, 115, 105, 105, 105, 105, - 105, 105 + 1, 0, 2, 104, 3, 6, 10, 13, + 16, 103, 4, 3, 103, 4, 5, 7, + 8, 9, 105, 11, 12, 106, 14, 15, + 107, 16, 17, 108, 18, 18, 19, 20, + 21, 22, 108, 21, 22, 24, 25, 31, + 109, 26, 28, 27, 29, 30, 33, 110, + 34, 33, 110, 34, 32, 35, 36, 37, + 38, 39, 33, 110, 34, 41, 42, 46, + 42, 46, 43, 45, 44, 111, 48, 49, + 50, 51, 52, 53, 54, 55, 56, 57, + 58, 59, 60, 61, 62, 63, 64, 65, + 66, 67, 73, 72, 68, 69, 70, 71, + 72, 112, 74, 67, 72, 76, 78, 79, + 82, 87, 91, 95, 77, 113, 113, 80, + 79, 77, 80, 81, 83, 84, 85, 86, + 113, 88, 89, 90, 113, 92, 93, 94, + 113, 95, 96, 102, 97, 97, 98, 99, + 100, 101, 102, 100, 101, 113, 103, 103, + 103, 103, 103, 103 }; static const char _json_trans_actions[] = { 0, 0, 84, 78, 33, 0, 0, 0, - 47, 39, 25, 0, 35, 0, 0, 0, + 96, 39, 25, 0, 35, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 31, 96, 31, 0, 72, 0, 27, - 0, 0, 25, 29, 29, 29, 0, 0, - 0, 0, 0, 3, 0, 0, 0, 0, - 0, 5, 15, 0, 0, 51, 7, 13, - 0, 54, 9, 9, 9, 57, 60, 11, - 17, 17, 17, 0, 0, 0, 19, 0, - 21, 23, 0, 0, 0, 0, 0, 0, + 0, 0, 72, 31, 27, 0, 0, 25, + 29, 29, 75, 0, 0, 0, 0, 0, + 3, 0, 0, 0, 0, 0, 5, 15, + 0, 0, 51, 7, 13, 0, 54, 9, + 9, 9, 57, 60, 11, 17, 17, 17, + 0, 0, 0, 19, 0, 21, 23, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 104, 63, 104, 0, - 0, 0, 0, 0, 69, 0, 66, 66, - 84, 78, 33, 0, 0, 0, 47, 39, - 49, 81, 25, 0, 35, 0, 0, 0, - 0, 0, 0, 90, 0, 0, 0, 93, - 0, 0, 0, 87, 31, 96, 31, 0, - 72, 0, 27, 0, 0, 25, 29, 29, - 29, 0, 0, 100, 0, 37, 43, 45, - 41, 75 + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 102, 63, 102, 0, 0, 0, 0, + 0, 69, 0, 66, 66, 84, 78, 33, + 0, 0, 0, 96, 39, 49, 81, 25, + 0, 35, 0, 0, 0, 0, 0, 0, + 90, 0, 0, 0, 93, 0, 0, 0, + 87, 0, 72, 31, 27, 0, 0, 25, + 29, 29, 75, 0, 0, 99, 0, 37, + 43, 45, 41, 47 }; 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, 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, @@ -14795,21 +15205,22 @@ 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, 37, 43, 45, 41, 75, 0, - 0, 0, 0, 0 + 0, 0, 0, 0, 0, 0, 0, 0, + 37, 43, 45, 41, 47, 0, 0, 0, + 0, 0 }; static const int json_start = 1; -static const int json_en_number_machine = 24; -static const int json_en_string_machine = 33; -static const int json_en_duration_machine = 41; -static const int json_en_timestamp_machine = 48; -static const int json_en_value_machine = 76; +static const int json_en_number_machine = 23; +static const int json_en_string_machine = 32; +static const int json_en_duration_machine = 40; +static const int json_en_timestamp_machine = 47; +static const int json_en_value_machine = 75; static const int json_en_main = 1; -//#line 2150 "upb/json/parser.rl" +#line 2579 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -14832,7 +15243,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -//#line 2290 "upb/json/parser.c" +#line 2692 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -14907,166 +15318,190 @@ _match: switch ( *_acts++ ) { case 1: -//#line 2021 "upb/json/parser.rl" +#line 2427 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 2: -//#line 2023 "upb/json/parser.rl" - { p--; {stack[top++] = cs; cs = 24; goto _again;} } +#line 2429 "upb/json/parser.rl" + { p--; {stack[top++] = cs; cs = 23; goto _again;} } break; case 3: -//#line 2027 "upb/json/parser.rl" +#line 2433 "upb/json/parser.rl" { start_text(parser, p); } break; case 4: -//#line 2028 "upb/json/parser.rl" +#line 2434 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_text(parser, p)); } break; case 5: -//#line 2034 "upb/json/parser.rl" +#line 2440 "upb/json/parser.rl" { start_hex(parser); } break; case 6: -//#line 2035 "upb/json/parser.rl" +#line 2441 "upb/json/parser.rl" { hexdigit(parser, p); } break; case 7: -//#line 2036 "upb/json/parser.rl" +#line 2442 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hex(parser)); } break; case 8: -//#line 2042 "upb/json/parser.rl" +#line 2448 "upb/json/parser.rl" { CHECK_RETURN_TOP(escape(parser, p)); } break; case 9: -//#line 2048 "upb/json/parser.rl" +#line 2454 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 10: -//#line 2060 "upb/json/parser.rl" +#line 2466 "upb/json/parser.rl" { start_duration_base(parser, p); } break; case 11: -//#line 2061 "upb/json/parser.rl" +#line 2467 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_duration_base(parser, p)); } break; case 12: -//#line 2063 "upb/json/parser.rl" +#line 2469 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 13: -//#line 2068 "upb/json/parser.rl" +#line 2474 "upb/json/parser.rl" { start_timestamp_base(parser, p); } break; case 14: -//#line 2069 "upb/json/parser.rl" +#line 2475 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_base(parser, p)); } break; case 15: -//#line 2071 "upb/json/parser.rl" +#line 2477 "upb/json/parser.rl" { start_timestamp_fraction(parser, p); } break; case 16: -//#line 2072 "upb/json/parser.rl" +#line 2478 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); } break; case 17: -//#line 2074 "upb/json/parser.rl" +#line 2480 "upb/json/parser.rl" { start_timestamp_zone(parser, p); } break; case 18: -//#line 2075 "upb/json/parser.rl" +#line 2481 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); } break; case 19: -//#line 2077 "upb/json/parser.rl" +#line 2483 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 20: -//#line 2082 "upb/json/parser.rl" +#line 2488 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) { - {stack[top++] = cs; cs = 48; goto _again;} + {stack[top++] = cs; cs = 47; goto _again;} } else if (is_wellknown_msg(parser, UPB_WELLKNOWN_DURATION)) { - {stack[top++] = cs; cs = 41; goto _again;} + {stack[top++] = cs; cs = 40; goto _again;} } else { - {stack[top++] = cs; cs = 33; goto _again;} + {stack[top++] = cs; cs = 32; goto _again;} } } break; case 21: -//#line 2093 "upb/json/parser.rl" - { p--; {stack[top++] = cs; cs = 76; goto _again;} } +#line 2499 "upb/json/parser.rl" + { p--; {stack[top++] = cs; cs = 75; goto _again;} } break; case 22: -//#line 2098 "upb/json/parser.rl" - { start_member(parser); } +#line 2504 "upb/json/parser.rl" + { + if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { + start_any_member(parser, p); + } else { + start_member(parser); + } + } break; case 23: -//#line 2099 "upb/json/parser.rl" +#line 2511 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_membername(parser)); } break; case 24: -//#line 2102 "upb/json/parser.rl" - { end_member(parser); } +#line 2514 "upb/json/parser.rl" + { + if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { + end_any_member(parser, p); + } else { + end_member(parser); + } + } break; case 25: -//#line 2108 "upb/json/parser.rl" - { start_object(parser); } +#line 2525 "upb/json/parser.rl" + { + if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { + start_any_object(parser, p); + } else { + start_object(parser); + } + } break; case 26: -//#line 2111 "upb/json/parser.rl" - { end_object(parser); } +#line 2534 "upb/json/parser.rl" + { + if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { + CHECK_RETURN_TOP(end_any_object(parser, p)); + } else { + end_object(parser); + } + } break; case 27: -//#line 2117 "upb/json/parser.rl" +#line 2546 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_array(parser)); } break; case 28: -//#line 2121 "upb/json/parser.rl" +#line 2550 "upb/json/parser.rl" { end_array(parser); } break; case 29: -//#line 2126 "upb/json/parser.rl" +#line 2555 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_number(parser, p)); } break; case 30: -//#line 2127 "upb/json/parser.rl" +#line 2556 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 31: -//#line 2129 "upb/json/parser.rl" +#line 2558 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_stringval(parser)); } break; case 32: -//#line 2130 "upb/json/parser.rl" +#line 2559 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_stringval(parser)); } break; case 33: -//#line 2132 "upb/json/parser.rl" +#line 2561 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 34: -//#line 2134 "upb/json/parser.rl" +#line 2563 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 35: -//#line 2136 "upb/json/parser.rl" +#line 2565 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 36: -//#line 2138 "upb/json/parser.rl" +#line 2567 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_subobject_full(parser)); } break; case 37: -//#line 2139 "upb/json/parser.rl" +#line 2568 "upb/json/parser.rl" { end_subobject_full(parser); } break; case 38: -//#line 2144 "upb/json/parser.rl" +#line 2573 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -//#line 2524 "upb/json/parser.c" +#line 2950 "upb/json/parser.c" } } @@ -15083,34 +15518,30 @@ _again: while ( __nacts-- > 0 ) { switch ( *__acts++ ) { case 0: -//#line 2019 "upb/json/parser.rl" +#line 2425 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; - case 26: -//#line 2111 "upb/json/parser.rl" - { end_object(parser); } - break; case 30: -//#line 2127 "upb/json/parser.rl" +#line 2556 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 33: -//#line 2132 "upb/json/parser.rl" +#line 2561 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 34: -//#line 2134 "upb/json/parser.rl" +#line 2563 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 35: -//#line 2136 "upb/json/parser.rl" +#line 2565 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 37: -//#line 2139 "upb/json/parser.rl" +#line 2568 "upb/json/parser.rl" { end_subobject_full(parser); } break; -//#line 2568 "upb/json/parser.c" +#line 2990 "upb/json/parser.c" } } } @@ -15118,7 +15549,7 @@ _again: _out: {} } -//#line 2172 "upb/json/parser.rl" +#line 2601 "upb/json/parser.rl" if (p != pe) { upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p); @@ -15135,7 +15566,7 @@ error: return p - buf; } -bool end(void *closure, const void *hd) { +static bool end(void *closure, const void *hd) { upb_json_parser *parser = closure; /* Prevent compile warning on unused static constants. */ @@ -15150,9 +15581,9 @@ bool end(void *closure, const void *hd) { parse(parser, hd, &eof_ch, 0, NULL); return parser->current_state >= -//#line 2608 "upb/json/parser.c" -105 -//#line 2202 "upb/json/parser.rl" +#line 3030 "upb/json/parser.c" +103 +#line 2631 "upb/json/parser.rl" ; } @@ -15164,17 +15595,19 @@ static void json_parser_reset(upb_json_parser *p) { p->top->f = NULL; p->top->is_map = false; p->top->is_mapentry = false; + p->top->is_any = false; + p->top->any_frame = NULL; p->top->is_unknown_field = false; /* Emit Ragel initialization of the parser. */ -//#line 2626 "upb/json/parser.c" +#line 3050 "upb/json/parser.c" { cs = json_start; top = 0; } -//#line 2217 "upb/json/parser.rl" +#line 2648 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); @@ -15261,6 +15694,7 @@ 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, + const upb_symtab* symtab, upb_sink *output, bool ignore_json_unknown) { #ifndef NDEBUG @@ -15279,7 +15713,17 @@ upb_json_parser *upb_json_parser_create(upb_env *env, json_parser_reset(p); upb_sink_reset(&p->top->sink, output->handlers, output->closure); p->top->m = upb_handlers_msgdef(output->handlers); + if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) { + p->top->is_any = true; + p->top->any_frame = + upb_env_malloc(p->env, sizeof(upb_jsonparser_any_frame)); + json_parser_any_frame_reset(p->top->any_frame); + } else { + p->top->is_any = false; + p->top->any_frame = NULL; + } set_name_table(p, p->top); + p->symtab = symtab; p->ignore_json_unknown = ignore_json_unknown; @@ -15389,6 +15833,15 @@ strpc *newstrpc(upb_handlers *h, const upb_fielddef *f, return ret; } +/* Convert a null-terminated const char* to a string piece. */ +strpc *newstrpc_str(upb_handlers *h, const char * str) { + strpc * ret = upb_gmalloc(sizeof(*ret)); + ret->ptr = upb_gstrdup(str); + ret->len = strlen(str); + upb_handlers_addcleanup(h, ret, freestrpc); + return ret; +} + /* ------------ JSON string printing: values, maps, arrays ------------------ */ static void print_data( @@ -16240,6 +16693,49 @@ static bool printer_endmsg_noframe( return true; } +static void *scalar_startstr_onlykey( + void *closure, const void *handler_data, size_t size_hint) { + upb_json_printer *p = closure; + UPB_UNUSED(size_hint); + CHK(putkey(closure, handler_data)); + return p; +} + +/* Set up handlers for an Any submessage. */ +void printer_sethandlers_any(const void *closure, upb_handlers *h) { + const upb_msgdef *md = upb_handlers_msgdef(h); + + const upb_fielddef* type_field = upb_msgdef_itof(md, UPB_ANY_TYPE); + const upb_fielddef* value_field = upb_msgdef_itof(md, UPB_ANY_VALUE); + + upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER; + + /* type_url's json name is "@type" */ + upb_handlerattr type_name_attr = UPB_HANDLERATTR_INITIALIZER; + upb_handlerattr value_name_attr = UPB_HANDLERATTR_INITIALIZER; + strpc *type_url_json_name = newstrpc_str(h, "@type"); + strpc *value_json_name = newstrpc_str(h, "value"); + + upb_handlerattr_sethandlerdata(&type_name_attr, type_url_json_name); + upb_handlerattr_sethandlerdata(&value_name_attr, value_json_name); + + /* Set up handlers. */ + upb_handlers_setstartmsg(h, printer_startmsg, &empty_attr); + upb_handlers_setendmsg(h, printer_endmsg, &empty_attr); + + upb_handlers_setstartstr(h, type_field, scalar_startstr, &type_name_attr); + upb_handlers_setstring(h, type_field, scalar_str, &empty_attr); + upb_handlers_setendstr(h, type_field, scalar_endstr, &empty_attr); + + /* This is not the full and correct JSON encoding for the Any value field. It + * requires further processing by the wrapper code based on the type URL. + */ + upb_handlers_setstartstr(h, value_field, scalar_startstr_onlykey, + &value_name_attr); + + UPB_UNUSED(closure); +} + /* Set up handlers for a duration submessage. */ void printer_sethandlers_duration(const void *closure, upb_handlers *h) { const upb_msgdef *md = upb_handlers_msgdef(h); @@ -16393,6 +16889,9 @@ void printer_sethandlers(const void *closure, upb_handlers *h) { switch (upb_msgdef_wellknowntype(md)) { case UPB_WELLKNOWN_UNSPECIFIED: break; + case UPB_WELLKNOWN_ANY: + printer_sethandlers_any(closure, h); + return; case UPB_WELLKNOWN_DURATION: printer_sethandlers_duration(closure, h); return; diff --git a/php/ext/google/protobuf/upb.h b/php/ext/google/protobuf/upb.h index 0670a8123..f59fffb77 100644 --- a/php/ext/google/protobuf/upb.h +++ b/php/ext/google/protobuf/upb.h @@ -2013,6 +2013,7 @@ typedef enum { */ typedef enum { UPB_WELLKNOWN_UNSPECIFIED, + UPB_WELLKNOWN_ANY, UPB_WELLKNOWN_DURATION, UPB_WELLKNOWN_TIMESTAMP, /* number wrappers */ @@ -2416,6 +2417,10 @@ typedef upb_strtable_iter upb_msg_oneof_iter; #define UPB_MAPENTRY_KEY 1 #define UPB_MAPENTRY_VALUE 2 +/* Well-known field tag numbers for Any messages. */ +#define UPB_ANY_TYPE 1 +#define UPB_ANY_VALUE 2 + /* Well-known field tag numbers for timestamp messages. */ #define UPB_DURATION_SECONDS 1 #define UPB_DURATION_NANOS 2 @@ -3271,6 +3276,8 @@ const upb_def *upb_symtab_resolve(const upb_symtab *s, const char *base, const char *sym); const upb_def *upb_symtab_lookup(const upb_symtab *s, const char *sym); const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym); +const upb_msgdef *upb_symtab_lookupmsg2( + const upb_symtab *s, const char *sym, size_t len); const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym); bool upb_symtab_add(upb_symtab *s, upb_def *const*defs, size_t n, void *ref_donor, upb_status *status); @@ -6877,7 +6884,7 @@ typedef enum { extern const upb_msglayout google_protobuf_FileDescriptorSet_msginit; UPB_INLINE google_protobuf_FileDescriptorSet *google_protobuf_FileDescriptorSet_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_FileDescriptorSet_msginit, arena); + return (google_protobuf_FileDescriptorSet *)upb_msg_new(&google_protobuf_FileDescriptorSet_msginit, arena); } UPB_INLINE google_protobuf_FileDescriptorSet *google_protobuf_FileDescriptorSet_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_FileDescriptorSet *ret = google_protobuf_FileDescriptorSet_new(arena); @@ -6896,7 +6903,7 @@ UPB_INLINE void google_protobuf_FileDescriptorSet_set_file(google_protobuf_FileD extern const upb_msglayout google_protobuf_FileDescriptorProto_msginit; UPB_INLINE google_protobuf_FileDescriptorProto *google_protobuf_FileDescriptorProto_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_FileDescriptorProto_msginit, arena); + return (google_protobuf_FileDescriptorProto *)upb_msg_new(&google_protobuf_FileDescriptorProto_msginit, arena); } UPB_INLINE google_protobuf_FileDescriptorProto *google_protobuf_FileDescriptorProto_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_FileDescriptorProto *ret = google_protobuf_FileDescriptorProto_new(arena); @@ -6937,7 +6944,7 @@ UPB_INLINE void google_protobuf_FileDescriptorProto_set_syntax(google_protobuf_F extern const upb_msglayout google_protobuf_DescriptorProto_msginit; UPB_INLINE google_protobuf_DescriptorProto *google_protobuf_DescriptorProto_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_DescriptorProto_msginit, arena); + return (google_protobuf_DescriptorProto *)upb_msg_new(&google_protobuf_DescriptorProto_msginit, arena); } UPB_INLINE google_protobuf_DescriptorProto *google_protobuf_DescriptorProto_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_DescriptorProto *ret = google_protobuf_DescriptorProto_new(arena); @@ -6974,7 +6981,7 @@ UPB_INLINE void google_protobuf_DescriptorProto_set_reserved_name(google_protobu extern const upb_msglayout google_protobuf_DescriptorProto_ExtensionRange_msginit; UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange *google_protobuf_DescriptorProto_ExtensionRange_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_DescriptorProto_ExtensionRange_msginit, arena); + return (google_protobuf_DescriptorProto_ExtensionRange *)upb_msg_new(&google_protobuf_DescriptorProto_ExtensionRange_msginit, arena); } UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange *google_protobuf_DescriptorProto_ExtensionRange_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_DescriptorProto_ExtensionRange *ret = google_protobuf_DescriptorProto_ExtensionRange_new(arena); @@ -6997,7 +7004,7 @@ UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_set_options(googl extern const upb_msglayout google_protobuf_DescriptorProto_ReservedRange_msginit; UPB_INLINE google_protobuf_DescriptorProto_ReservedRange *google_protobuf_DescriptorProto_ReservedRange_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_DescriptorProto_ReservedRange_msginit, arena); + return (google_protobuf_DescriptorProto_ReservedRange *)upb_msg_new(&google_protobuf_DescriptorProto_ReservedRange_msginit, arena); } UPB_INLINE google_protobuf_DescriptorProto_ReservedRange *google_protobuf_DescriptorProto_ReservedRange_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_DescriptorProto_ReservedRange *ret = google_protobuf_DescriptorProto_ReservedRange_new(arena); @@ -7018,7 +7025,7 @@ UPB_INLINE void google_protobuf_DescriptorProto_ReservedRange_set_end(google_pro extern const upb_msglayout google_protobuf_ExtensionRangeOptions_msginit; UPB_INLINE google_protobuf_ExtensionRangeOptions *google_protobuf_ExtensionRangeOptions_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_ExtensionRangeOptions_msginit, arena); + return (google_protobuf_ExtensionRangeOptions *)upb_msg_new(&google_protobuf_ExtensionRangeOptions_msginit, arena); } UPB_INLINE google_protobuf_ExtensionRangeOptions *google_protobuf_ExtensionRangeOptions_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_ExtensionRangeOptions *ret = google_protobuf_ExtensionRangeOptions_new(arena); @@ -7037,7 +7044,7 @@ UPB_INLINE void google_protobuf_ExtensionRangeOptions_set_uninterpreted_option(g extern const upb_msglayout google_protobuf_FieldDescriptorProto_msginit; UPB_INLINE google_protobuf_FieldDescriptorProto *google_protobuf_FieldDescriptorProto_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_FieldDescriptorProto_msginit, arena); + return (google_protobuf_FieldDescriptorProto *)upb_msg_new(&google_protobuf_FieldDescriptorProto_msginit, arena); } UPB_INLINE google_protobuf_FieldDescriptorProto *google_protobuf_FieldDescriptorProto_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_FieldDescriptorProto *ret = google_protobuf_FieldDescriptorProto_new(arena); @@ -7074,7 +7081,7 @@ UPB_INLINE void google_protobuf_FieldDescriptorProto_set_json_name(google_protob extern const upb_msglayout google_protobuf_OneofDescriptorProto_msginit; UPB_INLINE google_protobuf_OneofDescriptorProto *google_protobuf_OneofDescriptorProto_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_OneofDescriptorProto_msginit, arena); + return (google_protobuf_OneofDescriptorProto *)upb_msg_new(&google_protobuf_OneofDescriptorProto_msginit, arena); } UPB_INLINE google_protobuf_OneofDescriptorProto *google_protobuf_OneofDescriptorProto_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_OneofDescriptorProto *ret = google_protobuf_OneofDescriptorProto_new(arena); @@ -7095,7 +7102,7 @@ UPB_INLINE void google_protobuf_OneofDescriptorProto_set_options(google_protobuf extern const upb_msglayout google_protobuf_EnumDescriptorProto_msginit; UPB_INLINE google_protobuf_EnumDescriptorProto *google_protobuf_EnumDescriptorProto_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_EnumDescriptorProto_msginit, arena); + return (google_protobuf_EnumDescriptorProto *)upb_msg_new(&google_protobuf_EnumDescriptorProto_msginit, arena); } UPB_INLINE google_protobuf_EnumDescriptorProto *google_protobuf_EnumDescriptorProto_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_EnumDescriptorProto *ret = google_protobuf_EnumDescriptorProto_new(arena); @@ -7122,7 +7129,7 @@ UPB_INLINE void google_protobuf_EnumDescriptorProto_set_reserved_name(google_pro extern const upb_msglayout google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit; UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange *google_protobuf_EnumDescriptorProto_EnumReservedRange_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, arena); + return (google_protobuf_EnumDescriptorProto_EnumReservedRange *)upb_msg_new(&google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, arena); } UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange *google_protobuf_EnumDescriptorProto_EnumReservedRange_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_EnumDescriptorProto_EnumReservedRange *ret = google_protobuf_EnumDescriptorProto_EnumReservedRange_new(arena); @@ -7143,7 +7150,7 @@ UPB_INLINE void google_protobuf_EnumDescriptorProto_EnumReservedRange_set_end(go extern const upb_msglayout google_protobuf_EnumValueDescriptorProto_msginit; UPB_INLINE google_protobuf_EnumValueDescriptorProto *google_protobuf_EnumValueDescriptorProto_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_EnumValueDescriptorProto_msginit, arena); + return (google_protobuf_EnumValueDescriptorProto *)upb_msg_new(&google_protobuf_EnumValueDescriptorProto_msginit, arena); } UPB_INLINE google_protobuf_EnumValueDescriptorProto *google_protobuf_EnumValueDescriptorProto_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_EnumValueDescriptorProto *ret = google_protobuf_EnumValueDescriptorProto_new(arena); @@ -7166,7 +7173,7 @@ UPB_INLINE void google_protobuf_EnumValueDescriptorProto_set_options(google_prot extern const upb_msglayout google_protobuf_ServiceDescriptorProto_msginit; UPB_INLINE google_protobuf_ServiceDescriptorProto *google_protobuf_ServiceDescriptorProto_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_ServiceDescriptorProto_msginit, arena); + return (google_protobuf_ServiceDescriptorProto *)upb_msg_new(&google_protobuf_ServiceDescriptorProto_msginit, arena); } UPB_INLINE google_protobuf_ServiceDescriptorProto *google_protobuf_ServiceDescriptorProto_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_ServiceDescriptorProto *ret = google_protobuf_ServiceDescriptorProto_new(arena); @@ -7189,7 +7196,7 @@ UPB_INLINE void google_protobuf_ServiceDescriptorProto_set_options(google_protob extern const upb_msglayout google_protobuf_MethodDescriptorProto_msginit; UPB_INLINE google_protobuf_MethodDescriptorProto *google_protobuf_MethodDescriptorProto_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_MethodDescriptorProto_msginit, arena); + return (google_protobuf_MethodDescriptorProto *)upb_msg_new(&google_protobuf_MethodDescriptorProto_msginit, arena); } UPB_INLINE google_protobuf_MethodDescriptorProto *google_protobuf_MethodDescriptorProto_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_MethodDescriptorProto *ret = google_protobuf_MethodDescriptorProto_new(arena); @@ -7218,7 +7225,7 @@ UPB_INLINE void google_protobuf_MethodDescriptorProto_set_server_streaming(googl extern const upb_msglayout google_protobuf_FileOptions_msginit; UPB_INLINE google_protobuf_FileOptions *google_protobuf_FileOptions_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_FileOptions_msginit, arena); + return (google_protobuf_FileOptions *)upb_msg_new(&google_protobuf_FileOptions_msginit, arena); } UPB_INLINE google_protobuf_FileOptions *google_protobuf_FileOptions_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_FileOptions *ret = google_protobuf_FileOptions_new(arena); @@ -7273,7 +7280,7 @@ UPB_INLINE void google_protobuf_FileOptions_set_uninterpreted_option(google_prot extern const upb_msglayout google_protobuf_MessageOptions_msginit; UPB_INLINE google_protobuf_MessageOptions *google_protobuf_MessageOptions_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_MessageOptions_msginit, arena); + return (google_protobuf_MessageOptions *)upb_msg_new(&google_protobuf_MessageOptions_msginit, arena); } UPB_INLINE google_protobuf_MessageOptions *google_protobuf_MessageOptions_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_MessageOptions *ret = google_protobuf_MessageOptions_new(arena); @@ -7300,7 +7307,7 @@ UPB_INLINE void google_protobuf_MessageOptions_set_uninterpreted_option(google_p extern const upb_msglayout google_protobuf_FieldOptions_msginit; UPB_INLINE google_protobuf_FieldOptions *google_protobuf_FieldOptions_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_FieldOptions_msginit, arena); + return (google_protobuf_FieldOptions *)upb_msg_new(&google_protobuf_FieldOptions_msginit, arena); } UPB_INLINE google_protobuf_FieldOptions *google_protobuf_FieldOptions_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_FieldOptions *ret = google_protobuf_FieldOptions_new(arena); @@ -7331,7 +7338,7 @@ UPB_INLINE void google_protobuf_FieldOptions_set_uninterpreted_option(google_pro extern const upb_msglayout google_protobuf_OneofOptions_msginit; UPB_INLINE google_protobuf_OneofOptions *google_protobuf_OneofOptions_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_OneofOptions_msginit, arena); + return (google_protobuf_OneofOptions *)upb_msg_new(&google_protobuf_OneofOptions_msginit, arena); } UPB_INLINE google_protobuf_OneofOptions *google_protobuf_OneofOptions_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_OneofOptions *ret = google_protobuf_OneofOptions_new(arena); @@ -7350,7 +7357,7 @@ UPB_INLINE void google_protobuf_OneofOptions_set_uninterpreted_option(google_pro extern const upb_msglayout google_protobuf_EnumOptions_msginit; UPB_INLINE google_protobuf_EnumOptions *google_protobuf_EnumOptions_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_EnumOptions_msginit, arena); + return (google_protobuf_EnumOptions *)upb_msg_new(&google_protobuf_EnumOptions_msginit, arena); } UPB_INLINE google_protobuf_EnumOptions *google_protobuf_EnumOptions_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_EnumOptions *ret = google_protobuf_EnumOptions_new(arena); @@ -7373,7 +7380,7 @@ UPB_INLINE void google_protobuf_EnumOptions_set_uninterpreted_option(google_prot extern const upb_msglayout google_protobuf_EnumValueOptions_msginit; UPB_INLINE google_protobuf_EnumValueOptions *google_protobuf_EnumValueOptions_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_EnumValueOptions_msginit, arena); + return (google_protobuf_EnumValueOptions *)upb_msg_new(&google_protobuf_EnumValueOptions_msginit, arena); } UPB_INLINE google_protobuf_EnumValueOptions *google_protobuf_EnumValueOptions_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_EnumValueOptions *ret = google_protobuf_EnumValueOptions_new(arena); @@ -7394,7 +7401,7 @@ UPB_INLINE void google_protobuf_EnumValueOptions_set_uninterpreted_option(google extern const upb_msglayout google_protobuf_ServiceOptions_msginit; UPB_INLINE google_protobuf_ServiceOptions *google_protobuf_ServiceOptions_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_ServiceOptions_msginit, arena); + return (google_protobuf_ServiceOptions *)upb_msg_new(&google_protobuf_ServiceOptions_msginit, arena); } UPB_INLINE google_protobuf_ServiceOptions *google_protobuf_ServiceOptions_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_ServiceOptions *ret = google_protobuf_ServiceOptions_new(arena); @@ -7415,7 +7422,7 @@ UPB_INLINE void google_protobuf_ServiceOptions_set_uninterpreted_option(google_p extern const upb_msglayout google_protobuf_MethodOptions_msginit; UPB_INLINE google_protobuf_MethodOptions *google_protobuf_MethodOptions_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_MethodOptions_msginit, arena); + return (google_protobuf_MethodOptions *)upb_msg_new(&google_protobuf_MethodOptions_msginit, arena); } UPB_INLINE google_protobuf_MethodOptions *google_protobuf_MethodOptions_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_MethodOptions *ret = google_protobuf_MethodOptions_new(arena); @@ -7438,7 +7445,7 @@ UPB_INLINE void google_protobuf_MethodOptions_set_uninterpreted_option(google_pr extern const upb_msglayout google_protobuf_UninterpretedOption_msginit; UPB_INLINE google_protobuf_UninterpretedOption *google_protobuf_UninterpretedOption_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena); + return (google_protobuf_UninterpretedOption *)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena); } UPB_INLINE google_protobuf_UninterpretedOption *google_protobuf_UninterpretedOption_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_UninterpretedOption *ret = google_protobuf_UninterpretedOption_new(arena); @@ -7469,7 +7476,7 @@ UPB_INLINE void google_protobuf_UninterpretedOption_set_aggregate_value(google_p extern const upb_msglayout google_protobuf_UninterpretedOption_NamePart_msginit; UPB_INLINE google_protobuf_UninterpretedOption_NamePart *google_protobuf_UninterpretedOption_NamePart_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_UninterpretedOption_NamePart_msginit, arena); + return (google_protobuf_UninterpretedOption_NamePart *)upb_msg_new(&google_protobuf_UninterpretedOption_NamePart_msginit, arena); } UPB_INLINE google_protobuf_UninterpretedOption_NamePart *google_protobuf_UninterpretedOption_NamePart_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_UninterpretedOption_NamePart *ret = google_protobuf_UninterpretedOption_NamePart_new(arena); @@ -7490,7 +7497,7 @@ UPB_INLINE void google_protobuf_UninterpretedOption_NamePart_set_is_extension(go extern const upb_msglayout google_protobuf_SourceCodeInfo_msginit; UPB_INLINE google_protobuf_SourceCodeInfo *google_protobuf_SourceCodeInfo_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_SourceCodeInfo_msginit, arena); + return (google_protobuf_SourceCodeInfo *)upb_msg_new(&google_protobuf_SourceCodeInfo_msginit, arena); } UPB_INLINE google_protobuf_SourceCodeInfo *google_protobuf_SourceCodeInfo_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_SourceCodeInfo *ret = google_protobuf_SourceCodeInfo_new(arena); @@ -7509,7 +7516,7 @@ UPB_INLINE void google_protobuf_SourceCodeInfo_set_location(google_protobuf_Sour extern const upb_msglayout google_protobuf_SourceCodeInfo_Location_msginit; UPB_INLINE google_protobuf_SourceCodeInfo_Location *google_protobuf_SourceCodeInfo_Location_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_SourceCodeInfo_Location_msginit, arena); + return (google_protobuf_SourceCodeInfo_Location *)upb_msg_new(&google_protobuf_SourceCodeInfo_Location_msginit, arena); } UPB_INLINE google_protobuf_SourceCodeInfo_Location *google_protobuf_SourceCodeInfo_Location_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_SourceCodeInfo_Location *ret = google_protobuf_SourceCodeInfo_Location_new(arena); @@ -7536,7 +7543,7 @@ UPB_INLINE void google_protobuf_SourceCodeInfo_Location_set_leading_detached_com extern const upb_msglayout google_protobuf_GeneratedCodeInfo_msginit; UPB_INLINE google_protobuf_GeneratedCodeInfo *google_protobuf_GeneratedCodeInfo_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_GeneratedCodeInfo_msginit, arena); + return (google_protobuf_GeneratedCodeInfo *)upb_msg_new(&google_protobuf_GeneratedCodeInfo_msginit, arena); } UPB_INLINE google_protobuf_GeneratedCodeInfo *google_protobuf_GeneratedCodeInfo_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_GeneratedCodeInfo *ret = google_protobuf_GeneratedCodeInfo_new(arena); @@ -7555,7 +7562,7 @@ UPB_INLINE void google_protobuf_GeneratedCodeInfo_set_annotation(google_protobuf extern const upb_msglayout google_protobuf_GeneratedCodeInfo_Annotation_msginit; UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation *google_protobuf_GeneratedCodeInfo_Annotation_new(upb_arena *arena) { - return upb_msg_new(&google_protobuf_GeneratedCodeInfo_Annotation_msginit, arena); + return (google_protobuf_GeneratedCodeInfo_Annotation *)upb_msg_new(&google_protobuf_GeneratedCodeInfo_Annotation_msginit, arena); } UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation *google_protobuf_GeneratedCodeInfo_Annotation_parsenew(upb_stringview buf, upb_arena *arena) { google_protobuf_GeneratedCodeInfo_Annotation *ret = google_protobuf_GeneratedCodeInfo_Annotation_new(arena); @@ -9567,7 +9574,7 @@ UPB_DECLARE_DERIVED_TYPE(upb::json::ParserMethod, upb::RefCounted, * constructed. This hint may be an overestimate for some build configurations. * But if the parser library is upgraded without recompiling the application, * it may be an underestimate. */ -#define UPB_JSON_PARSER_SIZE 4160 +#define UPB_JSON_PARSER_SIZE 5712 #ifdef __cplusplus @@ -9576,6 +9583,7 @@ UPB_DECLARE_DERIVED_TYPE(upb::json::ParserMethod, upb::RefCounted, class upb::json::Parser { public: static Parser* Create(Environment* env, const ParserMethod* method, + const SymbolTable* symtab, Sink* output, bool ignore_json_unknown); BytesSink* input(); @@ -9610,6 +9618,7 @@ UPB_BEGIN_EXTERN_C upb_json_parser* upb_json_parser_create(upb_env* e, const upb_json_parsermethod* m, + const upb_symtab* symtab, upb_sink* output, bool ignore_json_unknown); upb_bytessink *upb_json_parser_input(upb_json_parser *p); @@ -9631,8 +9640,10 @@ UPB_END_EXTERN_C namespace upb { namespace json { inline Parser* Parser::Create(Environment* env, const ParserMethod* method, + const SymbolTable* symtab, Sink* output, bool ignore_json_unknown) { - return upb_json_parser_create(env, method, output, ignore_json_unknown); + return upb_json_parser_create( + env, method, symtab, output, ignore_json_unknown); } inline BytesSink* Parser::input() { return upb_json_parser_input(this); diff --git a/php/tests/encode_decode_test.php b/php/tests/encode_decode_test.php index 06f9a9006..8018f9798 100644 --- a/php/tests/encode_decode_test.php +++ b/php/tests/encode_decode_test.php @@ -5,12 +5,14 @@ require_once('test_util.php'); use Google\Protobuf\RepeatedField; use Google\Protobuf\GPBType; +use Foo\TestAny; use Foo\TestEnum; use Foo\TestMessage; use Foo\TestMessage\Sub; use Foo\TestPackedMessage; use Foo\TestRandomFieldOrder; use Foo\TestUnpackedMessage; +use Google\Protobuf\Any; use Google\Protobuf\DoubleValue; use Google\Protobuf\FloatValue; use Google\Protobuf\Int32Value; @@ -915,4 +917,143 @@ class EncodeDecodeTest extends TestBase $this->assertSame("{\"a\":1.5}", $m->serializeToJsonString()); } + public function testDecodeTopLevelAny() + { + // Make sure packed message has been created at least once. + $packed = new TestMessage(); + + $m1 = new Any(); + $m1->mergeFromJsonString( + "{\"optionalInt32\": 1, " . + "\"@type\":\"type.googleapis.com/foo.TestMessage\"}"); + $this->assertSame("type.googleapis.com/foo.TestMessage", + $m1->getTypeUrl()); + $this->assertSame("0801", bin2hex($m1->getValue())); + + $m2 = new Any(); + $m2->mergeFromJsonString( + "{\"@type\":\"type.googleapis.com/foo.TestMessage\", " . + "\"optionalInt32\": 1}"); + $this->assertSame("type.googleapis.com/foo.TestMessage", + $m2->getTypeUrl()); + $this->assertSame("0801", bin2hex($m2->getValue())); + + $m3 = new Any(); + $m3->mergeFromJsonString( + "{\"optionalInt32\": 1, " . + "\"@type\":\"type.googleapis.com/foo.TestMessage\", " . + "\"optionalInt64\": 2}"); + $this->assertSame("type.googleapis.com/foo.TestMessage", + $m3->getTypeUrl()); + $this->assertSame("08011002", bin2hex($m3->getValue())); + } + + public function testDecodeAny() + { + // Make sure packed message has been created at least once. + $packed = new TestMessage(); + + $m1 = new TestAny(); + $m1->mergeFromJsonString( + "{\"any\": {\"optionalInt32\": 1, " . + "\"@type\":\"type.googleapis.com/foo.TestMessage\"}}"); + $this->assertSame("type.googleapis.com/foo.TestMessage", + $m1->getAny()->getTypeUrl()); + $this->assertSame("0801", bin2hex($m1->getAny()->getValue())); + + $m2 = new TestAny(); + $m2->mergeFromJsonString( + "{\"any\":{\"@type\":\"type.googleapis.com/foo.TestMessage\", " . + "\"optionalInt32\": 1}}"); + $this->assertSame("type.googleapis.com/foo.TestMessage", + $m2->getAny()->getTypeUrl()); + $this->assertSame("0801", bin2hex($m2->getAny()->getValue())); + + $m3 = new TestAny(); + $m3->mergeFromJsonString( + "{\"any\":{\"optionalInt32\": 1, " . + "\"@type\":\"type.googleapis.com/foo.TestMessage\", " . + "\"optionalInt64\": 2}}"); + $this->assertSame("type.googleapis.com/foo.TestMessage", + $m3->getAny()->getTypeUrl()); + $this->assertSame("08011002", bin2hex($m3->getAny()->getValue())); + } + + public function testDecodeAnyWithWellKnownPacked() + { + // Make sure packed message has been created at least once. + $packed = new Int32Value(); + + $m1 = new TestAny(); + $m1->mergeFromJsonString( + "{\"any\":" . + " {\"@type\":\"type.googleapis.com/google.protobuf.Int32Value\"," . + " \"value\":1}}"); + $this->assertSame("type.googleapis.com/google.protobuf.Int32Value", + $m1->getAny()->getTypeUrl()); + $this->assertSame("0801", bin2hex($m1->getAny()->getValue())); + } + + /** + * @expectedException Exception + */ + public function testDecodeAnyWithUnknownPacked() + { + $m = new TestAny(); + $m->mergeFromJsonString( + "{\"any\":" . + " {\"@type\":\"type.googleapis.com/unknown\"," . + " \"value\":1}}"); + } + + public function testEncodeTopLevelAny() + { + // Test a normal message. + $packed = new TestMessage(); + $packed->setOptionalInt32(123); + $packed->setOptionalString("abc"); + + $m = new Any(); + $m->pack($packed); + $expected1 = + "{\"@type\":\"type.googleapis.com/foo.TestMessage\"," . + "\"optional_int32\":123,\"optional_string\":\"abc\"}"; + $expected2 = + "{\"@type\":\"type.googleapis.com/foo.TestMessage\"," . + "\"optionalInt32\":123,\"optionalString\":\"abc\"}"; + $result = $m->serializeToJsonString(); + $this->assertTrue($expected1 === $result || $expected2 === $result); + + // Test a well known message. + $packed = new Int32Value(); + $packed->setValue(123); + + $m = new Any(); + $m->pack($packed); + $this->assertSame( + "{\"@type\":\"type.googleapis.com/google.protobuf.Int32Value\"," . + "\"value\":123}", + $m->serializeToJsonString()); + + // Test an Any message. + $outer = new Any(); + $outer->pack($m); + $this->assertSame( + "{\"@type\":\"type.googleapis.com/google.protobuf.Any\"," . + "\"value\":{\"@type\":\"type.googleapis.com/google.protobuf.Int32Value\"," . + "\"value\":123}}", + $outer->serializeToJsonString()); + + // Test a Timestamp message. + $packed = new Google\Protobuf\Timestamp(); + $packed->setSeconds(946684800); + $packed->setNanos(123456789); + $m = new Any(); + $m->pack($packed); + $this->assertSame( + "{\"@type\":\"type.googleapis.com/google.protobuf.Timestamp\"," . + "\"value\":\"2000-01-01T00:00:00.123456789Z\"}", + $m->serializeToJsonString()); + } + } diff --git a/php/tests/gdb_test.sh b/php/tests/gdb_test.sh index 36fa31bbb..da5f3f3ac 100755 --- a/php/tests/gdb_test.sh +++ b/php/tests/gdb_test.sh @@ -11,7 +11,8 @@ php -i | grep "Configuration" # gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which # phpunit` --bootstrap autoload.php tmp_test.php # -gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which phpunit` --bootstrap autoload.php generated_class_test.php +# gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which phpunit` --bootstrap autoload.php generated_class_test.php +gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so `which phpunit` --bootstrap autoload.php encode_decode_test.php # # gdb --args php -dextension=../ext/google/protobuf/modules/protobuf.so memory_leak_test.php # diff --git a/php/tests/memory_leak_test.php b/php/tests/memory_leak_test.php index f3bcb963f..142f46766 100644 --- a/php/tests/memory_leak_test.php +++ b/php/tests/memory_leak_test.php @@ -19,6 +19,7 @@ require_once('generated/Bar/TestLegacyMessage/NestedEnum.php'); require_once('generated/Bar/TestLegacyMessage/NestedMessage.php'); require_once('generated/Foo/PBARRAY.php'); require_once('generated/Foo/PBEmpty.php'); +require_once('generated/Foo/TestAny.php'); require_once('generated/Foo/TestEnum.php'); require_once('generated/Foo/TestIncludeNamespaceMessage.php'); require_once('generated/Foo/TestIncludePrefixMessage.php'); @@ -49,6 +50,7 @@ require_once('test_util.php'); use Google\Protobuf\Internal\RepeatedField; use Google\Protobuf\Internal\GPBType; +use Foo\TestAny; use Foo\TestMessage; use Foo\TestMessage\Sub; @@ -191,3 +193,16 @@ $to = new TestMessage(); TestUtil::setTestMessage($from); $to->mergeFrom($from); TestUtil::assertTestMessage($to); + +// Test decode Any +// Make sure packed message has been created at least once. +$packed = new TestMessage(); + +$m = new TestAny(); +$m->mergeFromJsonString( + "{\"any\":" . + " {\"@type\":\"type.googleapis.com/foo.TestMessage\"," . + " \"optionalInt32\":1}}"); +assert("type.googleapis.com/foo.TestMessage" === + $m->getAny()->getTypeUrl()); +assert("0801" === bin2hex($m->getAny()->getValue())); diff --git a/php/tests/proto/test.proto b/php/tests/proto/test.proto index d0109d83a..e610c581b 100644 --- a/php/tests/proto/test.proto +++ b/php/tests/proto/test.proto @@ -1,5 +1,6 @@ syntax = "proto3"; +import 'google/protobuf/any.proto'; import 'proto/test_include.proto'; import 'proto/test_no_namespace.proto'; import 'proto/test_php_namespace.proto'; @@ -201,3 +202,7 @@ message testLowerCaseMessage { enum testLowerCaseEnum { VALUE = 0; } + +message TestAny { + google.protobuf.Any any = 1; +}