diff --git a/lib/generator.js b/lib/generator.js index 50ead3b..65bf5ad 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -14,6 +14,8 @@ const RUNTIME = 'runtime_'; const { _name, _type, + _escape, + _string, _lowerFirst, _subModelName, remove, @@ -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 { diff --git a/lib/helper.js b/lib/helper.js index ff93df6..1da9be4 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -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 }; diff --git a/package.json b/package.json index 5e127a2..151b420 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/fixtures/function/Client.java b/test/fixtures/function/Client.java index 4edf743..97070ce 100644 --- a/test/fixtures/function/Client.java +++ b/test/fixtures/function/Client.java @@ -15,7 +15,8 @@ public static java.util.Map 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 ); diff --git a/test/fixtures/function/main.dara b/test/fixtures/function/main.dara index 02ff0a6..b79a19a 100644 --- a/test/fixtures/function/main.dara +++ b/test/fixtures/function/main.dara @@ -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, }; }