Skip to content

Commit d4c0493

Browse files
authored
Add suport for list struct in requests with headers (#1500)
1 parent a4e80bf commit d4c0493

File tree

1 file changed

+95
-44
lines changed

1 file changed

+95
-44
lines changed

src/Generator/InputGenerator.php

Lines changed: 95 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use AsyncAws\CodeGenerator\Definition\MapShape;
99
use AsyncAws\CodeGenerator\Definition\Member;
1010
use AsyncAws\CodeGenerator\Definition\Operation;
11+
use AsyncAws\CodeGenerator\Definition\StructureMember;
1112
use AsyncAws\CodeGenerator\Definition\StructureShape;
1213
use AsyncAws\CodeGenerator\Generator\CodeGenerator\TypeGenerator;
1314
use AsyncAws\CodeGenerator\Generator\Composer\RequirementsRegistry;
@@ -304,6 +305,84 @@ public function generate(Operation $operation): ClassName
304305
return $className;
305306
}
306307

308+
private function inputClassRequestPartGettersEnumGenerator(Member $member, string $requestPart, string $input): string
309+
{
310+
$memberShape = $member->getShape();
311+
$validateEnum = '';
312+
if (!empty($memberShape->getEnum())) {
313+
$enumClassName = $this->namespaceRegistry->getEnum($memberShape);
314+
$validateEnum = strtr('if (!ENUM_CLASS::exists(VALUE)) {
315+
throw new InvalidArgument(sprintf(\'Invalid parameter "MEMBER_NAME" for "%s". The value "%s" is not a valid "ENUM_CLASS".\', __CLASS__, INPUT));
316+
}', [
317+
'VALUE' => $this->stringify($input, $member, $requestPart),
318+
'ENUM_CLASS' => $enumClassName->getName(),
319+
'INPUT' => $input,
320+
]);
321+
}
322+
323+
return $validateEnum;
324+
}
325+
326+
private function inputClassRequestPartGettersHookGenerator(StructureMember $member, Operation $operation, string $requestPart, string $input): string
327+
{
328+
$applyHook = '';
329+
foreach ($operation->getService()->getHooks($requestPart . '_parameters') as $hook) {
330+
if (!\in_array($member->getLocationName() ?? $member->getName(), $hook->getFilters())) {
331+
continue;
332+
}
333+
334+
$applyHook .= $this->hookGenerator->generate($hook, 'VALUE');
335+
}
336+
337+
if (!$applyHook) {
338+
return '';
339+
}
340+
341+
return strtr($applyHook, [
342+
'VALUE' => $this->stringify($input, $member, $requestPart),
343+
]);
344+
}
345+
346+
private function inputClassRequestPartGetters(StructureMember $member, Operation $operation, string $requestPart, string $input, string $output): string
347+
{
348+
$memberShape = $member->getShape();
349+
if ($memberShape instanceof ListShape) {
350+
if ('header' !== $requestPart) {
351+
throw new \InvalidArgumentException(sprintf('ListShape in request part "%s" is not yet implemented', $requestPart));
352+
}
353+
354+
$bodyCode = '
355+
APPLY_HOOK
356+
$items = [];
357+
foreach (INPUT as $value) {
358+
VALIDATE_ENUM
359+
$items[] = VALUE;
360+
}
361+
OUTPUT = implode(\',\', $items);
362+
';
363+
364+
return strtr($bodyCode, [
365+
'INPUT' => $input,
366+
'OUTPUT' => $output,
367+
'VALUE' => $this->stringify('$value', $memberShape->getMember(), $requestPart),
368+
'VALIDATE_ENUM' => $this->inputClassRequestPartGettersEnumGenerator($memberShape->getMember(), $requestPart, '$value'),
369+
'APPLY_HOOK' => $this->inputClassRequestPartGettersHookGenerator($member, $operation, $requestPart, $input),
370+
]);
371+
}
372+
373+
$bodyCode = '
374+
VALIDATE_ENUM
375+
APPLY_HOOK
376+
OUTPUT = VALUE;';
377+
378+
return strtr($bodyCode, [
379+
'VALIDATE_ENUM' => $this->inputClassRequestPartGettersEnumGenerator($member, $requestPart, $input),
380+
'APPLY_HOOK' => $this->inputClassRequestPartGettersHookGenerator($member, $operation, $requestPart, $input),
381+
'OUTPUT' => $output,
382+
'VALUE' => $this->stringify($input, $member, $requestPart),
383+
]);
384+
}
385+
307386
private function inputClassRequestGetters(StructureShape $inputShape, ClassBuilder $classBuilder, Operation $operation): void
308387
{
309388
$serializer = $this->serializer->get($operation->getService());
@@ -324,67 +403,39 @@ private function inputClassRequestGetters(StructureShape $inputShape, ClassBuild
324403
if ($requestPart !== $member->getLocation()) {
325404
continue;
326405
}
327-
328406
if ('querystring' === $requestPart && $usesEndpointDiscovery) {
329407
if (0 !== strpos($classBuilder->getClassName()->getFqdn(), 'AsyncAws\Core\\')) {
330408
$this->requirementsRegistry->addRequirement('async-aws/core', '^1.19');
331409
}
332410
}
333411

334-
$memberShape = $member->getShape();
412+
if (!isset($body[$requestPart])) {
413+
$body[$requestPart] = $varName . ' = [];' . "\n";
414+
}
415+
335416
if ($member->isRequired()) {
336417
$bodyCode = 'if (null === $v = $this->PROPERTY) {
337-
throw new InvalidArgument(sprintf(\'Missing parameter "NAME" for "%s". The value cannot be null.\', __CLASS__));
418+
throw new InvalidArgument(sprintf(\'Missing parameter "MEMBER_NAME" for "%s". The value cannot be null.\', __CLASS__));
338419
}
339-
VALIDATE_ENUM
340-
APPLY_HOOK
341-
VAR_NAME["LOCATION"] = VALUE;';
342-
$inputElement = '$v';
420+
GETTER_CODE';
421+
$input = '$v';
422+
$output = 'VAR_NAME["LOCATION"]';
343423
} else {
344424
$bodyCode = 'if (null !== $this->PROPERTY) {
345-
VALIDATE_ENUM
346-
APPLY_HOOK
347-
VAR_NAME["LOCATION"] = VALUE;
425+
GETTER_CODE
348426
}';
349-
$inputElement = '$this->' . GeneratorHelper::normalizeName($member->getName());
427+
$input = '$this->PROPERTY';
428+
$output = 'VAR_NAME["LOCATION"]';
350429
}
351-
$validateEnum = '';
352-
if (!empty($memberShape->getEnum())) {
353-
$enumClassName = $this->namespaceRegistry->getEnum($memberShape);
354-
$validateEnum = strtr('if (!ENUM_CLASS::exists(VALUE)) {
355-
throw new InvalidArgument(sprintf(\'Invalid parameter "NAME" for "%s". The value "%s" is not a valid "ENUM_CLASS".\', __CLASS__, $this->PROPERTY));
356-
}', [
357-
'VALUE' => $this->stringify($inputElement, $member, $requestPart),
358-
'ENUM_CLASS' => $enumClassName->getName(),
359-
'PROPERTY' => GeneratorHelper::normalizeName($member->getName()),
360-
'NAME' => $member->getName(),
361-
]);
362-
}
363-
364-
$applyHook = '';
365-
foreach ($operation->getService()->getHooks($requestPart . '_parameters') as $hook) {
366-
if (!\in_array($member->getLocationName() ?? $member->getName(), $hook->getFilters())) {
367-
continue;
368-
}
369-
370-
$applyHook .= $this->hookGenerator->generate($hook, 'VALUE');
371-
}
372-
$applyHook = strtr($applyHook, [
373-
'VALUE' => $this->stringify($inputElement, $member, $requestPart),
374-
]);
375430

376-
$bodyCode = strtr($bodyCode, [
377-
'PROPERTY' => GeneratorHelper::normalizeName($member->getName()),
378-
'NAME' => $member->getName(),
431+
$bodyCode = strtr(strtr($bodyCode, [
432+
'GETTER_CODE' => $this->inputClassRequestPartGetters($member, $operation, $requestPart, $input, $output),
433+
]), [
434+
'MEMBER_NAME' => $member->getName(),
379435
'VAR_NAME' => $varName,
436+
'PROPERTY' => GeneratorHelper::normalizeName($member->getName()),
380437
'LOCATION' => $member->getLocationName() ?? $member->getName(),
381-
'VALIDATE_ENUM' => $validateEnum,
382-
'APPLY_HOOK' => $applyHook,
383-
'VALUE' => $this->stringify($inputElement, $member, $requestPart),
384438
]);
385-
if (!isset($body[$requestPart])) {
386-
$body[$requestPart] = $varName . ' = [];' . "\n";
387-
}
388439
$body[$requestPart] .= $bodyCode . "\n";
389440
}
390441
}

0 commit comments

Comments
 (0)