apigen: Work with versioned apis

This commit is contained in:
Łukasz Magiera 2021-03-23 17:01:56 +01:00
parent 571114bc44
commit 3fe4b50a13
7 changed files with 1378 additions and 41 deletions

View File

@ -326,9 +326,9 @@ method-gen:
(cd ./lotuspond/front/src/chain && go run ./methodgen.go)
api-gen:
go run ./gen/api > api/apistruct/struct.go
goimports -w api/apistruct
goimports -w api/apistruct
go run ./gen/api
goimports -w api/apistruct api/v0api
goimports -w api/apistruct api/v0api
.PHONY: api-gen
docsgen: docsgen-md docsgen-openrpc

View File

@ -32,10 +32,10 @@ import (
"github.com/filecoin-project/specs-storage/storage"
"github.com/google/uuid"
"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p-core/metrics"
metrics "github.com/libp2p/go-libp2p-core/metrics"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
protocol "github.com/libp2p/go-libp2p-core/protocol"
)
type ChainIOStruct struct {
@ -2162,11 +2162,11 @@ func (s *WorkerStruct) WaitQuiet(p0 context.Context) error {
return s.Internal.WaitQuiet(p0)
}
var _ api.ChainIO = new(ChainIOStruct)
var _ api.Common = new(CommonStruct)
var _ api.FullNode = new(FullNodeStruct)
var _ api.Gateway = new(GatewayStruct)
var _ api.Signable = new(SignableStruct)
var _ api.StorageMiner = new(StorageMinerStruct)
var _ api.Wallet = new(WalletStruct)
var _ api.Worker = new(WorkerStruct)
var _ ChainIO = new(ChainIOStruct)
var _ Common = new(CommonStruct)
var _ FullNode = new(FullNodeStruct)
var _ Gateway = new(GatewayStruct)
var _ Signable = new(SignableStruct)
var _ StorageMiner = new(StorageMinerStruct)
var _ Wallet = new(WalletStruct)
var _ Worker = new(WorkerStruct)

12
api/apistruct/types.go Normal file
View File

@ -0,0 +1,12 @@
package apistruct
import "github.com/filecoin-project/lotus/api"
type ChainIO = api.ChainIO
type Common = api.Common
type FullNode = api.FullNode
type Gateway = api.Gateway
type Signable = api.Signable
type StorageMiner = api.StorageMiner
type Wallet = api.Wallet
type Worker = api.Worker

77
api/v0api/common.go Normal file
View File

@ -0,0 +1,77 @@
package v0api
import (
"context"
"github.com/filecoin-project/lotus/api"
"github.com/google/uuid"
"github.com/filecoin-project/go-jsonrpc/auth"
metrics "github.com/libp2p/go-libp2p-core/metrics"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
protocol "github.com/libp2p/go-libp2p-core/protocol"
apitypes "github.com/filecoin-project/lotus/api/types"
)
type Common interface {
// MethodGroup: Auth
AuthVerify(ctx context.Context, token string) ([]auth.Permission, error) //perm:read
AuthNew(ctx context.Context, perms []auth.Permission) ([]byte, error) //perm:admin
// MethodGroup: Net
NetConnectedness(context.Context, peer.ID) (network.Connectedness, error) //perm:read
NetPeers(context.Context) ([]peer.AddrInfo, error) //perm:read
NetConnect(context.Context, peer.AddrInfo) error //perm:write
NetAddrsListen(context.Context) (peer.AddrInfo, error) //perm:read
NetDisconnect(context.Context, peer.ID) error //perm:write
NetFindPeer(context.Context, peer.ID) (peer.AddrInfo, error) //perm:read
NetPubsubScores(context.Context) ([]api.PubsubScore, error) //perm:read
NetAutoNatStatus(context.Context) (api.NatInfo, error) //perm:read
NetAgentVersion(ctx context.Context, p peer.ID) (string, error) //perm:read
NetPeerInfo(context.Context, peer.ID) (*api.ExtendedPeerInfo, error) //perm:read
// NetBandwidthStats returns statistics about the nodes total bandwidth
// usage and current rate across all peers and protocols.
NetBandwidthStats(ctx context.Context) (metrics.Stats, error) //perm:read
// NetBandwidthStatsByPeer returns statistics about the nodes bandwidth
// usage and current rate per peer
NetBandwidthStatsByPeer(ctx context.Context) (map[string]metrics.Stats, error) //perm:read
// NetBandwidthStatsByProtocol returns statistics about the nodes bandwidth
// usage and current rate per protocol
NetBandwidthStatsByProtocol(ctx context.Context) (map[protocol.ID]metrics.Stats, error) //perm:read
// ConnectionGater API
NetBlockAdd(ctx context.Context, acl api.NetBlockList) error //perm:admin
NetBlockRemove(ctx context.Context, acl api.NetBlockList) error //perm:admin
NetBlockList(ctx context.Context) (api.NetBlockList, error) //perm:read
// MethodGroup: Common
// Discover returns an OpenRPC document describing an RPC API.
Discover(ctx context.Context) (apitypes.OpenRPCDocument, error) //perm:read
// ID returns peerID of libp2p node backing this API
ID(context.Context) (peer.ID, error) //perm:read
// Version provides information about API provider
Version(context.Context) (api.APIVersion, error) //perm:read
LogList(context.Context) ([]string, error) //perm:write
LogSetLevel(context.Context, string, string) error //perm:write
// trigger graceful shutdown
Shutdown(context.Context) error //perm:admin
// Session returns a random UUID of api provider session
Session(context.Context) (uuid.UUID, error) //perm:read
Closing(context.Context) (<-chan struct{}, error) //perm:read
}

View File

@ -25,7 +25,7 @@ import (
// FullNode API is a low-level interface to the Filecoin network full node
type FullNode interface {
api.Common
Common
// MethodGroup: Chain
// The Chain method group contains methods for interacting with the
@ -676,4 +676,3 @@ type FullNode interface {
// the path specified when calling CreateBackup is within the base path
CreateBackup(ctx context.Context, fpath string) error //perm:admin
}

1231
api/v0api/struct.go Normal file

File diff suppressed because it is too large Load Diff

View File

@ -53,7 +53,13 @@ func (v *Visitor) Visit(node ast.Node) ast.Visitor {
return v
}
func main() {
if err := runMain(); err != nil {
// latest (v1)
if err := generate("./api", "api", "apistruct", "./api/apistruct/struct.go"); err != nil {
fmt.Println("error: ", err)
}
// v0
if err := generate("./api/v0api", "v0api", "v0api", "./api/v0api/struct.go"); err != nil {
fmt.Println("error: ", err)
}
}
@ -116,9 +122,13 @@ func typeName(e ast.Expr) (string, error) {
}
}
func runMain() error {
func generate(path, pkg, outpkg, outfile string) error {
fset := token.NewFileSet()
apiDir, err := filepath.Abs("./api")
apiDir, err := filepath.Abs(path)
if err != nil {
return err
}
outfile, err = filepath.Abs(outfile)
if err != nil {
return err
}
@ -127,7 +137,7 @@ func runMain() error {
return err
}
ap := pkgs["api"]
ap := pkgs[pkg]
v := &Visitor{make(map[string]map[string]*methodMeta), map[string][]string{}}
ast.Walk(v, ap)
@ -148,9 +158,11 @@ func runMain() error {
type meta struct {
Infos map[string]*strinfo
Imports map[string]string
OutPkg string
}
m := &meta{
OutPkg: outpkg,
Infos: map[string]*strinfo{},
Imports: map[string]string{},
}
@ -165,6 +177,9 @@ func runMain() error {
for _, im := range f.Imports {
m.Imports[im.Path.Value] = im.Path.Value
if im.Name != nil {
m.Imports[im.Path.Value] = im.Name.Name + " " + m.Imports[im.Path.Value]
}
}
for ifname, methods := range v.Methods {
@ -244,11 +259,14 @@ func runMain() error {
}
fmt.Println(string(jb))*/
w := os.Stdout
w, err := os.OpenFile(outfile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
if err != nil {
return err
}
err = doTemplate(w, m, `// Code generated by github.com/filecoin-project/lotus/gen/api. DO NOT EDIT.
package apistruct
package {{.OutPkg}}
import (
{{range .Imports}} {{.}}
@ -282,7 +300,7 @@ func (s *{{$name}}Struct) {{.Name}}({{.NamedParams}}) ({{.Results}}) {
{{end}}
{{end}}
{{range .Infos}}var _ api.{{.Name}} = new({{.Name}}Struct)
{{range .Infos}}var _ {{.Name}} = new({{.Name}}Struct)
{{end}}
`)