bson go-HookDecoder,如何设置值
我正在寻找一种方法来更改reflect.Value的内容。在下面的代码中(主要是DecodeValue方法),该值似乎表示它需要一个关税限制项。然而,限制是Time实现的接口,因此我想将bson go-HookDecoder,如何设置值,go,bson,Go,Bson,我正在寻找一种方法来更改reflect.Value的内容。在下面的代码中(主要是DecodeValue方法),该值似乎表示它需要一个关税限制项。然而,限制是Time实现的接口,因此我想将value指向的内存位置与实现该接口的实际结构交换 代码的主要目标是允许序列化和反序列化我只有在接口形式中的元素。对于上下文:我试图序列化的结构有多个接口类型片,这些接口类型片后面有特定的实现。因此,我希望使用以下结构将它们存储在mongodb中:{:{:,…}}。到目前为止,我已经成功地以这种方式存储了它们。现
value
指向的内存位置与实现该接口的实际结构交换
代码的主要目标是允许序列化和反序列化我只有在接口形式中的元素。对于上下文:我试图序列化的结构有多个接口类型片,这些接口类型片后面有特定的实现。因此,我希望使用以下结构将它们存储在mongodb中:{:{:,…}}
。到目前为止,我已经成功地以这种方式存储了它们。现在的目标是从数据库中获取它们,自己找出类型,然后在变量value
中链接正确类型的结构
有没有办法到达value
指向的内存位置,然后用我的(新)结构替换内存位置的内容
登记册制作:
timeType := reflect.TypeOf(limitations.Time{})
codec := limitations.NewLimitationCodec()
rb.RegisterCodec(timeType, codec)
t := reflect.TypeOf((*tariff.Limitation)(nil)).Elem()
rb.RegisterHookDecoder(t, codec)
LimitationCodec.DecodeValue
func (l LimitationCodec) DecodeValue(context bsoncodec.DecodeContext, reader bsonrw.ValueReader, value reflect.Value) error {
logrus.Infoln("In LimitationCodec - Decode")
documentReader, err := reader.ReadDocument()
key, valueReader, err := documentReader.ReadElement()
logrus.Infoln(key)
logrus.Infoln(valueReader)
v := Time{}
z := reflect.ValueOf(&v)
err = l.codec.DecodeValue(context, valueReader, z)
// The above line seems to be able to read my struct without a problem
p := value.Addr()
p.Set(z)
// I only need to set the values in v to whatever memory position value is pointing to
return err
}