diff --git a/lib/generator.js b/lib/generator.js index 8ac211c..dac15a3 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -9,7 +9,7 @@ const DSL = require('@darabonba/parser'); const { _name, _string, _subModelName, _vid, _isBinaryOp, _modelName, _isBuiltinModel, - REQUEST, RESPONSE, CORE, _upperFirst, _avoidKeywords, + REQUEST, RESPONSE, CORE, _upperFirst, _avoidKeywords, _filedsAccess, ERROR, MODEL, STREAM, UNRETRY_ERROR, RETRY_CONTEXT, SSE_EVENT, RESP_ERROR, RETRY_OPTION, RUNTIME_OPTION, EXTEND_PARAM } = require('./helper'); @@ -1298,13 +1298,14 @@ ${this.config.clientName}::main(array_slice($argv, 1));`); let node; for (let i = 0; i < ast.nodes.length; i++) { node = ast.nodes[i]; + const fieldName = _name(node.fieldName); // TODO document gen this.emit('/**\n', level); this.emit('* @var ', level); - this.visitFieldType(node.fieldValue, level, _name(node.fieldName)); + this.visitFieldType(node.fieldValue, level, fieldName); this.emit('\n'); this.emit('*/\n', level); - this.emit(`protected $${_name(node.fieldName)};\n`, level); + this.emit(`${_filedsAccess(fieldName)} $${fieldName};\n`, level); } this.emit('\n'); diff --git a/lib/helper.js b/lib/helper.js index 5289a25..11f5768 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -17,6 +17,12 @@ const keywords = [ 'try', 'unset', 'use', 'var', 'while', 'xor' ]; +const publicFields = [ + // be compatible with TeaError + 'message', 'errCode', 'data', 'name', 'statusCode', + 'description', 'accessDeniedDetail', 'errorInfo', +]; + const builtinModels = ['$Request', '$Response', '$Error', '$SSEEvent', '$Model']; const NAMESPACE = 'AlibabaCloud\\Dara'; const CORE = 'AlibabaCloud\\Dara\\Dara'; @@ -61,6 +67,13 @@ function _avoidKeywords(str) { return str; } +function _filedsAccess(str) { + if (publicFields.indexOf(str) > -1) { + return 'public'; + } + return 'protected'; +} + function _modelName(str) { if (keywords.indexOf(str.toLowerCase()) > -1 || str.toLowerCase() === 'model') { return str + '_'; @@ -98,7 +111,7 @@ function _isBuiltinModel(name){ module.exports = { _name, _string, _subModelName, _vid, _upperFirst, _isBuiltinModel, - _isBinaryOp, _modelName, _avoidKeywords, RETRY_CONTEXT, + _isBinaryOp, _modelName, _avoidKeywords, _filedsAccess, RETRY_CONTEXT, REQUEST, RESPONSE, MODEL, NAMESPACE, ERROR, STREAM, UNRETRY_ERROR, CORE, SSE_EVENT, RESP_ERROR, RETRY_OPTION, RUNTIME_OPTION, EXTEND_PARAM }; \ No newline at end of file diff --git a/package.json b/package.json index 27efadf..01b5c27 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@darabonba/php-generator", - "version": "1.2.1", + "version": "2.0.0", "description": "The SDK generator for PHP", "main": "lib/generator", "directories": { diff --git a/tests/expected/complex/Exceptions/Err1Exception.php b/tests/expected/complex/Exceptions/Err1Exception.php index b865108..d9b89a6 100644 --- a/tests/expected/complex/Exceptions/Err1Exception.php +++ b/tests/expected/complex/Exceptions/Err1Exception.php @@ -8,7 +8,7 @@ class Err1Exception extends DaraException { /** * @var string[] */ - protected $data; + public $data; public function __construct($map) { diff --git a/tests/expected/model/Exceptions/MainFileException.php b/tests/expected/model/Exceptions/MainFileException.php index 675ebc0..2a5c437 100644 --- a/tests/expected/model/Exceptions/MainFileException.php +++ b/tests/expected/model/Exceptions/MainFileException.php @@ -13,7 +13,7 @@ class MainFileException extends DaraException { /** * @var model_[] */ - protected $data; + public $data; /** * @var \Dara\PHP\Tests\Models\MainFile\model_ */