Skip to content

Commit

Permalink
change the error generate logic
Browse files Browse the repository at this point in the history
  • Loading branch information
peze committed Nov 14, 2024
1 parent 1f1f6b7 commit b0d04fb
Show file tree
Hide file tree
Showing 28 changed files with 255 additions and 206 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ jobs:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm run ci
- run: npm run test
- name: Upload Coverage Report
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }} # required
75 changes: 53 additions & 22 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const getBuiltin = require('./builtin');
const {
_name, _format, _string, _type, _initValue, _avoidReserveName, _importFilter, _avoidVariableKeywords,
_setExtendFunc, _isFilterType, _getAttr, _setValueFunc, _vid, _pointerType, _lowerFirst, _escape,
_isBinaryOp, _upperFirst, _isIterator, _modelName, _extendFieldName, _field
_isBinaryOp, _upperFirst, _isIterator, _modelName,
} = require('./helper');

function getAttr(node, attrName) {
Expand Down Expand Up @@ -171,7 +171,9 @@ class Visitor {
for (let i = 0; i < imports.length; i++) {
const item = imports[i];
const aliasId = _name(item);
const moduleDir = this.config.libraries[aliasId];
const main = item.mainModule;
const inner = item.module;
const moduleDir = main ? this.config.libraries[main] : this.config.libraries[aliasId];
const innerPath = item.innerPath;

if (innerPath) {
Expand Down Expand Up @@ -234,7 +236,14 @@ class Visitor {
goPkg: importTeaFile.go
});
}
this.importPackages += ` ${_importFilter(aliasId.toLowerCase())} "${pkgName}"\n`;
if(inner) {
const mainPath = pkgName.substring(0, pkgName.lastIndexOf('/'));
const innerPath = importTeaFile.exports[inner].replace(/(\.tea)$|(\.spec)$|(\.dara)$/gi, '');
const pkgPath = path.join(mainPath, innerPath);
this.importPackages += ` ${_importFilter(aliasId.toLowerCase())} "${pkgPath}"\n`;
} else {
this.importPackages += ` ${_importFilter(aliasId.toLowerCase())} "${pkgName}"\n`;
}
}
this.goPackages = pack.join('\n');
}
Expand Down Expand Up @@ -351,6 +360,8 @@ class Visitor {

ast.innerModule = new Map();
this.comments = ast.comments;
this.predefined = ast.models;
this.usedExternException = ast.usedExternException;
this.builtin = getBuiltin(this);
this.builtinModule = [];
this.tryFunc = [];
Expand Down Expand Up @@ -1160,13 +1171,16 @@ class Visitor {

visitType(ast, level) {
if (ast.type === 'moduleModel') {
this.emit(`*${_importFilter(_name(ast.path[0]).toLowerCase())}`);
for (let i = 1; i < ast.path.length; i++) {
if (i === 1) {
this.emit(`.`);
}
this.emit(`${_format(_name(ast.path[i]))}`);
}
const [ mainId, ...rest ] = ast.path;
let moduleName = _importFilter(_name(ast.path[0]).toLowerCase());
let modelName = rest.map(node => {
return _upperFirst(_name(node));
}).join('');
const externEx = this.usedExternException.get(_name(mainId));
if (externEx && externEx.has(modelName)) {
modelName += 'Error';
}
this.emit(`*${moduleName}.${_format(modelName)}`);
} else if (ast.type === 'moduleTypedef') {
this.emit(`*`);
for (let i = 1; i < ast.path.length; i++) {
Expand Down Expand Up @@ -1484,9 +1498,9 @@ class Visitor {
this.builtinModule.push({
path: 'fmt'
});
this.emit(`func (err ${structName}) Error() string {\n`, level);
this.emit(`func (err ${structName}Error) Error() string {\n`, level);
this.emit('if err.Message == nil {\n', level + 1);
this.emit(`str := fmt.Sprintf("${structName}:\\n Name: %s\\n Code: %s\\n",\n`, level + 2);
this.emit(`str := fmt.Sprintf("${structName}Error:\\n Name: %s\\n Code: %s\\n",\n`, level + 2);
this.emit('dara.StringValue(err.Name), dara.StringValue(err.Code))\n', level + 3);
this.emit('err.Message = dara.String(str)\n', level + 2);
this.emit(`}\n`, level + 1);
Expand All @@ -1502,13 +1516,14 @@ class Visitor {

for (let i = 0; i < ast.nodes.length; i++) {
const node = ast.nodes[i];
this.visitGetFunc(node, structName, level);
this.visitGetFunc(node, structName, type, level);
}
}

visitGetFunc(ast, structName, level = 0){
visitGetFunc(ast, structName, type = 'model',level = 0){
const fieldName = _format(_name(ast.fieldName));
this.emit(`func (s *${structName}) Get${fieldName}() `, level);
this.emit(`func (s *${structName}${type === 'model' ? '' : 'Error'}) Get${fieldName}() `, level);
structName = structName + _format(fieldName);
this.visitFieldType(ast, structName, [], [], level);
this.emit(' {\n');
this.emit(`return s.${fieldName}\n`, level + 1);
Expand Down Expand Up @@ -1646,6 +1661,9 @@ class Visitor {
}
this.emit(`${_modelName(_format(_name(extendOn)))}`);
}
if(type === 'exception') {
this.emit('Error');
}
this.emit('\n');
}

Expand Down Expand Up @@ -1714,7 +1732,7 @@ class Visitor {
this.visitAnnotation(ast.annotation, level);
let comments = DSL.comment.getFrontComments(this.comments, ast.tokenRange[0]);
this.visitComments(comments, level);
this.emit(`type ${exceptionName} struct {\n`, level);
this.emit(`type ${exceptionName}Error struct {\n`, level);
this.visitExtendOn(ast.extendOn, level + 1, 'exception');
this.dealExtendFileds(ast.exceptionBody);
this.visitExceptionBody(ast.exceptionBody, exceptionName, level + 1);
Expand All @@ -1723,7 +1741,7 @@ class Visitor {
visitExceptionInterface(ast, level) {
assert.equal(ast.type, 'exception');
const exceptionName = _upperFirst(_name(ast.exceptionName));
this.emit(`type i${exceptionName} interface {\n`, level);
this.emit(`type i${exceptionName}Error interface {\n`, level);
this.visitExtendOn(ast.extendOn, level + 1, 'exception');
this.visitExceptionInterfceBody(ast.exceptionBody, exceptionName, level + 1);
this.emit('}\n\n', level);
Expand All @@ -1735,7 +1753,7 @@ class Visitor {
const node = ast.nodes[i];
const fieldName = _format(_name(node.fieldName));
this.emit(`Get${fieldName}() `, level);
this.visitFieldType(node, structName, [], [], level);
this.visitFieldType(node, structName + fieldName, [], [], level);
this.emit('\n');
}
}
Expand Down Expand Up @@ -1787,7 +1805,7 @@ class Visitor {
this.emit(`) *${structName}\n`);
}
this.emit(`Get${fieldName}() `, level);
this.visitFieldType(node, structName, [], [], level);
this.visitFieldType(node, itemName, [], [], level);
this.emit('\n');
}
}
Expand Down Expand Up @@ -2603,15 +2621,23 @@ class Visitor {

visitConstructModel(ast, level, env) {
assert.equal(ast.type, 'construct_model');
let modelName;
let modelName = '';
if ((ast.inferred && ast.inferred.moduleName) || ast.aliasId.isModule) {
modelName = `${_format(_name(ast.aliasId)).toLowerCase()}.`;
const moduleName = _format(_name(ast.aliasId)).toLowerCase();
for (let i = 0; i < ast.propertyPath.length; i++) {
const item = ast.propertyPath[i];
modelName += _format(_name(item));
}
const externEx = this.usedExternException.get(_name(ast.aliasId));
if (externEx && externEx.has(modelName)) {
modelName += 'Error';
}
modelName = `${moduleName}.${modelName}`;
} else {
modelName = _modelName(_format(_name(ast.aliasId)));
if (this.predefined[modelName] && this.predefined[modelName].isException) {
modelName += 'Error';
}
}
if (ast.object && ast.object.fields.length > 0) {
const fields = ast.object.fields;
Expand Down Expand Up @@ -3520,7 +3546,7 @@ class Visitor {
if (this.importPackages !== '') {
this.header += `${this.importPackages}`;
}
this.header += ` dara "github.com/alibabacloud-go/tea/tea"\n`;
this.header += ` "github.com/alibabacloud-go/tea/dara"\n`;
const modules = new Set();
this.builtinModule.forEach(builtinModule => {
let content = '';
Expand Down Expand Up @@ -3721,6 +3747,11 @@ class Visitor {
return this.typedef[type.lexeme].type;
}
}

if (this.predefined[_name(type)] && this.predefined[_name(type)].isException) {
return `${_name(type)}Error`;
}

return _name(type);
}
}
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
"eslint": "^6",
"expect.js": "^0.3.1",
"mocha": "^10.4.0",
"nyc": "^15.1.0",
"codecov": "^3"
"nyc": "^15.1.0"
},
"dependencies": {
"@darabonba/annotation-parser": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/annotation/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package client

import (
dara "github.com/alibabacloud-go/tea/tea"
"github.com/alibabacloud-go/tea/dara"

)

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package client

import (
dara "github.com/alibabacloud-go/tea/tea"
"github.com/alibabacloud-go/tea/dara"

)

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/builtin/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package client

import (
"io"
dara "github.com/alibabacloud-go/tea/tea"
"github.com/alibabacloud-go/tea/dara"
"encoding/hex"
"encoding/base64"
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/comment/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package client

import (
source "github.com/aliyun/darabonba-go-generator/test"
dara "github.com/alibabacloud-go/tea/tea"
"github.com/alibabacloud-go/tea/dara"

)

Expand Down
Loading

0 comments on commit b0d04fb

Please sign in to comment.