I am trying to decode a struct which contains a result field, where the result field is either a null or string value. I'm trying to decode this into a string pointer but I get the error:
type MyStruct struct {
Result *string `json:"result,omitempty" bson:"result"`
}
The bson/json is something like:
{"result" : null}
or
{"result" : "somedata"}
Decode with:
var result MyStruct
err := collection.FindOne(ctx, filter).Decode(&result)
if err != nil {
s.logFile.Fatal(err)
}
Error:
cannot decode null into a string type
I'm using the official driver: "github.com/mongodb/mongo-go-driver/mongo" & "github.com/mongodb/mongo-go-driver/x/bsonx"