From 29b313ea6f6c0df61f0b01f9617de8476df3a1df Mon Sep 17 00:00:00 2001 From: Kevin Franklin Kim Date: Thu, 18 Sep 2025 23:22:52 +0200 Subject: [PATCH] test: add types test --- example/types/gotsrpc.yml | 11 ++ example/types/gotsrpc_gen.go | 107 +++++++++++++ example/types/gotsrpcclient_gen.go | 57 +++++++ example/types/handler.go | 10 ++ example/types/handler_test.go | 19 +++ example/types/service.go | 10 ++ example/types/vo.go | 231 +++++++++++++++++++++++++++++ 7 files changed, 445 insertions(+) create mode 100644 example/types/gotsrpc.yml create mode 100644 example/types/gotsrpc_gen.go create mode 100644 example/types/gotsrpcclient_gen.go create mode 100644 example/types/handler.go create mode 100644 example/types/handler_test.go create mode 100644 example/types/service.go create mode 100644 example/types/vo.go diff --git a/example/types/gotsrpc.yml b/example/types/gotsrpc.yml new file mode 100644 index 0000000..4e411e6 --- /dev/null +++ b/example/types/gotsrpc.yml @@ -0,0 +1,11 @@ +module: + name: github.com/foomo/gotsrpc/v2 + path: ../../ + +targets: + basic: + services: + /service: Service + package: github.com/foomo/gotsrpc/v2/example/types + tsrpc: + - Service diff --git a/example/types/gotsrpc_gen.go b/example/types/gotsrpc_gen.go new file mode 100644 index 0000000..572e61f --- /dev/null +++ b/example/types/gotsrpc_gen.go @@ -0,0 +1,107 @@ +// Code generated by gotsrpc https://github.com/foomo/gotsrpc/v2 - DO NOT EDIT. + +package service + +import ( + io "io" + http "net/http" + time "time" + + gotsrpc "github.com/foomo/gotsrpc/v2" +) + +const ( + ServiceGoTSRPCProxyString = "String" + ServiceGoTSRPCProxyStrings = "Strings" +) + +type ServiceGoTSRPCProxy struct { + EndPoint string + service Service +} + +func NewDefaultServiceGoTSRPCProxy(service Service) *ServiceGoTSRPCProxy { + return NewServiceGoTSRPCProxy(service, "/service") +} + +func NewServiceGoTSRPCProxy(service Service, endpoint string) *ServiceGoTSRPCProxy { + return &ServiceGoTSRPCProxy{ + EndPoint: endpoint, + service: service, + } +} + +// ServeHTTP exposes your service +func (p *ServiceGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodOptions { + return + } else if r.Method != http.MethodPost { + gotsrpc.ErrorMethodNotAllowed(w) + return + } + defer io.Copy(io.Discard, r.Body) // Drain Request Body + + funcName := gotsrpc.GetCalledFunc(r, p.EndPoint) + callStats, _ := gotsrpc.GetStatsForRequest(r) + callStats.Func = funcName + callStats.Package = "github.com/foomo/gotsrpc/v2/example/types" + callStats.Service = "Service" + switch funcName { + case ServiceGoTSRPCProxyString: + var ( + args []interface{} + rets []interface{} + ) + var ( + arg_a string + ) + args = []interface{}{&arg_a} + if err := gotsrpc.LoadArgs(&args, callStats, r); err != nil { + gotsrpc.ErrorCouldNotLoadArgs(w) + return + } + executionStart := time.Now() + rw := gotsrpc.ResponseWriter{ResponseWriter: w} + p.service.String(&rw, r, arg_a) + callStats.Execution = time.Since(executionStart) + if rw.Status() == http.StatusOK { + rets = []interface{}{} + if err := gotsrpc.Reply(rets, callStats, r, w); err != nil { + gotsrpc.ErrorCouldNotReply(w) + return + } + } + gotsrpc.Monitor(w, r, args, rets, callStats) + return + case ServiceGoTSRPCProxyStrings: + var ( + args []interface{} + rets []interface{} + ) + var ( + arg_a string + arg_b string + ) + args = []interface{}{&arg_a, &arg_b} + if err := gotsrpc.LoadArgs(&args, callStats, r); err != nil { + gotsrpc.ErrorCouldNotLoadArgs(w) + return + } + executionStart := time.Now() + rw := gotsrpc.ResponseWriter{ResponseWriter: w} + p.service.Strings(&rw, r, arg_a, arg_b) + callStats.Execution = time.Since(executionStart) + if rw.Status() == http.StatusOK { + rets = []interface{}{} + if err := gotsrpc.Reply(rets, callStats, r, w); err != nil { + gotsrpc.ErrorCouldNotReply(w) + return + } + } + gotsrpc.Monitor(w, r, args, rets, callStats) + return + default: + gotsrpc.ClearStats(r) + gotsrpc.ErrorFuncNotFound(w) + } +} diff --git a/example/types/gotsrpcclient_gen.go b/example/types/gotsrpcclient_gen.go new file mode 100644 index 0000000..c5a1d92 --- /dev/null +++ b/example/types/gotsrpcclient_gen.go @@ -0,0 +1,57 @@ +// Code generated by gotsrpc https://github.com/foomo/gotsrpc/v2 - DO NOT EDIT. + +package service + +import ( + go_context "context" + go_net_http "net/http" + + gotsrpc "github.com/foomo/gotsrpc/v2" + pkg_errors "github.com/pkg/errors" +) + +type ServiceGoTSRPCClient interface { + String(ctx go_context.Context, a string) (clientErr error) + Strings(ctx go_context.Context, a string, b string) (clientErr error) +} + +type HTTPServiceGoTSRPCClient struct { + URL string + EndPoint string + Client gotsrpc.Client +} + +func NewDefaultServiceGoTSRPCClient(url string) *HTTPServiceGoTSRPCClient { + return NewServiceGoTSRPCClient(url, "/service") +} + +func NewServiceGoTSRPCClient(url string, endpoint string) *HTTPServiceGoTSRPCClient { + return NewServiceGoTSRPCClientWithClient(url, endpoint, nil) +} + +func NewServiceGoTSRPCClientWithClient(url string, endpoint string, client *go_net_http.Client) *HTTPServiceGoTSRPCClient { + return &HTTPServiceGoTSRPCClient{ + URL: url, + EndPoint: endpoint, + Client: gotsrpc.NewClientWithHttpClient(client), + } +} +func (tsc *HTTPServiceGoTSRPCClient) String(ctx go_context.Context, a string) (clientErr error) { + args := []interface{}{a} + reply := []interface{}{} + clientErr = tsc.Client.Call(ctx, tsc.URL, tsc.EndPoint, "String", args, reply) + if clientErr != nil { + clientErr = pkg_errors.WithMessage(clientErr, "failed to call service.ServiceGoTSRPCProxy String") + } + return +} + +func (tsc *HTTPServiceGoTSRPCClient) Strings(ctx go_context.Context, a string, b string) (clientErr error) { + args := []interface{}{a, b} + reply := []interface{}{} + clientErr = tsc.Client.Call(ctx, tsc.URL, tsc.EndPoint, "Strings", args, reply) + if clientErr != nil { + clientErr = pkg_errors.WithMessage(clientErr, "failed to call service.ServiceGoTSRPCProxy Strings") + } + return +} diff --git a/example/types/handler.go b/example/types/handler.go new file mode 100644 index 0000000..9bc44ec --- /dev/null +++ b/example/types/handler.go @@ -0,0 +1,10 @@ +package service + +import ( + "net/http" +) + +type Handler struct{} + +func (h *Handler) String(w http.ResponseWriter, r *http.Request, a string) {} +func (h *Handler) Strings(w http.ResponseWriter, r *http.Request, a, b string) {} diff --git a/example/types/handler_test.go b/example/types/handler_test.go new file mode 100644 index 0000000..a8de91c --- /dev/null +++ b/example/types/handler_test.go @@ -0,0 +1,19 @@ +package service_test + +import ( + "net/http/httptest" + "testing" + + service "github.com/foomo/gotsrpc/v2/example/types" + "github.com/stretchr/testify/require" +) + +func TestHandler(t *testing.T) { + s := httptest.NewServer(service.NewDefaultServiceGoTSRPCProxy(&service.Handler{})) + c := service.NewDefaultServiceGoTSRPCClient(s.URL) + c.Client.SetTransportHttpClient(s.Client()) + + require.NoError(t, c.String(t.Context(), "s")) + + require.NoError(t, c.Strings(t.Context(), "a", "b")) +} diff --git a/example/types/service.go b/example/types/service.go new file mode 100644 index 0000000..e77091c --- /dev/null +++ b/example/types/service.go @@ -0,0 +1,10 @@ +package service + +import ( + "net/http" +) + +type Service interface { + String(w http.ResponseWriter, r *http.Request, a string) + Strings(w http.ResponseWriter, r *http.Request, a, b string) +} diff --git a/example/types/vo.go b/example/types/vo.go new file mode 100644 index 0000000..66fe0cd --- /dev/null +++ b/example/types/vo.go @@ -0,0 +1,231 @@ +package service + +type IntType int + +const ( + IntAType IntType = 1 + IntBType IntType = 2 +) + +type Int32Type int32 + +const ( + Int32AType Int32Type = 1 + Int32BType Int32Type = 2 +) + +type Int64Type int64 + +const ( + Int64AType Int64Type = 1 + Int64BType Int64Type = 2 +) + +type UIntType int + +const ( + UIntAType UIntType = 1 + UIntBType UIntType = 2 +) + +type UInt32Type uint32 + +const ( + UInt32AType UInt32Type = 1 + UInt32BType UInt32Type = 2 +) + +type UInt64Type uint64 + +const ( + UInt64AType UInt64Type = 1 + UInt64BType UInt64Type = 2 +) + +type Float32Type float32 + +const ( + Float32AType Float32Type = 1 + Float32BType Float32Type = 2 +) + +type Float64Type float64 + +const ( + Float64AType Float64Type = 1 + Float64BType Float64Type = 2 +) + +type StringType string + +const ( + StringAType StringType = "A" + StringBType StringType = "B" +) + +type IntTypeMapKey int + +const ( + IntATypeMapKey IntTypeMapKey = 1 + IntBTypeMapKey IntTypeMapKey = 2 +) + +type Int32TypeMapKey int32 + +const ( + Int32ATypeMapKey Int32TypeMapKey = 1 + Int32BTypeMapKey Int32TypeMapKey = 2 +) + +type Int64TypeMapKey int64 + +const ( + Int64ATypeMapKey Int64TypeMapKey = 1 + Int64BTypeMapKey Int64TypeMapKey = 2 +) + +type UIntTypeMapKey int + +const ( + UIntATypeMapKey UIntTypeMapKey = 1 + UIntBTypeMapKey UIntTypeMapKey = 2 +) + +type UInt32TypeMapKey uint32 + +const ( + UInt32ATypeMapKey UInt32TypeMapKey = 1 + UInt32BTypeMapKey UInt32TypeMapKey = 2 +) + +type UInt64TypeMapKey uint64 + +const ( + UInt64ATypeMapKey UInt64TypeMapKey = 1 + UInt64BTypeMapKey UInt64TypeMapKey = 2 +) + +type Float32TypeMapKey float32 + +const ( + Float32ATypeMapKey Float32TypeMapKey = 1 + Float32BTypeMapKey Float32TypeMapKey = 2 +) + +type Float64TypeMapKey float64 + +const ( + Float64ATypeMapKey Float64TypeMapKey = 1 + Float64BTypeMapKey Float64TypeMapKey = 2 +) + +type StringTypeMapKey string + +const ( + StringATypeMapKey StringTypeMapKey = "A" + StringBTypeMapKey StringTypeMapKey = "B" +) + +type IntTypeMapValue int + +const ( + IntATypeMapValue IntTypeMapValue = 1 + IntBTypeMapValue IntTypeMapValue = 2 +) + +type Int32TypeMapValue int32 + +const ( + Int32ATypeMapValue Int32TypeMapValue = 1 + Int32BTypeMapValue Int32TypeMapValue = 2 +) + +type Int64TypeMapValue int64 + +const ( + Int64ATypeMapValue Int64TypeMapValue = 1 + Int64BTypeMapValue Int64TypeMapValue = 2 +) + +type UIntTypeMapValue int + +const ( + UIntATypeMapValue UIntTypeMapValue = 1 + UIntBTypeMapValue UIntTypeMapValue = 2 +) + +type UInt32TypeMapValue uint32 + +const ( + UInt32ATypeMapValue UInt32TypeMapValue = 1 + UInt32BTypeMapValue UInt32TypeMapValue = 2 +) + +type UInt64TypeMapValue uint64 + +const ( + UInt64ATypeMapValue UInt64TypeMapValue = 1 + UInt64BTypeMapValue UInt64TypeMapValue = 2 +) + +type Float32TypeMapValue float32 + +const ( + Float32ATypeMapValue Float32TypeMapValue = 1 + Float32BTypeMapValue Float32TypeMapValue = 2 +) + +type Float64TypeMapValue float64 + +const ( + Float64ATypeMapValue Float64TypeMapValue = 1 + Float64BTypeMapValue Float64TypeMapValue = 2 +) + +type StringTypeMapValue string + +const ( + StringATypeMapValue StringTypeMapValue = "A" + StringBTypeMapValue StringTypeMapValue = "B" +) + +type ( + IntTypeMapTyped map[IntTypeMapKey]IntTypeMapValue + Int32TypeMapTyped map[Int32TypeMapKey]Int32TypeMapValue + Int64TypeMapTyped map[Int64TypeMapKey]Int64TypeMapValue + UIntTypeMapTyped map[UIntTypeMapKey]UIntTypeMapValue + UInt32TypeMapTyped map[UInt32TypeMapKey]UInt32TypeMapValue + UInt64TypeMapTyped map[UInt64TypeMapKey]UInt64TypeMapValue + Float32TypeMapTyped map[Float32TypeMapKey]Float32TypeMapValue + Float64TypeMapTyped map[Float64TypeMapKey]Float64TypeMapValue + StringTypeMapTyped map[StringTypeMapKey]StringTypeMapValue +) + +type Struct struct { + Int int + Int32 int32 + Int64 int64 + UInt uint + UInt32 uint32 + UInt64 uint64 + Float32 float32 + Float64 float64 + String string + Interface interface{} + IntTypeMapTyped map[IntTypeMapKey]IntTypeMapValue + Int32TypeMapTyped map[Int32TypeMapKey]Int32TypeMapValue + Int64TypeMapTyped map[Int64TypeMapKey]Int64TypeMapValue + UIntTypeMapTyped map[UIntTypeMapKey]UIntTypeMapValue + UInt32TypeMapTyped map[UInt32TypeMapKey]UInt32TypeMapValue + UInt64TypeMapTyped map[UInt64TypeMapKey]UInt64TypeMapValue + Float32TypeMapTyped map[Float32TypeMapKey]Float32TypeMapValue + Float64TypeMapTyped map[Float64TypeMapKey]Float64TypeMapValue + StringTypeMapTyped map[StringTypeMapKey]StringTypeMapValue +} + +type ( + NestedType map[NestedTypeKey]map[NestedTypeSubKey][]*NestedTypeSubType + NestedTypeKey string + NestedTypeSubKey string + NestedTypeSubType struct{} +)