diff --git a/desc/service.go b/desc/service.go index 586964d9..da0cdb08 100644 --- a/desc/service.go +++ b/desc/service.go @@ -210,6 +210,7 @@ func (s Service) rpcStub( Predicate: rrs.GetPredicate(), Encoding: getEncoding(rrs), } + if dto, ok := stub.getDTO(reflect.TypeOf(c.Input)); ok { m.Request = dto } @@ -245,6 +246,7 @@ func (s Service) restStub( Path: rrs.GetPath(), Encoding: getEncoding(rrs), } + if dto, ok := stub.getDTO(reflect.TypeOf(c.Input)); ok { m.Request = dto } diff --git a/desc/stub.go b/desc/stub.go index 42372815..4d898258 100644 --- a/desc/stub.go +++ b/desc/stub.go @@ -153,3 +153,7 @@ func (d *Stub) getDTO(mTyp reflect.Type) (DTO, bool) { return dto, ok } + +func (d *Stub) Tags() []string { + return d.tags +} diff --git a/exmples/simple-rest-server/cmd/client/main.go b/exmples/simple-rest-server/cmd/client/main.go index 3979341c..1fa0b4e0 100644 --- a/exmples/simple-rest-server/cmd/client/main.go +++ b/exmples/simple-rest-server/cmd/client/main.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "net/http" - "os" sampleservicestub "github.com/clubpay/ronykit/exmples/simple-rest-server/stub" "github.com/clubpay/ronykit/stub" @@ -14,32 +13,33 @@ import ( func main() { res1 := sampleservicestub.EchoResponse{} s := stub.New("127.0.0.1") - err := s.REST(). + httpCtx := s.REST(). SetMethod(http.MethodGet). SetPath("echo/1230"). + SetQuery("ok", "true"). SetResponseHandler( http.StatusOK, func(ctx context.Context, r stub.RESTResponse) *stub.Error { return stub.WrapError(json.Unmarshal(r.GetBody(), &res1)) }, ). - Run(context.Background()). - Err() - if err != nil { - panic(err) + Run(context.Background()) + defer httpCtx.Release() + + if httpCtx.Err() != nil { + panic(httpCtx.Err()) } //nolint:forbidigo fmt.Println("RESPONSE1: ", res1.Ok, res1.RandomID) s2 := sampleservicestub.NewSampleServiceStub( "127.0.0.1", - stub.DumpTo(os.Stdout), ) res2, err := s2.Echo( context.Background(), &sampleservicestub.EchoRequest{ RandomID: 1450, - Ok: false, + Ok: true, }, ) if err != nil { diff --git a/exmples/simple-rest-server/stub/sampleservice.go b/exmples/simple-rest-server/stub/sampleservice.go index 575deeef..e406b910 100755 --- a/exmples/simple-rest-server/stub/sampleservice.go +++ b/exmples/simple-rest-server/stub/sampleservice.go @@ -11,13 +11,13 @@ import ( ) func init() { - reflector.Register(&EchoRequest{}) - reflector.Register(&EchoResponse{}) - reflector.Register(&EmbeddedHeader{}) - reflector.Register(&ErrorMessage{}) - reflector.Register(&RedirectRequest{}) - reflector.Register(&SumRequest{}) - reflector.Register(&SumResponse{}) + reflector.Register(&EchoRequest{}, "json") + reflector.Register(&EchoResponse{}, "json") + reflector.Register(&EmbeddedHeader{}, "json") + reflector.Register(&ErrorMessage{}, "json") + reflector.Register(&RedirectRequest{}, "json") + reflector.Register(&SumRequest{}, "json") + reflector.Register(&SumResponse{}, "json") } // EchoRequest is a data transfer object @@ -81,7 +81,7 @@ func NewSampleServiceStub(hostPort string, opts ...stub.Option) *SampleServiceSt func (s SampleServiceStub) Echo(ctx context.Context, req *EchoRequest) (*EchoResponse, *stub.Error) { res := &EchoResponse{} - err := s.s.REST(). + httpCtx := s.s.REST(). SetMethod("GET"). SetResponseHandler( 400, @@ -100,9 +100,10 @@ func (s SampleServiceStub) Echo(ctx context.Context, req *EchoRequest) (*EchoRes return stub.WrapError(ronykit.UnmarshalMessage(r.GetBody(), res)) }, ). - AutoRun(ctx, "/echo/:randomID", ronykit.JSON, req). - Err() - if err != nil { + AutoRun(ctx, "/echo/:randomID", ronykit.JSON, req) + defer httpCtx.Release() + + if err := httpCtx.Err(); err != nil { return nil, err } @@ -111,7 +112,7 @@ func (s SampleServiceStub) Echo(ctx context.Context, req *EchoRequest) (*EchoRes func (s SampleServiceStub) Sum1(ctx context.Context, req *SumRequest) (*SumResponse, *stub.Error) { res := &SumResponse{} - err := s.s.REST(). + httpCtx := s.s.REST(). SetMethod("GET"). SetResponseHandler( 400, @@ -130,9 +131,10 @@ func (s SampleServiceStub) Sum1(ctx context.Context, req *SumRequest) (*SumRespo return stub.WrapError(ronykit.UnmarshalMessage(r.GetBody(), res)) }, ). - AutoRun(ctx, "/sum/:val1/:val2", ronykit.JSON, req). - Err() - if err != nil { + AutoRun(ctx, "/sum/:val1/:val2", ronykit.JSON, req) + defer httpCtx.Release() + + if err := httpCtx.Err(); err != nil { return nil, err } @@ -141,7 +143,7 @@ func (s SampleServiceStub) Sum1(ctx context.Context, req *SumRequest) (*SumRespo func (s SampleServiceStub) Sum2(ctx context.Context, req *SumRequest) (*SumResponse, *stub.Error) { res := &SumResponse{} - err := s.s.REST(). + httpCtx := s.s.REST(). SetMethod("POST"). SetResponseHandler( 400, @@ -160,9 +162,10 @@ func (s SampleServiceStub) Sum2(ctx context.Context, req *SumRequest) (*SumRespo return stub.WrapError(ronykit.UnmarshalMessage(r.GetBody(), res)) }, ). - AutoRun(ctx, "/sum", ronykit.JSON, req). - Err() - if err != nil { + AutoRun(ctx, "/sum", ronykit.JSON, req) + defer httpCtx.Release() + + if err := httpCtx.Err(); err != nil { return nil, err } @@ -171,7 +174,7 @@ func (s SampleServiceStub) Sum2(ctx context.Context, req *SumRequest) (*SumRespo func (s SampleServiceStub) SumRedirect(ctx context.Context, req *SumRequest) (*SumResponse, *stub.Error) { res := &SumResponse{} - err := s.s.REST(). + httpCtx := s.s.REST(). SetMethod("GET"). SetResponseHandler( 400, @@ -190,9 +193,10 @@ func (s SampleServiceStub) SumRedirect(ctx context.Context, req *SumRequest) (*S return stub.WrapError(ronykit.UnmarshalMessage(r.GetBody(), res)) }, ). - AutoRun(ctx, "/sum-redirect/:val1/:val2", ronykit.JSON, req). - Err() - if err != nil { + AutoRun(ctx, "/sum-redirect/:val1/:val2", ronykit.JSON, req) + defer httpCtx.Release() + + if err := httpCtx.Err(); err != nil { return nil, err } diff --git a/stub/gen/go/tpl.go b/stub/gen/go/tpl.go index ca421e7b..e0e9d865 100644 --- a/stub/gen/go/tpl.go +++ b/stub/gen/go/tpl.go @@ -2,6 +2,7 @@ package stubgengo import ( _ "embed" + "fmt" "strings" "text/template" @@ -19,6 +20,16 @@ func init() { } var funcMaps = map[string]interface{}{ + "strQuote": func(elems []string) []string { + out := make([]string, len(elems)) + for i, e := range elems { + out[i] = fmt.Sprintf("%q", e) + } + + return out + }, + "strJoin": strings.Join, + "strSplit": strings.Split, "toUpper": strings.ToUpper, "toLower": strings.ToLower, "toTitle": strings.ToTitle, diff --git a/stub/gen/go/tpl/stub.gotmpl b/stub/gen/go/tpl/stub.gotmpl index 841f1a6a..6924c1f5 100644 --- a/stub/gen/go/tpl/stub.gotmpl +++ b/stub/gen/go/tpl/stub.gotmpl @@ -29,9 +29,10 @@ import ( "github.com/clubpay/ronykit/utils/reflector" ) +{{$tags := strQuote .Tags}} func init() { {{- range $dtoName, $dto := .DTOs }} -reflector.Register(&{{$dtoName}}{}) +reflector.Register(&{{$dtoName}}{}, {{strJoin $tags ","}}) {{- end }} } @@ -62,7 +63,7 @@ func New{{$serviceName}}Stub(hostPort string, opts ...stub.Option) *{{$serviceNa {{- if ne $methodName "" }} func (s {{$serviceName}}Stub) {{$methodName}}(ctx context.Context, req *{{.Request.Name}}) (*{{.Response.Name}}, *stub.Error){ res := &{{.Response.Name}}{} - err := s.s.REST(). + httpCtx := s.s.REST(). SetMethod("{{.Method}}"). {{ range $idx, $errDto := .PossibleErrors }} SetResponseHandler( @@ -83,9 +84,10 @@ func (s {{$serviceName}}Stub) {{$methodName}}(ctx context.Context, req *{{.Reque return stub.WrapError(ronykit.UnmarshalMessage(r.GetBody(), res)) }, ). - AutoRun(ctx, "{{.Path}}", {{.Encoding}}, req). - Err() - if err != nil { + AutoRun(ctx, "{{.Path}}", {{.Encoding}}, req) + defer httpCtx.Release() + + if err := httpCtx.Err(); err != nil { return nil, err } diff --git a/utils/reflector/obj.go b/utils/reflector/obj.go index 967cec09..8d6553fe 100644 --- a/utils/reflector/obj.go +++ b/utils/reflector/obj.go @@ -49,9 +49,13 @@ func (fields Fields) Get(m ronykit.Message, fieldName string) interface{} { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: return mVal.Int() case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - return mVal.Int() + return mVal.Uint() case reflect.String: return mVal.String() + case reflect.Float64, reflect.Float32: + return mVal.Float() + case reflect.Bool: + return mVal.Bool() } if !mVal.CanInterface() {