Skip to content

Commit

Permalink
[rony] add helper method to generate stub
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsannm committed Jun 30, 2024
1 parent 47c919e commit 8b52032
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions rony/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/clubpay/ronykit/contrib v0.16.0
github.com/clubpay/ronykit/kit v0.16.0
github.com/clubpay/ronykit/std/gateways/fasthttp v0.16.0
github.com/clubpay/ronykit/stub v0.16.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions rony/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/clubpay/ronykit/kit v0.16.0 h1:89nrRSXVc0FaLNcHzShRZqtaRrk7hFjE5TVgkO
github.com/clubpay/ronykit/kit v0.16.0/go.mod h1:JmRsAjBYhNsrU7iaQM9oVw5OSIQpMET+VDKx1suUPbk=
github.com/clubpay/ronykit/std/gateways/fasthttp v0.16.0 h1:+hw9yPt/b2vgnAE1UaXKrvHdT23Gd0xm7rtIMJNK3Bg=
github.com/clubpay/ronykit/std/gateways/fasthttp v0.16.0/go.mod h1:BdeIyy2GIYbNlz8QUr/z17+rNwMAXtxJop6AR0QZhvY=
github.com/clubpay/ronykit/stub v0.16.0 h1:elxm5/GmWevgZy77wSc61NcmlEeTiFEI1xaIQmRBnhE=
github.com/clubpay/ronykit/stub v0.16.0/go.mod h1:XS9NvlW26riWRYs1z2H8yVahQq4TtolQoZmeWP+Z9nc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
1 change: 0 additions & 1 deletion rony/setup_swagger.go

This file was deleted.

26 changes: 26 additions & 0 deletions rony/stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package rony

import "github.com/clubpay/ronykit/stub/stubgen"

// GenerateStub generates a stub file for the given service description.
// The generated file will be placed in the `outputDir/folderName` directory.
// The package name of the generated file will be `pkgName`.
// The struct holding the stub functions will be named `name`.
func GenerateStub[S State[A], A Action](
name, folderName, outputDir, pkgName string,
genFunc stubgen.GenFunc, ext string,
opt ...SetupOption[S, A],
) error {
ctx := SetupContext[S, A]{}
for _, o := range opt {
o(&ctx)
}

return stubgen.New(
stubgen.WithStubName(name),
stubgen.WithFolderName(folderName),
stubgen.WithOutputDir(outputDir),
stubgen.WithPkgName(pkgName),
stubgen.WithGenFunc(genFunc, ext),
).Generate(ctx.cfg.allServiceDesc()...)
}
12 changes: 12 additions & 0 deletions ronyup/internal/skeleton/rony/cmd/gen-stub/main.go.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"{{.ModulePath}}/service"
)

func main() {
err := service.GenerateStub()
if err != nil {
panic(err)
}
}
10 changes: 10 additions & 0 deletions ronyup/internal/skeleton/rony/service/setup.go.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"github.com/clubpay/ronykit/rony"
"github.com/clubpay/ronykit/stub/stubgen"
"{{ .ModulePath }}/service/endpoint"
"{{ .ModulePath }}/service/state"
)
Expand All @@ -27,3 +28,12 @@ func Setup() rony.SetupOption[*state.Counter, state.Action] {
),
)
}

//go:generate go run {{.ModulePath}}/cmd/gen-stub/main.go}}
func GenerateStub() error {
return rony.GenerateStub(
"CounterStub", "counter", ".", "counterc",
stubgen.GolangStub, ".go",
Setup(),
)
}

0 comments on commit 8b52032

Please sign in to comment.