string_codec_options.go raw

   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 defaultDecodeOIDAsHex = true
  10  
  11  // StringCodecOptions represents all possible options for string 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 StringCodecOptions struct {
  16  	DecodeObjectIDAsHex *bool // Specifies if we should decode ObjectID as the hex value. Defaults to true.
  17  }
  18  
  19  // StringCodec creates a new *StringCodecOptions
  20  //
  21  // Deprecated: Use the bson.Encoder and bson.Decoder configuration methods to set the desired BSON marshal
  22  // and unmarshal behavior instead.
  23  func StringCodec() *StringCodecOptions {
  24  	return &StringCodecOptions{}
  25  }
  26  
  27  // SetDecodeObjectIDAsHex specifies if object IDs should be decoded as their hex representation. If false, a string made
  28  // from the raw object ID bytes will be used. Defaults to true.
  29  //
  30  // Deprecated: Decoding object IDs as raw bytes will not be supported in Go Driver 2.0.
  31  func (t *StringCodecOptions) SetDecodeObjectIDAsHex(b bool) *StringCodecOptions {
  32  	t.DecodeObjectIDAsHex = &b
  33  	return t
  34  }
  35  
  36  // MergeStringCodecOptions combines the given *StringCodecOptions into a single *StringCodecOptions in a last one wins fashion.
  37  //
  38  // Deprecated: Merging options structs will not be supported in Go Driver 2.0. Users should create a
  39  // single options struct instead.
  40  func MergeStringCodecOptions(opts ...*StringCodecOptions) *StringCodecOptions {
  41  	s := &StringCodecOptions{&defaultDecodeOIDAsHex}
  42  	for _, opt := range opts {
  43  		if opt == nil {
  44  			continue
  45  		}
  46  		if opt.DecodeObjectIDAsHex != nil {
  47  			s.DecodeObjectIDAsHex = opt.DecodeObjectIDAsHex
  48  		}
  49  	}
  50  
  51  	return s
  52  }
  53