[GODRIVER-842] bson can't marshal an interface Created: 21/Feb/19  Updated: 11/Sep/19  Resolved: 04/Mar/19

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

Type: Task Priority: Major - P3
Reporter: ollevche Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I have an interface, let's say:

 
type ITest interface {
    Hello()
}
 
Struct, which implements this:

type STest struct {
    Greeting string
}

func (s *STest) Hello() {
    fmt.Println(s.Greeting)
}
And an array of ITest interfaces with a STest struct as first element:
 
var iArr []ITest
iArr = append(iArr, &STest{"heelloo"})
 
When i'm trying to create an update document for UpdateOne method, i do the following:
 
bson.D{{"$set", bson.D{{"somefield", iArr}}}}
 
and getting an error:
 
cannot transform type primitive.D to a BSON Document: no encoder found for models.ITest
 
But for the separate element it works like a charm (no error, present in db):
 
bson.D{{"$set", bson.D{{"somefield", iArr[0]}}}}
 
 
So is it bug or something?

PS: i'm using v1.0.0-rc1+prerelease version



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

https://jira.mongodb.org/browse/GODRIVER-1117

Comment by ollevche [ 20/May/19 ]

How can i do this? Can't find any doc
thx in advance

Comment by Ian Whalen (Inactive) [ 04/Mar/19 ]

Hey ollevche, you'll have to register a custom codec if you'd like to do this.

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