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

fix the try func err #117

Merged
merged 1 commit into from
Jan 6, 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
80 changes: 51 additions & 29 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@
errNum += this.checkExpr(ast.condition);
errNum += this.stmtsErrCount(ast.stmts.stmts);
if(ast.elseIfs && ast.elseIfs.length > 0) {
ast.elseIfs.map(elseIf => {

Check warning on line 741 in lib/generator.js

View workflow job for this annotation

GitHub Actions / build (12.x)

Function declared in a loop contains unsafe references to variable(s) 'errNum', 'errNum'

Check warning on line 741 in lib/generator.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Function declared in a loop contains unsafe references to variable(s) 'errNum', 'errNum'

Check warning on line 741 in lib/generator.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Function declared in a loop contains unsafe references to variable(s) 'errNum', 'errNum'

Check warning on line 741 in lib/generator.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Function declared in a loop contains unsafe references to variable(s) 'errNum', 'errNum'

Check warning on line 741 in lib/generator.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Function declared in a loop contains unsafe references to variable(s) 'errNum', 'errNum'
errNum += this.checkExpr(elseIf.condition);
errNum += this.stmtsErrCount(elseIf.stmts.stmts);
});
Expand Down Expand Up @@ -803,6 +803,19 @@
return expr;
}

getModuleType(moduleType) {
if(moduleType.type === 'basic' || moduleType.type === 'model') {
return {
type: moduleType.type,
name: moduleType.name
};
}
return {
idType: 'module',
name: moduleType.name
};
}

getExprVars(ast) {
let vars = [];
if (ast.type === 'property_access') {
Expand Down Expand Up @@ -841,21 +854,17 @@
}

if(ast.left.type === 'instance_call') {
const method = _name(ast.left.propertyPath[0]);
const builtinInstance = ast.builtinModule && this.builtin[ast.builtinModule];
if(builtinInstance && this.builtin[ast.builtinModule][method]) {
return vars;
}

if (_name(ast.left.id).indexOf('@') === 0) {
vars.push({
name: 'client',
type: this.structName,
});
} else {
const type = this.getModuleType(ast.left.id.moduleType);
vars.push({
name: _name(ast.left.id),
type: this.clientName[_name(ast.left.id)]
type: type
});
}
} else if(ast.left.type === 'method_call') {
Expand Down Expand Up @@ -923,7 +932,7 @@
args = args.concat(this.getExprVars(ast.condition));
args = args.concat(this.getStmtsVars(ast.stmts.stmts));
if(ast.elseIfs && ast.elseIfs.length > 0) {
ast.elseIfs.map(elseIf => {

Check warning on line 935 in lib/generator.js

View workflow job for this annotation

GitHub Actions / build (12.x)

Function declared in a loop contains unsafe references to variable(s) 'args', 'args', 'args', 'args'

Check warning on line 935 in lib/generator.js

View workflow job for this annotation

GitHub Actions / build (14.x)

Function declared in a loop contains unsafe references to variable(s) 'args', 'args', 'args', 'args'

Check warning on line 935 in lib/generator.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Function declared in a loop contains unsafe references to variable(s) 'args', 'args', 'args', 'args'

Check warning on line 935 in lib/generator.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Function declared in a loop contains unsafe references to variable(s) 'args', 'args', 'args', 'args'

Check warning on line 935 in lib/generator.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Function declared in a loop contains unsafe references to variable(s) 'args', 'args', 'args', 'args'
args = args.concat(this.getExprVars(elseIf.condition));
args = args.concat(this.getStmtsVars(elseIf.stmts.stmts));
});
Expand Down Expand Up @@ -1182,15 +1191,15 @@
if (ast.fieldType === 'array') {
if (ast.fieldItemType.type === 'modelBody') {
this.emit('{\n');
this.visitModelBody(ast.fieldItemType, structName, level);
this.visitModelBody(ast.fieldItemType, ast.fieldItemType.nodes, structName, level);
}
return;
}
}

if (ast.type === 'modelBody') {
this.emit('{\n');
this.visitModelBody(ast, structName, level);
this.visitModelBody(ast, ast.nodes, structName, level);
return;
}

Expand Down Expand Up @@ -1337,13 +1346,13 @@
return { type, omitemptyEnable };
}

visitModelBody(ast, lastName, level) {
visitModelBody(ast, nodes, lastName, level) {
assert.equal(ast.type, 'modelBody');
var fields = [];
const structMap = [];
let node;
for (let i = 0; i < ast.nodes.length; i++) {
node = ast.nodes[i];
for (let i = 0; i < nodes.length; i++) {
node = nodes[i];
let comments = DSL.comment.getFrontComments(this.comments, node.tokenRange[0]);
this.visitComments(comments, level);
var fieldName = _name(node.fieldName);
Expand Down Expand Up @@ -1458,8 +1467,8 @@
}
this.emit(`}\n`);
this.emit(`\n`);
this.eachGetFunc(ast, lastName, 'model');
this.eachSetFunc(ast, lastName);
this.eachGetFunc(nodes, lastName, 'model');
this.eachSetFunc(nodes, lastName);
for (let i = 0; i < fields.length; i++) {
this.visitModelField(fields[i], structMap[i], level);
}
Expand Down Expand Up @@ -1512,7 +1521,7 @@
}
}

eachGetFunc(ast, structName, type = 'model',level = 0) {
eachGetFunc(nodes, structName, type = 'model',level = 0) {
if(type === 'model') {
this.emit(`func (s ${structName}) String() string {\n`, level);
this.emit(`return dara.Prettify(s)\n`, level + 1);
Expand Down Expand Up @@ -1542,8 +1551,8 @@
// this.visitGetFunc(node, structName, level);
// }

for (let i = 0; i < ast.nodes.length; i++) {
const node = ast.nodes[i];
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];
this.visitGetFunc(node, structName, type, level);
}
}
Expand All @@ -1559,14 +1568,14 @@
this.emit(`\n`, level);
}

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

for (let i = 0; i < ast.nodes.length; i++) {
const node = ast.nodes[i];
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];
this.visitSetFunc(node, structName, level);
}
}
Expand Down Expand Up @@ -1696,25 +1705,38 @@
}

