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

feat: adjust field name for object #116

Merged
merged 1 commit into from
Jan 2, 2025
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
2,488 changes: 2,488 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"author": "Alibaba Cloud OpenAPI Team",
"license": "Apache-2.0",
"dependencies": {
"@darabonba/parser": "^1.2.10",
"@darabonba/parser": "^1.4.7",
"enum": "^3.0.4"
}
}
12 changes: 11 additions & 1 deletion src/lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ function _subModelName(name) {
}

function _string(str) {
return str.string;
if (str.string === '""') {
return '\\"\\"';
}
return str.string.replace(/([^\\])"+|^"/g, function (str) {
return str.replace(/"/g, '\\"');
});
}

function _isBasicType(type) {
Expand Down Expand Up @@ -172,9 +177,14 @@ function _toSnakeCase(str) {
return res;
}

function _escape(str) {
return str.includes('-') ? `${str}` : str;
}


module.exports = {
_config,
_escape,
_upperFirst,
_camelCase,
_string,
Expand Down
4 changes: 3 additions & 1 deletion src/resolver/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const debug = require('../lib/debug');

const {
_string,
_escape,
_isBasicType
} = require('../lib/helper');

Expand Down Expand Up @@ -176,7 +178,7 @@ class BaseResolver {
} else if (typeNode.type === 'modelBody') {
// is sub model
typeInfo['objectType'] = 'model';
typeInfo['lexeme'] = this.combinator.addModelInclude([this.object.name, sourceNode.fieldName.lexeme].join('.'));
typeInfo['lexeme'] = this.combinator.addModelInclude([this.object.name, _escape(sourceNode.fieldName.lexeme) || _string(sourceNode.fieldName)].join('.'));
return typeInfo;
} else if (_isBasicType(typeNode.type)) {
return this.resolveType(typeNode.type, typeNode);
Expand Down
11 changes: 7 additions & 4 deletions src/resolver/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ const {
} = require('../langs/common/enum');

const {
_isBasicType
_isBasicType,
_string,
_escape

} = require('../lib/helper');

const systemPackage = ['Util'];
Expand Down Expand Up @@ -158,7 +161,7 @@ class ClientResolver extends BaseResolver {
ast.params.params.forEach(p => {
var param = new GrammerValue();
param.type = this.resolveType(p.paramType, p);
param.key = p.paramName.lexeme;
param.key = _escape(p.paramName.lexeme || _string(p.paramName));
if (p.needValidate) {
func.addBodyNode(new GrammerCall('method', [
{ type: 'object', name: param.key },
Expand Down Expand Up @@ -450,8 +453,8 @@ class ClientResolver extends BaseResolver {
valGrammer.needCast = true;
exprChild.isExpand = true;
}
if (field.fieldName && field.fieldName.lexeme) {
exprChild.key = field.fieldName.lexeme;
if (field.fieldName && (field.fieldName.lexeme || field.fieldName.string)) {
exprChild.key = (field.fieldName.lexeme) || _string(field.fieldName);
}
this.renderGrammerValue(exprChild, field.expr, expectedType);
this.findComments(valGrammer, field);
Expand Down
8 changes: 5 additions & 3 deletions src/resolver/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ const assert = require('assert');
const BaseResolver = require('./base');

const {
_isBasicType
_string,
_escape,
_isBasicType,
} = require('../lib/helper');

const {
Expand Down Expand Up @@ -95,7 +97,7 @@ class ModelResolver extends BaseResolver {

const prop = new PropItem();
prop.belong = object.index;
prop.name = node.fieldName.lexeme;
prop.name = _escape(node.fieldName.lexeme) || _string(node.fieldName);
prop.type = this.resolveType(node.fieldValue, node);

prop.modify.push(Modify.public());
Expand Down Expand Up @@ -141,7 +143,7 @@ class ModelResolver extends BaseResolver {
}

findSubModelsUsed(node, subModelUsed = [], pre = '') {
let name = node.fieldName.lexeme;
let name = _escape(node.fieldName.lexeme) || _string(node.fieldName);
if (pre !== '') {
name = pre + '.' + name;
}
Expand Down
4 changes: 3 additions & 1 deletion tests/expected/python2/function/tea_python_tests/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ def hello_map():
m = {}
return TeaCore.merge({
'key': 'value',
'key-1': 'value-1'
'key-1': 'value-1',
'key-2': 'value-2',
'\"\"': 'value-3'
}, m)

@staticmethod
Expand Down
4 changes: 3 additions & 1 deletion tests/expected/python3/function/tea_python_tests/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ def hello_map() -> Dict[str, str]:
m = {}
return TeaCore.merge({
'key': 'value',
'key-1': 'value-1'
'key-1': 'value-1',
'key-2': 'value-2',
'\"\"': 'value-3'
}, m)

@staticmethod
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/function/main.dara
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ static function helloMap(): map[string]string {
return {
key = 'value',
key-1 = 'value-1',
'key-2' = 'value-2',
'""' = 'value-3',
...m,
};
}
Expand Down
Loading