Skip to content

Commit

Permalink
feat: include function signatures in rpc cli autocompletions (celesti…
Browse files Browse the repository at this point in the history
…aorg#2026)

Adds function signatures (excluding input context field, and output
error field) to the RPC CLI autocompletions. Now the autocompletions for
the methods looks like this:


![image](https://user-images.githubusercontent.com/16523232/230006576-a7263d3f-5360-4277-adaa-e500eed3fed6.png)
  • Loading branch information
distractedm1nd authored Apr 12, 2023
1 parent c37fcd4 commit b9eef30
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
28 changes: 28 additions & 0 deletions cmd/celestia/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,39 @@ import (
"bytes"
"context"
"os"
"reflect"
"testing"

"github.com/stretchr/testify/require"

"github.com/celestiaorg/celestia-node/header"
)

func TestCompletionHelpString(t *testing.T) {
type TestFields struct {
NoInputOneOutput func(context.Context) (*header.ExtendedHeader, error)
TwoInputsOneOutputArray func(
context.Context,
*header.ExtendedHeader,
uint64,
) ([]*header.ExtendedHeader, error)
OneInputOneOutput func(context.Context, uint64) (*header.ExtendedHeader, error)
NoInputsNoOutputs func(ctx context.Context) error
NoInputsChanOutput func(ctx context.Context) (<-chan *header.ExtendedHeader, error)
}
testOutputs := []string{
"() -> (*header.ExtendedHeader)",
"(*header.ExtendedHeader, uint64) -> ([]*header.ExtendedHeader)",
"(uint64) -> (*header.ExtendedHeader)",
"() -> ()",
"() -> (<-chan *header.ExtendedHeader)",
}
methods := reflect.VisibleFields(reflect.TypeOf(TestFields{}))
for i, method := range methods {
require.Equal(t, testOutputs[i], parseSignatureForHelpstring(method))
}
}

func TestLight(t *testing.T) {
// Run the tests in a temporary directory
tmpDir := t.TempDir()
Expand Down
22 changes: 21 additions & 1 deletion cmd/celestia/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var rpcCmd = &cobra.Command{
methods := reflect.VisibleFields(reflect.TypeOf(module).Elem())
var methodNames []string
for _, m := range methods {
methodNames = append(methodNames, m.Name)
methodNames = append(methodNames, m.Name+"\t"+parseSignatureForHelpstring(m))
}
return methodNames, cobra.ShellCompDirectiveNoFileComp
}
Expand Down Expand Up @@ -297,3 +297,23 @@ func parseAddressFromString(addrStr string) (state.Address, error) {

return addr, nil
}

func parseSignatureForHelpstring(methodSig reflect.StructField) string {
simplifiedSignature := "("
in, out := methodSig.Type.NumIn(), methodSig.Type.NumOut()
for i := 1; i < in; i++ {
simplifiedSignature += methodSig.Type.In(i).String()
if i != in-1 {
simplifiedSignature += ", "
}
}
simplifiedSignature += ") -> ("
for i := 0; i < out-1; i++ {
simplifiedSignature += methodSig.Type.Out(i).String()
if i != out-2 {
simplifiedSignature += ", "
}
}
simplifiedSignature += ")"
return simplifiedSignature
}

0 comments on commit b9eef30

Please sign in to comment.