Skip to content

Commit

Permalink
feat: adjust field name for object
Browse files Browse the repository at this point in the history
  • Loading branch information
yndu13 committed Dec 31, 2024
1 parent e7c21f5 commit 5ecbd9d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
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 RUNTIME = 'runtime_';
const {
_name,
_type,
_escape,
_string,
_lowerFirst,
_subModelName,
remove,
Expand Down Expand Up @@ -1027,7 +1029,7 @@ class Visitor {
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

0 comments on commit 5ecbd9d

Please sign in to comment.