yarpc / yarpc-go

A message passing platform for Go
MIT License
411 stars 103 forks source link

Documentation example does not work #1911

Closed yagehu closed 4 years ago

yagehu commented 4 years ago

The linked documentation example does not work. Here's a minimal example:

package main

import (
        "context"
        "fmt"
        "log"

        "go.uber.org/yarpc"
        "go.uber.org/yarpc/encoding/json"
)

func main() {
        dispatcher := yarpc.NewDispatcher(yarpc.Config{
                Name: "yarpc-dev",
        })

        if err := dispatcher.Start(); err != nil {
                log.Fatal(err)
        }
        defer dispatcher.Stop()

        handler := func(ctx context.Context, key string) (string, error) {
                fmt.Println("key", key)

                return "value", nil
        }

        dispatcher.Register(json.Procedure("get", handler))
}

The error message:

$ go run main.go
panic: the second argument of the handler for "get" must be a struct pointer, a map[string]interface{}, or interface{}, and not: string

goroutine 1 [running]:
go.uber.org/yarpc/encoding/json.verifyInputSignature(0x96cc3f, 0x3, 0xa3b360, 0x8ccfc0, 0xc0002efc88, 0x7282b4)
        /home/huyage/go/pkg/mod/go.uber.org/yarpc@v1.44.0/encoding/json/register.go:197 +0x2a0
go.uber.org/yarpc/encoding/json.verifyUnarySignature(0x96cc3f, 0x3, 0xa3b360, 0x8ccfc0, 0x946360, 0x0)
        /home/huyage/go/pkg/mod/go.uber.org/yarpc@v1.44.0/encoding/json/register.go:117 +0x67
go.uber.org/yarpc/encoding/json.wrapUnaryHandler(0x96cc3f, 0x3, 0x8ccfc0, 0x990400, 0x0, 0x0)
        /home/huyage/go/pkg/mod/go.uber.org/yarpc@v1.44.0/encoding/json/register.go:86 +0x86
go.uber.org/yarpc/encoding/json.Procedure(...)
        /home/huyage/go/pkg/mod/go.uber.org/yarpc@v1.44.0/encoding/json/register.go:59
main.main()
        /home/huyage/dev/yarpcdev/main.go:28 +0x138
exit status 2
peats-bond commented 4 years ago

Thanks for filing, the fix is up, also note that you should register your procedures before running the dispatcher