...

Source file src/go.mongodb.org/mongo-driver/bson/bsoncodec/byte_slice_codec.go

Documentation: go.mongodb.org/mongo-driver/bson/bsoncodec

     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 bsoncodec
     8  
     9  import (
    10  	"fmt"
    11  	"reflect"
    12  
    13  	"go.mongodb.org/mongo-driver/bson/bsonoptions"
    14  	"go.mongodb.org/mongo-driver/bson/bsonrw"
    15  	"go.mongodb.org/mongo-driver/bson/bsontype"
    16  )
    17  
    18  // ByteSliceCodec is the Codec used for []byte values.
    19  //
    20  // Deprecated: ByteSliceCodec will not be directly configurable in Go Driver
    21  // 2.0. To configure the byte slice encode and decode behavior, use the
    22  // configuration methods on a [go.mongodb.org/mongo-driver/bson.Encoder] or
    23  // [go.mongodb.org/mongo-driver/bson.Decoder]. To configure the byte slice
    24  // encode and decode behavior for a mongo.Client, use
    25  // [go.mongodb.org/mongo-driver/mongo/options.ClientOptions.SetBSONOptions].
    26  //
    27  // For example, to configure a mongo.Client to encode nil byte slices as empty
    28  // BSON binary values, use:
    29  //
    30  //	opt := options.Client().SetBSONOptions(&options.BSONOptions{
    31  //	    NilByteSliceAsEmpty: true,
    32  //	})
    33  //
    34  // See the deprecation notice for each field in ByteSliceCodec for the
    35  // corresponding settings.
    36  type ByteSliceCodec struct {
    37  	// EncodeNilAsEmpty causes EncodeValue to marshal nil Go byte slices as empty BSON binary values
    38  	// instead of BSON null.
    39  	//
    40  	// Deprecated: Use bson.Encoder.NilByteSliceAsEmpty or options.BSONOptions.NilByteSliceAsEmpty
    41  	// instead.
    42  	EncodeNilAsEmpty bool
    43  }
    44  
    45  var (
    46  	defaultByteSliceCodec = NewByteSliceCodec()
    47  
    48  	// Assert that defaultByteSliceCodec satisfies the typeDecoder interface, which allows it to be
    49  	// used by collection type decoders (e.g. map, slice, etc) to set individual values in a
    50  	// collection.
    51  	_ typeDecoder = defaultByteSliceCodec
    52  )
    53  
    54  // NewByteSliceCodec returns a ByteSliceCodec with options opts.
    55  //
    56  // Deprecated: NewByteSliceCodec will not be available in Go Driver 2.0. See
    57  // [ByteSliceCodec] for more details.
    58  func NewByteSliceCodec(opts ...*bsonoptions.ByteSliceCodecOptions) *ByteSliceCodec {
    59  	byteSliceOpt := bsonoptions.MergeByteSliceCodecOptions(opts...)
    60  	codec := ByteSliceCodec{}
    61  	if byteSliceOpt.EncodeNilAsEmpty != nil {
    62  		codec.EncodeNilAsEmpty = *byteSliceOpt.EncodeNilAsEmpty
    63  	}
    64  	return &codec
    65  }
    66  
    67  // EncodeValue is the ValueEncoder for []byte.
    68  func (bsc *ByteSliceCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
    69  	if !val.IsValid() || val.Type() != tByteSlice {
    70  		return ValueEncoderError{Name: "ByteSliceEncodeValue", Types: []reflect.Type{tByteSlice}, Received: val}
    71  	}
    72  	if val.IsNil() && !bsc.EncodeNilAsEmpty && !ec.nilByteSliceAsEmpty {
    73  		return vw.WriteNull()
    74  	}
    75  	return vw.WriteBinary(val.Interface().([]byte))
    76  }
    77  
    78  func (bsc *ByteSliceCodec) decodeType(_ DecodeContext, vr bsonrw.ValueReader, t reflect.Type) (reflect.Value, error) {
    79  	if t != tByteSlice {
    80  		return emptyValue, ValueDecoderError{
    81  			Name:     "ByteSliceDecodeValue",
    82  			Types:    []reflect.Type{tByteSlice},
    83  			Received: reflect.Zero(t),
    84  		}
    85  	}
    86  
    87  	var data []byte
    88  	var err error
    89  	switch vrType := vr.Type(); vrType {
    90  	case bsontype.String:
    91  		str, err := vr.ReadString()
    92  		if err != nil {
    93  			return emptyValue, err
    94  		}
    95  		data = []byte(str)
    96  	case bsontype.Symbol:
    97  		sym, err := vr.ReadSymbol()
    98  		if err != nil {
    99  			return emptyValue, err
   100  		}
   101  		data = []byte(sym)
   102  	case bsontype.Binary:
   103  		var subtype byte
   104  		data, subtype, err = vr.ReadBinary()
   105  		if err != nil {
   106  			return emptyValue, err
   107  		}
   108  		if subtype != bsontype.BinaryGeneric && subtype != bsontype.BinaryBinaryOld {
   109  			return emptyValue, decodeBinaryError{subtype: subtype, typeName: "[]byte"}
   110  		}
   111  	case bsontype.Null:
   112  		err = vr.ReadNull()
   113  	case bsontype.Undefined:
   114  		err = vr.ReadUndefined()
   115  	default:
   116  		return emptyValue, fmt.Errorf("cannot decode %v into a []byte", vrType)
   117  	}
   118  	if err != nil {
   119  		return emptyValue, err
   120  	}
   121  
   122  	return reflect.ValueOf(data), nil
   123  }
   124  
   125  // DecodeValue is the ValueDecoder for []byte.
   126  func (bsc *ByteSliceCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
   127  	if !val.CanSet() || val.Type() != tByteSlice {
   128  		return ValueDecoderError{Name: "ByteSliceDecodeValue", Types: []reflect.Type{tByteSlice}, Received: val}
   129  	}
   130  
   131  	elem, err := bsc.decodeType(dc, vr, tByteSlice)
   132  	if err != nil {
   133  		return err
   134  	}
   135  
   136  	val.Set(elem)
   137  	return nil
   138  }
   139  

View as plain text