[GODRIVER-1117] Allow marshaling custom interfaces inside arrays or structs Created: 10/Jun/19  Updated: 09/Jul/19  Resolved: 09/Jul/19

Status: Closed
Project: Go Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Yao Wei Assignee: Emmanuel Eppinger (Inactive)
Resolution: Done Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

This can be considered a follow-up of GODRIVER-842, that the custom interfaces inside arrays or structs cannot be marshaled.

Example is shown as follows:

type I interface {}
 
type A struct {
	X string
}
 
type B struct {
	Y I
}
 
func (*A) Hello() {}
 
func main() {
	a := A{X: "meow"}
	b := B{Y: &a}
	_, err := bson.Marshal(b)
	fmt.Println(err)
}

It returns "no encoder found for main.I" in current state. However, bson.Marshal can actaully work with interface{} inside struct, as well as custom interfaces if it is not inside a struct, so I would expect interface{} inside struct should work.



 Comments   
Comment by Emmanuel Eppinger (Inactive) [ 09/Jul/19 ]

Hi,

 

As indicated in GODRIVER-842, you need to register a custom codec.

 

You can do this by doing implementing the codec interface by implementing EncodeValue(EncodeContext, bsonrw.ValueWriter, reflect.Value) error and DecodeValue(DecodeContext, bsonrw.ValueReader, reflect.Value) error.

 

Thanks,

Emmanuel

 

Generated at Thu Feb 08 08:35:43 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.