Skip to content

Commit

Permalink
improve stub generator code
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsannm committed Jul 9, 2022
1 parent c59db71 commit dbbd79b
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 37 deletions.
2 changes: 2 additions & 0 deletions desc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions desc/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,7 @@ func (d *Stub) getDTO(mTyp reflect.Type) (DTO, bool) {

return dto, ok
}

func (d *Stub) Tags() []string {
return d.tags
}
16 changes: 8 additions & 8 deletions exmples/simple-rest-server/cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down
50 changes: 27 additions & 23 deletions exmples/simple-rest-server/stub/sampleservice.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions stub/gen/go/tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package stubgengo

import (
_ "embed"
"fmt"
"strings"
"text/template"

Expand All @@ -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,
Expand Down
12 changes: 7 additions & 5 deletions stub/gen/go/tpl/stub.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
}

Expand Down Expand Up @@ -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(
Expand All @@ -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
}

Expand Down
6 changes: 5 additions & 1 deletion utils/reflector/obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit dbbd79b

Please sign in to comment.