Skip to content

fix uint32 encode. #193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

fix uint32 encode. #193

wants to merge 1 commit into from

Conversation

xtutu
Copy link

@xtutu xtutu commented Oct 10, 2019

When encode a uint32 type, it will got NumberLong() in bson.

for example

type Data struct {
	Number uint32 `bson:"Number"`
}

func TestBson(t *testing.T) {
	data := &Data{
		Number: 100,
	}
	byteData, _ := bson.Marshal(data)
	fmt.Println(byteData)

	var after = &Data{}
	bson.Unmarshal(byteData, after)
	fmt.Printf("%d\n", after.Number)
}

Output is :
[21 0 0 0 18 78 117 109 98 101 114 0 100 0 0 0 0 0 0 0 0]

According to this description: http://bsonspec.org/spec.html
18 = "\x12", it means 64-bit integer type

so, the more right output should be:
[17 0 0 0 16 78 117 109 98 101 114 0 100 0 0 0 0]

@divjotarora
Copy link
Contributor

Hi @xtutu,

Thank you for your contribution to the Go driver. We recognize that this is an issue, but we don't think this PR is the correct solution. For your use case specifically, you can call bson.MarshalWithContext with an EncodeContext that has minSize set to true. We are also working on adding options to codecs so that users who want minSize behavior can easily create a BSON Registry to enable it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants