Skip to content

Commit

Permalink
Merge pull request aws#667 from jeskew/feature/cache-member-shape-lis…
Browse files Browse the repository at this point in the history
…t-on-structures

Cache the members list in StructureShape
  • Loading branch information
jeskew committed Jul 1, 2015
2 parents ed94ff4 + bfc32a0 commit e9614c3
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/Api/StructureShape.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
*/
class StructureShape extends Shape
{
/**
* @var Shape[]
*/
private $members;

public function __construct(array $definition, ShapeMap $shapeMap)
{
$definition['type'] = 'structure';
Expand All @@ -24,12 +29,11 @@ public function __construct(array $definition, ShapeMap $shapeMap)
*/
public function getMembers()
{
$result = [];
foreach ($this->definition['members'] as $name => $definition) {
$result[$name] = $this->shapeFor($definition);
if (empty($this->members)) {
$this->generateMembersHash();
}

return $result;
return $this->members;
}

/**
Expand All @@ -54,10 +58,22 @@ public function hasMember($name)
*/
public function getMember($name)
{
if (!isset($this->definition['members'][$name])) {
$members = $this->getMembers();

if (!isset($members[$name])) {
throw new \InvalidArgumentException('Unknown member ' . $name);
}

return $this->shapeFor($this->definition['members'][$name]);
return $members[$name];
}


private function generateMembersHash()
{
$this->members = [];

foreach ($this->definition['members'] as $name => $definition) {
$this->members[$name] = $this->shapeFor($definition);
}
}
}

0 comments on commit e9614c3

Please sign in to comment.