Skip to content

Commit

Permalink
feat: enable edition for php
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed May 2, 2024
1 parent 05afdc2 commit 3b212ea
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 34 deletions.
32 changes: 32 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ load("//bazel:proto_library.bzl", "proto_library")
load("//bazel/toolchains:proto_lang_toolchain.bzl", "proto_lang_toolchain")
load("//build_defs:cpp_opts.bzl", "COPTS", "LINK_OPTS")
load(":protobuf.bzl", "internal_objc_proto_library", "internal_php_proto_library", "internal_py_proto_library")
load("//python:internal.bzl", "internal_copy_files")

licenses(["notice"])

Expand Down Expand Up @@ -615,6 +616,37 @@ internal_php_proto_library(
"//php:__subpackages__",
],
)
internal_copy_files(
name = "copied_conformance_editions_php_test_files",
testonly = 1,
srcs = [
"//editions:golden/test_messages_proto3_editions.proto",
],
strip_prefix = "editions/golden/",
)
internal_php_proto_library(
name = "test_messages_proto3_editions_php_proto",
testonly = 1,
srcs = [":copied_conformance_editions_php_test_files"],
outs = [
"GPBMetadata/TestMessagesProto3Editions.php",
"Protobuf_test_messages/Editions/Proto3/EnumOnlyProto3/PBBool.php",
"Protobuf_test_messages/Editions/Proto3/EnumOnlyProto3.php",
"Protobuf_test_messages/Editions/Proto3/TestAllTypesProto3/NestedMessage.php",
"Protobuf_test_messages/Editions/Proto3/TestAllTypesProto3/NestedEnum.php",
"Protobuf_test_messages/Editions/Proto3/TestAllTypesProto3/AliasedEnum.php",
"Protobuf_test_messages/Editions/Proto3/TestAllTypesProto3.php",
"Protobuf_test_messages/Editions/Proto3/NullHypothesisProto3.php",
"Protobuf_test_messages/Editions/Proto3/ForeignEnum.php",
"Protobuf_test_messages/Editions/Proto3/ForeignMessage.php",
],
includes = [
".",
"src"
],
proto_deps = [":well_known_protos"],
visibility = ["//conformance:__pkg__"],
)

