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 14, 2022
1 parent 936a9fd commit f1c22ac
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 29 deletions.
4 changes: 2 additions & 2 deletions desc/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ var (
NewStub = newStub
)

func (d *Stub) AddDTO(mTyp reflect.Type) error {
return d.addDTO(mTyp)
func (d *Stub) AddDTO(mTyp reflect.Type, isErr bool) error {
return d.addDTO(mTyp, isErr)
}
26 changes: 14 additions & 12 deletions desc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,26 +166,26 @@ func (s Service) Stub(pkgName string, tags ...string) (*Stub, error) {

func (s Service) dtoStub(stub *Stub) error {
for _, c := range s.Contracts {
err := stub.addDTO(reflect.TypeOf(c.Input))
err := stub.addDTO(reflect.TypeOf(c.Input), false)
if err != nil {
return err
}
if c.Output != nil {
err = stub.addDTO(reflect.TypeOf(c.Output))
err = stub.addDTO(reflect.TypeOf(c.Output), false)
if err != nil {
return err
}
}

for _, pe := range s.PossibleErrors {
err = stub.addDTO(reflect.TypeOf(pe.Message))
err = stub.addDTO(reflect.TypeOf(pe.Message), true)
if err != nil {
return err
}
}

for _, pe := range c.PossibleErrors {
err = stub.addDTO(reflect.TypeOf(pe.Message))
err = stub.addDTO(reflect.TypeOf(pe.Message), true)
if err != nil {
return err
}
Expand All @@ -207,17 +207,18 @@ func (s Service) rpcStub(
if dto, ok := stub.getDTO(reflect.TypeOf(c.Input)); ok {
m.Request = dto
}
if dto, ok := stub.getDTO(reflect.TypeOf(c.Output)); ok {
m.Response = dto
if c.Output != nil {
if dto, ok := stub.getDTO(reflect.TypeOf(c.Output)); ok {
m.Response = dto
}
}

var possibleErrors []Error
possibleErrors = append(possibleErrors, s.PossibleErrors...)
possibleErrors = append(possibleErrors, c.PossibleErrors...)
for _, e := range possibleErrors {
if dto, ok := stub.getDTO(reflect.TypeOf(e.Message)); ok {
m.PossibleErrors = append(
m.PossibleErrors,
m.addPossibleError(
ErrorDTO{
Code: e.Code,
Item: e.Item,
Expand All @@ -241,17 +242,18 @@ func (s Service) restStub(
if dto, ok := stub.getDTO(reflect.TypeOf(c.Input)); ok {
m.Request = dto
}
if dto, ok := stub.getDTO(reflect.TypeOf(c.Output)); ok {
m.Response = dto
if c.Output != nil {
if dto, ok := stub.getDTO(reflect.TypeOf(c.Output)); ok {
m.Response = dto
}
}

var possibleErrors []Error
possibleErrors = append(possibleErrors, s.PossibleErrors...)
possibleErrors = append(possibleErrors, c.PossibleErrors...)
for _, e := range possibleErrors {
if dto, ok := stub.getDTO(reflect.TypeOf(e.Message)); ok {
m.PossibleErrors = append(
m.PossibleErrors,
m.addPossibleError(
ErrorDTO{
Code: e.Code,
Item: e.Item,
Expand Down
66 changes: 62 additions & 4 deletions desc/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package desc
import (
"fmt"
"reflect"
"strings"

"github.com/clubpay/ronykit"
)
Expand All @@ -12,9 +13,46 @@ type DTO struct {
Comments []string
Name string
Type string
IsErr bool
Fields []DTOField
}

func (dto DTO) CodeField() string {
var fn string
for _, f := range dto.Fields {
x := strings.ToLower(f.Name)
if f.Type != "int" {
continue
}
if x == "code" {
return f.Name
}
if strings.HasPrefix(f.Name, "code") {
fn = f.Name
}
}

return fn
}

func (dto DTO) ItemField() string {
var fn string
for _, f := range dto.Fields {
x := strings.ToLower(f.Name)
if f.Type != "string" {
continue
}
if x == "item" || x == "items" {
return f.Name
}
if strings.HasPrefix(f.Name, "item") {
fn = f.Name
}
}

return fn
}

// DTOField represents description of a field of the DTO
type DTOField struct {
Name string
Expand Down Expand Up @@ -49,6 +87,15 @@ type RESTMethod struct {
PossibleErrors []ErrorDTO
}

func (rm *RESTMethod) addPossibleError(dto ErrorDTO) {
for _, e := range rm.PossibleErrors {
if e.Code == dto.Code {
return
}
}
rm.PossibleErrors = append(rm.PossibleErrors, dto)
}

// RPCMethod represents description of a Contract with ronykit.RPCRouteSelector
type RPCMethod struct {
Name string
Expand All @@ -61,6 +108,15 @@ type RPCMethod struct {
ronykit.OutgoingRPCContainer
}

func (rm *RPCMethod) addPossibleError(dto ErrorDTO) {
for _, e := range rm.PossibleErrors {
if e.Code == dto.Code {
return
}
}
rm.PossibleErrors = append(rm.PossibleErrors, dto)
}

// Stub represents description of a stub of the service described by Service descriptor.
type Stub struct {
tags []string
Expand All @@ -78,8 +134,10 @@ func newStub(tags ...string) *Stub {
}
}

func (d *Stub) addDTO(mTyp reflect.Type) error {
dto := DTO{}
func (d *Stub) addDTO(mTyp reflect.Type, isErr bool) error {
dto := DTO{
IsErr: isErr,
}
if mTyp.Kind() == reflect.Ptr {
mTyp = mTyp.Elem()
}
Expand All @@ -93,12 +151,12 @@ func (d *Stub) addDTO(mTyp reflect.Type) error {

switch {
case k == reflect.Struct:
err := d.addDTO(ft.Type)
err := d.addDTO(ft.Type, false)
if err != nil {
return err
}
case k == reflect.Ptr && ft.Type.Elem().Kind() == reflect.Struct:
err := d.addDTO(ft.Type.Elem())
err := d.addDTO(ft.Type.Elem(), false)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion desc/stub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var _ = Describe("Desc", func() {
It("should detect all DTOs", func() {
d := desc.NewStub("json")

Expect(d.AddDTO(reflect.TypeOf(&customStruct{}))).To(Succeed())
Expect(d.AddDTO(reflect.TypeOf(&customStruct{}), false)).To(Succeed())
Expect(d.DTOs).To(HaveLen(2))
Expect(d.DTOs["customSubStruct"].Fields).To(HaveLen(2))
Expect(d.DTOs["customSubStruct"].Fields[0].Name).To(Equal("SubParam1"))
Expand Down
15 changes: 11 additions & 4 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.

14 changes: 13 additions & 1 deletion internal/tpl/go/stub.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@
{{- end }}
{{- end }}
}
{{ if .IsErr }}
{{- if ne .CodeField ""}}
func (x {{.Name}}) GetCode() int {
return x.{{.CodeField}}
}
{{- end }}
{{- if ne .ItemField ""}}
func (x {{.Name}}) GetItem() string {
return x.{{.ItemField}}
}
{{- end }}
{{- end }}
{{ end }}
// Code generated by RonyKIT Stub Generator (Golang); DO NOT EDIT.

Expand Down Expand Up @@ -75,7 +87,7 @@ func (s {{$serviceName}}Stub) {{$methodName}}(ctx context.Context, req *{{.Reque
return err
}

return stub.NewErrorWithMsg({{$errDto.Code}}, "{{$errDto.Item}}", res)
return stub.NewErrorWithMsg(res)
},
).
{{- end }}
Expand Down
16 changes: 11 additions & 5 deletions stub/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ func NewError(code int, item string) *Error {
}
}

func NewErrorWithMsg(code int, item string, msg ronykit.Message) *Error {
return &Error{
code: code,
item: item,
msg: msg,
func NewErrorWithMsg(msg ronykit.Message) *Error {
wErr := &Error{
msg: msg,
}
if e, ok := msg.(interface{ GetCode() int }); ok {
wErr.code = e.GetCode()
}
if e, ok := msg.(interface{ GetItem() string }); ok {
wErr.item = e.GetItem()
}

return wErr
}

func WrapError(err error) *Error {
Expand Down

0 comments on commit f1c22ac

Please sign in to comment.