Skip to content

Commit

Permalink
update generic test case
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Oct 9, 2024
1 parent 6ebb8aa commit 9cba1fd
Show file tree
Hide file tree
Showing 19 changed files with 103 additions and 28 deletions.
3 changes: 3 additions & 0 deletions src/Generator/Python.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ protected function writeStruct(Code\Name $name, array $properties, ?string $exte

$parts = [];
if (!empty($extends)) {
if (!empty($templates)) {
$extends.= $this->generator->getGenericType($templates);
}
$parts[] = $extends;
} else {
$parts[] = 'BaseModel';
Expand Down
9 changes: 6 additions & 3 deletions tests/Generator/resource/csharp/csharp_oop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,26 @@ public class Student : Human
}

using System.Text.Json.Serialization;
public class Map<T>
public class Map<P, T>
{
[JsonPropertyName("totalResults")]
public int? TotalResults { get; set; }

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

[JsonPropertyName("entries")]
public System.Collections.Generic.List<T>? Entries { get; set; }

}

using System.Text.Json.Serialization;
public class StudentMap : Map<Student>
public class StudentMap : Map<Human, Student>
{
}

using System.Text.Json.Serialization;
public class HumanMap : Map<Human>
public class HumanMap : Map<Human, Human>
{
}

Expand Down
5 changes: 4 additions & 1 deletion tests/Generator/resource/go/go_oop.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ type Student struct {

type StudentMap struct {
TotalResults int `json:"totalResults"`
Parent *Human `json:"parent"`
Entries []Student `json:"entries"`
}

type HumanMap struct {
TotalResults int `json:"totalResults"`
Parent *Human `json:"parent"`
Entries []Human `json:"entries"`
}

type Map[T any] struct {
type Map[P any, T any] struct {
TotalResults int `json:"totalResults"`
Parent P `json:"parent"`
Entries []T `json:"entries"`
}

Expand Down
5 changes: 3 additions & 2 deletions tests/Generator/resource/html/html_oop.htm
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
<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;T&gt;</h1><pre class="psx-object-json"><span class="psx-object-json-pun">{</span>
<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 ">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>
<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>
Expand Down
17 changes: 14 additions & 3 deletions tests/Generator/resource/java/java_oop.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ public String getMatricleNumber() {

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonSetter;
public class Map<T> {
public class Map<P, T> {
private Integer totalResults;
private P parent;
private java.util.List<T> entries;

@JsonSetter("totalResults")
Expand All @@ -57,6 +58,16 @@ public Integer getTotalResults() {
return this.totalResults;
}

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

@JsonGetter("parent")
public P getParent() {
return this.parent;
}

@JsonSetter("entries")
public void setEntries(java.util.List<T> entries) {
this.entries = entries;
Expand All @@ -70,12 +81,12 @@ public java.util.List<T> getEntries() {

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonSetter;
public class StudentMap extends Map<Student> {
public class StudentMap extends Map<Human, Student> {
}

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonSetter;
public class HumanMap extends Map<Human> {
public class HumanMap extends Map<Human, Human> {
}

import com.fasterxml.jackson.annotation.JsonGetter;
Expand Down
1 change: 1 addition & 0 deletions tests/Generator/resource/jsonschema/jsonschema_import.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"totalResults": {
"type": "integer"
},
"parent": [],
"entries": {
"type": "array",
"items": []
Expand Down
1 change: 1 addition & 0 deletions tests/Generator/resource/jsonschema/jsonschema_oop.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"totalResults": {
"type": "integer"
},
"parent": [],
"entries": {
"type": "array",
"items": []
Expand Down
7 changes: 4 additions & 3 deletions tests/Generator/resource/kotlin/kotlin_oop.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ open class Student : Human {
var matricleNumber: String? = null
}

open class Map<T> {
open class Map<P, T> {
var totalResults: Int? = null
var parent: P? = null
var entries: Array<T>? = null
}

open class StudentMap : Map<Student> {
open class StudentMap : Map<Human, Student> {
}

open class HumanMap : Map<Human> {
open class HumanMap : Map<Human, Human> {
}

open class RootSchema {
Expand Down
15 changes: 13 additions & 2 deletions tests/Generator/resource/php/php_oop.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ public function jsonSerialize() : object
}

/**
* @template P
* @template T
*/
class Map implements \JsonSerializable, \PSX\Record\RecordableInterface
{
protected ?int $totalResults = null;
protected ?P $parent = null;
/**
* @var array<T>|null
*/
Expand All @@ -74,6 +76,14 @@ public function getTotalResults() : ?int
{
return $this->totalResults;
}
public function setParent(?P $parent) : void
{
$this->parent = $parent;
}
public function getParent() : ?P
{
return $this->parent;
}
/**
* @param array<T>|null $entries
*/
Expand All @@ -93,6 +103,7 @@ public function toRecord() : \PSX\Record\RecordInterface
/** @var \PSX\Record\Record<mixed> $record */
$record = new \PSX\Record\Record();
$record->put('totalResults', $this->totalResults);
$record->put('parent', $this->parent);
$record->put('entries', $this->entries);
return $record;
}
Expand All @@ -103,14 +114,14 @@ public function jsonSerialize() : object
}

/**
* @extends Map<Student>
* @extends Map<Human, Student>
*/
class StudentMap extends Map implements \JsonSerializable, \PSX\Record\RecordableInterface
{
}

/**
* @extends Map<Human>
* @extends Map<Human, Human>
*/
class HumanMap extends Map implements \JsonSerializable, \PSX\Record\RecordableInterface
{
Expand Down
9 changes: 6 additions & 3 deletions tests/Generator/resource/protobuf/protobuf_oop.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ message Student {

message StudentMap {
optional int64 totalResults = 1 [json_name="totalResults"];
optional repeated Student entries = 2 [json_name="entries"];
optional Human parent = 2 [json_name="parent"];
optional repeated Student entries = 3 [json_name="entries"];
}

message HumanMap {
optional int64 totalResults = 1 [json_name="totalResults"];
optional repeated Human entries = 2 [json_name="entries"];
optional Human parent = 2 [json_name="parent"];
optional repeated Human entries = 3 [json_name="entries"];
}

message Map {
optional int64 totalResults = 1 [json_name="totalResults"];
optional repeated T entries = 2 [json_name="entries"];
optional P parent = 2 [json_name="parent"];
optional repeated T entries = 3 [json_name="entries"];
}

message RootSchema {
Expand Down
9 changes: 6 additions & 3 deletions tests/Generator/resource/python/python_oop.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,29 @@ class Student(Human):
from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar, UserList, UserDict
P = TypeVar("P")
T = TypeVar("T")
class Map(BaseModel, Generic[T]):
class Map(BaseModel, Generic[P], Generic[T]):
total_results: Optional[int] = Field(default=None, alias="totalResults")
parent: Optional[P] = Field(default=None, alias="parent")
entries: Optional[List[T]] = Field(default=None, alias="entries")
pass

from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar, UserList, UserDict
from .map import Map
from .human import Human
from .student import Student
class StudentMap(Map):
class StudentMap(Map[Human, Student]):
pass

from pydantic import BaseModel, Field, GetCoreSchemaHandler
from pydantic_core import CoreSchema, core_schema
from typing import Any, Dict, Generic, List, Optional, TypeVar, UserList, UserDict
from .map import Map
from .human import Human
class HumanMap(Map):
class HumanMap(Map[Human, Human]):
pass

from pydantic import BaseModel, Field, GetCoreSchemaHandler
Expand Down
5 changes: 3 additions & 2 deletions tests/Generator/resource/ruby/ruby_oop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ def initialize(matricle_number)
end

class Map
attr_accessor :total_results, :entries
attr_accessor :total_results, :parent, :entries

def initialize(total_results, entries)
def initialize(total_results, parent, entries)
@total_results = total_results
@parent = parent
@entries = entries
end
end
Expand Down
10 changes: 10 additions & 0 deletions tests/Generator/resource/rust/rust_oop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ pub struct Student {

use serde::{Serialize, Deserialize};
use map::Map;
use human::Human;
use student::Student;
#[derive(Serialize, Deserialize)]
pub struct StudentMap {
#[serde(rename = "totalResults")]
total_results: Option<u64>,

#[serde(rename = "parent")]
parent: Option<Human>,

#[serde(rename = "entries")]
entries: Option<Vec<Student>>,

Expand All @@ -46,6 +50,9 @@ pub struct HumanMap {
#[serde(rename = "totalResults")]
total_results: Option<u64>,

#[serde(rename = "parent")]
parent: Option<Human>,

#[serde(rename = "entries")]
entries: Option<Vec<Human>>,

Expand All @@ -57,6 +64,9 @@ pub struct Map {
#[serde(rename = "totalResults")]
total_results: Option<u64>,

#[serde(rename = "parent")]
parent: Option<P>,

#[serde(rename = "entries")]
entries: Option<Vec<T>>,

Expand Down
6 changes: 6 additions & 0 deletions tests/Generator/resource/source_oop.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
"type": "struct",
"parent": "Map",
"template": {
"P": "Human",
"T": "Student"
}
},
"HumanMap": {
"type": "struct",
"parent": "Map",
"template": {
"P": "Human",
"T": "Human"
}
},
Expand All @@ -41,6 +43,10 @@
"totalResults": {
"type": "integer"
},
"parent": {
"type": "generic",
"name": "P"
},
"entries": {
"type": "array",
"items": {
Expand Down
2 changes: 2 additions & 0 deletions tests/Generator/resource/swift/swift_oop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ class Student: Human {

class Map: Codable {
var totalResults: Int
var parent: P
var entries: Array<T>

enum CodingKeys: String, CodingKey {
case totalResults = "totalResults"
case parent = "parent"
case entries = "entries"
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/Generator/resource/typeschema/typeschema_import.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"type": "reference",
"target": "my_import:Map",
"template": {
"P": "my_import:Human",
"T": "my_import:Human"
}
}
Expand All @@ -28,6 +29,9 @@
"totalResults": {
"type": "integer"
},
"parent": {
"type": "generic"
},
"entries": {
"type": "array",
"schema": {
Expand Down Expand Up @@ -63,6 +67,7 @@
"type": "reference",
"target": "my_import:Map",
"template": {
"P": "my_import:Human",
"T": "my_import:Student"
}
}
Expand Down
Loading

0 comments on commit 9cba1fd

Please sign in to comment.