diff --git a/src/Generation/GapicClientV2Generator.php b/src/Generation/GapicClientV2Generator.php index bf77523b..3cf48e34 100644 --- a/src/Generation/GapicClientV2Generator.php +++ b/src/Generation/GapicClientV2Generator.php @@ -414,7 +414,7 @@ private function resourceMethods(): Vector return Vector::new(); } $formattedName = AST::param(ResolvedType::string(), AST::var('formattedName')); - $template = AST::param(ResolvedType::string(), AST::var('template'), AST::NULL); + $template = AST::param(ResolvedType::string(true), AST::var('template'), AST::NULL); return $this->serviceDetails->resourceParts ->map(fn ($x) => $x->getFormatMethod() diff --git a/src/Utils/ResolvedType.php b/src/Utils/ResolvedType.php index 983a1d3e..fc0ecf91 100644 --- a/src/Utils/ResolvedType.php +++ b/src/Utils/ResolvedType.php @@ -18,6 +18,8 @@ namespace Google\Generator\Utils; +use Closure; + /** * Represent a resolved type, ready to use in code output. * This class is required to allow a resolved type to be differentiated from other plain strings. @@ -29,9 +31,9 @@ class ResolvedType * * @return ResolvedType */ - public static function array(): ResolvedType + public static function array(bool $optional = false): ResolvedType { - return new ResolvedType(Type::array(), fn () => 'array'); + return new ResolvedType(Type::array(), fn () => 'array', $optional); } /** @@ -39,9 +41,9 @@ public static function array(): ResolvedType * * @return ResolvedType */ - public static function string(): ResolvedType + public static function string(bool $optional = false): ResolvedType { - return new ResolvedType(Type::string(), fn () => 'string'); + return new ResolvedType(Type::string(), fn () => 'string', $optional); } /** @@ -69,19 +71,16 @@ public static function self(): ResolvedType * * @param string $typeName The resolved name of the type. */ - public function __construct(Type $type, \Closure $fnToCode) - { - $this->type = $type; - $this->fnToCode = $fnToCode; + public function __construct( + /** @var Type *Readonly* The type of this resolved-type. */ + public ReadOnly Type $type, + private Closure $fnToCode, + private bool $optional = false + ) { } - /** @var Type *Readonly* The type of this resolved-type. */ - public Type $type; - - private \Closure $fnToCode; - public function toCode(): string { - return ($this->fnToCode)(); + return ($this->optional ? '?' : '') . ($this->fnToCode)(); } } diff --git a/tests/Integration/goldens/functions/src/V1/Client/CloudFunctionsServiceClient.php b/tests/Integration/goldens/functions/src/V1/Client/CloudFunctionsServiceClient.php index 227a91b6..cd4add0e 100644 --- a/tests/Integration/goldens/functions/src/V1/Client/CloudFunctionsServiceClient.php +++ b/tests/Integration/goldens/functions/src/V1/Client/CloudFunctionsServiceClient.php @@ -267,14 +267,14 @@ public static function repositoryName(string $project, string $location, string * listed, then parseName will check each of the supported templates, and return * the first match. * - * @param string $formattedName The formatted name string - * @param string $template Optional name of template to match + * @param string $formattedName The formatted name string + * @param ?string $template Optional name of template to match * * @return array An associative array from name component IDs to component values. * * @throws ValidationException If $formattedName could not be matched. */ - public static function parseName(string $formattedName, string $template = null): array + public static function parseName(string $formattedName, ?string $template = null): array { return self::parseFormattedName($formattedName, $template); } diff --git a/tests/Integration/goldens/redis/src/V1/Client/CloudRedisClient.php b/tests/Integration/goldens/redis/src/V1/Client/CloudRedisClient.php index d0ace6cc..1e586773 100644 --- a/tests/Integration/goldens/redis/src/V1/Client/CloudRedisClient.php +++ b/tests/Integration/goldens/redis/src/V1/Client/CloudRedisClient.php @@ -244,14 +244,14 @@ public static function locationName(string $project, string $location): string * listed, then parseName will check each of the supported templates, and return * the first match. * - * @param string $formattedName The formatted name string - * @param string $template Optional name of template to match + * @param string $formattedName The formatted name string + * @param ?string $template Optional name of template to match * * @return array An associative array from name component IDs to component values. * * @throws ValidationException If $formattedName could not be matched. */ - public static function parseName(string $formattedName, string $template = null): array + public static function parseName(string $formattedName, ?string $template = null): array { return self::parseFormattedName($formattedName, $template); } diff --git a/tests/Integration/goldens/securitycenter/src/V1/Client/SecurityCenterClient.php b/tests/Integration/goldens/securitycenter/src/V1/Client/SecurityCenterClient.php index 6fb65850..247ffa02 100644 --- a/tests/Integration/goldens/securitycenter/src/V1/Client/SecurityCenterClient.php +++ b/tests/Integration/goldens/securitycenter/src/V1/Client/SecurityCenterClient.php @@ -1233,14 +1233,14 @@ public static function topicName(string $project, string $topic): string * listed, then parseName will check each of the supported templates, and return * the first match. * - * @param string $formattedName The formatted name string - * @param string $template Optional name of template to match + * @param string $formattedName The formatted name string + * @param ?string $template Optional name of template to match * * @return array An associative array from name component IDs to component values. * * @throws ValidationException If $formattedName could not be matched. */ - public static function parseName(string $formattedName, string $template = null): array + public static function parseName(string $formattedName, ?string $template = null): array { return self::parseFormattedName($formattedName, $template); } diff --git a/tests/Integration/goldens/spanner/src/V1/Client/DatabaseAdminClient.php b/tests/Integration/goldens/spanner/src/V1/Client/DatabaseAdminClient.php index 6253dbba..7018fdd0 100644 --- a/tests/Integration/goldens/spanner/src/V1/Client/DatabaseAdminClient.php +++ b/tests/Integration/goldens/spanner/src/V1/Client/DatabaseAdminClient.php @@ -304,14 +304,14 @@ public static function instanceName(string $project, string $instance): string * listed, then parseName will check each of the supported templates, and return * the first match. * - * @param string $formattedName The formatted name string - * @param string $template Optional name of template to match + * @param string $formattedName The formatted name string + * @param ?string $template Optional name of template to match * * @return array An associative array from name component IDs to component values. * * @throws ValidationException If $formattedName could not be matched. */ - public static function parseName(string $formattedName, string $template = null): array + public static function parseName(string $formattedName, ?string $template = null): array { return self::parseFormattedName($formattedName, $template); } diff --git a/tests/Unit/ProtoTests/ResourceNames/out/src/Client/ResourceNamesClient.php b/tests/Unit/ProtoTests/ResourceNames/out/src/Client/ResourceNamesClient.php index 378aa96e..f726cee3 100644 --- a/tests/Unit/ProtoTests/ResourceNames/out/src/Client/ResourceNamesClient.php +++ b/tests/Unit/ProtoTests/ResourceNames/out/src/Client/ResourceNamesClient.php @@ -421,14 +421,14 @@ public static function wildcardMultiPatternName(string $item1Id): string * listed, then parseName will check each of the supported templates, and return * the first match. * - * @param string $formattedName The formatted name string - * @param string $template Optional name of template to match + * @param string $formattedName The formatted name string + * @param ?string $template Optional name of template to match * * @return array An associative array from name component IDs to component values. * * @throws ValidationException If $formattedName could not be matched. */ - public static function parseName(string $formattedName, string $template = null): array + public static function parseName(string $formattedName, ?string $template = null): array { return self::parseFormattedName($formattedName, $template); }