From 18a0c2c4d2894e820f96494f33d8ca3ea33dec3a Mon Sep 17 00:00:00 2001 From: Yilun Chong Date: Tue, 27 Jun 2017 18:24:15 -0700 Subject: add proto2 supported for cpp,python,nodejs,ruby,php --- conformance/conformance_python.py | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) (limited to 'conformance/conformance_python.py') diff --git a/conformance/conformance_python.py b/conformance/conformance_python.py index 7ace9b16..846ccbc6 100755 --- a/conformance/conformance_python.py +++ b/conformance/conformance_python.py @@ -41,6 +41,7 @@ import os 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 import conformance_pb2 sys.stdout = os.fdopen(sys.stdout.fileno(), 'wb', 0) @@ -56,14 +57,26 @@ def do_test(request): test_message = test_messages_proto3_pb2.TestAllTypes() response = conformance_pb2.ConformanceResponse() test_message = test_messages_proto3_pb2.TestAllTypes() + test_message_proto2 = test_messages_proto2_pb2.TestAllTypesProto2() + isProto3 = (request.message_type == "proto3") + isJson = (request.WhichOneof('payload') == 'json_payload') try: if request.WhichOneof('payload') == 'protobuf_payload': - try: - test_message.ParseFromString(request.protobuf_payload) - except message.DecodeError as e: - response.parse_error = str(e) - return response + if isProto3: + try: + test_message.ParseFromString(request.protobuf_payload) + except message.DecodeError as e: + response.parse_error = str(e) + return response + elif request.message_type == "proto2": + try: + test_message_proto2.ParseFromString(request.protobuf_payload) + except message.DecodeError as e: + response.parse_error = str(e) + return response + else: + raise ProtocolError("Protobuf request doesn't have specific payload type") elif request.WhichOneof('payload') == 'json_payload': try: @@ -79,11 +92,17 @@ def do_test(request): raise ProtocolError("Unspecified output format") elif request.requested_output_format == conformance_pb2.PROTOBUF: - response.protobuf_payload = test_message.SerializeToString() + if isProto3 or isJson: + response.protobuf_payload = test_message.SerializeToString() + else: + response.protobuf_payload = test_message_proto2.SerializeToString() elif request.requested_output_format == conformance_pb2.JSON: try: - response.json_payload = json_format.MessageToJson(test_message) + if isProto3 or isJson: + response.json_payload = json_format.MessageToJson(test_message) + else: + response.json_payload = json_format.MessageToJson(test_message_proto2) except Exception as e: response.serialize_error = str(e) return response -- cgit v1.2.3