blob: b78ec0a1078767b17dd32fce4a0992e0f387422c [file] [log] [blame]
Don Newton379ae252019-04-01 12:17:06 -04001// 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
7package bsoncodec
8
9import (
10 "reflect"
11 "sync"
12
13 "github.com/mongodb/mongo-go-driver/bson/bsonrw"
14 "github.com/mongodb/mongo-go-driver/bson/bsontype"
15)
16
17var defaultPointerCodec = &PointerCodec{
18 ecache: make(map[reflect.Type]ValueEncoder),
19 dcache: make(map[reflect.Type]ValueDecoder),
20}
21
22var _ ValueEncoder = &PointerCodec{}
23var _ ValueDecoder = &PointerCodec{}
24
25// PointerCodec is the Codec used for pointers.
26type PointerCodec struct {
27 ecache map[reflect.Type]ValueEncoder
28 dcache map[reflect.Type]ValueDecoder
29 l sync.RWMutex
30}
31
32// NewPointerCodec returns a PointerCodec that has been initialized.
33func NewPointerCodec() *PointerCodec {
34 return &PointerCodec{
35 ecache: make(map[reflect.Type]ValueEncoder),
36 dcache: make(map[reflect.Type]ValueDecoder),
37 }
38}
39
40// EncodeValue handles encoding a pointer by either encoding it to BSON Null if the pointer is nil
41// or looking up an encoder for the type of value the pointer points to.
42func (pc *PointerCodec) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
43 if val.Kind() != reflect.Ptr {
44 if !val.IsValid() {
45 return vw.WriteNull()
46 }
47 return ValueEncoderError{Name: "PointerCodec.EncodeValue", Kinds: []reflect.Kind{reflect.Ptr}, Received: val}
48 }
49
50 if val.IsNil() {
51 return vw.WriteNull()
52 }
53
54 pc.l.RLock()
55 enc, ok := pc.ecache[val.Type()]
56 pc.l.RUnlock()
57 if ok {
58 if enc == nil {
59 return ErrNoEncoder{Type: val.Type()}
60 }
61 return enc.EncodeValue(ec, vw, val.Elem())
62 }
63
64 enc, err := ec.LookupEncoder(val.Type().Elem())
65 pc.l.Lock()
66 pc.ecache[val.Type()] = enc
67 pc.l.Unlock()
68 if err != nil {
69 return err
70 }
71
72 return enc.EncodeValue(ec, vw, val.Elem())
73}
74
75// DecodeValue handles decoding a pointer by looking up a decoder for the type it points to and
76// using that to decode. If the BSON value is Null, this method will set the pointer to nil.
77func (pc *PointerCodec) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
78 if !val.CanSet() || val.Kind() != reflect.Ptr {
79 return ValueDecoderError{Name: "PointerCodec.DecodeValue", Kinds: []reflect.Kind{reflect.Ptr}, Received: val}
80 }
81
82 if vr.Type() == bsontype.Null {
83 val.Set(reflect.Zero(val.Type()))
84 return vr.ReadNull()
85 }
86
87 if val.IsNil() {
88 val.Set(reflect.New(val.Type().Elem()))
89 }
90
91 pc.l.RLock()
92 dec, ok := pc.dcache[val.Type()]
93 pc.l.RUnlock()
94 if ok {
95 if dec == nil {
96 return ErrNoDecoder{Type: val.Type()}
97 }
98 return dec.DecodeValue(dc, vr, val.Elem())
99 }
100
101 dec, err := dc.LookupDecoder(val.Type().Elem())
102 pc.l.Lock()
103 pc.dcache[val.Type()] = dec
104 pc.l.Unlock()
105 if err != nil {
106 return err
107 }
108
109 return dec.DecodeValue(dc, vr, val.Elem())
110}