...

Source file src/go.mongodb.org/mongo-driver/bson/bsoncodec/pointer_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  	"reflect"
    11  
    12  	"go.mongodb.org/mongo-driver/bson/bsonrw"
    13  	"go.mongodb.org/mongo-driver/bson/bsontype"
    14  )
    15  
    16  var _ ValueEncoder = &PointerCodec{}
    17  var _ ValueDecoder = &PointerCodec{}
    18  
    19  // PointerCodec is the Codec used for pointers.
    20  //
    21  // Deprecated: PointerCodec will not be directly accessible in Go Driver 2.0. To
    22  // override the default pointer encode and decode behavior, create a new registry
    23  // with [go.mongodb.org/mongo-driver/bson.NewRegistry] and register a new
    24  // encoder and decoder for pointers.
    25  //
    26  // For example,
    27  //
    28  //	reg := bson.NewRegistry()
    29  //	reg.RegisterKindEncoder(reflect.Ptr, myPointerEncoder)
    30  //	reg.RegisterKindDecoder(reflect.Ptr, myPointerDecoder)
    31  type PointerCodec struct {
    32  	ecache typeEncoderCache
    33  	dcache typeDecoderCache
    34  }
    35  
    36  // NewPointerCodec returns a PointerCodec that has been initialized.
    37  //
    38  // Deprecated: NewPointerCodec will not be available in Go Driver 2.0. See
    39  // [PointerCodec] for more details.
    40  func NewPointerCodec() *PointerCodec {
    41  	return &PointerCodec{}
    42  }
    43  
    44  // EncodeValue handles encoding a pointer by either encoding it to BSON Null if the pointer is nil
    45  // or looking up an encoder for the type of value the pointer points to.
    46  func (pc *PointerCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
    47  	if val.Kind() != reflect.Ptr {
    48  		if !val.IsValid() {
    49  			return vw.WriteNull()
    50  		}
    51  		return ValueEncoderError{Name: "PointerCodec.EncodeValue", Kinds: []reflect.Kind{reflect.Ptr}, Received: val}
    52  	}
    53  
    54  	if val.IsNil() {
    55  		return vw.WriteNull()
    56  	}
    57  
    58  	typ := val.Type()
    59  	if v, ok := pc.ecache.Load(typ); ok {
    60  		if v == nil {
    61  			return ErrNoEncoder{Type: typ}
    62  		}
    63  		return v.EncodeValue(ec, vw, val.Elem())
    64  	}
    65  	// TODO(charlie): handle concurrent requests for the same type
    66  	enc, err := ec.LookupEncoder(typ.Elem())
    67  	enc = pc.ecache.LoadOrStore(typ, enc)
    68  	if err != nil {
    69  		return err
    70  	}
    71  	return enc.EncodeValue(ec, vw, val.Elem())
    72  }
    73  
    74  // DecodeValue handles decoding a pointer by looking up a decoder for the type it points to and
    75  // using that to decode. If the BSON value is Null, this method will set the pointer to nil.
    76  func (pc *PointerCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
    77  	if !val.CanSet() || val.Kind() != reflect.Ptr {
    78  		return ValueDecoderError{Name: "PointerCodec.DecodeValue", Kinds: []reflect.Kind{reflect.Ptr}, Received: val}
    79  	}
    80  
    81  	typ := val.Type()
    82  	if vr.Type() == bsontype.Null {
    83  		val.Set(reflect.Zero(typ))
    84  		return vr.ReadNull()
    85  	}
    86  	if vr.Type() == bsontype.Undefined {
    87  		val.Set(reflect.Zero(typ))
    88  		return vr.ReadUndefined()
    89  	}
    90  
    91  	if val.IsNil() {
    92  		val.Set(reflect.New(typ.Elem()))
    93  	}
    94  
    95  	if v, ok := pc.dcache.Load(typ); ok {
    96  		if v == nil {
    97  			return ErrNoDecoder{Type: typ}
    98  		}
    99  		return v.DecodeValue(dc, vr, val.Elem())
   100  	}
   101  	// TODO(charlie): handle concurrent requests for the same type
   102  	dec, err := dc.LookupDecoder(typ.Elem())
   103  	dec = pc.dcache.LoadOrStore(typ, dec)
   104  	if err != nil {
   105  		return err
   106  	}
   107  	return dec.DecodeValue(dc, vr, val.Elem())
   108  }
   109  

View as plain text