Skip to content

Commit

Permalink
fix the try func err
Browse files Browse the repository at this point in the history
  • Loading branch information
peze committed Jan 6, 2025
1 parent bf100e7 commit eb58df8
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 73 deletions.
80 changes: 51 additions & 29 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,19 @@ class Visitor {
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 @@ class Visitor {
}

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 @@ -1182,15 +1191,15 @@ class Visitor {
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 @@ class Visitor {
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 @@ class Visitor {
}
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 @@ class Visitor {
}
}

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 @@ class Visitor {
// 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 @@ class Visitor {
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 @@ class Visitor {
}

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 @@ class Visitor {
}
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 @@ class Visitor {
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 @@ class Visitor {
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

0 comments on commit eb58df8

Please sign in to comment.