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 #101

Merged
merged 1 commit into from
Dec 31, 2024
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
4 changes: 3 additions & 1 deletion lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
const {
_name,
_type,
_escape,
_string,
_lowerFirst,
_subModelName,
remove,
Expand Down Expand Up @@ -71,7 +73,7 @@
'artifactId', 'version'
];
needParamsKeys.forEach(key => {
if (params[key] === undefined) {

Check warning on line 76 in lib/generator.js

View workflow job for this annotation

GitHub Actions / build (12.x)

Unexpected use of undefined

Check warning on line 76 in lib/generator.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Unexpected use of undefined

Check warning on line 76 in lib/generator.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected use of undefined

Check warning on line 76 in lib/generator.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected use of undefined

Check warning on line 76 in lib/generator.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected use of undefined
params[key] = '';
}
});
Expand Down Expand Up @@ -1027,7 +1029,7 @@
let comments = DSL.comment.getFrontComments(this.comments, ast.tokenRange[0]);
this.visitComments(comments, level);
if (ast.type === 'objectField') {
var key = _name(ast.fieldName);
var key = _escape(_name(ast.fieldName) || _string(ast.fieldName));
this.emit(`new TeaPair("${key}", `, level);
this.visitObjectFieldValue(ast.expr, level);
} else {
Expand Down
15 changes: 14 additions & 1 deletion lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,20 @@ function md2Html(mdText) {
return htmlText;
}

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

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

module.exports = {
_name, _type,
_name, _type, _escape, _string,
_lowerFirst, _subModelName, remove, _upperFirst, md2Html
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"author": "Alibaba Cloud OpenAPI Team",
"license": "Apache-2.0",
"dependencies": {
"@darabonba/parser": "^1.2.9",
"@darabonba/parser": "^1.4.7",
"@darabonba/annotation-parser": "^1.0.0",
"html-entities": "^1.3.1",
"xml2js": "^0.5.0",
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/function/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public static java.util.Map<String, String> helloMap() throws Exception {
return TeaConverter.merge(String.class,
TeaConverter.buildMap(
new TeaPair("key", "value"),
new TeaPair("key-1", "value-1")
new TeaPair("'key-1'", "value-1"),
new TeaPair("\"\"", "value-2")
),
m
);
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/function/main.dara
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ static function helloMap(): map[string]string {
m.test = "test";
return {
key = 'value',
key-1 = 'value-1',
'key-1' = 'value-1',
'""' = 'value-2',
...m,
};
}
Expand Down
Loading