1 // Copyright (C) MongoDB, Inc. 2017-present.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may
4 // not use this file except in compliance with the License. You may obtain
5 // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6 7 package bsonoptions
8 9 var defaultOverwriteDuplicatedInlinedFields = true
10 11 // StructCodecOptions represents all possible options for struct encoding and decoding.
12 //
13 // Deprecated: Use the bson.Encoder and bson.Decoder configuration methods to set the desired BSON marshal
14 // and unmarshal behavior instead.
15 type StructCodecOptions struct {
16 DecodeZeroStruct *bool // Specifies if structs should be zeroed before decoding into them. Defaults to false.
17 DecodeDeepZeroInline *bool // Specifies if structs should be recursively zeroed when a inline value is decoded. Defaults to false.
18 EncodeOmitDefaultStruct *bool // Specifies if default structs should be considered empty by omitempty. Defaults to false.
19 AllowUnexportedFields *bool // Specifies if unexported fields should be marshaled/unmarshaled. Defaults to false.
20 OverwriteDuplicatedInlinedFields *bool // Specifies if fields in inlined structs can be overwritten by higher level struct fields with the same key. Defaults to true.
21 }
22 23 // StructCodec creates a new *StructCodecOptions
24 //
25 // Deprecated: Use the bson.Encoder and bson.Decoder configuration methods to set the desired BSON marshal
26 // and unmarshal behavior instead.
27 func StructCodec() *StructCodecOptions {
28 return &StructCodecOptions{}
29 }
30 31 // SetDecodeZeroStruct specifies if structs should be zeroed before decoding into them. Defaults to false.
32 //
33 // Deprecated: Use [go.mongodb.org/mongo-driver/bson.Decoder.ZeroStructs] instead.
34 func (t *StructCodecOptions) SetDecodeZeroStruct(b bool) *StructCodecOptions {
35 t.DecodeZeroStruct = &b
36 return t
37 }
38 39 // SetDecodeDeepZeroInline specifies if structs should be zeroed before decoding into them. Defaults to false.
40 //
41 // Deprecated: DecodeDeepZeroInline will not be supported in Go Driver 2.0.
42 func (t *StructCodecOptions) SetDecodeDeepZeroInline(b bool) *StructCodecOptions {
43 t.DecodeDeepZeroInline = &b
44 return t
45 }
46 47 // SetEncodeOmitDefaultStruct specifies if default structs should be considered empty by omitempty. A default struct has all
48 // its values set to their default value. Defaults to false.
49 //
50 // Deprecated: Use [go.mongodb.org/mongo-driver/bson.Encoder.OmitZeroStruct] instead.
51 func (t *StructCodecOptions) SetEncodeOmitDefaultStruct(b bool) *StructCodecOptions {
52 t.EncodeOmitDefaultStruct = &b
53 return t
54 }
55 56 // SetOverwriteDuplicatedInlinedFields specifies if inlined struct fields can be overwritten by higher level struct fields with the
57 // same bson key. When true and decoding, values will be written to the outermost struct with a matching key, and when
58 // encoding, keys will have the value of the top-most matching field. When false, decoding and encoding will error if
59 // there are duplicate keys after the struct is inlined. Defaults to true.
60 //
61 // Deprecated: Use [go.mongodb.org/mongo-driver/bson.Encoder.ErrorOnInlineDuplicates] instead.
62 func (t *StructCodecOptions) SetOverwriteDuplicatedInlinedFields(b bool) *StructCodecOptions {
63 t.OverwriteDuplicatedInlinedFields = &b
64 return t
65 }
66 67 // SetAllowUnexportedFields specifies if unexported fields should be marshaled/unmarshaled. Defaults to false.
68 //
69 // Deprecated: AllowUnexportedFields does not work on recent versions of Go and will not be
70 // supported in Go Driver 2.0.
71 func (t *StructCodecOptions) SetAllowUnexportedFields(b bool) *StructCodecOptions {
72 t.AllowUnexportedFields = &b
73 return t
74 }
75 76 // MergeStructCodecOptions combines the given *StructCodecOptions into a single *StructCodecOptions in a last one wins fashion.
77 //
78 // Deprecated: Merging options structs will not be supported in Go Driver 2.0. Users should create a
79 // single options struct instead.
80 func MergeStructCodecOptions(opts ...*StructCodecOptions) *StructCodecOptions {
81 s := &StructCodecOptions{
82 OverwriteDuplicatedInlinedFields: &defaultOverwriteDuplicatedInlinedFields,
83 }
84 for _, opt := range opts {
85 if opt == nil {
86 continue
87 }
88 89 if opt.DecodeZeroStruct != nil {
90 s.DecodeZeroStruct = opt.DecodeZeroStruct
91 }
92 if opt.DecodeDeepZeroInline != nil {
93 s.DecodeDeepZeroInline = opt.DecodeDeepZeroInline
94 }
95 if opt.EncodeOmitDefaultStruct != nil {
96 s.EncodeOmitDefaultStruct = opt.EncodeOmitDefaultStruct
97 }
98 if opt.OverwriteDuplicatedInlinedFields != nil {
99 s.OverwriteDuplicatedInlinedFields = opt.OverwriteDuplicatedInlinedFields
100 }
101 if opt.AllowUnexportedFields != nil {
102 s.AllowUnexportedFields = opt.AllowUnexportedFields
103 }
104 }
105 106 return s
107 }
108