podman
92 строки · 3.2 Кб
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
7package bson
8
9import (
10"errors"
11"reflect"
12
13"go.mongodb.org/mongo-driver/bson/bsoncodec"
14"go.mongodb.org/mongo-driver/bson/bsonrw"
15)
16
17var tRawValue = reflect.TypeOf(RawValue{})
18var tRaw = reflect.TypeOf(Raw(nil))
19
20var primitiveCodecs PrimitiveCodecs
21
22// PrimitiveCodecs is a namespace for all of the default bsoncodec.Codecs for the primitive types
23// defined in this package.
24type PrimitiveCodecs struct{}
25
26// RegisterPrimitiveCodecs will register the encode and decode methods attached to PrimitiveCodecs
27// with the provided RegistryBuilder. if rb is nil, a new empty RegistryBuilder will be created.
28func (pc PrimitiveCodecs) RegisterPrimitiveCodecs(rb *bsoncodec.RegistryBuilder) {
29if rb == nil {
30panic(errors.New("argument to RegisterPrimitiveCodecs must not be nil"))
31}
32
33rb.
34RegisterTypeEncoder(tRawValue, bsoncodec.ValueEncoderFunc(pc.RawValueEncodeValue)).
35RegisterTypeEncoder(tRaw, bsoncodec.ValueEncoderFunc(pc.RawEncodeValue)).
36RegisterTypeDecoder(tRawValue, bsoncodec.ValueDecoderFunc(pc.RawValueDecodeValue)).
37RegisterTypeDecoder(tRaw, bsoncodec.ValueDecoderFunc(pc.RawDecodeValue))
38}
39
40// RawValueEncodeValue is the ValueEncoderFunc for RawValue.
41func (PrimitiveCodecs) RawValueEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
42if !val.IsValid() || val.Type() != tRawValue {
43return bsoncodec.ValueEncoderError{Name: "RawValueEncodeValue", Types: []reflect.Type{tRawValue}, Received: val}
44}
45
46rawvalue := val.Interface().(RawValue)
47
48return bsonrw.Copier{}.CopyValueFromBytes(vw, rawvalue.Type, rawvalue.Value)
49}
50
51// RawValueDecodeValue is the ValueDecoderFunc for RawValue.
52func (PrimitiveCodecs) RawValueDecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
53if !val.CanSet() || val.Type() != tRawValue {
54return bsoncodec.ValueDecoderError{Name: "RawValueDecodeValue", Types: []reflect.Type{tRawValue}, Received: val}
55}
56
57t, value, err := bsonrw.Copier{}.CopyValueToBytes(vr)
58if err != nil {
59return err
60}
61
62val.Set(reflect.ValueOf(RawValue{Type: t, Value: value}))
63return nil
64}
65
66// RawEncodeValue is the ValueEncoderFunc for Reader.
67func (PrimitiveCodecs) RawEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
68if !val.IsValid() || val.Type() != tRaw {
69return bsoncodec.ValueEncoderError{Name: "RawEncodeValue", Types: []reflect.Type{tRaw}, Received: val}
70}
71
72rdr := val.Interface().(Raw)
73
74return bsonrw.Copier{}.CopyDocumentFromBytes(vw, rdr)
75}
76
77// RawDecodeValue is the ValueDecoderFunc for Reader.
78func (PrimitiveCodecs) RawDecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
79if !val.CanSet() || val.Type() != tRaw {
80return bsoncodec.ValueDecoderError{Name: "RawDecodeValue", Types: []reflect.Type{tRaw}, Received: val}
81}
82
83if val.IsNil() {
84val.Set(reflect.MakeSlice(val.Type(), 0, 0))
85}
86
87val.SetLen(0)
88
89rdr, err := bsonrw.Copier{}.AppendDocumentBytes(val.Interface().(Raw), vr)
90val.Set(reflect.ValueOf(rdr))
91return err
92}
93