dealExtendFileds(ast) {
const fileds = [];
for (let i = 0; i < ast.nodes.length; i++) {
const node = ast.nodes[i];
const fieldName = _name(node.fieldName);
fileds.push(fieldName);
}
const extendFileds = [];
for (let i = 0; i < ast.extendFileds.length; i++) {
const node = ast.extendFileds[i];
node.extend = true;
const fieldName = _name(node.fieldName);
if(fileds.includes(fieldName)) {
continue;
}
extendFileds.push(node);

}
ast.nodes = ast.extendFileds.concat(ast.nodes);
return extendFileds.concat(ast.nodes);
}

visitExceptionBody(ast, lastName, level) {
visitExceptionBody(ast, nodes, lastName, level) {
assert.equal(ast.type, 'exceptionBody');
const fields = [];
const structMap = [];
let node;
for (let i = 0; i < ast.nodes.length; i++) {
node = ast.nodes[i];
for (let i = 0; i < nodes.length; i++) {
node = nodes[i];
if(!node.extend) {
let comments = DSL.comment.getFrontComments(this.comments, node.tokenRange[0]);
this.visitComments(comments, level);
}
if(i !== ast.nodes.length - 1) {
if(i !== nodes.length - 1) {
node.extend = false;
}
var fieldName = _name(node.fieldName);
Expand Down Expand Up @@ -1748,7 +1770,7 @@
}
this.emit(`}\n`);
this.emit(`\n`);
this.eachGetFunc(ast, lastName, 'exception');
this.eachGetFunc(nodes, lastName, 'exception');
for (let i = 0; i < fields.length; i++) {
this.visitModelField(fields[i], structMap[i], level);
}
Expand All @@ -1762,8 +1784,8 @@
this.visitComments(comments, 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);
const nodes = this.dealExtendFileds(ast.exceptionBody);
this.visitExceptionBody(ast.exceptionBody, nodes, exceptionName, level + 1);
}

visitExceptionInterface(ast, level) {
Expand Down Expand Up @@ -1846,8 +1868,8 @@
this.visitComments(comments, level);
this.emit(`type ${modelName} struct {\n`, level);
this.visitExtendOn(ast.extendOn, level + 1, 'model');
ast.modelBody.nodes = ast.modelBody.nodes.concat(ast.modelBody.extendFileds);
this.visitModelBody(ast.modelBody, modelName, level + 1);
const nodes = this.dealExtendFileds(ast.modelBody);
this.visitModelBody(ast.modelBody, nodes, modelName, level + 1);
}

getModelFileds(ast) {
Expand Down
32 changes: 16 additions & 16 deletions test/fixtures/extends/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ type iSub interface {

type Sub struct {
iBase
Age *int `json:"age,omitempty" xml:"age,omitempty" require:"true"`
Name *string `json:"name,omitempty" xml:"name,omitempty" require:"true"`
Code *string `json:"code,omitempty" xml:"code,omitempty" require:"true"`
Age *int `json:"age,omitempty" xml:"age,omitempty" require:"true"`
}

func (s Sub) String() string {
Expand All @@ -74,6 +74,10 @@ func (s Sub) GoString() string {
return s.String()
}

func (s *Sub) GetAge() *int {
return s.Age
}

func (s *Sub) GetName() *string {
return s.Name
}
Expand All @@ -82,8 +86,9 @@ func (s *Sub) GetCode() *string {
return s.Code
}

func (s *Sub) GetAge() *int {
return s.Age
func (s *Sub) SetAge(v int) *Sub {
s.Age = &v
return s
}

func (s *Sub) SetName(v string) *Sub {
Expand All @@ -96,11 +101,6 @@ func (s *Sub) SetCode(v string) *Sub {
return s
}

func (s *Sub) SetAge(v int) *Sub {
s.Age = &v
return s
}

type iSubModel interface {
source.iConfig
String() string
Expand All @@ -111,8 +111,8 @@ type iSubModel interface {

type SubModel struct {
source.iConfig
Name *string `json:"name,omitempty" xml:"name,omitempty" require:"true"`
MaxAttemp *int `json:"maxAttemp,omitempty" xml:"maxAttemp,omitempty" require:"true"`
Name *string `json:"name,omitempty" xml:"name,omitempty" require:"true"`
}

func (s SubModel) String() string {
Expand All @@ -123,24 +123,24 @@ func (s SubModel) GoString() string {
return s.String()
}

func (s *SubModel) GetName() *string {
return s.Name
}

func (s *SubModel) GetMaxAttemp() *int {
return s.MaxAttemp
}

func (s *SubModel) SetName(v string) *SubModel {
s.Name = &v
return s
func (s *SubModel) GetName() *string {
return s.Name
}

func (s *SubModel) SetMaxAttemp(v int) *SubModel {
s.MaxAttemp = &v
return s
}

func (s *SubModel) SetName(v string) *SubModel {
s.Name = &v
return s
}

type Client struct {
source.Client
}
Expand Down
Loading
Loading