英文:
Unmarshal to a interface type
问题
我有一些代码,我被倾倒了,实际上我被难住了 - 我之前使用过RPC和JSON方面的东西,但是当它在本地正常工作时,我似乎无法通过RPC使其工作。
package main
import (
"log"
"net"
"net/rpc"
"net/rpc/jsonrpc"
"reflect"
)
type Foo interface {
SayHello() error
}
type fakeFoo struct {
internalValue string
}
func NewFakeFoo() *fakeFoo {
f := &fakeFoo{}
f.internalValue = "123456789012347"
return f
}
func (m *fakeFoo) SayHello() error {
return nil
}
type FooManager struct {
availableFoos []Foo
}
func NewFooManager() *FooManager {
p := new(FooManager)
p.availableFoos = make([]Foo, 0)
return p
}
func AddFoo(mm *FooManager, m Foo) {
mm.availableFoos = append(mm.availableFoos, m)
log.Println("Added type ", reflect.TypeOf(m))
}
func (mm *FooManager) GetAvailableFoos(in []Foo, out *[]Foo) error {
log.Println("availableFoos:", reflect.TypeOf(mm.availableFoos))
log.Println("*out is", reflect.TypeOf(*out))
*out = append(in, mm.availableFoos...)
log.Println("Out is:", reflect.TypeOf(*out))
return nil
}
func startServer(mm *FooManager) {
server := rpc.NewServer()
server.Register(mm)
l, e := net.Listen("tcp", ":8222")
if e != nil {
log.Fatal("listen error:", e)
}
for {
conn, err := l.Accept()
log.Println("Incoming!")
if err != nil {
log.Fatal(err)
}
go server.ServeCodec(jsonrpc.NewServerCodec(conn))
}
}
func main() {
fake1 := NewFakeFoo()
fooHolder := NewFooManager()
AddFoo(fooHolder, fake1)
go startServer(fooHolder)
log.Println("Using standard function call")
var foos []Foo
fooHolder.GetAvailableFoos(foos, &foos)
log.Println(foos)
log.Println("Using RPC call")
conn, err := net.Dial("tcp", "localhost:8222")
if err != nil {
log.Fatalln(err)
}
defer conn.Close()
c := jsonrpc.NewClient(conn)
err = c.Call("FooManager.GetAvailableFoos", foos, &foos)
if err != nil {
log.Println(foos)
log.Fatal("GetAvailableFoos error:", err)
}
log.Println("Success: ", foos)
}
输出结果非常令人惊讶,因为它表明出现了与编组而不是实际数据有关的问题 - 在wireshark中运行它,我可以看到以正确的形式发送的数据(我在另一个问题中使用了类似的技术),但是我无法让它停止抛出编组错误。
运行此代码的输出如下所示:
2015/09/07 10:04:35 Added type *main.fakeFoo
2015/09/07 10:04:35 Using standard function call
2015/09/07 10:04:35 availableFoos: []main.Foo
2015/09/07 10:04:35 *out is []main.Foo
2015/09/07 10:04:35 Out is: []main.Foo
2015/09/07 10:04:35 [0x1870a540]
2015/09/07 10:04:35 Using RPC call
2015/09/07 10:04:35 Incoming!
2015/09/07 10:04:35 [0x1870a540]
2015/09/07 10:04:35 GetAvailableFoos error:json: cannot unmarshal object into Go value of type main.Foo
exit status 1
我是不是漏掉了一个接口/类型技巧,还是这是Go编组的一个错误?
英文:
I have some code I've been dumped with and am actually stumped - I've worked with RPC and the JSON side of things before but I can't seem to get it to work over RPC when it works fine locally.
package main
import (
"log"
"net"
"net/rpc"
"net/rpc/jsonrpc"
"reflect"
)
type Foo interface {
SayHello() error
}
type fakeFoo struct {
internalValue string
}
func NewFakeFoo() *fakeFoo {
f := &fakeFoo{}
f.internalValue = "123456789012347"
return f
}
func (m *fakeFoo) SayHello() error {
return nil
}
type FooManager struct {
availableFoos []Foo
}
func NewFooManager() *FooManager {
p := new(FooManager)
p.availableFoos = make([]Foo, 0)
return p
}
func AddFoo(mm *FooManager, m Foo) {
mm.availableFoos = append(mm.availableFoos, m)
log.Println("Added type ", reflect.TypeOf(m))
}
func (mm *FooManager) GetAvailableFoos(in []Foo, out *[]Foo) error {
log.Println("availableFoos:", reflect.TypeOf(mm.availableFoos))
log.Println("*out is", reflect.TypeOf(*out))
*out = append(in, mm.availableFoos...)
log.Println("Out is:", reflect.TypeOf(*out))
return nil
}
func startServer(mm *FooManager) {
server := rpc.NewServer()
server.Register(mm)
l, e := net.Listen("tcp", ":8222")
if e != nil {
log.Fatal("listen error:", e)
}
for {
conn, err := l.Accept()
log.Println("Incoming!")
if err != nil {
log.Fatal(err)
}
go server.ServeCodec(jsonrpc.NewServerCodec(conn))
}
}
func main() {
fake1 := NewFakeFoo()
fooHolder := NewFooManager()
AddFoo(fooHolder, fake1)
go startServer(fooHolder)
log.Println("Using standard function call")
var foos []Foo
fooHolder.GetAvailableFoos(foos, &foos)
log.Println(foos)
log.Println("Using RPC call")
conn, err := net.Dial("tcp", "localhost:8222")
if err != nil {
log.Fatalln(err)
}
defer conn.Close()
c := jsonrpc.NewClient(conn)
err = c.Call("FooManager.GetAvailableFoos", foos, &foos)
if err != nil {
log.Println(foos)
log.Fatal("GetAvailableFoos error:", err)
}
log.Println("Success: ", foos)
}
(also here but no tcp available urgh! http://play.golang.org/p/HmK-K09D2J )
The output is pretty surprising as it indicates something going wrong with the marshalling rather than with the actual data - Running it in wireshark I can see the data being sent in the correct form (I had success using a similar technique in another question) but can't for the life of me get this to stop throwing marshalling bugs.
The output from running this is as follows:
2015/09/07 10:04:35 Added type *main.fakeFoo
2015/09/07 10:04:35 Using standard function call
2015/09/07 10:04:35 availableFoos: []main.Foo
2015/09/07 10:04:35 *out is []main.Foo
2015/09/07 10:04:35 Out is: []main.Foo
2015/09/07 10:04:35 [0x1870a540]
2015/09/07 10:04:35 Using RPC call
2015/09/07 10:04:35 Incoming!
2015/09/07 10:04:35 [0x1870a540]
2015/09/07 10:04:35 GetAvailableFoos error:json: cannot unmarshal object into Go value of type main.Foo
exit status 1
Am I missing an interface/type trick or is this a bug in Go's marshalling?
答案1
得分: 9
所有的编组/解组都有这个问题。
你可以从接口类型变量进行编组,因为对象存在于本地,所以反射器知道底层类型。
你不能解组到接口类型,因为反射器不知道要给一个新实例哪个具体类型来接收编组的数据。
在一些编组/解组框架中,我们需要额外的信息来帮助反射器。例如,在Java的Json(jackson)中,我们使用JsonTypeInfo
注解来指定类类型,参考这个链接。
对于Go语言,你可以为自己的类型实现Unmarshaler接口。参考这个链接:https://stackoverflow.com/questions/23798283/golang-unmarshal-json
// RawString is a raw encoded JSON object.
// It implements Marshaler and Unmarshaler and can
// be used to delay JSON decoding or precompute a JSON encoding.
type RawString string
// MarshalJSON returns *m as the JSON encoding of m.
func (m *RawString) MarshalJSON() ([]byte, error) {
return []byte(*m), nil
}
// UnmarshalJSON sets *m to a copy of data.
func (m *RawString) UnmarshalJSON(data []byte) error {
if m == nil {
return errors.New("RawString: UnmarshalJSON on nil pointer")
}
*m += RawString(data)
return nil
}
const data = `{"i":3, "S":{"phone": {"sales": "2223334444"}}}`
type A struct {
I int64
S RawString `sql:"type:json"`
}
func main() {
a := A{}
err := json.Unmarshal([]byte(data), &a)
if err != nil {
log.Fatal("Unmarshal failed", err)
}
fmt.Println("Done", a)
}
英文:
All marshaling/unmarshaling has this problem.
You can marshal from an interface type variable, because the object exists locally, so the reflector knows the underlying type.
You cannot unmarshal to an interface type, because the reflector does not know which concrete type to give to a new instance to receive the marshaled data.
In some marshal/unmarshal frameworks we need additional information to help the reflector. For example, in Java Json(jackson), we use the JsonTypeInfo
annotation to specify the class type, refer to this.
For golang, you can implement the Unmarshaler interface for your own type yourself. Refer to https://stackoverflow.com/questions/23798283/golang-unmarshal-json
// RawString is a raw encoded JSON object.
// It implements Marshaler and Unmarshaler and can
// be used to delay JSON decoding or precompute a JSON encoding.
type RawString string
// MarshalJSON returns *m as the JSON encoding of m.
func (m *RawString) MarshalJSON() ([]byte, error) {
return []byte(*m), nil
}
// UnmarshalJSON sets *m to a copy of data.
func (m *RawString) UnmarshalJSON(data []byte) error {
if m == nil {
return errors.New("RawString: UnmarshalJSON on nil pointer")
}
*m += RawString(data)
return nil
}
const data = `{"i":3, "S":{"phone": {"sales": "2223334444"}}}`
type A struct {
I int64
S RawString `sql:"type:json"`
}
func main() {
a := A{}
err := json.Unmarshal([]byte(data), &a)
if err != nil {
log.Fatal("Unmarshal failed", err)
}
fmt.Println("Done", a)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论