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) {
|
||||
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;
|
||||
return BarClient;
|
||||
}());
|
||||
|
||||
@ -2,4 +2,5 @@ package demo
|
||||
|
||||
type Bar interface {
|
||||
Hello(number int64) int
|
||||
Repeat(one, two string) (three, four bool)
|
||||
}
|
||||
|
||||
@ -300,11 +300,22 @@ type (
|
||||
BarHelloResponse struct {
|
||||
RetHello_0 int
|
||||
}
|
||||
|
||||
BarRepeatRequest struct {
|
||||
One string
|
||||
Two string
|
||||
}
|
||||
BarRepeatResponse struct {
|
||||
Three bool
|
||||
Four bool
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
gob.Register(BarHelloRequest{})
|
||||
gob.Register(BarHelloResponse{})
|
||||
gob.Register(BarRepeatRequest{})
|
||||
gob.Register(BarRepeatResponse{})
|
||||
}
|
||||
|
||||
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)
|
||||
retHello_0 := p.service.Hello(req.Number)
|
||||
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:
|
||||
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)
|
||||
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)
|
||||
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:
|
||||
gotsrpc.ClearStats(r)
|
||||
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 {
|
||||
Hello(number int64) (retHello_0 int, clientErr error)
|
||||
Repeat(one string, two string) (three bool, four bool, clientErr error)
|
||||
}
|
||||
|
||||
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)
|
||||
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> {
|
||||
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) {
|
||||
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) {
|
||||
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 {
|
||||
name, value, _ := readField(param, fileImports)
|
||||
fields = append(fields, &Field{
|
||||
Name: name,
|
||||
Value: value,
|
||||
})
|
||||
names, value, _ := readField(param, fileImports)
|
||||
for _, name := range names {
|
||||
fields = append(fields, &Field{
|
||||
Name: name,
|
||||
Value: value,
|
||||
})
|
||||
}
|
||||
}
|
||||
trace("done reading fields")
|
||||
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) {
|
||||
name = ""
|
||||
if len(astField.Names) > 0 {
|
||||
name = astField.Names[0].Name
|
||||
func readField(astField *ast.Field, fileImports fileImportSpecMap) (names []string, v *Value, jsonInfo *JSONInfo) {
|
||||
if len(astField.Names) == 0 {
|
||||
names = append(names, "")
|
||||
} else {
|
||||
for _, name := range astField.Names {
|
||||
names = append(names, name.Name)
|
||||
}
|
||||
}
|
||||
v = &Value{}
|
||||
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) {
|
||||
fields = []*Field{}
|
||||
for _, field := range fieldList {
|
||||
name, value, jsonInfo := readField(field, fileImports)
|
||||
if len(name) == 0 {
|
||||
trace("i do not understand this one", field, name, value, jsonInfo)
|
||||
names, value, jsonInfo := readField(field, fileImports)
|
||||
if len(names) == 0 {
|
||||
trace("i do not understand this one", field, names, value, jsonInfo)
|
||||
continue
|
||||
}
|
||||
// this is not unicode proof
|
||||
if strings.Compare(strings.ToLower(name[:1]), name[:1]) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if value != nil {
|
||||
fields = append(fields, &Field{
|
||||
Name: name,
|
||||
Value: value,
|
||||
JSONInfo: jsonInfo,
|
||||
})
|
||||
for _, name := range names {
|
||||
// this is not unicode proof
|
||||
if strings.Compare(strings.ToLower(name[:1]), name[:1]) == 0 {
|
||||
continue
|
||||
}
|
||||
fields = append(fields, &Field{
|
||||
Name: name,
|
||||
Value: value,
|
||||
JSONInfo: jsonInfo,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
|
||||
Loading…
Reference in New Issue
Block a user