Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: model name eq model #88

Merged
merged 2 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/langs/php/combinator.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,13 @@ class Combinator extends CombinatorBase {
}

// avoid keywords
realFullClassName = realFullClassName.split('\\').map(m => {
let realFullClassNameArr = realFullClassName.split('\\');
realFullClassName = realFullClassNameArr.map((m, i) => {
if (i === realFullClassNameArr.length - 1 && m.toLowerCase() === 'model') {
// If the model class name is 'model'
// add the '_' suffix.
return m + '_';
}
return _avoidKeywords(m);
}).join('\\');

Expand Down Expand Up @@ -217,7 +223,9 @@ class Combinator extends CombinatorBase {
this.classNameMap = {};

let emitter, outputParts = { head: '', body: '', foot: '' };

if (object.type === 'model' && object.name.toLowerCase() === 'model') {
object.name = object.name + '_';
}
/******************************** emit body ********************************/
emitter = new Emitter(this.config);
if (object.name.indexOf('.') > -1) {
Expand Down Expand Up @@ -1011,7 +1019,7 @@ class Combinator extends CombinatorBase {
} else {
emitter.emitln(' {}');
}

if (gram.elseItem.length && gram.elseItem.length > 0) {
gram.elseItem.forEach(e => {
this.grammer(emitter, e);
Expand Down
13 changes: 13 additions & 0 deletions tests/expected/model/Models/MyModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use import\Client as importClient;
use GuzzleHttp\Psr7\Stream;

use Tea\PHP\Tests\Models\model_;
use Tea\PHP\Tests\Models\MyModel\submodel;
use Tea\PHP\Tests\Models\MyModel\subarraymodel;
use Tea\PHP\Tests\Models\M;
Expand All @@ -19,6 +20,7 @@ class MyModel extends Model {
'link' => 'link',
];
public function validate() {
Model::validateRequired('model', $this->model, true);
Model::validateRequired('stringfield', $this->stringfield, true);
Model::validateRequired('bytesfield', $this->bytesfield, true);
Model::validateRequired('stringarrayfield', $this->stringarrayfield, true);
Expand Down Expand Up @@ -57,6 +59,9 @@ public function validate() {
}
public function toMap() {
$res = [];
if (null !== $this->model) {
$res['model'] = null !== $this->model ? $this->model->toMap() : null;
}
if (null !== $this->stringfield) {
$res['stringfield'] = $this->stringfield;
}
Expand Down Expand Up @@ -215,6 +220,9 @@ public function toMap() {
*/
public static function fromMap($map = []) {
$model = new self();
if(isset($map['model'])){
$model->model = model_::fromMap($map['model']);
}
if(isset($map['stringfield'])){
$model->stringfield = $map['stringfield'];
}
Expand Down Expand Up @@ -343,6 +351,11 @@ public static function fromMap($map = []) {
}
return $model;
}
/**
* @var model_
*/
public $model;

/**
* @var string
*/
Expand Down
35 changes: 35 additions & 0 deletions tests/expected/model/Models/model_.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

// This file is auto-generated, don't edit it. Thanks.
namespace Tea\PHP\Tests\Models;

use AlibabaCloud\Tea\Model;

class model_ extends Model {
public function validate() {
Model::validateRequired('str', $this->str, true);
}
public function toMap() {
$res = [];
if (null !== $this->str) {
$res['str'] = $this->str;
}
return $res;
}
/**
* @param array $map
* @return model_
*/
public static function fromMap($map = []) {
$model = new self();
if(isset($map['str'])){
$model->str = $map['str'];
}
return $model;
}
/**
* @var string
*/
public $str;

}
6 changes: 6 additions & 0 deletions tests/fixtures/model/main.dara
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ model M = {

model Class = {};

model model = {
str: string
}

model MyModel = {
model: model,
stringfield: string,
bytesfield: bytes,
stringarrayfield: [ string ],
Expand Down Expand Up @@ -46,3 +51,4 @@ model MyModel = {
uint64Field: uint64,
link?: string(name='link', example='http://*/*.png'),
};

3 changes: 2 additions & 1 deletion tests/php.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ describe('PHP Generator', function () {
'Models/MyModel.php',
'Models/MyModel/subarraymodel.php',
'Models/MyModel/submodel.php',
'Models/Class_.php'
'Models/Class_.php',
'Models/model_.php',
]);
});

Expand Down