Remove extraneous empty struct returns from RPC interface

This commit is contained in:
Brian Picciano 2024-09-05 17:28:10 +02:00
parent 038a28bb02
commit 8d3b17e1cb
7 changed files with 66 additions and 54 deletions

View File

@ -116,8 +116,7 @@ var subCmdHostRemove = subCmd{
return errors.New("--hostname is required") return errors.New("--hostname is required")
} }
_, err := ctx.daemonRPC.RemoveHost(ctx, hostName.V) if err := ctx.daemonRPC.RemoveHost(ctx, hostName.V); err != nil {
if err != nil {
return fmt.Errorf("calling RemoveHost: %w", err) return fmt.Errorf("calling RemoveHost: %w", err)
} }

View File

@ -51,7 +51,7 @@ var subCmdNetworkCreate = subCmd{
return errors.New("--name, --domain, --ip-net, and --hostname are required") return errors.New("--name, --domain, --ip-net, and --hostname are required")
} }
_, err := ctx.daemonRPC.CreateNetwork( err := ctx.daemonRPC.CreateNetwork(
ctx, *name, *domain, ipNet.V, hostName.V, ctx, *name, *domain, ipNet.V, hostName.V,
) )
if err != nil { if err != nil {
@ -88,8 +88,7 @@ var subCmdNetworkJoin = subCmd{
) )
} }
_, err := ctx.daemonRPC.JoinNetwork(ctx, newBootstrap) return ctx.daemonRPC.JoinNetwork(ctx, newBootstrap)
return err
}, },
} }

View File

@ -44,11 +44,10 @@ func (c *rpcClient) CreateNebulaCertificate(ctx context.Context, hostName nebula
return return
} }
func (c *rpcClient) CreateNetwork(ctx context.Context, name string, domain string, ipNet nebula.IPNet, hostName nebula.HostName) (st1 struct { func (c *rpcClient) CreateNetwork(ctx context.Context, name string, domain string, ipNet nebula.IPNet, hostName nebula.HostName) (err error) {
}, err error) {
err = c.client.Call( err = c.client.Call(
ctx, ctx,
&st1, nil,
"CreateNetwork", "CreateNetwork",
name, name,
domain, domain,
@ -85,22 +84,20 @@ func (c *rpcClient) GetNebulaCAPublicCredentials(ctx context.Context) (c2 nebula
return return
} }
func (c *rpcClient) JoinNetwork(ctx context.Context, req JoiningBootstrap) (st1 struct { func (c *rpcClient) JoinNetwork(ctx context.Context, req JoiningBootstrap) (err error) {
}, err error) {
err = c.client.Call( err = c.client.Call(
ctx, ctx,
&st1, nil,
"JoinNetwork", "JoinNetwork",
req, req,
) )
return return
} }
func (c *rpcClient) RemoveHost(ctx context.Context, hostName nebula.HostName) (st1 struct { func (c *rpcClient) RemoveHost(ctx context.Context, hostName nebula.HostName) (err error) {
}, err error) {
err = c.client.Call( err = c.client.Call(
ctx, ctx,
&st1, nil,
"RemoveHost", "RemoveHost",
hostName, hostName,
) )

View File

@ -17,11 +17,21 @@ func {{.Interface.Name}}FromClient(client jsonrpc2.Client) {{.Interface.Name}} {
{{range $method := .Interface.Methods}} {{range $method := .Interface.Methods}}
func (c *{{$t}}) {{$method.Declaration}} { func (c *{{$t}}) {{$method.Declaration}} {
{{- $ctx := (index $method.Params 0).Name}} {{- $ctx := (index $method.Params 0).Name}}
{{- $rcv := (index $method.Results 0).Name}}
{{- $err := (index $method.Results 1).Name}} {{- $rcv := ""}}
{{- $err := ""}}
{{- if (eq (len $method.Results) 1)}}
{{- $rcv = "nil" }}
{{- $err = (index $method.Results 0).Name}}
{{- else}}
{{- $rcv = printf "&%s" (index $method.Results 0).Name}}
{{- $err = (index $method.Results 1).Name}}
{{- end}}
{{- $err}} = c.client.Call( {{- $err}} = c.client.Call(
{{$ctx}}, {{$ctx}},
&{{$rcv}}, {{$rcv}},
"{{$method.Name}}", "{{$method.Name}}",
{{- range $param := (slice $method.Params 1)}} {{- range $param := (slice $method.Params 1)}}
{{$param.Name}}, {{$param.Name}},

View File

@ -48,16 +48,26 @@ func newMethodDispatchFunc(
} }
var ( var (
callResV = method.Call(callVals) callResV = method.Call(callVals)
resV = callResV[0] resV, errV reflect.Value
errV = callResV[1]
) )
if errV.IsNil() { if len(callResV) == 1 {
errV = callResV[0]
} else {
resV = callResV[0]
errV = callResV[1]
}
if !errV.IsNil() {
return nil, errV.Interface().(error)
}
if resV.IsValid() {
return resV.Interface(), nil return resV.Interface(), nil
} }
return nil, errV.Interface().(error) return nil, nil
} }
} }
@ -96,8 +106,9 @@ func NewDispatchHandler(i any) Handler {
if !method.IsExported() || if !method.IsExported() ||
methodT.NumIn() < 1 || methodT.NumIn() < 1 ||
methodT.In(0) != ctxT || methodT.In(0) != ctxT ||
methodT.NumOut() != 2 || (methodT.NumOut() == 1 && methodT.Out(0) != errT) ||
methodT.Out(1) != errT { (methodT.NumOut() == 2 && methodT.Out(1) != errT) ||
methodT.NumOut() > 2 {
continue continue
} }

View File

@ -36,12 +36,16 @@ func (dividerImpl) Divide2(ctx context.Context, top, bottom int) (int, error) {
return top / bottom, nil return top / bottom, nil
} }
func (i dividerImpl) Noop(ctx context.Context) (int, error) { func (i dividerImpl) Divide(ctx context.Context, p DivideParams) (int, error) {
return i.Divide2(ctx, p.Top, p.Bottom)
}
func (i dividerImpl) One(ctx context.Context) (int, error) {
return 1, nil return 1, nil
} }
func (i dividerImpl) Divide(ctx context.Context, p DivideParams) (int, error) { func (i dividerImpl) Noop(ctx context.Context) error {
return i.Divide2(ctx, p.Top, p.Bottom) return nil
} }
func (dividerImpl) Hidden(ctx context.Context, p struct{}) (int, error) { func (dividerImpl) Hidden(ctx context.Context, p struct{}) (int, error) {
@ -49,9 +53,10 @@ func (dividerImpl) Hidden(ctx context.Context, p struct{}) (int, error) {
} }
type divider interface { type divider interface {
Noop(ctx context.Context) (int, error)
Divide2(ctx context.Context, top, bottom int) (int, error) Divide2(ctx context.Context, top, bottom int) (int, error)
Divide(ctx context.Context, p DivideParams) (int, error) Divide(ctx context.Context, p DivideParams) (int, error)
One(ctx context.Context) (int, error)
Noop(ctx context.Context) error
} }
var testHandler = func() Handler { var testHandler = func() Handler {
@ -104,7 +109,7 @@ func testClient(t *testing.T, client Client) {
t.Run("success/no_params", func(t *testing.T) { t.Run("success/no_params", func(t *testing.T) {
var res int var res int
err := client.Call(ctx, &res, "Noop") err := client.Call(ctx, &res, "One")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} else if res != 1 { } else if res != 1 {
@ -112,6 +117,13 @@ func testClient(t *testing.T, client Client) {
} }
}) })
t.Run("success/no_results", func(t *testing.T) {
err := client.Call(ctx, nil, "Noop")
if err != nil {
t.Fatal(err)
}
})
t.Run("err/application", func(t *testing.T) { t.Run("err/application", func(t *testing.T) {
err := client.Call(ctx, nil, "Divide", DivideParams{}) err := client.Call(ctx, nil, "Divide", DivideParams{})
if !errors.Is(err, ErrDivideByZero) { if !errors.Is(err, ErrDivideByZero) {

View File

@ -45,16 +45,10 @@ type RPC interface {
domain string, domain string,
ipNet nebula.IPNet, ipNet nebula.IPNet,
hostName nebula.HostName, hostName nebula.HostName,
) ( ) error
struct{}, error,
)
// JoinNetwork passes through to the Daemon method of the same name. // JoinNetwork passes through to the Daemon method of the same name.
JoinNetwork( JoinNetwork(ctx context.Context, req JoiningBootstrap) error
ctx context.Context, req JoiningBootstrap,
) (
struct{}, error,
)
// GetHosts returns all hosts known to the network, sorted by their name. // GetHosts returns all hosts known to the network, sorted by their name.
GetHosts(ctx context.Context) (GetHostsResult, error) GetHosts(ctx context.Context) (GetHostsResult, error)
@ -72,11 +66,7 @@ type RPC interface {
) )
// RemoveHost passes the call through to the Daemon method of the same name. // RemoveHost passes the call through to the Daemon method of the same name.
RemoveHost( RemoveHost(ctx context.Context, hostName nebula.HostName) error
ctx context.Context, hostName nebula.HostName,
) (
struct{}, error,
)
// CreateHost passes the call through to the Daemon method of the same name. // CreateHost passes the call through to the Daemon method of the same name.
CreateHost( CreateHost(
@ -111,20 +101,16 @@ func (r *rpcImpl) CreateNetwork(
domain string, domain string,
ipNet nebula.IPNet, ipNet nebula.IPNet,
hostName nebula.HostName, hostName nebula.HostName,
) ( ) error {
struct{}, error, return r.daemon.CreateNetwork(
) {
return struct{}{}, r.daemon.CreateNetwork(
ctx, name, domain, ipNet, hostName, ctx, name, domain, ipNet, hostName,
) )
} }
func (r *rpcImpl) JoinNetwork( func (r *rpcImpl) JoinNetwork(
ctx context.Context, req JoiningBootstrap, ctx context.Context, req JoiningBootstrap,
) ( ) error {
struct{}, error, return r.daemon.JoinNetwork(ctx, req)
) {
return struct{}{}, r.daemon.JoinNetwork(ctx, req)
} }
func (r *rpcImpl) GetHosts(ctx context.Context) (GetHostsResult, error) { func (r *rpcImpl) GetHosts(ctx context.Context) (GetHostsResult, error) {
@ -166,10 +152,8 @@ func (r *rpcImpl) GetNebulaCAPublicCredentials(
func (r *rpcImpl) RemoveHost( func (r *rpcImpl) RemoveHost(
ctx context.Context, hostName nebula.HostName, ctx context.Context, hostName nebula.HostName,
) ( ) error {
struct{}, error, return r.daemon.RemoveHost(ctx, hostName)
) {
return struct{}{}, r.daemon.RemoveHost(ctx, hostName)
} }
func (r *rpcImpl) CreateHost( func (r *rpcImpl) CreateHost(