Skip to content

Conversation

@qingyang-hu
Copy link
Collaborator

@qingyang-hu qingyang-hu commented Feb 20, 2025

GODRIVER-3427

Summary

Add the "OmitEmpty" to both Encoder and BSONOptions so it can be enabled globally.

Background & Motivation

The global flag can be added without impacting any encoding behavior.

@mongodb-drivers-pr-bot mongodb-drivers-pr-bot bot added the review-priority-low Low Priority PR for Review: within 3 business days label Feb 20, 2025
@mongodb-drivers-pr-bot
Copy link
Contributor

API Change Report

./v2/bson

compatible changes

(*Encoder).OmitEmpty: added

./v2/mongo/options

compatible changes

BSONOptions.OmitEmpty: added

@qingyang-hu qingyang-hu marked this pull request as ready for review February 20, 2025 20:53
{
description: "OmitEmpty",
configure: func(enc *Encoder) {
enc.OmitZeroStruct()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest renaming this test "OmitEmpty with non-zeroer struct".

Additionally, we need to creating a new test where enc.OmitZeroStruct() is not used in the configuration. The new test should include the types referenced in the documentation and should be called "OmitEmpty":

Numbers, booleans, and strings are considered empty if their value is equal to the zero value for the type (i.e. 0 for numbers, false for booleans, and "" for strings). Slices, maps, and arrays are considered empty if they are of length zero. Interfaces and pointers are considered empty if their value is nil. By default, structs are only considered empty if the struct type implements bsoncodec.Zeroer and the IsZero method returns true. Struct types that do not implement bsoncodec.Zeroer are never considered empty and will be marshaled as embedded documents.

type zeroer struct {
	MyString string
}

func (zeroer) IsZero() bool {
	return true
}

var _ bson.Zeroer = zeroer{}

type myStruct struct {
	Zero    zeroer
	I64     int64
	F64     float64
	String  string
	Boolean bool
	Array   []int
	Map     map[string]int
	Bytes   []byte
	Time    time.Time
	Pointer *int
}

wantRaw: bson.Raw(bsoncore.NewDocumentBuilder().Build()),
},
{
name: "OmitEmpty",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest renaming this test "OmitEmpty with non-zeroer struct".

@qingyang-hu qingyang-hu merged commit b225485 into mongodb:master Mar 4, 2025
30 of 35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review-priority-low Low Priority PR for Review: within 3 business days

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants