From 4fe4cea0c0acea00e7086d51bcaa163de7670eb1 Mon Sep 17 00:00:00 2001 From: peze <954152927@qq.com> Date: Mon, 6 Jan 2025 15:22:44 +0800 Subject: [PATCH] fix the try func err --- lib/generator.js | 80 +++++++++++++++++++++------------ test/fixtures/extends/client.go | 32 ++++++------- test/fixtures/multi/sdk/user.go | 48 ++++++++++---------- test/fixtures/try/client.go | 14 ++++-- test/fixtures/try/main.dara | 7 ++- 5 files changed, 108 insertions(+), 73 deletions(-) diff --git a/lib/generator.js b/lib/generator.js index 34712ee..afac7bd 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -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') { @@ -841,11 +854,6 @@ 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({ @@ -853,9 +861,10 @@ class Visitor { 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') { @@ -1182,7 +1191,7 @@ 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; } @@ -1190,7 +1199,7 @@ class Visitor { if (ast.type === 'modelBody') { this.emit('{\n'); - this.visitModelBody(ast, structName, level); + this.visitModelBody(ast, ast.nodes, structName, level); return; } @@ -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); @@ -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); } @@ -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); @@ -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); } } @@ -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); } } @@ -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); @@ -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); } @@ -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) { @@ -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) { diff --git a/test/fixtures/extends/client.go b/test/fixtures/extends/client.go index b989b45..21c6307 100644 --- a/test/fixtures/extends/client.go +++ b/test/fixtures/extends/client.go @@ -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 { @@ -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 } @@ -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 { @@ -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 @@ -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 { @@ -123,17 +123,12 @@ 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 { @@ -141,6 +136,11 @@ func (s *SubModel) SetMaxAttemp(v int) *SubModel { return s } +func (s *SubModel) SetName(v string) *SubModel { + s.Name = &v + return s +} + type Client struct { source.Client } diff --git a/test/fixtures/multi/sdk/user.go b/test/fixtures/multi/sdk/user.go index 677da2a..be65df0 100644 --- a/test/fixtures/multi/sdk/user.go +++ b/test/fixtures/multi/sdk/user.go @@ -18,7 +18,6 @@ type iBaseInfo interface { type BaseInfo struct { darautil.iRuntimeOptions - MaxAttemp *int `json:"maxAttemp,omitempty" xml:"maxAttemp,omitempty" require:"true"` // whether to try again Autoretry *bool `json:"autoretry,omitempty" xml:"autoretry,omitempty"` // ignore SSL validation @@ -55,6 +54,7 @@ type BaseInfo struct { Socks5NetWork *string `json:"socks5NetWork,omitempty" xml:"socks5NetWork,omitempty"` // whether to enable keep-alive KeepAlive *bool `json:"keepAlive,omitempty" xml:"keepAlive,omitempty"` + MaxAttemp *int `json:"maxAttemp,omitempty" xml:"maxAttemp,omitempty" require:"true"` } func (s BaseInfo) String() string { @@ -65,10 +65,6 @@ func (s BaseInfo) GoString() string { return s.String() } -func (s *BaseInfo) GetMaxAttemp() *int { - return s.MaxAttemp -} - func (s *BaseInfo) GetAutoretry() *bool { return s.Autoretry } @@ -141,9 +137,8 @@ func (s *BaseInfo) GetKeepAlive() *bool { return s.KeepAlive } -func (s *BaseInfo) SetMaxAttemp(v int) *BaseInfo { - s.MaxAttemp = &v - return s +func (s *BaseInfo) GetMaxAttemp() *int { + return s.MaxAttemp } func (s *BaseInfo) SetAutoretry(v bool) *BaseInfo { @@ -236,6 +231,11 @@ func (s *BaseInfo) SetKeepAlive(v bool) *BaseInfo { return s } +func (s *BaseInfo) SetMaxAttemp(v int) *BaseInfo { + s.MaxAttemp = &v + return s +} + type iInfo interface { iBaseInfo String() string @@ -248,8 +248,6 @@ type iInfo interface { type Info struct { iBaseInfo - Name *string `json:"name,omitempty" xml:"name,omitempty" require:"true"` - Age *int `json:"age,omitempty" xml:"age,omitempty" require:"true"` MaxAttemp *int `json:"maxAttemp,omitempty" xml:"maxAttemp,omitempty" require:"true"` // whether to try again Autoretry *bool `json:"autoretry,omitempty" xml:"autoretry,omitempty"` @@ -287,6 +285,8 @@ type Info struct { Socks5NetWork *string `json:"socks5NetWork,omitempty" xml:"socks5NetWork,omitempty"` // whether to enable keep-alive KeepAlive *bool `json:"keepAlive,omitempty" xml:"keepAlive,omitempty"` + Name *string `json:"name,omitempty" xml:"name,omitempty" require:"true"` + Age *int `json:"age,omitempty" xml:"age,omitempty" require:"true"` } func (s Info) String() string { @@ -297,14 +297,6 @@ func (s Info) GoString() string { return s.String() } -func (s *Info) GetName() *string { - return s.Name -} - -func (s *Info) GetAge() *int { - return s.Age -} - func (s *Info) GetMaxAttemp() *int { return s.MaxAttemp } @@ -381,14 +373,12 @@ func (s *Info) GetKeepAlive() *bool { return s.KeepAlive } -func (s *Info) SetName(v string) *Info { - s.Name = &v - return s +func (s *Info) GetName() *string { + return s.Name } -func (s *Info) SetAge(v int) *Info { - s.Age = &v - return s +func (s *Info) GetAge() *int { + return s.Age } func (s *Info) SetMaxAttemp(v int) *Info { @@ -486,6 +476,16 @@ func (s *Info) SetKeepAlive(v bool) *Info { return s } +func (s *Info) SetName(v string) *Info { + s.Name = &v + return s +} + +func (s *Info) SetAge(v int) *Info { + s.Age = &v + return s +} + func Test () (_result <-chan *string, _err error) { _yield := make(chan *string) diff --git a/test/fixtures/try/client.go b/test/fixtures/try/client.go index 9a86624..bf2766e 100644 --- a/test/fixtures/try/client.go +++ b/test/fixtures/try/client.go @@ -115,8 +115,8 @@ func (client *Client)Init(config *source.Config)(_err error) { -func (client *Client) TryMultiCatch (a *int) (_result *int, _err error) { - _result, _err = tryMultiCatch_opTryFunc(a) +func (client *Client) TryMultiCatch (a *int, client *source.Client, b *string, c *int, m *source.Config) (_result *int, _err error) { + _result, _err = tryMultiCatch_opTryFunc(client, b, m, c, a) final := "ok" if _err != nil { if _t, ok := _err.(*Err1Error); ok { @@ -210,7 +210,15 @@ func (client *Client) MultiTryCatch (a *int) (_result map[string]*string, _err e return _result, _err } -func tryMultiCatch_opTryFunc (a *int)( _result *int, _err error) { +func tryMultiCatch_opTryFunc (client *source.Client, b *string, m *Config, c *int, a *int)( _result *int, _err error) { + obj := map[string]interface{}{} + client.Print(obj, dara.String("test")) + b.Split(",") + _err = m.Validate() + if _err != nil { + return _result, _err + } + int(c) if dara.IntValue(a) > 0 { a = dara.Int(20) _err = &Err1Error{ diff --git a/test/fixtures/try/main.dara b/test/fixtures/try/main.dara index 4875edd..662bbf4 100644 --- a/test/fixtures/try/main.dara +++ b/test/fixtures/try/main.dara @@ -16,8 +16,13 @@ exception Err2 = { -async function tryMultiCatch(a: number): number { +async function tryMultiCatch(a: number, client: Source, b: string, c: number, m: Source.Config): number { try { + var obj = {}; + client.print(obj, 'test'); + b.split(','); + m.validate(); + c.parseInt(); if(a > 0) { a = 20; throw new Err1 {