filegroup(
name = "bzl_srcs",
Expand Down
1 change: 1 addition & 0 deletions conformance/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ inline_sh_binary(
deps = [
":conformance_php_proto",
"//:test_messages_proto3_php_proto",
"//:test_messages_proto3_editions_php_proto",
],
)

Expand Down
105 changes: 73 additions & 32 deletions conformance/conformance_php.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,18 @@
require_once("Protobuf_test_messages/Proto3/TestAllTypesProto3/AliasedEnum.php");
require_once("Protobuf_test_messages/Proto3/TestAllTypesProto3/NestedMessage.php");
require_once("Protobuf_test_messages/Proto3/TestAllTypesProto3/NestedEnum.php");

require_once("Protobuf_test_messages/Editions/Proto3/EnumOnlyProto3/PBBool.php");
require_once("Protobuf_test_messages/Editions/Proto3/EnumOnlyProto3.php");
require_once("Protobuf_test_messages/Editions/Proto3/TestAllTypesProto3/NestedMessage.php");
require_once("Protobuf_test_messages/Editions/Proto3/TestAllTypesProto3/NestedEnum.php");
require_once("Protobuf_test_messages/Editions/Proto3/TestAllTypesProto3/AliasedEnum.php");
require_once("Protobuf_test_messages/Editions/Proto3/TestAllTypesProto3.php");
require_once("Protobuf_test_messages/Editions/Proto3/NullHypothesisProto3.php");
require_once("Protobuf_test_messages/Editions/Proto3/ForeignEnum.php");
require_once("Protobuf_test_messages/Editions/Proto3/ForeignMessage.php");
require_once("GPBMetadata/Conformance.php");
require_once("GPBMetadata/TestMessagesProto3.php");
require_once("GPBMetadata/TestMessagesProto3Editions.php");

use \Conformance\TestCategory;
use \Conformance\WireFormat;
Expand All @@ -27,57 +36,89 @@

function doTest($request)
{
$test_message = new \Protobuf_test_messages\Proto3\TestAllTypesProto3();
$response = new \Conformance\ConformanceResponse();
if ($request->getPayload() == "protobuf_payload") {
if ($request->getMessageType() == "conformance.FailureSet") {
$response->setProtobufPayload("");
return $response;
} elseif ($request->getMessageType() == "protobuf_test_messages.proto3.TestAllTypesProto3") {
try {
$test_message->mergeFromString($request->getProtobufPayload());
} catch (Exception $e) {
$response->setParseError($e->getMessage());
$response = new \Conformance\ConformanceResponse();

switch ($request->getPayload()) {
case "protobuf_payload":
switch ($request->getMessageType()) {
case "protobuf_test_messages.proto3.TestAllTypesProto3":
$test_message = new \Protobuf_test_messages\Proto3\TestAllTypesProto3();
break;
case "protobuf_test_messages.editions.proto3.TestAllTypesProto3":
$test_message = new \Protobuf_test_messages\Editions\Proto3\TestAllTypesProto3();
break;
case "conformance.FailureSet":
$response->setProtobufPayload("");
return $response;
case "protobuf_test_messages.proto2.TestAllTypesProto2":
case "protobuf_test_messages.editions.proto2.TestAllTypesProto2":
$response->setSkipped("PHP doesn't support proto2");
return $response;
case "protobuf_test_messages.editions.TestAllTypesEdition2023":
$response->setSkipped("PHP doesn't support editions-specific features yet");
return $response;
}
} elseif ($request->getMessageType() == "protobuf_test_messages.proto2.TestAllTypesProto2") {
$response->setSkipped("PHP doesn't support proto2");
case "":
trigger_error("Protobuf request doesn't have specific payload type", E_USER_ERROR);
default:
trigger_error(
sprintf("Protobuf request doesn't support %s message type", $request->getMessageType()),
E_USER_ERROR
);
}

try {
$test_message->mergeFromString($request->getProtobufPayload());
} catch (Exception $e) {
$response->setParseError($e->getMessage());
return $response;
} else {
trigger_error("Protobuf request doesn't have specific payload type", E_USER_ERROR);
}
} elseif ($request->getPayload() == "json_payload") {
break;
case "json_payload":
switch ($request->getMessageType()) {
case "protobuf_test_messages.editions.proto3.TestAllTypesProto3":
$test_message = new \Protobuf_test_messages\Editions\Proto3\TestAllTypesProto3();
break;
case "protobuf_test_messages.editions.proto2.TestAllTypesProto2":
$response->setSkipped("PHP doesn't support proto2");
return $response;
default:
$test_message = new \Protobuf_test_messages\Proto3\TestAllTypesProto3();
}

$ignore_json_unknown =
($request->getTestCategory() ==
TestCategory::JSON_IGNORE_UNKNOWN_PARSING_TEST);
try {
$test_message->mergeFromJsonString($request->getJsonPayload(),
$ignore_json_unknown);
$ignore_json_unknown);
} catch (Exception $e) {
$response->setParseError($e->getMessage());
return $response;
}
} elseif ($request->getPayload() == "text_payload") {
$response->setSkipped("PHP doesn't support text format yet");
return $response;
} else {
break;
case "text_payload":
$response->setSkipped("PHP doesn't support text format yet");
return $response;
default:
trigger_error("Request didn't have payload.", E_USER_ERROR);
}
}

if ($request->getRequestedOutputFormat() == WireFormat::UNSPECIFIED) {
switch ($request->getRequestedOutputFormat()) {
case WireFormat::UNSPECIFIED:
trigger_error("Unspecified output format.", E_USER_ERROR);
} elseif ($request->getRequestedOutputFormat() == WireFormat::PROTOBUF) {
case WireFormat::PROTOBUF:
$response->setProtobufPayload($test_message->serializeToString());
} elseif ($request->getRequestedOutputFormat() == WireFormat::JSON) {
break;
case WireFormat::JSON:
try {
$response->setJsonPayload($test_message->serializeToJsonString());
$response->setJsonPayload($test_message->serializeToJsonString());
} catch (Exception $e) {
$response->setSerializeError($e->getMessage());
return $response;
$response->setSerializeError($e->getMessage());
return $response;
}
}
}

return $response;
return $response;
}

function doTestIO()
Expand Down
8 changes: 8 additions & 0 deletions conformance/text_format_failure_list_php.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@ Recommended.Proto3.ProtobufInput.RepeatedUnknownFields_Drop.TextFormatOutput
Recommended.Proto3.ProtobufInput.RepeatedUnknownFields_Print.TextFormatOutput
Recommended.Proto3.ProtobufInput.ScalarUnknownFields_Drop.TextFormatOutput
Recommended.Proto3.ProtobufInput.ScalarUnknownFields_Print.TextFormatOutput
Recommended.Editions_Proto3.ProtobufInput.GroupUnknownFields_Drop.TextFormatOutput
Recommended.Editions_Proto3.ProtobufInput.GroupUnknownFields_Print.TextFormatOutput
Recommended.Editions_Proto3.ProtobufInput.MessageUnknownFields_Drop.TextFormatOutput
Recommended.Editions_Proto3.ProtobufInput.MessageUnknownFields_Print.TextFormatOutput
Recommended.Editions_Proto3.ProtobufInput.RepeatedUnknownFields_Drop.TextFormatOutput
Recommended.Editions_Proto3.ProtobufInput.RepeatedUnknownFields_Print.TextFormatOutput
Recommended.Editions_Proto3.ProtobufInput.ScalarUnknownFields_Drop.TextFormatOutput
Recommended.Editions_Proto3.ProtobufInput.ScalarUnknownFields_Print.TextFormatOutput
1 change: 1 addition & 0 deletions editions/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ exports_files(
visibility = [
"//ruby:__pkg__",
"//src/google/protobuf/csharp:__pkg__",
"//:__pkg__",
],
)

Expand Down
2 changes: 2 additions & 0 deletions php/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ conformance_test(
"@platforms//os:osx": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
maximum_edition = "2023",
testee = "//conformance:conformance_php",
text_format_failure_list = "//conformance:text_format_failure_list_php.txt",
)
Expand All @@ -79,6 +80,7 @@ conformance_test(
"@platforms//os:osx": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
maximum_edition = "2023",
testee = "//conformance:conformance_php_c",
text_format_failure_list = "//conformance:text_format_failure_list_php.txt",
)
Expand Down
3 changes: 2 additions & 1 deletion php/generate_test_protos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ if [[ -z "${PROTOC}" ]]; then
fi
if [ ! -f $PROTOC ]; then
${BAZEL:-bazel} $BAZEL_STARTUP_FLAGS build -c opt //:protoc $BAZEL_FLAGS
PROTOC=$(pwd)/bazel-bin/protoc
PROTOC="$(pwd)/bazel-bin/protoc --experimental_editions"
echo "USING LOCAL PROTOC ($PROTOC)"
fi

if [[ -d php/tmp && -z $(find php/tests/proto $PROTOC -newer php/tmp) ]]; then
Expand Down
8 changes: 7 additions & 1 deletion src/google/protobuf/compiler/php/php_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ class PROTOC_EXPORT Generator : public CodeGenerator {
std::string* error) const override;

uint64_t GetSupportedFeatures() const override {
return FEATURE_PROTO3_OPTIONAL;
return Feature::FEATURE_PROTO3_OPTIONAL | Feature::FEATURE_SUPPORTS_EDITIONS;
}

Edition GetMinimumEdition() const override { return Edition::EDITION_PROTO2; }
Edition GetMaximumEdition() const override { return Edition::EDITION_2023; }
std::vector<const FieldDescriptor*> GetFeatureExtensions() const override {
return {};
}

private:
Expand Down

0 comments on commit 3b212ea

Please sign in to comment.