Skip to content

Commit

Permalink
check custom map support
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Oct 9, 2024
1 parent 7ba8909 commit 44ed371
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
20 changes: 20 additions & 0 deletions src/Generator/CodeGeneratorAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ private function generateStruct(string $className, StructDefinitionType $type):
$property = $this->replaceGeneric($property, $templates);
}

// in case the generator does not support custom map or array implementations we transform them to properties
if ($property instanceof ReferencePropertyType) {
$targetType = $this->definitions->getType($property->getTarget());
if ($targetType instanceof MapDefinitionType && !$this->supportsMap($name, $targetType)) {
$property = PropertyTypeFactory::getMap($targetType->getSchema());
} elseif ($targetType instanceof ArrayDefinitionType && !$this->supportsArray($name, $targetType)) {
$property = PropertyTypeFactory::getArray($targetType->getSchema());
}
}

$props[] = new Code\Property(
$name,
$this->generator->getType($property),
Expand Down Expand Up @@ -269,6 +279,16 @@ protected function supportsExtends(): bool
return true;
}

private function supportsMap(Code\Name $name, MapDefinitionType $type): bool
{
return !!$this->writeMap($name, $this->generator->getType($type->getSchema()), $type);
}

private function supportsArray(Code\Name $name, ArrayDefinitionType $type): bool
{
return !!$this->writeArray($name, $this->generator->getType($type->getSchema()), $type);
}

abstract protected function writeStruct(Code\Name $name, array $properties, ?string $extends, ?array $generics, ?array $templates, StructDefinitionType $origin): string;

protected function writeMap(Code\Name $name, string $type, MapDefinitionType $origin): string
Expand Down
4 changes: 2 additions & 2 deletions tests/Generator/resource/graphql/graphql.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ type Author {
}

type News {
config: Meta
config: [String]
inlineConfig: [String]
mapTags: [String]
mapReceiver: [Author]
tags: [String]
receiver: [Author]
read: Boolean
author: Author
meta: Meta
meta: [String]
sendDate: String
readDate: String
price: Float
Expand Down
4 changes: 2 additions & 2 deletions tests/Generator/resource/protobuf/protobuf.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ message Author {
}

message News {
optional Meta config = 1 [json_name="config"];
optional map<string, string> config = 1 [json_name="config"];
optional map<string, string> inlineConfig = 2 [json_name="inlineConfig"];
optional map<string, string> mapTags = 3 [json_name="mapTags"];
optional map<string, Author> mapReceiver = 4 [json_name="mapReceiver"];
optional repeated string tags = 5 [json_name="tags"];
optional repeated Author receiver = 6 [json_name="receiver"];
optional bool read = 7 [json_name="read"];
optional Author author = 8 [json_name="author"];
optional Meta meta = 9 [json_name="meta"];
optional map<string, string> meta = 9 [json_name="meta"];
optional Timestamp sendDate = 10 [json_name="sendDate"];
optional Timestamp readDate = 11 [json_name="readDate"];
optional float price = 12 [json_name="price"];
Expand Down

0 comments on commit 44ed371

Please sign in to comment.