podman

Форк
0
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

7
package bson
8

9
import (
10
	"errors"
11
	"reflect"
12

13
	"go.mongodb.org/mongo-driver/bson/bsoncodec"
14
	"go.mongodb.org/mongo-driver/bson/bsonrw"
15
)
16

17
var tRawValue = reflect.TypeOf(RawValue{})
18
var tRaw = reflect.TypeOf(Raw(nil))
19

20
var primitiveCodecs PrimitiveCodecs
21

22
// PrimitiveCodecs is a namespace for all of the default bsoncodec.Codecs for the primitive types
23
// defined in this package.
24
type 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.
28
func (pc PrimitiveCodecs) RegisterPrimitiveCodecs(rb *bsoncodec.RegistryBuilder) {
29
	if rb == nil {
30
		panic(errors.New("argument to RegisterPrimitiveCodecs must not be nil"))
31
	}
32

33
	rb.
34
		RegisterTypeEncoder(tRawValue, bsoncodec.ValueEncoderFunc(pc.RawValueEncodeValue)).
35
		RegisterTypeEncoder(tRaw, bsoncodec.ValueEncoderFunc(pc.RawEncodeValue)).
36
		RegisterTypeDecoder(tRawValue, bsoncodec.ValueDecoderFunc(pc.RawValueDecodeValue)).
37
		RegisterTypeDecoder(tRaw, bsoncodec.ValueDecoderFunc(pc.RawDecodeValue))
38
}
39

40
// RawValueEncodeValue is the ValueEncoderFunc for RawValue.
41
func (PrimitiveCodecs) RawValueEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
42
	if !val.IsValid() || val.Type() != tRawValue {
43
		return bsoncodec.ValueEncoderError{Name: "RawValueEncodeValue", Types: []reflect.Type{tRawValue}, Received: val}
44
	}
45

46
	rawvalue := val.Interface().(RawValue)
47

48
	return bsonrw.Copier{}.CopyValueFromBytes(vw, rawvalue.Type, rawvalue.Value)
49
}
50

51
// RawValueDecodeValue is the ValueDecoderFunc for RawValue.
52
func (PrimitiveCodecs) RawValueDecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
53
	if !val.CanSet() || val.Type() != tRawValue {
54
		return bsoncodec.ValueDecoderError{Name: "RawValueDecodeValue", Types: []reflect.Type{tRawValue}, Received: val}
55
	}
56

57
	t, value, err := bsonrw.Copier{}.CopyValueToBytes(vr)
58
	if err != nil {
59
		return err
60
	}
61

62
	val.Set(reflect.ValueOf(RawValue{Type: t, Value: value}))
63
	return nil
64
}
65

66
// RawEncodeValue is the ValueEncoderFunc for Reader.
67
func (PrimitiveCodecs) RawEncodeValue(ec bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
68
	if !val.IsValid() || val.Type() != tRaw {
69
		return bsoncodec.ValueEncoderError{Name: "RawEncodeValue", Types: []reflect.Type{tRaw}, Received: val}
70
	}
71

72
	rdr := val.Interface().(Raw)
73

74
	return bsonrw.Copier{}.CopyDocumentFromBytes(vw, rdr)
75
}
76

77
// RawDecodeValue is the ValueDecoderFunc for Reader.
78
func (PrimitiveCodecs) RawDecodeValue(dc bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
79
	if !val.CanSet() || val.Type() != tRaw {
80
		return bsoncodec.ValueDecoderError{Name: "RawDecodeValue", Types: []reflect.Type{tRaw}, Received: val}
81
	}
82

83
	if val.IsNil() {
84
		val.Set(reflect.MakeSlice(val.Type(), 0, 0))
85
	}
86

87
	val.SetLen(0)
88

89
	rdr, err := bsonrw.Copier{}.AppendDocumentBytes(val.Interface().(Raw), vr)
90
	val.Set(reflect.ValueOf(rdr))
91
	return err
92
}
93

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.