mirror of
https://github.com/foomo/gotsrpc.git
synced 2025-10-16 12:35:35 +00:00
support for floats and ints as keys in map types
This commit is contained in:
parent
efa7e6351b
commit
31387b839b
@ -63,6 +63,9 @@ var GoTSRPC;
|
||||
DemoClient.prototype.helloInterface = function (anything, anythingMap, anythingSlice, success, err) {
|
||||
this.transport(this.endPoint, "HelloInterface", [anything, anythingMap, anythingSlice], success, err);
|
||||
};
|
||||
DemoClient.prototype.helloNumberMaps = function (intMap, success, err) {
|
||||
this.transport(this.endPoint, "HelloNumberMaps", [intMap], success, err);
|
||||
};
|
||||
DemoClient.prototype.helloScalarError = function (success, err) {
|
||||
this.transport(this.endPoint, "HelloScalarError", [], success, err);
|
||||
};
|
||||
|
||||
@ -31,6 +31,14 @@ func (d *Demo) HelloInterface(anything interface{}, anythingMap map[string]inter
|
||||
|
||||
}
|
||||
|
||||
func (d *Demo) HelloNumberMaps(intMap map[int]string) (floatMap map[float64]string) {
|
||||
floatMap = map[float64]string{}
|
||||
for i, str := range intMap {
|
||||
floatMap[float64(i)] = str
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Demo) HelloScalarError() (err *ScalarError) {
|
||||
return
|
||||
}
|
||||
|
||||
@ -132,6 +132,13 @@ type (
|
||||
DemoHelloInterfaceResponse struct {
|
||||
}
|
||||
|
||||
DemoHelloNumberMapsRequest struct {
|
||||
IntMap map[int]string
|
||||
}
|
||||
DemoHelloNumberMapsResponse struct {
|
||||
FloatMap map[float64]string
|
||||
}
|
||||
|
||||
DemoHelloScalarErrorRequest struct {
|
||||
}
|
||||
DemoHelloScalarErrorResponse struct {
|
||||
@ -166,6 +173,8 @@ func init() {
|
||||
gob.Register(DemoHelloResponse{})
|
||||
gob.Register(DemoHelloInterfaceRequest{})
|
||||
gob.Register(DemoHelloInterfaceResponse{})
|
||||
gob.Register(DemoHelloNumberMapsRequest{})
|
||||
gob.Register(DemoHelloNumberMapsResponse{})
|
||||
gob.Register(DemoHelloScalarErrorRequest{})
|
||||
gob.Register(DemoHelloScalarErrorResponse{})
|
||||
gob.Register(DemoMapCrapRequest{})
|
||||
@ -229,6 +238,10 @@ func (p *DemoGoRPCProxy) handler(clientAddr string, request interface{}) (respon
|
||||
req := request.(DemoHelloInterfaceRequest)
|
||||
p.service.HelloInterface(req.Anything, req.AnythingMap, req.AnythingSlice)
|
||||
response = DemoHelloInterfaceResponse{}
|
||||
case "DemoHelloNumberMapsRequest":
|
||||
req := request.(DemoHelloNumberMapsRequest)
|
||||
floatMap := p.service.HelloNumberMaps(req.IntMap)
|
||||
response = DemoHelloNumberMapsResponse{FloatMap: floatMap}
|
||||
case "DemoHelloScalarErrorRequest":
|
||||
err := p.service.HelloScalarError()
|
||||
response = DemoHelloScalarErrorResponse{Err: err}
|
||||
|
||||
@ -107,6 +107,17 @@ func (tsc *DemoGoRPCClient) HelloInterface(anything interface{}, anythingMap map
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tsc *DemoGoRPCClient) HelloNumberMaps(intMap map[int]string) (floatMap map[float64]string, clientErr error) {
|
||||
req := DemoHelloNumberMapsRequest{IntMap: intMap}
|
||||
rpcCallRes, rpcCallErr := tsc.Client.Call(req)
|
||||
if rpcCallErr != nil {
|
||||
clientErr = rpcCallErr
|
||||
return
|
||||
}
|
||||
response := rpcCallRes.(DemoHelloNumberMapsResponse)
|
||||
return response.FloatMap, nil
|
||||
}
|
||||
|
||||
func (tsc *DemoGoRPCClient) HelloScalarError() (err *ScalarError, clientErr error) {
|
||||
req := DemoHelloScalarErrorRequest{}
|
||||
rpcCallRes, rpcCallErr := tsc.Client.Call(req)
|
||||
|
||||
@ -187,6 +187,23 @@ func (p *DemoGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
gotsrpc.Reply([]interface{}{}, callStats, r, w)
|
||||
return
|
||||
case "HelloNumberMaps":
|
||||
var (
|
||||
arg_intMap map[int]string
|
||||
)
|
||||
args = []interface{}{&arg_intMap}
|
||||
err := gotsrpc.LoadArgs(&args, callStats, r)
|
||||
if err != nil {
|
||||
gotsrpc.ErrorCouldNotLoadArgs(w)
|
||||
return
|
||||
}
|
||||
executionStart := time.Now()
|
||||
helloNumberMapsFloatMap := p.service.HelloNumberMaps(arg_intMap)
|
||||
if callStats != nil {
|
||||
callStats.Execution = time.Now().Sub(executionStart)
|
||||
}
|
||||
gotsrpc.Reply([]interface{}{helloNumberMapsFloatMap}, callStats, r, w)
|
||||
return
|
||||
case "HelloScalarError":
|
||||
executionStart := time.Now()
|
||||
helloScalarErrorErr := p.service.HelloScalarError()
|
||||
|
||||
@ -46,6 +46,7 @@ type DemoGoTSRPCClient interface {
|
||||
GiveMeAScalar() (amount github_com_foomo_gotsrpc_demo_nested.Amount, wahr github_com_foomo_gotsrpc_demo_nested.True, hier ScalarInPlace, clientErr error)
|
||||
Hello(name string) (retHello_0 string, retHello_1 *Err, clientErr error)
|
||||
HelloInterface(anything interface{}, anythingMap map[string]interface{}, anythingSlice []interface{}) (clientErr error)
|
||||
HelloNumberMaps(intMap map[int]string) (floatMap map[float64]string, clientErr error)
|
||||
HelloScalarError() (err *ScalarError, clientErr error)
|
||||
MapCrap() (crap map[string][]int, clientErr error)
|
||||
Nest() (retNest_0 []*github_com_foomo_gotsrpc_demo_nested.Nested, clientErr error)
|
||||
@ -101,6 +102,13 @@ func (tsc *HTTPDemoGoTSRPCClient) HelloInterface(anything interface{}, anythingM
|
||||
return
|
||||
}
|
||||
|
||||
func (tsc *HTTPDemoGoTSRPCClient) HelloNumberMaps(intMap map[int]string) (floatMap map[float64]string, clientErr error) {
|
||||
args := []interface{}{intMap}
|
||||
reply := []interface{}{&floatMap}
|
||||
clientErr = tsc.Client.Call(tsc.URL, tsc.EndPoint, "HelloNumberMaps", args, reply)
|
||||
return
|
||||
}
|
||||
|
||||
func (tsc *HTTPDemoGoTSRPCClient) HelloScalarError() (err *ScalarError, clientErr error) {
|
||||
args := []interface{}{}
|
||||
reply := []interface{}{&err}
|
||||
|
||||
@ -2,14 +2,15 @@ package demo
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/foomo/gotsrpc/demo/nested"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"math/rand"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/foomo/gotsrpc/demo/nested"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -26,7 +27,6 @@ func RandStringRunes(n int) string {
|
||||
return string(b)
|
||||
}
|
||||
|
||||
|
||||
var (
|
||||
client DemoGoTSRPCClient
|
||||
server *httptest.Server
|
||||
@ -52,6 +52,18 @@ func TestDefault(t *testing.T) {
|
||||
fmt.Println(resp)
|
||||
}
|
||||
|
||||
func TestHelloNumberMaps(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
intMap := map[int]string{1: "one", 2: "two", 3: "three"}
|
||||
floatMap, errClient := client.HelloNumberMaps(intMap)
|
||||
assert.NoError(t, errClient)
|
||||
for f, fstr := range floatMap {
|
||||
i := int(f)
|
||||
assert.Equal(t, fstr, intMap[i])
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkRequests(b *testing.B, count int) {
|
||||
setup()
|
||||
defer teardown()
|
||||
@ -81,7 +93,7 @@ func GeneratePerson(count int) *Person {
|
||||
}
|
||||
|
||||
func GenerateAddress() *Address {
|
||||
gen := func() string{
|
||||
gen := func() string {
|
||||
return RandStringRunes(32)
|
||||
}
|
||||
genarr := func(count int) (ret []string) {
|
||||
|
||||
@ -23,6 +23,9 @@ export class DemoClient {
|
||||
helloInterface(anything:any, anythingMap:{[index:string]:any}, anythingSlice:any[], success:() => void, err:(request:XMLHttpRequest, e?:Error) => void) {
|
||||
this.transport(this.endPoint, "HelloInterface", [anything, anythingMap, anythingSlice], success, err);
|
||||
}
|
||||
helloNumberMaps(intMap:{[index:number]:string}, success:(floatMap:{[index:number]:string}) => void, err:(request:XMLHttpRequest, e?:Error) => void) {
|
||||
this.transport(this.endPoint, "HelloNumberMaps", [intMap], success, err);
|
||||
}
|
||||
helloScalarError(success:(err:string) => void, err:(request:XMLHttpRequest, e?:Error) => void) {
|
||||
this.transport(this.endPoint, "HelloScalarError", [], success, err);
|
||||
}
|
||||
|
||||
@ -48,6 +48,9 @@ module GoTSRPC.Demo {
|
||||
helloInterface(anything:any, anythingMap:{[index:string]:any}, anythingSlice:any[], success:() => void, err:(request:XMLHttpRequest, e?:Error) => void) {
|
||||
this.transport(this.endPoint, "HelloInterface", [anything, anythingMap, anythingSlice], success, err);
|
||||
}
|
||||
helloNumberMaps(intMap:{[index:number]:string}, success:(floatMap:{[index:number]:string}) => void, err:(request:XMLHttpRequest, e?:Error) => void) {
|
||||
this.transport(this.endPoint, "HelloNumberMaps", [intMap], success, err);
|
||||
}
|
||||
helloScalarError(success:(err:string) => void, err:(request:XMLHttpRequest, e?:Error) => void) {
|
||||
this.transport(this.endPoint, "HelloScalarError", [], success, err);
|
||||
}
|
||||
|
||||
4
go.go
4
go.go
@ -30,7 +30,7 @@ func (v *Value) goType(aliases map[string]string, packageName string) (t string)
|
||||
}
|
||||
t += v.StructType.Name
|
||||
case v.Map != nil:
|
||||
t += `map[` + v.Map.KeyType + `]` + v.Map.Value.goType(aliases, packageName)
|
||||
t += `map[` + v.Map.KeyGoType + `]` + v.Map.Value.goType(aliases, packageName)
|
||||
case v.Scalar != nil:
|
||||
// TODO this is a hack to retrieve string types
|
||||
if packageName != v.Scalar.Package {
|
||||
@ -54,7 +54,7 @@ func (v *Value) emptyLiteral(aliases map[string]string) (e string) {
|
||||
}
|
||||
switch true {
|
||||
case v.Map != nil:
|
||||
e += "map[" + v.Map.KeyType + "]" + v.Map.Value.emptyLiteral(aliases)
|
||||
e += "map[" + v.Map.KeyGoType + "]" + v.Map.Value.emptyLiteral(aliases)
|
||||
case len(v.GoScalarType) > 0:
|
||||
switch v.GoScalarType {
|
||||
case "string":
|
||||
|
||||
5
model.go
5
model.go
@ -40,8 +40,9 @@ type Array struct {
|
||||
}
|
||||
|
||||
type Map struct {
|
||||
Value *Value
|
||||
KeyType string
|
||||
Value *Value
|
||||
KeyType string
|
||||
KeyGoType string
|
||||
}
|
||||
|
||||
type Field struct {
|
||||
|
||||
@ -183,6 +183,7 @@ func readAstMapType(m *Map, mapType *ast.MapType, fileImports fileImportSpecMap)
|
||||
case "*ast.Ident":
|
||||
_, scalarType := getTypesFromAstType(mapType.Key.(*ast.Ident))
|
||||
m.KeyType = string(scalarType)
|
||||
m.KeyGoType = mapType.Key.(*ast.Ident).Name
|
||||
default:
|
||||
// todo: implement support for "*ast.Scalar" type (sca)
|
||||
// this is important for scalar types in map keys
|
||||
|
||||
Loading…
Reference in New Issue
Block a user