Skip to content

Commit

Permalink
feat: generate client constructor for apis in venus-shared (#4747)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtynn authored Jan 26, 2022
1 parent 3914774 commit 002ee9b
Show file tree
Hide file tree
Showing 12 changed files with 273 additions and 44 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ test-venus-shared:

api-gen:
cd ./venus-devtool/ && go run ./api-gen/ proxy
cd ./venus-devtool/ && go run ./api-gen/ client

v0APIDoc = ../venus-shared/api/v0-api-document.md
v1APIDoc = ../venus-shared/api/v1-api-document.md
Expand Down
102 changes: 102 additions & 0 deletions venus-devtool/api-gen/client.go

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

61 changes: 61 additions & 0 deletions venus-devtool/api-gen/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package main

import (
"bytes"
"fmt"
"go/format"
"io/ioutil"
"path/filepath"
"reflect"

"github.com/filecoin-project/venus/venus-devtool/util"
"github.com/filecoin-project/venus/venus-shared/api/messager"
"github.com/filecoin-project/venus/venus-shared/api/wallet"
)

func init() {
for _, capi := range util.ChainAPIPairs {
apiTargets = append(apiTargets, capi.Venus)
}

apiTargets = append(apiTargets,
util.APIMeta{
Type: reflect.TypeOf((*messager.IMessager)(nil)).Elem(),
ParseOpt: util.InterfaceParseOption{
ImportPath: "github.com/filecoin-project/venus/venus-shared/api/messager",
IncludeAll: true,
},
RPCMeta: util.RPCMeta{
MethodNamespace: "Message",
},
},
util.APIMeta{
Type: reflect.TypeOf((*wallet.IFullAPI)(nil)).Elem(),
ParseOpt: util.InterfaceParseOption{
ImportPath: "github.com/filecoin-project/venus/venus-shared/api/wallet",
IncludeAll: true,
},
},
)
}

var apiTargets []util.APIMeta

func structName(ifaceName string) string {
return ifaceName + "Struct"
}

func outputSourceFile(location, fname string, buf *bytes.Buffer) error {
formatted, err := format.Source(buf.Bytes())
if err != nil {
return fmt.Errorf("format source content: %w", err)
}

outputFile := filepath.Join(location, fname)
err = ioutil.WriteFile(outputFile, formatted, 0644)
if err != nil {
return fmt.Errorf("write to output %s: %w", outputFile, err)
}

return nil
}
1 change: 1 addition & 0 deletions venus-devtool/api-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func main() {
Flags: []cli.Flag{},
Commands: []*cli.Command{
proxyCmd,
clientCmd,
},
}

Expand Down
46 changes: 2 additions & 44 deletions venus-devtool/api-gen/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,22 @@ import (
"bytes"
"fmt"
"go/ast"
"go/format"
"go/printer"
"io"
"io/ioutil"
"log"
"path/filepath"
"reflect"
"strings"

"github.com/urfave/cli/v2"

"github.com/filecoin-project/venus/venus-devtool/util"
"github.com/filecoin-project/venus/venus-shared/api/messager"
"github.com/filecoin-project/venus/venus-shared/api/wallet"
)

func init() {
for _, capi := range util.ChainAPIPairs {
proxyTargets = append(proxyTargets, capi.Venus)
}

proxyTargets = append(proxyTargets, util.APIMeta{
Type: reflect.TypeOf((*messager.IMessager)(nil)).Elem(),
ParseOpt: util.InterfaceParseOption{
ImportPath: "github.com/filecoin-project/venus/venus-shared/api/messager",
IncludeAll: true,
},
}, util.APIMeta{
Type: reflect.TypeOf((*wallet.IFullAPI)(nil)).Elem(),
ParseOpt: util.InterfaceParseOption{
ImportPath: "github.com/filecoin-project/venus/venus-shared/api/wallet",
IncludeAll: true,
},
})
}

var proxyTargets []util.APIMeta

var proxyCmd = &cli.Command{
Name: "proxy",
Flags: []cli.Flag{},
Action: func(cctx *cli.Context) error {
for _, target := range proxyTargets {
for _, target := range apiTargets {
err := genProxyForAPI(target)
if err != nil {
log.Fatalf("got error while generating proxy codes for %s: %s", target.Type, err)
Expand Down Expand Up @@ -100,18 +73,7 @@ func genProxyForAPI(t util.APIMeta) error {
return fmt.Errorf("copy contents into output: %w", err)
}

formatted, err := format.Source(fileBuffer.Bytes())
if err != nil {
return fmt.Errorf("format source content: %w", err)
}

outputFile := filepath.Join(astMeta.Location, "proxy_gen.go")
err = ioutil.WriteFile(outputFile, formatted, 0644)
if err != nil {
return fmt.Errorf("write to output %s: %w", outputFile, err)
}

return nil
return outputSourceFile(astMeta.Location, "proxy_gen.go", &fileBuffer)
}

func writeImports(deps map[string]util.ImportMeta, dst *bytes.Buffer) error {
Expand Down Expand Up @@ -200,10 +162,6 @@ type %s struct {
`
)

func structName(ifaceName string) string {
return ifaceName + "Struct"
}

func writeStruct(dst *bytes.Buffer, ifaceMeta *util.InterfaceMeta, astMeta *util.ASTMeta) error {
fmt.Fprintf(dst, structHeadFormat, structName(ifaceMeta.Name))

Expand Down
1 change: 1 addition & 0 deletions venus-devtool/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gostaticanalysis/analysisutil v0.0.0-20190318220348-4088753ea4d3/go.mod h1:eEOZF4jCKGi+aprrirO9e7WKB3beBRtWgqGunKl6pKE=
github.com/gostaticanalysis/analysisutil v0.0.3/go.mod h1:eEOZF4jCKGi+aprrirO9e7WKB3beBRtWgqGunKl6pKE=
Expand Down
6 changes: 6 additions & 0 deletions venus-devtool/util/api_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,15 @@ var ChainAPIPairs = []struct {

var LatestChainAPIPair = ChainAPIPairs[len(ChainAPIPairs)-1]

type RPCMeta struct {
Namespace string
MethodNamespace string
}

type APIMeta struct {
Type reflect.Type
ParseOpt InterfaceParseOption
RPCMeta
}

func GetAPIMethodPerm(m InterfaceMethodMeta) string {
Expand Down
24 changes: 24 additions & 0 deletions venus-shared/api/chain/v0/client_gen.go

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

24 changes: 24 additions & 0 deletions venus-shared/api/chain/v1/client_gen.go

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

24 changes: 24 additions & 0 deletions venus-shared/api/messager/client_gen.go

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

3 changes: 3 additions & 0 deletions venus-shared/api/request_header.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package api

const VenusAPINamespaceHeader = "X-VENUS-API-NAMESPACE"
24 changes: 24 additions & 0 deletions venus-shared/api/wallet/client_gen.go

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

0 comments on commit 002ee9b

Please sign in to comment.