Unverified Commit cb95a7f6 authored by Yilun Chong's avatar Yilun Chong Committed by GitHub

Down-integrate internal changes to github. (#5566)

* Down-integrate internal changes to github.

* fix csharp conformance test

* add comments in conformance.proto for text format

* fix comments

* fix comments, re-generated csharp file

* fix comments, re-generated csharp file
parent b1b9eaa6
import com.google.protobuf.ByteString;
import com.google.protobuf.AbstractMessage;
import com.google.protobuf.Parser;
import com.google.protobuf.ByteString;
import com.google.protobuf.CodedInputStream;
import com.google.protobuf.conformance.Conformance;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf_test_messages.proto3.TestMessagesProto3;
import com.google.protobuf_test_messages.proto3.TestMessagesProto3.TestAllTypesProto3;
import com.google.protobuf_test_messages.proto2.TestMessagesProto2;
import com.google.protobuf_test_messages.proto2.TestMessagesProto2.TestAllTypesProto2;
import com.google.protobuf.ExtensionRegistry;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Parser;
import com.google.protobuf.TextFormat;
import com.google.protobuf.conformance.Conformance;
import com.google.protobuf.util.JsonFormat;
import com.google.protobuf.util.JsonFormat.TypeRegistry;
import com.google.protobuf_test_messages.proto2.TestMessagesProto2;
import com.google.protobuf_test_messages.proto2.TestMessagesProto2.TestAllTypesProto2;
import com.google.protobuf_test_messages.proto3.TestMessagesProto3;
import com.google.protobuf_test_messages.proto3.TestMessagesProto3.TestAllTypesProto3;
import java.nio.ByteBuffer;
import java.util.ArrayList;
......@@ -247,6 +248,17 @@ class ConformanceJava {
}
break;
}
case TEXT_PAYLOAD: {
try {
TestMessagesProto3.TestAllTypesProto3.Builder builder =
TestMessagesProto3.TestAllTypesProto3.newBuilder();
TextFormat.merge(request.getTextPayload(), builder);
testMessage = builder.build();
} catch (TextFormat.ParseException e) {
return Conformance.ConformanceResponse.newBuilder().setParseError(e.getMessage()).build();
}
break;
}
case PAYLOAD_NOT_SET: {
throw new RuntimeException("Request didn't have payload.");
}
......@@ -274,6 +286,10 @@ class ConformanceJava {
e.getMessage()).build();
}
case TEXT_FORMAT:
return Conformance.ConformanceResponse.newBuilder().setTextPayload(
TextFormat.printToString(testMessage)).build();
default: {
throw new RuntimeException("Unexpected request output.");
}
......
......@@ -56,6 +56,7 @@ enum WireFormat {
PROTOBUF = 1;
JSON = 2;
JSPB = 3; // Google internal only. Opensource testees just skip it.
TEXT_FORMAT = 4;
}
enum TestCategory {
......@@ -70,8 +71,14 @@ enum TestCategory {
JSON_IGNORE_UNKNOWN_PARSING_TEST = 3;
// Test jspb wire format. Google internal only. Opensource testees just skip it.
JSPB_TEST = 4;
// Test text format. For cpp, java and python, testees can already deal with
// this type. Testees of other languages can simply skip it.
TEXT_FORMAT_TEST = 5;
}
// The conformance runner will request a list of failures as the first request.
// This will be known by message_type == "conformance.FailureSet", a conformance
// test should return a serialized FailureSet in protobuf_payload.
message FailureSet {
repeated string failure = 1;
}
......@@ -94,6 +101,7 @@ message ConformanceRequest {
string json_payload = 2;
// Google internal only. Opensource testees just skip it.
string jspb_payload = 7;
string text_payload = 8;
}
// Which format should the testee serialize its message to?
......@@ -149,6 +157,10 @@ message ConformanceResponse {
// serialize to JSPB and set it in this field. JSPB is google internal only
// format. Opensource testees can just skip it.
string jspb_payload = 7;
// If the input was successfully parsed and the requested output was
// TEXT_FORMAT, serialize to TEXT_FORMAT and set it in this field.
string text_payload = 8;
}
}
......
......@@ -36,6 +36,7 @@
#include <google/protobuf/test_messages_proto3.pb.h>
#include <google/protobuf/test_messages_proto2.pb.h>
#include <google/protobuf/message.h>
#include <google/protobuf/text_format.h>
#include <google/protobuf/util/json_util.h>
#include <google/protobuf/util/type_resolver_util.h>
......@@ -45,14 +46,15 @@ using google::protobuf::Descriptor;
using google::protobuf::DescriptorPool;
using google::protobuf::Message;
using google::protobuf::MessageFactory;
using google::protobuf::TextFormat;
using google::protobuf::util::BinaryToJsonString;
using google::protobuf::util::JsonParseOptions;
using google::protobuf::util::JsonToBinaryString;
using google::protobuf::util::NewTypeResolverForDescriptorPool;
using google::protobuf::util::Status;
using google::protobuf::util::TypeResolver;
using protobuf_test_messages::proto3::TestAllTypesProto3;
using protobuf_test_messages::proto2::TestAllTypesProto2;
using protobuf_test_messages::proto3::TestAllTypesProto3;
using std::string;
static const char kTypeUrlPrefix[] = "type.googleapis.com";
......@@ -163,6 +165,14 @@ void DoTest(const ConformanceRequest& request, ConformanceResponse* response) {
break;
}
case ConformanceRequest::kTextPayload: {
if (!TextFormat::ParseFromString(request.text_payload(), test_message)) {
response->set_parse_error("Parse error");
return;
}
break;
}
case ConformanceRequest::PAYLOAD_NOT_SET:
GOOGLE_LOG(FATAL) << "Request didn't have payload.";
break;
......@@ -203,6 +213,12 @@ void DoTest(const ConformanceRequest& request, ConformanceResponse* response) {
break;
}
case conformance::TEXT_FORMAT: {
GOOGLE_CHECK(TextFormat::PrintToString(*test_message,
response->mutable_text_payload()));
break;
}
default:
GOOGLE_LOG(FATAL) << "Unknown output format: "
<< request.requested_output_format();
......
......@@ -44,6 +44,7 @@ from google.protobuf import json_format
from google.protobuf import message
from google.protobuf import test_messages_proto3_pb2
from google.protobuf import test_messages_proto2_pb2
from google.protobuf import text_format
import conformance_pb2
sys.stdout = os.fdopen(sys.stdout.fileno(), 'wb', 0)
......@@ -136,6 +137,13 @@ def do_test(request):
response.parse_error = str(e)
return response
elif request.WhichOneof('payload') == 'text_payload':
try:
text_format.Parse(request.text_payload, test_message)
except Exception as e:
response.parse_error = str(e)
return response
else:
raise ProtocolError("Request didn't have payload.")
......@@ -152,6 +160,9 @@ def do_test(request):
response.serialize_error = str(e)
return response
elif request.requested_output_format == conformance_pb2.TEXT_FORMAT:
response.text_payload = text_format.MessageToString(test_message)
except Exception as e:
response.runtime_error = str(e)
......
......@@ -85,6 +85,11 @@ ConformanceTestSuite::ConformanceRequestSetting::ConformanceRequestSetting(
break;
}
case conformance::TEXT_FORMAT: {
request_.set_text_payload(input);
break;
}
default:
GOOGLE_LOG(FATAL) << "Unspecified input format";
}
......@@ -131,6 +136,8 @@ string ConformanceTestSuite::ConformanceRequestSetting::
return "ProtobufInput";
case conformance::JSON:
return "JsonInput";
case conformance::TEXT_FORMAT:
return "TextFormatInput";
default:
GOOGLE_LOG(FATAL) << "Unspecified output format";
}
......@@ -144,6 +151,8 @@ string ConformanceTestSuite::ConformanceRequestSetting::
return "ProtobufOutput";
case conformance::JSON:
return "JsonOutput";
case conformance::TEXT_FORMAT:
return "TextFormatOutput";
default:
GOOGLE_LOG(FATAL) << "Unspecified output format";
}
......@@ -341,6 +350,8 @@ string ConformanceTestSuite::WireFormatToString(
return "JSON";
case conformance::JSPB:
return "JSPB";
case conformance::TEXT_FORMAT:
return "TEXT_FORMAT";
case conformance::UNSPECIFIED:
return "UNSPECIFIED";
default:
......
......@@ -39,8 +39,7 @@ import "google/protobuf/descriptor.proto";
package jspb.test;
message Empty {
}
message Empty {}
enum OuterEnum {
FOO = 1;
......@@ -137,12 +136,13 @@ message DefaultValues {
E1 = 13;
E2 = 77;
}
optional string string_field = 1 [default="default<>\'\"abc"];
optional bool bool_field = 2 [default=true];
optional int64 int_field = 3 [default=11];
optional Enum enum_field = 4 [default=E1];
optional string empty_field = 6 [default=""];
optional bytes bytes_field = 8 [default="moo"]; // Base64 encoding is "bW9v"
optional string string_field = 1 [default = "default<>\'\"abc"];
optional bool bool_field = 2 [default = true];
optional int64 int_field = 3 [default = 11];
optional Enum enum_field = 4 [default = E1];
optional string empty_field = 6 [default = ""];
optional bytes bytes_field = 8
[default = "moo"]; // Base64 encoding is "bW9v"
}
message FloatingPointFields {
......@@ -254,9 +254,9 @@ extend TestLastFieldBeforePivot {
message Int64Types {
optional int64 int64_normal = 1 [jstype=JS_NORMAL];
optional sint64 int64_string = 2 [jstype=JS_STRING];
optional uint64 int64_number = 3 [jstype=JS_NUMBER];
optional int64 int64_normal = 1 [jstype = JS_NORMAL];
optional sint64 int64_string = 2 [jstype = JS_STRING];
optional uint64 int64_number = 3 [jstype = JS_NUMBER];
}
......@@ -297,3 +297,4 @@ message Deeply {
}
}
......@@ -2847,52 +2847,69 @@ void Generator::GenerateClassField(const GeneratorOptions& options,
// Generate clearFoo() method for map fields, repeated fields, and other
// fields with presence.
if (field->is_map()) {
// clang-format off
printer->Print(
"/** Clears values from the map. The map will be non-null. */\n"
"/**\n"
" * Clears values from the map. The map will be non-null."
"$returndoc$\n"
" */\n"
"$class$.prototype.$clearername$ = function() {\n"
" this.$gettername$().clear();$returnvalue$\n"
"};\n"
"\n"
"\n",
"returndoc", JSReturnDoc(options, field),
"class", GetMessagePath(options, field->containing_type()),
"clearername", "clear" + JSGetterName(options, field), "gettername",
"get" + JSGetterName(options, field), "returnvalue",
JSReturnClause(field));
"clearername", "clear" + JSGetterName(options, field),
"gettername", "get" + JSGetterName(options, field),
"returnvalue", JSReturnClause(field));
// clang-format on
printer->Annotate("clearername", field);
} else if (field->is_repeated() ||
(field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
!field->is_required())) {
// Fields where we can delegate to the regular setter.
// clang-format off
printer->Print(
"/** $jsdoc$ */\n"
"/**\n"
" * $jsdoc$$returndoc$\n"
" */\n"
"$class$.prototype.$clearername$ = function() {\n"
" this.$settername$($clearedvalue$);$returnvalue$\n"
"};\n"
"\n"
"\n",
"jsdoc",
field->is_repeated() ? "Clears the list making it empty but non-null."
: "Clears the message field making it undefined.",
"jsdoc", field->is_repeated()
? "Clears the list making it empty but non-null."
: "Clears the message field making it undefined.",
"returndoc", JSReturnDoc(options, field),
"class", GetMessagePath(options, field->containing_type()),
"clearername", "clear" + JSGetterName(options, field), "settername",
"set" + JSGetterName(options, field), "clearedvalue",
(field->is_repeated() ? "[]" : "undefined"), "returnvalue",
JSReturnClause(field));
"clearername", "clear" + JSGetterName(options, field),
"settername", "set" + JSGetterName(options, field),
"clearedvalue", (field->is_repeated() ? "[]" : "undefined"),
"returnvalue", JSReturnClause(field));
// clang-format on
printer->Annotate("clearername", field);
} else if (HasFieldPresence(options, field)) {
// Fields where we can't delegate to the regular setter because it doesn't
// accept "undefined" as an argument.
// clang-format off
printer->Print(
"/** Clears the field making it undefined. */\n"
"/**\n"
" * Clears the field making it undefined.$returndoc$\n"
" */\n"
"$class$.prototype.$clearername$ = function() {\n"
" jspb.Message.set$maybeoneof$Field(this, "
"$index$$maybeoneofgroup$, ",
"returndoc", JSReturnDoc(options, field),
"class", GetMessagePath(options, field->containing_type()),
"clearername", "clear" + JSGetterName(options, field), "maybeoneof",
(field->containing_oneof() ? "Oneof" : ""), "maybeoneofgroup",
(field->containing_oneof() ? (", " + JSOneofArray(options, field))
: ""),
"clearername", "clear" + JSGetterName(options, field),
"maybeoneof", (field->containing_oneof() ? "Oneof" : ""),
"maybeoneofgroup", (field->containing_oneof()
? (", " + JSOneofArray(options, field))
: ""),
"index", JSFieldIndex(field));
// clang-format on
printer->Annotate("clearername", field);
printer->Print(
"$clearedvalue$);$returnvalue$\n"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment