github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.mongodb.org/mongo-driver/bson/bsonoptions/map_codec_options.go (about)

     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  // MapCodecOptions represents all possible options for map encoding and decoding.
    10  //
    11  // Deprecated: Use the bson.Encoder and bson.Decoder configuration methods to set the desired BSON marshal
    12  // and unmarshal behavior instead.
    13  type MapCodecOptions struct {
    14  	DecodeZerosMap   *bool // Specifies if the map should be zeroed before decoding into it. Defaults to false.
    15  	EncodeNilAsEmpty *bool // Specifies if a nil map should encode as an empty document instead of null. Defaults to false.
    16  	// Specifies how keys should be handled. If false, the behavior matches encoding/json, where the encoding key type must
    17  	// either be a string, an integer type, or implement bsoncodec.KeyMarshaler and the decoding key type must either be a
    18  	// string, an integer type, or implement bsoncodec.KeyUnmarshaler. If true, keys are encoded with fmt.Sprint() and the
    19  	// encoding key type must be a string, an integer type, or a float. If true, the use of Stringer will override
    20  	// TextMarshaler/TextUnmarshaler. Defaults to false.
    21  	EncodeKeysWithStringer *bool
    22  }
    23  
    24  // MapCodec creates a new *MapCodecOptions
    25  //
    26  // Deprecated: Use the bson.Encoder and bson.Decoder configuration methods to set the desired BSON marshal
    27  // and unmarshal behavior instead.
    28  func MapCodec() *MapCodecOptions {
    29  	return &MapCodecOptions{}
    30  }
    31  
    32  // SetDecodeZerosMap specifies if the map should be zeroed before decoding into it. Defaults to false.
    33  //
    34  // Deprecated: Use [go.mongodb.org/mongo-driver/bson.Decoder.ZeroMaps] instead.
    35  func (t *MapCodecOptions) SetDecodeZerosMap(b bool) *MapCodecOptions {
    36  	t.DecodeZerosMap = &b
    37  	return t
    38  }
    39  
    40  // SetEncodeNilAsEmpty specifies if a nil map should encode as an empty document instead of null. Defaults to false.
    41  //
    42  // Deprecated: Use [go.mongodb.org/mongo-driver/bson.Encoder.NilMapAsEmpty] instead.
    43  func (t *MapCodecOptions) SetEncodeNilAsEmpty(b bool) *MapCodecOptions {
    44  	t.EncodeNilAsEmpty = &b
    45  	return t
    46  }
    47  
    48  // SetEncodeKeysWithStringer specifies how keys should be handled. If false, the behavior matches encoding/json, where the
    49  // encoding key type must either be a string, an integer type, or implement bsoncodec.KeyMarshaler and the decoding key
    50  // type must either be a string, an integer type, or implement bsoncodec.KeyUnmarshaler. If true, keys are encoded with
    51  // fmt.Sprint() and the encoding key type must be a string, an integer type, or a float. If true, the use of Stringer
    52  // will override TextMarshaler/TextUnmarshaler. Defaults to false.
    53  //
    54  // Deprecated: Use [go.mongodb.org/mongo-driver/bson.Encoder.StringifyMapKeysWithFmt] instead.
    55  func (t *MapCodecOptions) SetEncodeKeysWithStringer(b bool) *MapCodecOptions {
    56  	t.EncodeKeysWithStringer = &b
    57  	return t
    58  }
    59  
    60  // MergeMapCodecOptions combines the given *MapCodecOptions into a single *MapCodecOptions in a last one wins fashion.
    61  //
    62  // Deprecated: Merging options structs will not be supported in Go Driver 2.0. Users should create a
    63  // single options struct instead.
    64  func MergeMapCodecOptions(opts ...*MapCodecOptions) *MapCodecOptions {
    65  	s := MapCodec()
    66  	for _, opt := range opts {
    67  		if opt == nil {
    68  			continue
    69  		}
    70  		if opt.DecodeZerosMap != nil {
    71  			s.DecodeZerosMap = opt.DecodeZerosMap
    72  		}
    73  		if opt.EncodeNilAsEmpty != nil {
    74  			s.EncodeNilAsEmpty = opt.EncodeNilAsEmpty
    75  		}
    76  		if opt.EncodeKeysWithStringer != nil {
    77  			s.EncodeKeysWithStringer = opt.EncodeKeysWithStringer
    78  		}
    79  	}
    80  
    81  	return s
    82  }