Skip to content

Commit a34752e

Browse files
author
chenjie
committed
update
1 parent 3435036 commit a34752e

File tree

3 files changed

+48
-42
lines changed

3 files changed

+48
-42
lines changed

rpc/context.go

+12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package rpc
22

33
import (
44
"context"
5+
6+
"github.com/chenjie199234/Corelib/util/common"
57
)
68

79
type Context struct {
@@ -43,6 +45,16 @@ func (c *Context) Write(resp []byte) {
4345
c.msg.Metadata = nil
4446
c.next = -1
4547
}
48+
49+
func (c *Context) WriteString(resp string) {
50+
c.msg.Path = ""
51+
c.msg.Deadline = 0
52+
c.msg.Body = common.Str2byte(resp)
53+
c.msg.Error = ""
54+
c.msg.Metadata = nil
55+
c.next = -1
56+
}
57+
4658
func (c *Context) GetBody() []byte {
4759
return c.msg.Body
4860
}

web/context.go

+6-12
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,24 @@ func (this *Context) Next() {
3535
}
3636
}
3737

38-
func (this *Context) Abort(code int, msg []byte) error {
38+
func (this *Context) Abort(code int, e error) {
3939
this.w.WriteHeader(code)
40-
if len(msg) > 0 {
41-
this.w.Write(msg)
40+
if e != nil {
41+
this.w.Write(common.Str2byte(e.Error()))
4242
}
4343
this.next = -1
44-
return nil
4544
}
4645

47-
func (this *Context) AbortString(code int, msg string) error {
48-
return this.Abort(code, common.Str2byte(msg))
49-
}
50-
51-
func (this *Context) Write(code int, msg []byte) error {
46+
func (this *Context) Write(code int, msg []byte) {
5247
this.w.WriteHeader(code)
5348
if len(msg) > 0 {
5449
this.w.Write(msg)
5550
}
5651
this.next = -1
57-
return nil
5852
}
5953

60-
func (this *Context) WriteString(code int, msg string) error {
61-
return this.Write(code, common.Str2byte(msg))
54+
func (this *Context) WriteString(code int, msg string) {
55+
this.Write(code, common.Str2byte(msg))
6256
}
6357

6458
func (this *Context) SetHeader(k, v string) {

web/protoc-gen-go-web/web.go

+30-30
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func geninit(file *protogen.File, g *protogen.GeneratedFile) {
117117
g.P("var e error")
118118
for reg := range allreg {
119119
g.P("if _", service.GoName, "WebRegs[", strconv.Quote(reg), "] ,e = ", g.QualifiedGoIdent(regexpPackage.Ident("Compile")), "(", strconv.Quote(reg), ");e!=nil{")
120-
g.P("panic(\"protoc-gen-go-rpc will check all regexp before generate this code,this may happen when the golang version build protoc-gen-go-rpc and golang version run this code isn't same and the two version's regexp package is different\")")
120+
g.P("panic(\"protoc-gen-go-web will check all regexp before generate this code,this may happen when the golang version build protoc-gen-go-web and golang version run this code isn't same and the two version's regexp package is different\")")
121121
g.P("}")
122122
}
123123
g.P()
@@ -1136,9 +1136,9 @@ func strcheck(prefix string, field *protogen.Field, isslice bool, fop *descripto
11361136
g.P("for _,v:=range ", prefix+field.GoName, "{")
11371137
for _, m := range match {
11381138
if isslice {
1139-
g.P("if !_", service.GoName, "RpcRegs[", strconv.Quote(m), "].Match(v){")
1139+
g.P("if !_", service.GoName, "WebRegs[", strconv.Quote(m), "].Match(v){")
11401140
} else {
1141-
g.P("if !_", service.GoName, "RpcRegs[", strconv.Quote(m), "].MatchString(v){")
1141+
g.P("if !_", service.GoName, "WebRegs[", strconv.Quote(m), "].MatchString(v){")
11421142
}
11431143
g.P("return \"field: ", string(field.Desc.Name()), " in object: ", string(field.Parent.Desc.Name()), " check value str match failed\"")
11441144
//g.P(g.QualifiedGoIdent(logPackage.Ident("Error")), "(\"field: ", string(field.Desc.Name()), " in object: ", string(field.Parent.Desc.Name()), " check value str match failed\")")
@@ -1149,9 +1149,9 @@ func strcheck(prefix string, field *protogen.Field, isslice bool, fop *descripto
11491149
} else {
11501150
for _, m := range match {
11511151
if isslice {
1152-
g.P("if !_", service.GoName, "RpcRegs[", strconv.Quote(m), "].Match(", prefix+field.GoName, "){")
1152+
g.P("if !_", service.GoName, "WebRegs[", strconv.Quote(m), "].Match(", prefix+field.GoName, "){")
11531153
} else {
1154-
g.P("if !_", service.GoName, "RpcRegs[", strconv.Quote(m), "].MatchString(", prefix+field.GoName, "){")
1154+
g.P("if !_", service.GoName, "WebRegs[", strconv.Quote(m), "].MatchString(", prefix+field.GoName, "){")
11551155
}
11561156
g.P("return \"field: ", string(field.Desc.Name()), " in object: ", string(field.Parent.Desc.Name()), " check value str match failed\"")
11571157
//g.P(g.QualifiedGoIdent(logPackage.Ident("Error")), "(\"field: ", string(field.Desc.Name()), " in object: ", string(field.Parent.Desc.Name()), " check value str match failed\")")
@@ -1166,9 +1166,9 @@ func strcheck(prefix string, field *protogen.Field, isslice bool, fop *descripto
11661166
g.P("for _,v:=range ", prefix+field.GoName, "{")
11671167
for _, m := range notmatch {
11681168
if isslice {
1169-
g.P("if _", service.GoName, "RpcRegs[", strconv.Quote(m), "].Match(v){")
1169+
g.P("if _", service.GoName, "WebRegs[", strconv.Quote(m), "].Match(v){")
11701170
} else {
1171-
g.P("if _", service.GoName, "RpcRegs[", strconv.Quote(m), "].MatchString(v){")
1171+
g.P("if _", service.GoName, "WebRegs[", strconv.Quote(m), "].MatchString(v){")
11721172
}
11731173
g.P("return \"field: ", string(field.Desc.Name()), " in object: ", string(field.Parent.Desc.Name()), " check value str not match failed\"")
11741174
//g.P(g.QualifiedGoIdent(logPackage.Ident("Error")), "(\"field: ", string(field.Desc.Name()), " in object: ", string(field.Parent.Desc.Name()), " check value str not match failed\")")
@@ -1179,9 +1179,9 @@ func strcheck(prefix string, field *protogen.Field, isslice bool, fop *descripto
11791179
} else {
11801180
for _, m := range notmatch {
11811181
if isslice {
1182-
g.P("if _", service.GoName, "RpcRegs[", strconv.Quote(m), "].Match(", prefix+field.GoName, "){")
1182+
g.P("if _", service.GoName, "WebRegs[", strconv.Quote(m), "].Match(", prefix+field.GoName, "){")
11831183
} else {
1184-
g.P("if _", service.GoName, "RpcRegs[", strconv.Quote(m), "].MatchString(", prefix+field.GoName, "){")
1184+
g.P("if _", service.GoName, "WebRegs[", strconv.Quote(m), "].MatchString(", prefix+field.GoName, "){")
11851185
}
11861186
g.P("return \"field: ", string(field.Desc.Name()), " in object: ", string(field.Parent.Desc.Name()), " check value str not match failed\"")
11871187
//g.P(g.QualifiedGoIdent(logPackage.Ident("Error")), "(\"field: ", string(field.Desc.Name()), " in object: ", string(field.Parent.Desc.Name()), " check value str not match failed\")")
@@ -1197,17 +1197,17 @@ func messagecheck(prefix string, field *protogen.Field, g *protogen.GeneratedFil
11971197
g.P("if v==nil{")
11981198
g.P("continue")
11991199
g.P("}")
1200-
//g.P("if !_", service.GoName, "RpcCheckers[", strconv.Quote(message.GoIdent.String()), "](v){")
1200+
//g.P("if !_", service.GoName, "WebCheckers[", strconv.Quote(message.GoIdent.String()), "](v){")
12011201
//g.P("return false")
1202-
g.P("if s:=_", service.GoName, "RpcCheckers[", strconv.Quote(field.Message.GoIdent.String()), "](v);s!=\"\"{")
1202+
g.P("if s:=_", service.GoName, "WebCheckers[", strconv.Quote(field.Message.GoIdent.String()), "](v);s!=\"\"{")
12031203
g.P("return s")
12041204
g.P("}")
12051205
g.P("}")
12061206
} else {
12071207
g.P("if ", prefix+field.GoName, "!=nil{")
1208-
//g.P("if !_", service.GoName, "RpcCheckers[", strconv.Quote(message.GoIdent.String()), "](", prefix+field.GoName, "){")
1208+
//g.P("if !_", service.GoName, "WebCheckers[", strconv.Quote(message.GoIdent.String()), "](", prefix+field.GoName, "){")
12091209
//g.P("return false")
1210-
g.P("if s:=_", service.GoName, "RpcCheckers[", strconv.Quote(field.Message.GoIdent.String()), "](", prefix+field.GoName, ");s!=\"\"{")
1210+
g.P("if s:=_", service.GoName, "WebCheckers[", strconv.Quote(field.Message.GoIdent.String()), "](", prefix+field.GoName, ");s!=\"\"{")
12111211
g.P("return s")
12121212
g.P("}")
12131213
g.P("}")
@@ -1497,7 +1497,7 @@ func mapcheck(prefix string, field *protogen.Field, fop *descriptorpb.FieldOptio
14971497
if proto.HasExtension(fop, pbex.E_MapKeyStringRegMatch) {
14981498
keymatch := proto.GetExtension(fop, pbex.E_MapKeyStringRegMatch).([]string)
14991499
for _, v := range keymatch {
1500-
g.P("if !_", service.GoName, "RpcRegs[", strconv.Quote(v), "].MatchString(k){")
1500+
g.P("if !_", service.GoName, "WebRegs[", strconv.Quote(v), "].MatchString(k){")
15011501
g.P("return \"field: ", string(field.Desc.Name()), " in object: ", string(field.Parent.Desc.Name()), " check map key str match failed\"")
15021502
//g.P(g.QualifiedGoIdent(logPackage.Ident("Error")), "(\"field: ", string(field.Desc.Name()), " in object: ", string(field.Parent.Desc.Name()), " check map key str match failed\")")
15031503
//g.P("return false")
@@ -1507,7 +1507,7 @@ func mapcheck(prefix string, field *protogen.Field, fop *descriptorpb.FieldOptio
15071507
if proto.HasExtension(fop, pbex.E_MapKeyStringRegNotMatch) {
15081508
keynotmatch := proto.GetExtension(fop, pbex.E_MapKeyStringRegNotMatch).([]string)
15091509
for _, v := range keynotmatch {
1510-
g.P("if _", service.GoName, "RpcRegs[", strconv.Quote(v), "].MatchString(k){")
1510+
g.P("if _", service.GoName, "WebRegs[", strconv.Quote(v), "].MatchString(k){")
15111511
g.P("return \"field: ", string(field.Desc.Name()), " in object: ", string(field.Parent.Desc.Name()), " check map key str not match failed\"")
15121512
//g.P(g.QualifiedGoIdent(logPackage.Ident("Error")), "(\"field: ", string(field.Desc.Name()), " in object: ", string(field.Parent.Desc.Name()), " check map key str not match failed\")")
15131513
//g.P("return false")
@@ -1794,9 +1794,9 @@ func mapcheck(prefix string, field *protogen.Field, fop *descriptorpb.FieldOptio
17941794
valmatch := proto.GetExtension(fop, pbex.E_MapValueStringBytesRegMatch).([]string)
17951795
for _, v := range valmatch {
17961796
if isbyteslice {
1797-
g.P("if !_", service.GoName, "RpcRegs[", strconv.Quote(v), "].Match(v){")
1797+
g.P("if !_", service.GoName, "WebRegs[", strconv.Quote(v), "].Match(v){")
17981798
} else {
1799-
g.P("if !_", service.GoName, "RpcRegs[", strconv.Quote(v), "].MatchString(v){")
1799+
g.P("if !_", service.GoName, "WebRegs[", strconv.Quote(v), "].MatchString(v){")
18001800
}
18011801
g.P("return \"field: ", string(field.Desc.Name()), " in object: ", string(field.Parent.Desc.Name()), " check map value str match failed\"")
18021802
//g.P(g.QualifiedGoIdent(logPackage.Ident("Error")), "(\"field: ", string(field.Desc.Name()), " in object: ", string(field.Parent.Desc.Name()), " check map value str match failed\")")
@@ -1808,9 +1808,9 @@ func mapcheck(prefix string, field *protogen.Field, fop *descriptorpb.FieldOptio
18081808
valnotmatch := proto.GetExtension(fop, pbex.E_MapValueStringBytesRegNotMatch).([]string)
18091809
for _, v := range valnotmatch {
18101810
if isbyteslice {
1811-
g.P("if _", service.GoName, "RpcRegs[", strconv.Quote(v), "].Match(v){")
1811+
g.P("if _", service.GoName, "WebRegs[", strconv.Quote(v), "].Match(v){")
18121812
} else {
1813-
g.P("if _", service.GoName, "RpcRegs[", strconv.Quote(v), "].MatchString(v){")
1813+
g.P("if _", service.GoName, "WebRegs[", strconv.Quote(v), "].MatchString(v){")
18141814
}
18151815
g.P("return \"field: ", string(field.Desc.Name()), " in object: ", string(field.Parent.Desc.Name()), " check map value str not match failed\"")
18161816
//g.P(g.QualifiedGoIdent(logPackage.Ident("Error")), "(\"field: ", string(field.Desc.Name()), " in object: ", string(field.Parent.Desc.Name()), " check map value str not match failed\")")
@@ -1871,9 +1871,9 @@ func mapcheck(prefix string, field *protogen.Field, fop *descriptorpb.FieldOptio
18711871
g.P("if v==nil{")
18721872
g.P("continue")
18731873
g.P("}")
1874-
g.P("if s:=_", service.GoName, "RpcCheckers[", strconv.Quote(val.Message.GoIdent.String()), "](v);s!=\"\"{")
1874+
g.P("if s:=_", service.GoName, "WebCheckers[", strconv.Quote(val.Message.GoIdent.String()), "](v);s!=\"\"{")
18751875
g.P("return s")
1876-
//g.P("if !_", service.GoName, "RpcCheckers[", strconv.Quote(val.Message.GoIdent.String()), "](v){")
1876+
//g.P("if !_", service.GoName, "WebCheckers[", strconv.Quote(val.Message.GoIdent.String()), "](v){")
18771877
//g.P("return false")
18781878
g.P("}")
18791879
}
@@ -1945,31 +1945,31 @@ func genServer(file *protogen.File, g *protogen.GeneratedFile) {
19451945
g.P("if ", g.QualifiedGoIdent(stringsPackage.Ident("HasPrefix")), "(ctx.GetContentType(),", strconv.Quote("application/json"), "){")
19461946
g.P("data,e:=ctx.GetBody()")
19471947
g.P("if e!=nil{")
1948-
g.P("ctx.AbortString(", g.QualifiedGoIdent(httpPackage.Ident("StatusBadRequest")), ",e.Error())")
1948+
g.P("ctx.Abort(", g.QualifiedGoIdent(httpPackage.Ident("StatusBadRequest")), ",e)")
19491949
g.P("return")
19501950
g.P("}")
19511951
g.P("if len(data)>0{")
19521952
g.P("e:=", g.QualifiedGoIdent(protojsonPackage.Ident("UnmarshalOptions{DiscardUnknown: true}")), ".Unmarshal(data,req)")
19531953
g.P("if e!=nil{")
1954-
g.P("ctx.AbortString(", g.QualifiedGoIdent(httpPackage.Ident("StatusBadRequest")), ",", g.QualifiedGoIdent(errorPackage.Ident("ErrReq")), ".String())")
1954+
g.P("ctx.Abort(", g.QualifiedGoIdent(httpPackage.Ident("StatusBadRequest")), ",", g.QualifiedGoIdent(errorPackage.Ident("ErrReq")), ")")
19551955
g.P("return")
19561956
g.P("}")
19571957
g.P("}")
19581958
g.P("}else if ", g.QualifiedGoIdent(stringsPackage.Ident("HasPrefix")), "(ctx.GetContentType(),", strconv.Quote("application/x-protobuf"), "){")
19591959
g.P("data,e:=ctx.GetBody()")
19601960
g.P("if e!=nil{")
1961-
g.P("ctx.AbortString(", g.QualifiedGoIdent(httpPackage.Ident("StatusBadRequest")), ",e.Error())")
1961+
g.P("ctx.Abort(", g.QualifiedGoIdent(httpPackage.Ident("StatusBadRequest")), ",e)")
19621962
g.P("return")
19631963
g.P("}")
19641964
g.P("if len(data)>0{")
19651965
g.P("if e:=", g.QualifiedGoIdent(protoPackage.Ident("Unmarshal")), "(data,req);e!=nil{")
1966-
g.P("ctx.AbortString(", g.QualifiedGoIdent(httpPackage.Ident("StatusBadRequest")), ",", g.QualifiedGoIdent(errorPackage.Ident("ErrReq")), ".String())")
1966+
g.P("ctx.Abort(", g.QualifiedGoIdent(httpPackage.Ident("StatusBadRequest")), ",", g.QualifiedGoIdent(errorPackage.Ident("ErrReq")), ")")
19671967
g.P("return")
19681968
g.P("}")
19691969
g.P("}")
19701970
g.P("}else{")
19711971
g.P("if e:=ctx.ParseForm();e!=nil{")
1972-
g.P("ctx.AbortString(", g.QualifiedGoIdent(httpPackage.Ident("StatusBadRequest")), ",", g.QualifiedGoIdent(errorPackage.Ident("ErrReq")), ".String())")
1972+
g.P("ctx.Abort(", g.QualifiedGoIdent(httpPackage.Ident("StatusBadRequest")), ",", g.QualifiedGoIdent(errorPackage.Ident("ErrReq")), ")")
19731973
g.P("return")
19741974
g.P("}")
19751975
g.P("data:=", g.QualifiedGoIdent(bufpoolPackage.Ident("GetBuffer()")))
@@ -2057,7 +2057,7 @@ func genServer(file *protogen.File, g *protogen.GeneratedFile) {
20572057
g.P("if data.Len()>2{")
20582058
g.P("e:=", g.QualifiedGoIdent(protojsonPackage.Ident("UnmarshalOptions{DiscardUnknown: true}")), ".Unmarshal(data.Bytes(),req)")
20592059
g.P("if e!=nil{")
2060-
g.P("ctx.AbortString(", g.QualifiedGoIdent(httpPackage.Ident("StatusBadRequest")), ",", g.QualifiedGoIdent(errorPackage.Ident("ErrReq")), ".String())")
2060+
g.P("ctx.Abort(", g.QualifiedGoIdent(httpPackage.Ident("StatusBadRequest")), ",", g.QualifiedGoIdent(errorPackage.Ident("ErrReq")), ")")
20612061
g.P("return")
20622062
g.P("}")
20632063
g.P("}")
@@ -2069,17 +2069,17 @@ func genServer(file *protogen.File, g *protogen.GeneratedFile) {
20692069
//g.P("if !_", service.GoName, "WebCheckers[", strconv.Quote(method.Input.GoIdent.String()), "](req){")
20702070
g.P("if s:=_", service.GoName, "WebCheckers[", strconv.Quote(method.Input.GoIdent.String()), "](req);s!=\"\"{")
20712071
g.P(g.QualifiedGoIdent(logPackage.Ident("Error")), "(\"[", pathurl, "]\",s)")
2072-
g.P("ctx.AbortString(", g.QualifiedGoIdent(httpPackage.Ident("StatusBadRequest")), ",", g.QualifiedGoIdent(errorPackage.Ident("ErrReq")), ".String())")
2072+
g.P("ctx.Abort(", g.QualifiedGoIdent(httpPackage.Ident("StatusBadRequest")), ",", g.QualifiedGoIdent(errorPackage.Ident("ErrReq")), ")")
20732073
g.P("return")
20742074
g.P("}")
20752075
}
20762076

20772077
g.P("resp,e:=handler(ctx,req)")
20782078
g.P("if e!=nil{")
20792079
g.P("if ", g.QualifiedGoIdent(errorPackage.Ident("Equal")), "(e,", g.QualifiedGoIdent(errorPackage.Ident("ErrReq")), "){")
2080-
g.P("ctx.AbortString(", g.QualifiedGoIdent(httpPackage.Ident("StatusBadRequest")), ",e.Error())")
2080+
g.P("ctx.Abort(", g.QualifiedGoIdent(httpPackage.Ident("StatusBadRequest")), ",e)")
20812081
g.P("}else{")
2082-
g.P("ctx.AbortString(", g.QualifiedGoIdent(httpPackage.Ident("StatusInternalServerError")), ",e.Error())")
2082+
g.P("ctx.Abort(", g.QualifiedGoIdent(httpPackage.Ident("StatusInternalServerError")), ",e)")
20832083
g.P("}")
20842084
g.P("return")
20852085
g.P("}")

0 commit comments

Comments
 (0)