Skip to content

Commit

Permalink
fix handle generic template names and improve oop test case
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Nov 4, 2024
1 parent 1430b1b commit 8a63d01
Show file tree
Hide file tree
Showing 57 changed files with 181 additions and 161 deletions.
16 changes: 15 additions & 1 deletion src/Generator/CodeGeneratorAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private function generateStruct(string $className, StructDefinitionType $type):
$type->setProperties($properties);
}

$code = $this->writeStruct($className, $props, $parent, $generics, $templates, $type);
$code = $this->writeStruct($className, $props, $parent, $generics, $this->buildTemplates($templates), $type);

if (!empty($code)) {
$this->chunks->append($className->getFile(), $this->wrap($code, $type, $className));
Expand Down Expand Up @@ -253,6 +253,20 @@ private function replaceGeneric(PropertyTypeAbstract $type, array $templates): ?
return $type;
}

private function buildTemplates(?array $templates): ?array
{
if (empty($templates)) {
return null;
}

$result = [];
foreach ($templates as $name => $type) {
$result[$name] = $this->normalizer->class($type);
}

return $result;
}

private function wrap(string $code, DefinitionTypeAbstract $type, Code\Name $className): string
{
return implode("\n", array_filter([
Expand Down
2 changes: 1 addition & 1 deletion tests/Generator/resource/csharp/oop/HumanMap.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;

public class HumanMap : Map<Human, Human>
public class HumanMap : Map<HumanType, HumanType>
{
}

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Text.Json.Serialization;

public class Human
public class HumanType
{
[JsonPropertyName("firstName")]
public string? FirstName { get; set; }

[JsonPropertyName("parent")]
public Human? Parent { get; set; }
public HumanType? Parent { get; set; }

}

2 changes: 1 addition & 1 deletion tests/Generator/resource/csharp/oop/Student.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;

public class Student : Human
public class Student : HumanType
{
[JsonPropertyName("matricleNumber")]
public string? MatricleNumber { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion tests/Generator/resource/csharp/oop/StudentMap.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;

public class StudentMap : Map<Human, Student>
public class StudentMap : Map<HumanType, Student>
{
}

2 changes: 1 addition & 1 deletion tests/Generator/resource/go/import/my_map.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type MyMap struct {
MatricleNumber string `json:"matricleNumber"`
FirstName string `json:"firstName"`
Parent *Human `json:"parent"`
Parent *HumanType `json:"parent"`
}

2 changes: 1 addition & 1 deletion tests/Generator/resource/go/namespace/my_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package Foo.Bar
type MyMap struct {
MatricleNumber string `json:"matricleNumber"`
FirstName string `json:"firstName"`
Parent *My.Import.Human `json:"parent"`
Parent *My.Import.HumanType `json:"parent"`
}

5 changes: 0 additions & 5 deletions tests/Generator/resource/go/oop/human.go

This file was deleted.

4 changes: 2 additions & 2 deletions tests/Generator/resource/go/oop/human_map.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type HumanMap struct {
TotalResults int `json:"totalResults"`
Parent *Human `json:"parent"`
Entries []Human `json:"entries"`
Parent *HumanType `json:"parent"`
Entries []HumanType `json:"entries"`
}

5 changes: 5 additions & 0 deletions tests/Generator/resource/go/oop/human_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type HumanType struct {
FirstName string `json:"firstName"`
Parent *HumanType `json:"parent"`
}

2 changes: 1 addition & 1 deletion tests/Generator/resource/go/oop/student.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type Student struct {
FirstName string `json:"firstName"`
Parent *Human `json:"parent"`
Parent *HumanType `json:"parent"`
MatricleNumber string `json:"matricleNumber"`
}

2 changes: 1 addition & 1 deletion tests/Generator/resource/go/oop/student_map.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type StudentMap struct {
TotalResults int `json:"totalResults"`
Parent *Human `json:"parent"`
Parent *HumanType `json:"parent"`
Entries []Student `json:"entries"`
}

14 changes: 10 additions & 4 deletions tests/Generator/resource/html/html_oop.htm
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
<div id="Human" class="psx-object psx-struct"><h1><a class="psx-type-link" data-name="Human">Human</a></h1><pre class="psx-object-json"><span class="psx-object-json-pun">{</span>
<div id="HumanType" class="psx-object psx-struct"><h1><a class="psx-type-link" data-name="HumanType">HumanType</a></h1><pre class="psx-object-json"><span class="psx-object-json-pun">{</span>
<span class="psx-object-json-key">"firstName"</span><span class="psx-object-json-pun">: </span><span class="psx-property-type">String</span><span class="psx-object-json-pun">,</span>
<span class="psx-object-json-key">"parent"</span><span class="psx-object-json-pun">: </span><span class="psx-property-type">Human</span><span class="psx-object-json-pun">,</span>
<span class="psx-object-json-pun">}</span></pre><table class="table psx-object-properties"><colgroup><col width="30%" /><col width="70%" /></colgroup><thead><tr><th>Field</th><th>Description</th></tr></thead><tbody><tr><td><span class="psx-property-name ">firstName</span></td><td><span class="psx-property-type"><a class="psx-type-link" data-name="String">String</a></span><br /><div class="psx-property-description"></div></td></tr><tr><td><span class="psx-property-name ">parent</span></td><td><span class="psx-property-type"><a class="psx-type-link" data-name="Human">Human</a></span><br /><div class="psx-property-description"></div></td></tr></tbody></table></div>
<span class="psx-object-json-key">"parent"</span><span class="psx-object-json-pun">: </span><span class="psx-property-type">Human_Type</span><span class="psx-object-json-pun">,</span>
<span class="psx-object-json-pun">}</span></pre><table class="table psx-object-properties"><colgroup><col width="30%" /><col width="70%" /></colgroup><thead><tr><th>Field</th><th>Description</th></tr></thead><tbody><tr><td><span class="psx-property-name ">firstName</span></td><td><span class="psx-property-type"><a class="psx-type-link" data-name="String">String</a></span><br /><div class="psx-property-description"></div></td></tr><tr><td><span class="psx-property-name ">parent</span></td><td><span class="psx-property-type"><a class="psx-type-link" data-name="Human_Type">Human_Type</a></span><br /><div class="psx-property-description"></div></td></tr></tbody></table></div>

<div id="Student" class="psx-object psx-struct"><h1><a class="psx-type-link" data-name="Student">Student</a> extends <a class="psx-type-link" data-name="Human">Human</a></h1><pre class="psx-object-json"><span class="psx-object-json-pun">{</span>

<div id="Student" class="psx-object psx-struct"><h1><a class="psx-type-link" data-name="Student">Student</a> extends <a class="psx-type-link" data-name="Human_Type">Human_Type</a></h1><pre class="psx-object-json"><span class="psx-object-json-pun">{</span>
<span class="psx-object-json-key">"matricleNumber"</span><span class="psx-object-json-pun">: </span><span class="psx-property-type">String</span><span class="psx-object-json-pun">,</span>
<span class="psx-object-json-pun">}</span></pre><table class="table psx-object-properties"><colgroup><col width="30%" /><col width="70%" /></colgroup><thead><tr><th>Field</th><th>Description</th></tr></thead><tbody><tr><td><span class="psx-property-name ">matricleNumber</span></td><td><span class="psx-property-type"><a class="psx-type-link" data-name="String">String</a></span><br /><div class="psx-property-description"></div></td></tr></tbody></table></div>


<div id="Map" class="psx-object psx-struct"><h1><a class="psx-type-link" data-name="Map">Map</a>&lt;P, T&gt;</h1><pre class="psx-object-json"><span class="psx-object-json-pun">{</span>
<span class="psx-object-json-key">"totalResults"</span><span class="psx-object-json-pun">: </span><span class="psx-property-type">Integer</span><span class="psx-object-json-pun">,</span>
<span class="psx-object-json-key">"parent"</span><span class="psx-object-json-pun">: </span><span class="psx-property-type">P</span><span class="psx-object-json-pun">,</span>
<span class="psx-object-json-key">"entries"</span><span class="psx-object-json-pun">: </span><span class="psx-property-type">Array (T)</span><span class="psx-object-json-pun">,</span>
<span class="psx-object-json-pun">}</span></pre><table class="table psx-object-properties"><colgroup><col width="30%" /><col width="70%" /></colgroup><thead><tr><th>Field</th><th>Description</th></tr></thead><tbody><tr><td><span class="psx-property-name ">totalResults</span></td><td><span class="psx-property-type"><a class="psx-type-link" data-name="Integer">Integer</a></span><br /><div class="psx-property-description"></div></td></tr><tr><td><span class="psx-property-name ">parent</span></td><td><span class="psx-property-type"><a class="psx-type-link" data-name="P">P</a></span><br /><div class="psx-property-description"></div></td></tr><tr><td><span class="psx-property-name ">entries</span></td><td><span class="psx-property-type"><a class="psx-type-link" data-name="Array (T)">Array (T)</a></span><br /><div class="psx-property-description"></div></td></tr></tbody></table></div>


<div id="StudentMap" class="psx-object psx-struct"><h1><a class="psx-type-link" data-name="StudentMap">StudentMap</a> extends <a class="psx-type-link" data-name="Map">Map</a></h1><pre class="psx-object-json"><span class="psx-object-json-pun">{</span>
<span class="psx-object-json-pun">}</span></pre><table class="table psx-object-properties"><colgroup><col width="30%" /><col width="70%" /></colgroup><thead><tr><th>Field</th><th>Description</th></tr></thead><tbody></tbody></table></div>


<div id="HumanMap" class="psx-object psx-struct"><h1><a class="psx-type-link" data-name="HumanMap">HumanMap</a> extends <a class="psx-type-link" data-name="Map">Map</a></h1><pre class="psx-object-json"><span class="psx-object-json-pun">{</span>
<span class="psx-object-json-pun">}</span></pre><table class="table psx-object-properties"><colgroup><col width="30%" /><col width="70%" /></colgroup><thead><tr><th>Field</th><th>Description</th></tr></thead><tbody></tbody></table></div>


<div id="RootSchema" class="psx-object psx-struct"><h1><a class="psx-type-link" data-name="RootSchema">RootSchema</a></h1><pre class="psx-object-json"><span class="psx-object-json-pun">{</span>
<span class="psx-object-json-key">"students"</span><span class="psx-object-json-pun">: </span><span class="psx-property-type">StudentMap</span><span class="psx-object-json-pun">,</span>
<span class="psx-object-json-pun">}</span></pre><table class="table psx-object-properties"><colgroup><col width="30%" /><col width="70%" /></colgroup><thead><tr><th>Field</th><th>Description</th></tr></thead><tbody><tr><td><span class="psx-property-name ">students</span></td><td><span class="psx-property-type"><a class="psx-type-link" data-name="StudentMap">StudentMap</a></span><br /><div class="psx-property-description"></div></td></tr></tbody></table></div>

2 changes: 1 addition & 1 deletion tests/Generator/resource/java/oop/HumanMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonSetter;

public class HumanMap extends Map<Human, Human> {
public class HumanMap extends Map<HumanType, HumanType> {
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonSetter;

public class Human {
public class HumanType {
private String firstName;
private Human parent;
private HumanType parent;

@JsonSetter("firstName")
public void setFirstName(String firstName) {
Expand All @@ -17,12 +17,12 @@ public String getFirstName() {
}

@JsonSetter("parent")
public void setParent(Human parent) {
public void setParent(HumanType parent) {
this.parent = parent;
}

@JsonGetter("parent")
public Human getParent() {
public HumanType getParent() {
return this.parent;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Generator/resource/java/oop/Student.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonSetter;

public class Student extends Human {
public class Student extends HumanType {
private String matricleNumber;

@JsonSetter("matricleNumber")
Expand Down
2 changes: 1 addition & 1 deletion tests/Generator/resource/java/oop/StudentMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonSetter;

public class StudentMap extends Map<Human, Student> {
public class StudentMap extends Map<HumanType, Student> {
}

30 changes: 15 additions & 15 deletions tests/Generator/resource/jsonschema/jsonschema_import.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
{
"definitions": {
"Human": {
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"parent": {
"$ref": "#/definitions/Human"
}
}
},
"HumanMap": {
"allOf": [
{
Expand All @@ -20,12 +9,12 @@
"type": "integer"
},
"parent": {
"$ref": "#/definitions/Human"
"$ref": "#/definitions/Human_Type"
},
"entries": {
"type": "array",
"items": {
"$ref": "#/definitions/Human"
"$ref": "#/definitions/Human_Type"
}
}
}
Expand All @@ -35,6 +24,17 @@
}
]
},
"Human_Type": {
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"parent": {
"$ref": "#/definitions/Human_Type"
}
}
},
"RootSchema": {
"type": "object",
"properties": {
Expand All @@ -46,7 +46,7 @@
"Student": {
"allOf": [
{
"$ref": "#/definitions/Human"
"$ref": "#/definitions/Human_Type"
},
{
"type": "object",
Expand All @@ -67,7 +67,7 @@
"type": "integer"
},
"parent": {
"$ref": "#/definitions/Human"
"$ref": "#/definitions/Human_Type"
},
"entries": {
"type": "array",
Expand Down
30 changes: 15 additions & 15 deletions tests/Generator/resource/jsonschema/jsonschema_oop.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
{
"definitions": {
"Human": {
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"parent": {
"$ref": "#/definitions/Human"
}
}
},
"HumanMap": {
"allOf": [
{
Expand All @@ -20,12 +9,12 @@
"type": "integer"
},
"parent": {
"$ref": "#/definitions/Human"
"$ref": "#/definitions/Human_Type"
},
"entries": {
"type": "array",
"items": {
"$ref": "#/definitions/Human"
"$ref": "#/definitions/Human_Type"
}
}
}
Expand All @@ -35,10 +24,21 @@
}
]
},
"Human_Type": {
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"parent": {
"$ref": "#/definitions/Human_Type"
}
}
},
"Student": {
"allOf": [
{
"$ref": "#/definitions/Human"
"$ref": "#/definitions/Human_Type"
},
{
"type": "object",
Expand All @@ -59,7 +59,7 @@
"type": "integer"
},
"parent": {
"$ref": "#/definitions/Human"
"$ref": "#/definitions/Human_Type"
},
"entries": {
"type": "array",
Expand Down
5 changes: 0 additions & 5 deletions tests/Generator/resource/kotlin/oop/Human.kt

This file was deleted.

2 changes: 1 addition & 1 deletion tests/Generator/resource/kotlin/oop/HumanMap.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
open class HumanMap : Map<Human, Human> {
open class HumanMap : Map<HumanType, HumanType> {
}

5 changes: 5 additions & 0 deletions tests/Generator/resource/kotlin/oop/HumanType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
open class HumanType {
@JsonProperty("firstName") var firstName: String? = null
@JsonProperty("parent") var parent: HumanType? = null
}

2 changes: 1 addition & 1 deletion tests/Generator/resource/kotlin/oop/Student.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
open class Student : Human {
open class Student : HumanType {
@JsonProperty("matricleNumber") var matricleNumber: String? = null
}

2 changes: 1 addition & 1 deletion tests/Generator/resource/kotlin/oop/StudentMap.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
open class StudentMap : Map<Human, Student> {
open class StudentMap : Map<HumanType, Student> {
}

2 changes: 1 addition & 1 deletion tests/Generator/resource/php/oop/HumanMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
declare(strict_types = 1);

/**
* @extends Map<Human, Human>
* @extends Map<HumanType, HumanType>
*/
class HumanMap extends Map implements \JsonSerializable, \PSX\Record\RecordableInterface
{
Expand Down
Loading

0 comments on commit 8a63d01

Please sign in to comment.