2020-04-20 18:34:07 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
2020-11-12 09:29:42 +00:00
|
|
|
"os"
|
2020-04-20 18:34:07 +00:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
"unicode"
|
|
|
|
|
2020-11-12 09:42:06 +00:00
|
|
|
"github.com/google/uuid"
|
2020-08-24 10:11:18 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"github.com/ipfs/go-filestore"
|
2020-09-02 22:45:57 +00:00
|
|
|
metrics "github.com/libp2p/go-libp2p-core/metrics"
|
2020-08-24 10:11:18 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/network"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2020-09-02 23:31:41 +00:00
|
|
|
protocol "github.com/libp2p/go-libp2p-core/protocol"
|
2020-08-24 10:11:18 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
|
|
"github.com/multiformats/go-multiaddr"
|
|
|
|
|
2020-04-20 18:34:07 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-bitfield"
|
2020-08-24 10:11:18 +00:00
|
|
|
datatransfer "github.com/filecoin-project/go-data-transfer"
|
2020-11-12 09:42:06 +00:00
|
|
|
filestore2 "github.com/filecoin-project/go-fil-markets/filestore"
|
2020-08-24 10:11:18 +00:00
|
|
|
"github.com/filecoin-project/go-fil-markets/retrievalmarket"
|
2020-05-20 18:23:51 +00:00
|
|
|
"github.com/filecoin-project/go-jsonrpc/auth"
|
2020-08-24 10:11:18 +00:00
|
|
|
"github.com/filecoin-project/go-multistore"
|
|
|
|
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/go-state-types/crypto"
|
|
|
|
"github.com/filecoin-project/go-state-types/exitcode"
|
2020-08-24 10:11:18 +00:00
|
|
|
|
2020-04-20 18:34:07 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2020-07-01 21:01:27 +00:00
|
|
|
"github.com/filecoin-project/lotus/api/apistruct"
|
2020-04-20 18:34:07 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-11-12 09:42:06 +00:00
|
|
|
"github.com/filecoin-project/lotus/extern/sector-storage/sealtasks"
|
|
|
|
"github.com/filecoin-project/lotus/extern/sector-storage/stores"
|
|
|
|
"github.com/filecoin-project/lotus/extern/sector-storage/storiface"
|
|
|
|
sealing "github.com/filecoin-project/lotus/extern/storage-sealing"
|
2020-04-20 18:34:07 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ExampleValues = map[reflect.Type]interface{}{
|
2020-05-20 18:23:51 +00:00
|
|
|
reflect.TypeOf(auth.Permission("")): auth.Permission("write"),
|
|
|
|
reflect.TypeOf(""): "string value",
|
|
|
|
reflect.TypeOf(uint64(42)): uint64(42),
|
|
|
|
reflect.TypeOf(byte(7)): byte(7),
|
|
|
|
reflect.TypeOf([]byte{}): []byte("byte array"),
|
2020-04-20 18:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func addExample(v interface{}) {
|
|
|
|
ExampleValues[reflect.TypeOf(v)] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
c, err := cid.Decode("bafy2bzacea3wsdh6y3a36tb3skempjoxqpuyompjbmfeyf34fi3uy6uue42v4")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ExampleValues[reflect.TypeOf(c)] = c
|
|
|
|
|
|
|
|
c2, err := cid.Decode("bafy2bzacebp3shtrn43k7g3unredz7fxn4gj533d3o43tqn2p2ipxxhrvchve")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tsk := types.NewTipSetKey(c, c2)
|
|
|
|
|
|
|
|
ExampleValues[reflect.TypeOf(tsk)] = tsk
|
|
|
|
|
|
|
|
addr, err := address.NewIDAddress(1234)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ExampleValues[reflect.TypeOf(addr)] = addr
|
|
|
|
|
2020-08-20 04:49:10 +00:00
|
|
|
pid, err := peer.Decode("12D3KooWGzxzKZYveHXtpG6AsrUJBcWxHBFS2HsEoGTxrMLvKXtf")
|
2020-04-20 18:34:07 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
addExample(pid)
|
2020-08-24 10:11:18 +00:00
|
|
|
addExample(&pid)
|
2020-04-20 18:34:07 +00:00
|
|
|
|
|
|
|
addExample(bitfield.NewFromSet([]uint64{5}))
|
2020-06-15 16:30:49 +00:00
|
|
|
addExample(abi.RegisteredSealProof_StackedDrg32GiBV1)
|
2020-06-18 12:30:00 +00:00
|
|
|
addExample(abi.RegisteredPoStProof_StackedDrgWindow32GiBV1)
|
2020-04-20 18:34:07 +00:00
|
|
|
addExample(abi.ChainEpoch(10101))
|
|
|
|
addExample(crypto.SigTypeBLS)
|
2020-10-11 18:12:01 +00:00
|
|
|
addExample(types.KTBLS)
|
2020-04-20 18:34:07 +00:00
|
|
|
addExample(int64(9))
|
2020-06-18 12:30:00 +00:00
|
|
|
addExample(12.3)
|
|
|
|
addExample(123)
|
|
|
|
addExample(uintptr(0))
|
2020-04-20 18:34:07 +00:00
|
|
|
addExample(abi.MethodNum(1))
|
|
|
|
addExample(exitcode.ExitCode(0))
|
|
|
|
addExample(crypto.DomainSeparationTag_ElectionProofProduction)
|
|
|
|
addExample(true)
|
|
|
|
addExample(abi.UnpaddedPieceSize(1024))
|
|
|
|
addExample(abi.UnpaddedPieceSize(1024).Padded())
|
|
|
|
addExample(abi.DealID(5432))
|
|
|
|
addExample(filestore.StatusFileChanged)
|
|
|
|
addExample(abi.SectorNumber(9))
|
|
|
|
addExample(abi.SectorSize(32 * 1024 * 1024 * 1024))
|
|
|
|
addExample(api.MpoolChange(0))
|
|
|
|
addExample(network.Connected)
|
|
|
|
addExample(dtypes.NetworkName("lotus"))
|
|
|
|
addExample(api.SyncStateStage(1))
|
2020-09-08 18:54:37 +00:00
|
|
|
addExample(build.FullAPIVersion)
|
2020-04-20 18:34:07 +00:00
|
|
|
addExample(api.PCHInbound)
|
|
|
|
addExample(time.Minute)
|
2020-08-24 10:11:18 +00:00
|
|
|
addExample(datatransfer.TransferID(3))
|
|
|
|
addExample(datatransfer.Ongoing)
|
|
|
|
addExample(multistore.StoreID(50))
|
|
|
|
addExample(retrievalmarket.ClientEventDealAccepted)
|
|
|
|
addExample(retrievalmarket.DealStatusNew)
|
|
|
|
addExample(network.ReachabilityPublic)
|
2020-09-17 15:31:09 +00:00
|
|
|
addExample(build.NewestNetworkVersion)
|
2020-06-11 00:47:28 +00:00
|
|
|
addExample(&types.ExecutionTrace{
|
2020-11-12 09:29:42 +00:00
|
|
|
Msg: exampleValue("init", reflect.TypeOf(&types.Message{}), nil).(*types.Message),
|
|
|
|
MsgRct: exampleValue("init", reflect.TypeOf(&types.MessageReceipt{}), nil).(*types.MessageReceipt),
|
2020-04-20 18:34:07 +00:00
|
|
|
})
|
|
|
|
addExample(map[string]types.Actor{
|
2020-11-12 09:29:42 +00:00
|
|
|
"t01236": exampleValue("init", reflect.TypeOf(types.Actor{}), nil).(types.Actor),
|
2020-04-20 18:34:07 +00:00
|
|
|
})
|
|
|
|
addExample(map[string]api.MarketDeal{
|
2020-11-12 09:29:42 +00:00
|
|
|
"t026363": exampleValue("init", reflect.TypeOf(api.MarketDeal{}), nil).(api.MarketDeal),
|
2020-04-20 18:34:07 +00:00
|
|
|
})
|
|
|
|
addExample(map[string]api.MarketBalance{
|
2020-11-12 09:29:42 +00:00
|
|
|
"t026363": exampleValue("init", reflect.TypeOf(api.MarketBalance{}), nil).(api.MarketBalance),
|
2020-04-20 18:34:07 +00:00
|
|
|
})
|
2020-08-24 10:11:18 +00:00
|
|
|
addExample(map[string]*pubsub.TopicScoreSnapshot{
|
|
|
|
"/blocks": {
|
|
|
|
TimeInMesh: time.Minute,
|
|
|
|
FirstMessageDeliveries: 122,
|
|
|
|
MeshMessageDeliveries: 1234,
|
|
|
|
InvalidMessageDeliveries: 3,
|
|
|
|
},
|
|
|
|
})
|
2020-09-02 22:45:57 +00:00
|
|
|
addExample(map[string]metrics.Stats{
|
2020-09-02 23:42:55 +00:00
|
|
|
"12D3KooWSXmXLJmBR1M7i9RW9GQPNUhZSzXKzxDHWtAgNuJAbyEJ": {
|
2020-09-02 22:45:57 +00:00
|
|
|
RateIn: 100,
|
|
|
|
RateOut: 50,
|
|
|
|
TotalIn: 174000,
|
|
|
|
TotalOut: 12500,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
addExample(map[protocol.ID]metrics.Stats{
|
2020-09-02 23:42:55 +00:00
|
|
|
"/fil/hello/1.0.0": {
|
2020-09-02 22:45:57 +00:00
|
|
|
RateIn: 100,
|
|
|
|
RateOut: 50,
|
|
|
|
TotalIn: 174000,
|
|
|
|
TotalOut: 12500,
|
|
|
|
},
|
|
|
|
})
|
2020-04-20 18:34:07 +00:00
|
|
|
|
|
|
|
maddr, err := multiaddr.NewMultiaddr("/ip4/52.36.61.156/tcp/1347/p2p/12D3KooWFETiESTf1v4PGUvtnxMAcEFMzLZbJGg4tjWfGEimYior")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// because reflect.TypeOf(maddr) returns the concrete type...
|
|
|
|
ExampleValues[reflect.TypeOf(struct{ A multiaddr.Multiaddr }{}).Field(0).Type] = maddr
|
|
|
|
|
2020-11-12 09:29:42 +00:00
|
|
|
// miner specific
|
|
|
|
addExample(filestore2.Path(".lotusminer/fstmp123"))
|
|
|
|
si := multistore.StoreID(12)
|
|
|
|
addExample(&si)
|
|
|
|
addExample(retrievalmarket.DealID(5))
|
|
|
|
addExample(abi.ActorID(1000))
|
|
|
|
addExample(map[string][]api.SealedRef{
|
|
|
|
"98000": {
|
|
|
|
api.SealedRef{
|
|
|
|
SectorID: 100,
|
|
|
|
Offset: 10 << 20,
|
|
|
|
Size: 1 << 20,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
addExample(api.SectorState(sealing.Proving))
|
|
|
|
addExample(stores.ID("76f1988b-ef30-4d7e-b3ec-9a627f4ba5a8"))
|
|
|
|
addExample(storiface.FTUnsealed)
|
|
|
|
addExample(storiface.PathSealing)
|
|
|
|
addExample(map[stores.ID][]stores.Decl{
|
|
|
|
"76f1988b-ef30-4d7e-b3ec-9a627f4ba5a8": {
|
|
|
|
{
|
|
|
|
SectorID: abi.SectorID{Miner: 1000, Number: 100},
|
|
|
|
SectorFileType: storiface.FTSealed,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
addExample(map[stores.ID]string{
|
|
|
|
"76f1988b-ef30-4d7e-b3ec-9a627f4ba5a8": "/data/path",
|
|
|
|
})
|
|
|
|
addExample(map[uuid.UUID][]storiface.WorkerJob{
|
|
|
|
uuid.MustParse("ef8d99a2-6865-4189-8ffa-9fef0f806eee"): {
|
|
|
|
{
|
2020-11-12 09:42:06 +00:00
|
|
|
ID: storiface.CallID{
|
2020-11-12 09:29:42 +00:00
|
|
|
Sector: abi.SectorID{Miner: 1000, Number: 100},
|
|
|
|
ID: uuid.MustParse("76081ba0-61bd-45a5-bc08-af05f1c26e5d"),
|
|
|
|
},
|
|
|
|
Sector: abi.SectorID{Miner: 1000, Number: 100},
|
|
|
|
Task: sealtasks.TTPreCommit2,
|
|
|
|
RunWait: 0,
|
2020-11-12 19:18:39 +00:00
|
|
|
Start: time.Unix(1605172927, 0).UTC(),
|
2020-11-12 09:29:42 +00:00
|
|
|
Hostname: "host",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
addExample(map[uuid.UUID]storiface.WorkerStats{
|
|
|
|
uuid.MustParse("ef8d99a2-6865-4189-8ffa-9fef0f806eee"): {
|
2020-11-12 09:42:06 +00:00
|
|
|
Info: storiface.WorkerInfo{
|
|
|
|
Hostname: "host",
|
2020-11-12 09:29:42 +00:00
|
|
|
Resources: storiface.WorkerResources{
|
|
|
|
MemPhysical: 256 << 30,
|
|
|
|
MemSwap: 120 << 30,
|
|
|
|
MemReserved: 2 << 30,
|
|
|
|
CPUs: 64,
|
|
|
|
GPUs: []string{"aGPU 1337"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Enabled: true,
|
|
|
|
MemUsedMin: 0,
|
|
|
|
MemUsedMax: 0,
|
|
|
|
GpuUsed: false,
|
|
|
|
CpuUse: 0,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
// worker specific
|
|
|
|
addExample(storiface.AcquireMove)
|
|
|
|
addExample(storiface.UnpaddedByteIndex(abi.PaddedPieceSize(1 << 20).Unpadded()))
|
2020-11-12 09:42:06 +00:00
|
|
|
addExample(map[sealtasks.TaskType]struct{}{
|
2020-11-12 09:29:42 +00:00
|
|
|
sealtasks.TTPreCommit2: {},
|
|
|
|
})
|
2020-04-20 18:34:07 +00:00
|
|
|
}
|
|
|
|
|
2020-11-12 09:29:42 +00:00
|
|
|
func exampleValue(method string, t, parent reflect.Type) interface{} {
|
2020-04-20 18:34:07 +00:00
|
|
|
v, ok := ExampleValues[t]
|
|
|
|
if ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
switch t.Kind() {
|
|
|
|
case reflect.Slice:
|
|
|
|
out := reflect.New(t).Elem()
|
2020-11-12 09:29:42 +00:00
|
|
|
reflect.Append(out, reflect.ValueOf(exampleValue(method, t.Elem(), t)))
|
2020-04-20 18:34:07 +00:00
|
|
|
return out.Interface()
|
|
|
|
case reflect.Chan:
|
2020-11-12 09:29:42 +00:00
|
|
|
return exampleValue(method, t.Elem(), nil)
|
2020-04-20 18:34:07 +00:00
|
|
|
case reflect.Struct:
|
2020-11-12 09:29:42 +00:00
|
|
|
es := exampleStruct(method, t, parent)
|
2020-04-20 18:34:07 +00:00
|
|
|
v := reflect.ValueOf(es).Elem().Interface()
|
|
|
|
ExampleValues[t] = v
|
|
|
|
return v
|
|
|
|
case reflect.Array:
|
|
|
|
out := reflect.New(t).Elem()
|
|
|
|
for i := 0; i < t.Len(); i++ {
|
2020-11-12 09:29:42 +00:00
|
|
|
out.Index(i).Set(reflect.ValueOf(exampleValue(method, t.Elem(), t)))
|
2020-04-20 18:34:07 +00:00
|
|
|
}
|
|
|
|
return out.Interface()
|
|
|
|
|
|
|
|
case reflect.Ptr:
|
|
|
|
if t.Elem().Kind() == reflect.Struct {
|
2020-11-12 09:29:42 +00:00
|
|
|
es := exampleStruct(method, t.Elem(), t)
|
2020-04-20 18:34:07 +00:00
|
|
|
//ExampleValues[t] = es
|
|
|
|
return es
|
|
|
|
}
|
|
|
|
case reflect.Interface:
|
|
|
|
return struct{}{}
|
|
|
|
}
|
|
|
|
|
2020-11-12 09:29:42 +00:00
|
|
|
panic(fmt.Sprintf("No example value for type: %s (method '%s')", t, method))
|
2020-04-20 18:34:07 +00:00
|
|
|
}
|
|
|
|
|
2020-11-12 09:29:42 +00:00
|
|
|
func exampleStruct(method string, t, parent reflect.Type) interface{} {
|
2020-04-20 18:34:07 +00:00
|
|
|
ns := reflect.New(t)
|
|
|
|
for i := 0; i < t.NumField(); i++ {
|
|
|
|
f := t.Field(i)
|
2020-06-18 12:30:00 +00:00
|
|
|
if f.Type == parent {
|
|
|
|
continue
|
|
|
|
}
|
2020-04-20 18:34:07 +00:00
|
|
|
if strings.Title(f.Name) == f.Name {
|
2020-11-12 09:29:42 +00:00
|
|
|
ns.Elem().Field(i).Set(reflect.ValueOf(exampleValue(method, f.Type, t)))
|
2020-04-20 18:34:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ns.Interface()
|
|
|
|
}
|
|
|
|
|
|
|
|
type Visitor struct {
|
2020-11-12 09:29:42 +00:00
|
|
|
Root string
|
2020-04-20 18:34:07 +00:00
|
|
|
Methods map[string]ast.Node
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Visitor) Visit(node ast.Node) ast.Visitor {
|
|
|
|
st, ok := node.(*ast.TypeSpec)
|
|
|
|
if !ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2020-11-12 09:29:42 +00:00
|
|
|
if st.Name.Name != v.Root {
|
2020-04-20 18:34:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
iface := st.Type.(*ast.InterfaceType)
|
|
|
|
for _, m := range iface.Methods.List {
|
|
|
|
if len(m.Names) > 0 {
|
|
|
|
v.Methods[m.Names[0].Name] = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2020-05-19 16:49:47 +00:00
|
|
|
const noComment = "There are not yet any comments for this method."
|
|
|
|
|
2020-11-12 09:29:42 +00:00
|
|
|
func parseApiASTInfo(apiFile, iface string) (map[string]string, map[string]string) { //nolint:golint
|
2020-04-20 18:34:07 +00:00
|
|
|
fset := token.NewFileSet()
|
|
|
|
pkgs, err := parser.ParseDir(fset, "./api", nil, parser.AllErrors|parser.ParseComments)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("parse error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ap := pkgs["api"]
|
|
|
|
|
2020-11-12 09:29:42 +00:00
|
|
|
f := ap.Files[apiFile]
|
2020-04-20 18:34:07 +00:00
|
|
|
|
|
|
|
cmap := ast.NewCommentMap(fset, f, f.Comments)
|
|
|
|
|
2020-11-12 09:29:42 +00:00
|
|
|
v := &Visitor{iface, make(map[string]ast.Node)}
|
2020-04-20 18:34:07 +00:00
|
|
|
ast.Walk(v, pkgs["api"])
|
|
|
|
|
2020-05-19 15:48:58 +00:00
|
|
|
groupDocs := make(map[string]string)
|
2020-04-20 18:34:07 +00:00
|
|
|
out := make(map[string]string)
|
|
|
|
for mn, node := range v.Methods {
|
|
|
|
cs := cmap.Filter(node).Comments()
|
|
|
|
if len(cs) == 0 {
|
2020-05-19 16:49:47 +00:00
|
|
|
out[mn] = noComment
|
2020-04-20 18:34:07 +00:00
|
|
|
} else {
|
2020-05-19 15:48:58 +00:00
|
|
|
for _, c := range cs {
|
|
|
|
if strings.HasPrefix(c.Text(), "MethodGroup:") {
|
|
|
|
parts := strings.Split(c.Text(), "\n")
|
|
|
|
groupName := strings.TrimSpace(parts[0][12:])
|
|
|
|
comment := strings.Join(parts[1:], "\n")
|
|
|
|
groupDocs[groupName] = comment
|
2020-05-19 16:49:47 +00:00
|
|
|
|
|
|
|
break
|
2020-05-19 15:48:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 16:49:47 +00:00
|
|
|
last := cs[len(cs)-1].Text()
|
|
|
|
if !strings.HasPrefix(last, "MethodGroup:") {
|
|
|
|
out[mn] = last
|
|
|
|
} else {
|
|
|
|
out[mn] = noComment
|
|
|
|
}
|
2020-04-20 18:34:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-19 15:48:58 +00:00
|
|
|
return out, groupDocs
|
2020-04-20 18:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type MethodGroup struct {
|
|
|
|
GroupName string
|
|
|
|
Header string
|
|
|
|
Methods []*Method
|
|
|
|
}
|
|
|
|
|
|
|
|
type Method struct {
|
|
|
|
Comment string
|
|
|
|
Name string
|
|
|
|
InputExample string
|
|
|
|
ResponseExample string
|
|
|
|
}
|
|
|
|
|
|
|
|
func methodGroupFromName(mn string) string {
|
|
|
|
i := strings.IndexFunc(mn[1:], func(r rune) bool {
|
|
|
|
return unicode.IsUpper(r)
|
|
|
|
})
|
|
|
|
if i < 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return mn[:i+1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2020-11-12 09:29:42 +00:00
|
|
|
comments, groupComments := parseApiASTInfo(os.Args[1], os.Args[2])
|
2020-04-20 18:34:07 +00:00
|
|
|
|
|
|
|
groups := make(map[string]*MethodGroup)
|
|
|
|
|
2020-11-12 09:29:42 +00:00
|
|
|
var t reflect.Type
|
|
|
|
var permStruct, commonPermStruct reflect.Type
|
|
|
|
|
|
|
|
switch os.Args[2] {
|
|
|
|
case "FullNode":
|
|
|
|
t = reflect.TypeOf(new(struct{ api.FullNode })).Elem()
|
|
|
|
permStruct = reflect.TypeOf(apistruct.FullNodeStruct{}.Internal)
|
|
|
|
commonPermStruct = reflect.TypeOf(apistruct.CommonStruct{}.Internal)
|
|
|
|
case "StorageMiner":
|
|
|
|
t = reflect.TypeOf(new(struct{ api.StorageMiner })).Elem()
|
|
|
|
permStruct = reflect.TypeOf(apistruct.StorageMinerStruct{}.Internal)
|
|
|
|
commonPermStruct = reflect.TypeOf(apistruct.CommonStruct{}.Internal)
|
|
|
|
case "WorkerAPI":
|
|
|
|
t = reflect.TypeOf(new(struct{ api.WorkerAPI })).Elem()
|
|
|
|
permStruct = reflect.TypeOf(apistruct.WorkerStruct{}.Internal)
|
|
|
|
commonPermStruct = reflect.TypeOf(apistruct.WorkerStruct{}.Internal)
|
|
|
|
default:
|
|
|
|
panic("unknown type")
|
|
|
|
}
|
|
|
|
|
2020-04-20 18:34:07 +00:00
|
|
|
for i := 0; i < t.NumMethod(); i++ {
|
|
|
|
m := t.Method(i)
|
|
|
|
|
|
|
|
groupName := methodGroupFromName(m.Name)
|
|
|
|
|
|
|
|
g, ok := groups[groupName]
|
|
|
|
if !ok {
|
|
|
|
g = new(MethodGroup)
|
2020-05-19 15:48:58 +00:00
|
|
|
g.Header = groupComments[groupName]
|
2020-04-20 18:34:07 +00:00
|
|
|
g.GroupName = groupName
|
|
|
|
groups[groupName] = g
|
|
|
|
}
|
|
|
|
|
|
|
|
var args []interface{}
|
|
|
|
ft := m.Func.Type()
|
|
|
|
for j := 2; j < ft.NumIn(); j++ {
|
|
|
|
inp := ft.In(j)
|
2020-11-12 09:29:42 +00:00
|
|
|
args = append(args, exampleValue(m.Name, inp, nil))
|
2020-04-20 18:34:07 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 12:37:36 +00:00
|
|
|
v, err := json.MarshalIndent(args, "", " ")
|
2020-04-20 18:34:07 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-11-12 09:29:42 +00:00
|
|
|
outv := exampleValue(m.Name, ft.Out(0), nil)
|
2020-04-20 18:34:07 +00:00
|
|
|
|
2020-06-18 12:37:36 +00:00
|
|
|
ov, err := json.MarshalIndent(outv, "", " ")
|
2020-04-20 18:34:07 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
g.Methods = append(g.Methods, &Method{
|
|
|
|
Name: m.Name,
|
|
|
|
Comment: comments[m.Name],
|
|
|
|
InputExample: string(v),
|
|
|
|
ResponseExample: string(ov),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var groupslice []*MethodGroup
|
|
|
|
for _, g := range groups {
|
|
|
|
groupslice = append(groupslice, g)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(groupslice, func(i, j int) bool {
|
|
|
|
return groupslice[i].GroupName < groupslice[j].GroupName
|
|
|
|
})
|
|
|
|
|
2020-06-18 12:56:00 +00:00
|
|
|
fmt.Printf("# Groups\n")
|
|
|
|
|
|
|
|
for _, g := range groupslice {
|
|
|
|
fmt.Printf("* [%s](#%s)\n", g.GroupName, g.GroupName)
|
|
|
|
for _, method := range g.Methods {
|
|
|
|
fmt.Printf(" * [%s](#%s)\n", method.Name, method.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 18:34:07 +00:00
|
|
|
for _, g := range groupslice {
|
2020-05-27 20:53:20 +00:00
|
|
|
g := g
|
2020-04-20 18:34:07 +00:00
|
|
|
fmt.Printf("## %s\n", g.GroupName)
|
|
|
|
fmt.Printf("%s\n\n", g.Header)
|
|
|
|
|
|
|
|
sort.Slice(g.Methods, func(i, j int) bool {
|
|
|
|
return g.Methods[i].Name < g.Methods[j].Name
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, m := range g.Methods {
|
|
|
|
fmt.Printf("### %s\n", m.Name)
|
|
|
|
fmt.Printf("%s\n\n", m.Comment)
|
|
|
|
|
2020-07-01 21:01:27 +00:00
|
|
|
meth, ok := permStruct.FieldByName(m.Name)
|
|
|
|
if !ok {
|
|
|
|
meth, ok = commonPermStruct.FieldByName(m.Name)
|
|
|
|
if !ok {
|
|
|
|
panic("no perms for method: " + m.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
perms := meth.Tag.Get("perm")
|
|
|
|
|
|
|
|
fmt.Printf("Perms: %s\n\n", perms)
|
|
|
|
|
2020-06-18 12:37:36 +00:00
|
|
|
if strings.Count(m.InputExample, "\n") > 0 {
|
|
|
|
fmt.Printf("Inputs:\n```json\n%s\n```\n\n", m.InputExample)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Inputs: `%s`\n\n", m.InputExample)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Count(m.ResponseExample, "\n") > 0 {
|
|
|
|
fmt.Printf("Response:\n```json\n%s\n```\n\n", m.ResponseExample)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Response: `%s`\n\n", m.ResponseExample)
|
|
|
|
}
|
2020-04-20 18:34:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|