Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: numeric enum option generation #497

Merged
merged 10 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions rules_php_gapic/php_gapic.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def php_gapic_srcjar(
service_yaml = None,
grpc_service_config = None,
transport = None,
rest_numeric_enums = False,
generator_binary = Label("//rules_php_gapic:php_gapic_generator_binary"),
**kwargs):
plugin_file_args = {}
Expand All @@ -70,9 +71,14 @@ def php_gapic_srcjar(
if transport != "grpc+rest" and transport != "rest":
fail("Error: Only 'grpc+rest' or 'rest' transports are supported")


# Set plugin arguments.
plugin_args = ["metadata"] # Generate the gapic_metadata.json file.
plugin_args.append("transport=%s" % transport)

# Generate REGAPIC param for requesting response enums be JSON-encoded as numbers, not strings.
if rest_numeric_enums:
plugin_args.append("rest-numeric-enums")

proto_custom_library(
name = name,
Expand Down Expand Up @@ -106,6 +112,7 @@ def php_gapic_library(
service_yaml = None,
grpc_service_config = None,
transport = None,
rest_numeric_enums = False,
generator_binary = Label("//rules_php_gapic:php_gapic_generator_binary"),
**kwargs):
srcjar_name = "%s_srcjar" % name
Expand All @@ -117,6 +124,7 @@ def php_gapic_library(
service_yaml = service_yaml,
grpc_service_config = grpc_service_config,
transport = transport,
rest_numeric_enums = rest_numeric_enums,
generator_binary = generator_binary,
**kwargs
)
Expand Down
15 changes: 11 additions & 4 deletions src/CodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class CodeGenerator
* @param ?string $grpcServiceConfigJson Optional grpc-serv-config json string.
* @param ?string $gapicYaml Optional gapic configuration yaml string.
* @param ?string $serviceYaml Optional service configuration yaml string.
* @param bool $numericEnums Whether to generate the numeric-enums JSON encoding system parameter.
* @param int $licenseYear The year to use in license headers.
*
* @return string[]
Expand All @@ -68,6 +69,7 @@ public static function generateFromDescriptor(
?string $grpcServiceConfigJson,
?string $gapicYaml,
?string $serviceYaml,
bool $numericEnums = false,
int $licenseYear = -1
) {
$descSet = new FileDescriptorSet();
Expand All @@ -76,7 +78,7 @@ public static function generateFromDescriptor(
$filesToGenerate = $fileDescs
->filter(fn ($x) => substr($x->getPackage(), 0, strlen($package)) === $package)
->map(fn ($x) => $x->getName());
yield from static::generate($fileDescs, $filesToGenerate, $transport, $generateGapicMetadata, $grpcServiceConfigJson, $gapicYaml, $serviceYaml, $licenseYear);
yield from static::generate($fileDescs, $filesToGenerate, $transport, $generateGapicMetadata, $grpcServiceConfigJson, $gapicYaml, $serviceYaml, $numericEnums, $licenseYear);
}

/**
Expand All @@ -91,6 +93,8 @@ public static function generateFromDescriptor(
* @param ?string $grpcServiceConfigJson Optional grpc-serv-config json string.
* @param ?string $gapicYaml Optional gapic configuration yaml string.
* @param ?string $serviceYaml Optional service configuration yaml string.
* @param bool $numericEnums Optional whether to include in requests the system parameter enabling JSON-encoded
* responses to encode enum values as numbers.
* @param int $licenseYear The year to use in license headers.
*
* @return array[] [0] (string) is relative path; [1] (string) is file content.
Expand All @@ -103,6 +107,7 @@ public static function generate(
?string $grpcServiceConfigJson,
?string $gapicYaml,
?string $serviceYaml,
bool $numericEnums = false,
int $licenseYear = -1
) {
if ($licenseYear < 0) {
Expand Down Expand Up @@ -221,7 +226,8 @@ public static function generate(
$gapicYaml,
$serviceYamlConfig,
$generateGapicMetadata,
$licenseYear
$licenseYear,
$numericEnums
) as $file) {
$result[] = $file;
}
Expand All @@ -245,7 +251,8 @@ private static function generateServices(
?string $gapicYaml,
?ServiceYamlConfig $serviceYamlConfig,
bool $generateGapicMetadata,
int $licenseYear
int $licenseYear,
bool $numericEnums = false
) {
$versionToNamespace = [];
foreach ($servicesToGenerate as $service) {
Expand Down Expand Up @@ -306,7 +313,7 @@ private static function generateServices(
$code = Formatter::format($code);
yield ["src/{$version}resources/{$service->descriptorConfigFilename}", $code];
// Resource: rest_client_config.php
$code = ResourcesGenerator::generateRestConfig($service, $serviceYamlConfig);
$code = ResourcesGenerator::generateRestConfig($service, $serviceYamlConfig, $numericEnums);
$code = Formatter::format($code);
yield ["src/{$version}resources/{$service->restConfigFilename}", $code];
// Resource: client_config.json
Expand Down
14 changes: 10 additions & 4 deletions src/Generation/ResourcesGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private static function restMethodDetails(ProtoCatalog $catalog, $methodOrMethod
return AST::array($methodDetails);
}

public static function generateRestConfig(ServiceDetails $serviceDetails, ServiceYamlConfig $serviceYamlConfig): string
public static function generateRestConfig(ServiceDetails $serviceDetails, ServiceYamlConfig $serviceYamlConfig, $numericEnums = false): string
{
$allInterfaces = static::compileRestConfigInterfaces($serviceDetails, $serviceYamlConfig);
if ($serviceDetails->hasCustomOp) {
Expand All @@ -198,11 +198,17 @@ public static function generateRestConfig(ServiceDetails $serviceDetails, Servic
$opInter = static::compileRestConfigInterfaces($customOpDetails, $serviceYamlConfig);
$allInterfaces = array_merge($allInterfaces, $opInter);
}

$config = [
'interfaces' => AST::array($allInterfaces)
];

if ($numericEnums) {
$config['numericEnums'] = true;
}

$return = AST::return(
AST::array([
'interfaces' => AST::array($allInterfaces)
])
AST::array($config)
);
return "<?php\n\n{$return->toCode()};";
}
Expand Down
8 changes: 5 additions & 3 deletions src/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function showUsageAndExit()
$descBytes = file_get_contents($opts['descriptor']);
$package = $opts['package'];
$outputDir = $opts['output'];
[$grpcServiceConfig, $gapicYaml, $serviceYaml, $transport, $generateGapicMetadata] = readOptions($opts);
[$grpcServiceConfig, $gapicYaml, $serviceYaml, $transport, $generateGapicMetadata, $numericEnums] = readOptions($opts);

// Generate PHP code.
$files = CodeGenerator::generateFromDescriptor(
Expand All @@ -148,7 +148,8 @@ function showUsageAndExit()
$generateGapicMetadata,
$grpcServiceConfig,
$gapicYaml,
$serviceYaml
$serviceYaml,
$numericEnums
);

if (is_null($outputDir)) {
Expand Down Expand Up @@ -218,6 +219,7 @@ function readOptions($opts, $sideLoadedRootDir = null)

// Flip the flag value because PHP is weird: the presence of the --metadata flag evaluates to false.
$generateGapicMetadata = (isset($opts['metadata']) && !$opts['metadata']);
$numericEnums = (isset($opts['rest-numeric-enums']) && !$opts['rest-numeric-enums']);

return [$grpcServiceConfig, $gapicYaml, $serviceYaml, $transport, $generateGapicMetadata];
return [$grpcServiceConfig, $gapicYaml, $serviceYaml, $transport, $generateGapicMetadata, $numericEnums];
}
2 changes: 2 additions & 0 deletions tests/Tools/GeneratorUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public static function generateFromProto(string $protoPath, ?string $package = n
$grpcServiceConfigJson = ConfigLoader::loadConfig("{$protoDirName}/grpc-service-config.json");
$gapicYaml = ConfigLoader::loadConfig("{$protoDirName}/{$baseName}_gapic.yaml");
$serviceYaml = ConfigLoader::loadConfig("{$protoDirName}/{$baseName}_service.yaml");
$numericEnums = true;

$licenseYear = 2022; // Avoid updating tests all the time.
$generateGapicMetadata = true;
Expand All @@ -55,6 +56,7 @@ public static function generateFromProto(string $protoPath, ?string $package = n
$grpcServiceConfigJson,
$gapicYaml,
$serviceYaml,
$numericEnums,
$licenseYear
);
return $codeIterator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
],
],
],
'numericEnums' => true,
];
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

return [
'interfaces' => [],
'numericEnums' => true,
];
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

return [
'interfaces' => [],
'numericEnums' => true,
];
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,5 @@
],
],
],
'numericEnums' => true,
];
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
],
],
],
'numericEnums' => true,
];
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
],
],
],
'numericEnums' => true,
];
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
],
],
],
'numericEnums' => true,
];
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
],
],
],
'numericEnums' => true,
];
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
],
],
],
'numericEnums' => true,
];
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@
],
],
],
'numericEnums' => true,
];
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

return [
'interfaces' => [],
'numericEnums' => true,
];
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@
],
],
],
'numericEnums' => true,
];
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@
],
],
],
'numericEnums' => true,
];
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,5 @@
],
],
],
'numericEnums' => true,
];