mirror of
https://github.com/foomo/gotsrpc.git
synced 2025-10-16 12:35:35 +00:00
feat: support variable grouping
This commit is contained in:
parent
0f6f97c37f
commit
b14f561357
@ -95,6 +95,9 @@ var GoTSRPC;
|
|||||||
BarClient.prototype.hello = function (number, success, err) {
|
BarClient.prototype.hello = function (number, success, err) {
|
||||||
this.transport(this.endPoint, "Hello", [number], success, err);
|
this.transport(this.endPoint, "Hello", [number], success, err);
|
||||||
};
|
};
|
||||||
|
BarClient.prototype.repeat = function (one, two, success, err) {
|
||||||
|
this.transport(this.endPoint, "Repeat", [one, two], success, err);
|
||||||
|
};
|
||||||
BarClient.defaultInst = new BarClient;
|
BarClient.defaultInst = new BarClient;
|
||||||
return BarClient;
|
return BarClient;
|
||||||
}());
|
}());
|
||||||
|
|||||||
@ -2,4 +2,5 @@ package demo
|
|||||||
|
|
||||||
type Bar interface {
|
type Bar interface {
|
||||||
Hello(number int64) int
|
Hello(number int64) int
|
||||||
|
Repeat(one, two string) (three, four bool)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -300,11 +300,22 @@ type (
|
|||||||
BarHelloResponse struct {
|
BarHelloResponse struct {
|
||||||
RetHello_0 int
|
RetHello_0 int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BarRepeatRequest struct {
|
||||||
|
One string
|
||||||
|
Two string
|
||||||
|
}
|
||||||
|
BarRepeatResponse struct {
|
||||||
|
Three bool
|
||||||
|
Four bool
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
gob.Register(BarHelloRequest{})
|
gob.Register(BarHelloRequest{})
|
||||||
gob.Register(BarHelloResponse{})
|
gob.Register(BarHelloResponse{})
|
||||||
|
gob.Register(BarRepeatRequest{})
|
||||||
|
gob.Register(BarRepeatResponse{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBarGoRPCProxy(addr string, service Bar, tlsConfig *tls.Config) *BarGoRPCProxy {
|
func NewBarGoRPCProxy(addr string, service Bar, tlsConfig *tls.Config) *BarGoRPCProxy {
|
||||||
@ -349,6 +360,10 @@ func (p *BarGoRPCProxy) handler(clientAddr string, request interface{}) (respons
|
|||||||
req := request.(BarHelloRequest)
|
req := request.(BarHelloRequest)
|
||||||
retHello_0 := p.service.Hello(req.Number)
|
retHello_0 := p.service.Hello(req.Number)
|
||||||
response = BarHelloResponse{RetHello_0: retHello_0}
|
response = BarHelloResponse{RetHello_0: retHello_0}
|
||||||
|
case "BarRepeatRequest":
|
||||||
|
req := request.(BarRepeatRequest)
|
||||||
|
three, four := p.service.Repeat(req.One, req.Two)
|
||||||
|
response = BarRepeatResponse{Three: three, Four: four}
|
||||||
default:
|
default:
|
||||||
fmt.Println("Unkown request type", reflect.TypeOf(request).String())
|
fmt.Println("Unkown request type", reflect.TypeOf(request).String())
|
||||||
}
|
}
|
||||||
|
|||||||
@ -205,3 +205,14 @@ func (tsc *BarGoRPCClient) Hello(number int64) (retHello_0 int, clientErr error)
|
|||||||
response := rpcCallRes.(BarHelloResponse)
|
response := rpcCallRes.(BarHelloResponse)
|
||||||
return response.RetHello_0, nil
|
return response.RetHello_0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tsc *BarGoRPCClient) Repeat(one string, two string) (three bool, four bool, clientErr error) {
|
||||||
|
req := BarRepeatRequest{One: one, Two: two}
|
||||||
|
rpcCallRes, rpcCallErr := tsc.Client.Call(req)
|
||||||
|
if rpcCallErr != nil {
|
||||||
|
clientErr = rpcCallErr
|
||||||
|
return
|
||||||
|
}
|
||||||
|
response := rpcCallRes.(BarRepeatResponse)
|
||||||
|
return response.Three, response.Four, nil
|
||||||
|
}
|
||||||
|
|||||||
@ -331,6 +331,24 @@ func (p *BarGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
gotsrpc.Reply([]interface{}{helloRet}, callStats, r, w)
|
gotsrpc.Reply([]interface{}{helloRet}, callStats, r, w)
|
||||||
return
|
return
|
||||||
|
case "Repeat":
|
||||||
|
var (
|
||||||
|
arg_one string
|
||||||
|
arg_two string
|
||||||
|
)
|
||||||
|
args = []interface{}{&arg_one, &arg_two}
|
||||||
|
err := gotsrpc.LoadArgs(&args, callStats, r)
|
||||||
|
if err != nil {
|
||||||
|
gotsrpc.ErrorCouldNotLoadArgs(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
executionStart := time.Now()
|
||||||
|
repeatThree, repeatFour := p.service.Repeat(arg_one, arg_two)
|
||||||
|
if callStats != nil {
|
||||||
|
callStats.Execution = time.Now().Sub(executionStart)
|
||||||
|
}
|
||||||
|
gotsrpc.Reply([]interface{}{repeatThree, repeatFour}, callStats, r, w)
|
||||||
|
return
|
||||||
default:
|
default:
|
||||||
gotsrpc.ClearStats(r)
|
gotsrpc.ClearStats(r)
|
||||||
http.Error(w, "404 - not found "+r.URL.Path, http.StatusNotFound)
|
http.Error(w, "404 - not found "+r.URL.Path, http.StatusNotFound)
|
||||||
|
|||||||
@ -147,6 +147,7 @@ func (tsc *HTTPDemoGoTSRPCClient) TestScalarInPlace() (retTestScalarInPlace_0 Sc
|
|||||||
|
|
||||||
type BarGoTSRPCClient interface {
|
type BarGoTSRPCClient interface {
|
||||||
Hello(number int64) (retHello_0 int, clientErr error)
|
Hello(number int64) (retHello_0 int, clientErr error)
|
||||||
|
Repeat(one string, two string) (three bool, four bool, clientErr error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type HTTPBarGoTSRPCClient struct {
|
type HTTPBarGoTSRPCClient struct {
|
||||||
@ -176,3 +177,10 @@ func (tsc *HTTPBarGoTSRPCClient) Hello(number int64) (retHello_0 int, clientErr
|
|||||||
clientErr = tsc.Client.Call(tsc.URL, tsc.EndPoint, "Hello", args, reply)
|
clientErr = tsc.Client.Call(tsc.URL, tsc.EndPoint, "Hello", args, reply)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tsc *HTTPBarGoTSRPCClient) Repeat(one string, two string) (three bool, four bool, clientErr error) {
|
||||||
|
args := []interface{}{one, two}
|
||||||
|
reply := []interface{}{&three, &four}
|
||||||
|
clientErr = tsc.Client.Call(tsc.URL, tsc.EndPoint, "Repeat", args, reply)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|||||||
@ -66,4 +66,9 @@ export class BarClient {
|
|||||||
async hello(number:number):Promise<number> {
|
async hello(number:number):Promise<number> {
|
||||||
return (await this.transport<{0:number}>("Hello", [number]))[0]
|
return (await this.transport<{0:number}>("Hello", [number]))[0]
|
||||||
}
|
}
|
||||||
|
async repeat(one:string, two:string):Promise<{three:boolean; four:boolean}> {
|
||||||
|
let response = await this.transport<{0:boolean; 1:boolean}>("Repeat", [one, two])
|
||||||
|
let responseObject = {three : response[0], four : response[1]};
|
||||||
|
return responseObject;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -47,4 +47,7 @@ export class BarClient {
|
|||||||
hello(number:number, success:(ret:number) => void, err:(request:XMLHttpRequest, e?:Error) => void) {
|
hello(number:number, success:(ret:number) => void, err:(request:XMLHttpRequest, e?:Error) => void) {
|
||||||
this.transport(this.endPoint, "Hello", [number], success, err);
|
this.transport(this.endPoint, "Hello", [number], success, err);
|
||||||
}
|
}
|
||||||
|
repeat(one:string, two:string, success:(three:boolean, four:boolean) => void, err:(request:XMLHttpRequest, e?:Error) => void) {
|
||||||
|
this.transport(this.endPoint, "Repeat", [one, two], success, err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -73,5 +73,8 @@ module GoTSRPC.Demo {
|
|||||||
hello(number:number, success:(ret:number) => void, err:(request:XMLHttpRequest, e?:Error) => void) {
|
hello(number:number, success:(ret:number) => void, err:(request:XMLHttpRequest, e?:Error) => void) {
|
||||||
this.transport(this.endPoint, "Hello", [number], success, err);
|
this.transport(this.endPoint, "Hello", [number], success, err);
|
||||||
}
|
}
|
||||||
|
repeat(one:string, two:string, success:(three:boolean, four:boolean) => void, err:(request:XMLHttpRequest, e?:Error) => void) {
|
||||||
|
this.transport(this.endPoint, "Repeat", [one, two], success, err);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -157,11 +157,13 @@ func readFields(fieldList *ast.FieldList, fileImports fileImportSpecMap) (fields
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, param := range fieldList.List {
|
for _, param := range fieldList.List {
|
||||||
name, value, _ := readField(param, fileImports)
|
names, value, _ := readField(param, fileImports)
|
||||||
fields = append(fields, &Field{
|
for _, name := range names {
|
||||||
Name: name,
|
fields = append(fields, &Field{
|
||||||
Value: value,
|
Name: name,
|
||||||
})
|
Value: value,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
trace("done reading fields")
|
trace("done reading fields")
|
||||||
return
|
return
|
||||||
|
|||||||
@ -291,10 +291,13 @@ func (v *Value) loadExpr(expr ast.Expr, fileImports fileImportSpecMap) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func readField(astField *ast.Field, fileImports fileImportSpecMap) (name string, v *Value, jsonInfo *JSONInfo) {
|
func readField(astField *ast.Field, fileImports fileImportSpecMap) (names []string, v *Value, jsonInfo *JSONInfo) {
|
||||||
name = ""
|
if len(astField.Names) == 0 {
|
||||||
if len(astField.Names) > 0 {
|
names = append(names, "")
|
||||||
name = astField.Names[0].Name
|
} else {
|
||||||
|
for _, name := range astField.Names {
|
||||||
|
names = append(names, name.Name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
v = &Value{}
|
v = &Value{}
|
||||||
v.loadExpr(astField.Type, fileImports)
|
v.loadExpr(astField.Type, fileImports)
|
||||||
@ -307,22 +310,23 @@ func readField(astField *ast.Field, fileImports fileImportSpecMap) (name string,
|
|||||||
func readFieldList(fieldList []*ast.Field, fileImports fileImportSpecMap) (fields []*Field) {
|
func readFieldList(fieldList []*ast.Field, fileImports fileImportSpecMap) (fields []*Field) {
|
||||||
fields = []*Field{}
|
fields = []*Field{}
|
||||||
for _, field := range fieldList {
|
for _, field := range fieldList {
|
||||||
name, value, jsonInfo := readField(field, fileImports)
|
names, value, jsonInfo := readField(field, fileImports)
|
||||||
if len(name) == 0 {
|
if len(names) == 0 {
|
||||||
trace("i do not understand this one", field, name, value, jsonInfo)
|
trace("i do not understand this one", field, names, value, jsonInfo)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// this is not unicode proof
|
|
||||||
if strings.Compare(strings.ToLower(name[:1]), name[:1]) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if value != nil {
|
if value != nil {
|
||||||
fields = append(fields, &Field{
|
for _, name := range names {
|
||||||
Name: name,
|
// this is not unicode proof
|
||||||
Value: value,
|
if strings.Compare(strings.ToLower(name[:1]), name[:1]) == 0 {
|
||||||
JSONInfo: jsonInfo,
|
continue
|
||||||
})
|
}
|
||||||
|
fields = append(fields, &Field{
|
||||||
|
Name: name,
|
||||||
|
Value: value,
|
||||||
|
JSONInfo: jsonInfo,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user