blob: 6536b2e12ff619b1c3ca8d77cb83d3e5edbd1c8b [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 bson
8
9import (
10 "errors"
11 "reflect"
12
13 "github.com/mongodb/mongo-go-driver/bson/bsoncodec"
14 "github.com/mongodb/mongo-go-driver/bson/bsonrw"
15)
16
17var primitiveCodecs PrimitiveCodecs
18
19// PrimitiveCodecs is a namespace for all of the default bsoncodec.Codecs for the primitive types
20// defined in this package.
21type PrimitiveCodecs struct{}
22
23// RegisterPrimitiveCodecs will register the encode and decode methods attached to PrimitiveCodecs
24// with the provided RegistryBuilder. if rb is nil, a new empty RegistryBuilder will be created.
25func (pc PrimitiveCodecs) RegisterPrimitiveCodecs(rb *bsoncodec.RegistryBuilder) {
26 if rb == nil {
27 panic(errors.New("argument to RegisterPrimitiveCodecs must not be nil"))
28 }
29
30 rb.
31 RegisterEncoder(tRawValue, bsoncodec.ValueEncoderFunc(pc.RawValueEncodeValue)).
32 RegisterEncoder(tRaw, bsoncodec.ValueEncoderFunc(pc.RawEncodeValue)).
33 RegisterDecoder(tRawValue, bsoncodec.ValueDecoderFunc(pc.RawValueDecodeValue)).
34 RegisterDecoder(tRaw, bsoncodec.ValueDecoderFunc(pc.RawDecodeValue))
35}
36
37// RawValueEncodeValue is the ValueEncoderFunc for RawValue.
38func (PrimitiveCodecs) RawValueEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
39 if !val.IsValid() || val.Type() != tRawValue {
40 return bsoncodec.ValueEncoderError{Name: "RawValueEncodeValue", Types: []reflect.Type{tRawValue}, Received: val}
41 }
42
43 rawvalue := val.Interface().(RawValue)
44
45 return bsonrw.Copier{}.CopyValueFromBytes(vw, rawvalue.Type, rawvalue.Value)
46}
47
48// RawValueDecodeValue is the ValueDecoderFunc for RawValue.
49func (PrimitiveCodecs) RawValueDecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
50 if !val.CanSet() || val.Type() != tRawValue {
51 return bsoncodec.ValueDecoderError{Name: "RawValueDecodeValue", Types: []reflect.Type{tRawValue}, Received: val}
52 }
53
54 t, value, err := bsonrw.Copier{}.CopyValueToBytes(vr)
55 if err != nil {
56 return err
57 }
58
59 val.Set(reflect.ValueOf(RawValue{Type: t, Value: value}))
60 return nil
61}
62
63// RawEncodeValue is the ValueEncoderFunc for Reader.
64func (PrimitiveCodecs) RawEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
65 if !val.IsValid() || val.Type() != tRaw {
66 return bsoncodec.ValueEncoderError{Name: "RawEncodeValue", Types: []reflect.Type{tRaw}, Received: val}
67 }
68
69 rdr := val.Interface().(Raw)
70
71 return bsonrw.Copier{}.CopyDocumentFromBytes(vw, rdr)
72}
73
74// RawDecodeValue is the ValueDecoderFunc for Reader.
75func (PrimitiveCodecs) RawDecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
76 if !val.CanSet() || val.Type() != tRaw {
77 return bsoncodec.ValueDecoderError{Name: "RawDecodeValue", Types: []reflect.Type{tRaw}, Received: val}
78 }
79
80 if val.IsNil() {
81 val.Set(reflect.MakeSlice(val.Type(), 0, 0))
82 }
83
84 val.SetLen(0)
85
86 rdr, err := bsonrw.Copier{}.AppendDocumentBytes(val.Interface().(Raw), vr)
87 val.Set(reflect.ValueOf(rdr))
88 return err
89}
90
91func (pc PrimitiveCodecs) encodeRaw(ec bsoncodec.EncodeContext, dw bsonrw.DocumentWriter, raw Raw) error {
92 var copier bsonrw.Copier
93 elems, err := raw.Elements()
94 if err != nil {
95 return err
96 }
97 for _, elem := range elems {
98 dvw, err := dw.WriteDocumentElement(elem.Key())
99 if err != nil {
100 return err
101 }
102
103 val := elem.Value()
104 err = copier.CopyValueFromBytes(dvw, val.Type, val.Value)
105 if err != nil {
106 return err
107 }
108 }
109
110 return dw.WriteDocumentEnd()
111}