diff --git a/mongo/bson/bsoncodec/struct_codec.go b/mongo/bson/bsoncodec/struct_codec.go --- a/mongo/bson/bsoncodec/struct_codec.go +++ b/mongo/bson/bsoncodec/struct_codec.go @@ -122,6 +122,10 @@ func (sc *StructCodec) EncodeValue(r EncodeContext, vw bsonrw.ValueWriter, val r } var rv reflect.Value for _, desc := range sd.fl { + if desc.omitAlways { + continue + } + if desc.inline == nil { rv = val.Field(desc.idx) } else { @@ -400,15 +404,16 @@ type structDescription struct { } type fieldDescription struct { - name string // BSON key name - fieldName string // struct field name - idx int - omitEmpty bool - minSize bool - truncate bool - inline []int - encoder ValueEncoder - decoder ValueDecoder + name string // BSON key name + fieldName string // struct field name + idx int + omitEmpty bool + omitAlways bool + minSize bool + truncate bool + inline []int + encoder ValueEncoder + decoder ValueDecoder } type byIndex []fieldDescription @@ -491,6 +496,7 @@ func (sc *StructCodec) describeStruct(r *Registry, t reflect.Type) (*structDescr } description.name = stags.Name description.omitEmpty = stags.OmitEmpty + description.omitAlways = stags.OmitAlways description.minSize = stags.MinSize description.truncate = stags.Truncate diff --git a/mongo/bson/bsoncodec/struct_tag_parser.go b/mongo/bson/bsoncodec/struct_tag_parser.go --- a/mongo/bson/bsoncodec/struct_tag_parser.go +++ b/mongo/bson/bsoncodec/struct_tag_parser.go @@ -52,12 +52,13 @@ func (stpf StructTagParserFunc) ParseStructTags(sf reflect.StructField) (StructT // // TODO(skriptble): Add tags for undefined as nil and for null as nil. type StructTags struct { - Name string - OmitEmpty bool - MinSize bool - Truncate bool - Inline bool - Skip bool + Name string + OmitEmpty bool + OmitAlways bool + MinSize bool + Truncate bool + Inline bool + Skip bool } // DefaultStructTagParser is the StructTagParser used by the StructCodec by default. @@ -108,6 +109,8 @@ func parseTags(key string, tag string) (StructTags, error) { switch str { case "omitempty": st.OmitEmpty = true + case "omitalways": + st.OmitAlways = true case "minsize": st.MinSize = true case "truncate":