Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unmarshalling to an interface type? #95

Open
prologic opened this issue Feb 4, 2023 · 1 comment
Open

Unmarshalling to an interface type? #95

prologic opened this issue Feb 4, 2023 · 1 comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@prologic
Copy link

prologic commented Feb 4, 2023

It this supported? For example:

// Package main implements a test JSON-RPC cli client
package main

import (
	"context"
	"flag"
	"fmt"
	"net"
	"net/http"

	"github.com/filecoin-project/go-jsonrpc"
	"go.mills.io/saltyim"
)

func main() {
	flag.Parse()

	httpc := &http.Client{
		Transport: &http.Transport{
			DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
				return net.Dial("unix", flag.Arg(0))
			},
		},
	}

	var cli struct {
		Me     func() saltyim.Addr
		String func() string
	}

	closer, err := jsonrpc.NewMergeClient(
		context.Background(),
		"http://example.com",
		"Client",
		[]any{&cli},
		nil,
		jsonrpc.WithHTTPClient(httpc),
	)
	if err != nil {
		panic(err)
	}
	defer closer()

	fmt.Printf("String():\n%s\n", cli.String())

	me := cli.Me()
	fmt.Printf("Me(): #%v\n", me)
}

Currently I get nil for a call to cli.me() 🤔

@magik6k
Copy link
Contributor

magik6k commented Mar 6, 2023

Interface types are supported as params through ParamEncoders.

See this test as an example:

go-jsonrpc/rpc_test.go

Lines 1047 to 1068 in 6fff219

func TestInterfaceHandler(t *testing.T) {
var client struct {
ReadAll func(ctx context.Context, r io.Reader) ([]byte, error)
}
serverHandler := &InterfaceHandler{}
rpcServer := NewServer(WithParamDecoder(new(io.Reader), readerDec))
rpcServer.Register("InterfaceHandler", serverHandler)
testServ := httptest.NewServer(rpcServer)
defer testServ.Close()
closer, err := NewMergeClient(context.Background(), "ws://"+testServ.Listener.Addr().String(), "InterfaceHandler", []interface{}{&client}, nil, WithParamEncoder(new(io.Reader), readerEnc))
require.NoError(t, err)
defer closer()
read, err := client.ReadAll(context.TODO(), strings.NewReader("pooooootato"))
require.NoError(t, err)
require.Equal(t, "pooooootato", string(read), "potatos weren't equal")
}

This does not support interfaces for Results, that is a separate feature, but shouldn't be hard to implement.

If you / anyone want to help tackle this, the change-set will likely look similar to the param-encoder feature - See #17

@magik6k magik6k added enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed labels Mar 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants
@prologic @magik6k and others