Patch mongo (add omitalways)

This commit is contained in:
2023-06-18 16:04:34 +02:00
parent d471d7c396
commit 2ec88e81f3
4 changed files with 127 additions and 16 deletions

View File

@@ -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

View File

@@ -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":