[GODRIVER-427] fix pointers' decoding Created: 24/May/18  Updated: 28/Oct/23  Resolved: 24/May/18

Status: Closed
Project: Go Driver
Component/s: BSON
Affects Version/s: 0.0.5
Fix Version/s: 0.0.6

Type: Bug Priority: Major - P3
Reporter: Kristofer Brandow (Inactive) Assignee: Unassigned
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Came across a problem with decoding pointers.
Example of code to reproduce common issue:

package main
 
import (
	"github.com/mongodb/mongo-go-driver/bson"
	"log"
)
 
type TestStruct struct {
	TestValue *string `bson:"test_value,omitempty"`
}
 
func main() {
	testString := "test value goes here"
	t := TestStruct{
		TestValue: &testString,
	}
	data, err := bson.Marshal(t)
	if err != nil {
		log.Fatalf("Failed to marshal. Error: %v", err)
	}
	unt := TestStruct{}
	err = bson.Unmarshal(data, &unt)
	if err != nil {
		log.Fatalf("Failed to unmarshal. Error: %v", err)
	}
	log.Printf("Result: %+v")
	if unt.TestValue == nil {
		return
	}
	log.Printf("Contained string: %+v", *unt.TestValue)
}

Code like the one above gives error like:

reflect.Value.Addr of unaddressable value



 Comments   
Comment by Githook User [ 24/May/18 ]

Author:

{'username': 'skriptble', 'name': 'Kris Brandow', 'email': 'kris@mongodb.com'}

Message: Merge branch 'pr/56'

GODRIVER-427

Change-Id: I00803813275c717a7ecc4ff65bcfe398a55daa74
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/75e2ec3acfd19f16e172802fd2e6961ea6ce0565

Comment by Kristofer Brandow (Inactive) [ 24/May/18 ]

Code Review: https://github.com/mongodb/mongo-go-driver/pull/56.

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