github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.mongodb.org/mongo-driver/bson/primitive_codecs.go (about) 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 "fmt" 12 "reflect" 13 14 "go.mongodb.org/mongo-driver/bson/bsoncodec" 15 "go.mongodb.org/mongo-driver/bson/bsonrw" 16 ) 17 18 var tRawValue = reflect.TypeOf(RawValue{}) 19 var tRaw = reflect.TypeOf(Raw(nil)) 20 21 var primitiveCodecs PrimitiveCodecs 22 23 // PrimitiveCodecs is a namespace for all of the default bsoncodec.Codecs for the primitive types 24 // defined in this package. 25 // 26 // Deprecated: Use bson.NewRegistry to get a registry with all primitive encoders and decoders 27 // registered. 28 type PrimitiveCodecs struct{} 29 30 // RegisterPrimitiveCodecs will register the encode and decode methods attached to PrimitiveCodecs 31 // with the provided RegistryBuilder. if rb is nil, a new empty RegistryBuilder will be created. 32 // 33 // Deprecated: Use bson.NewRegistry to get a registry with all primitive encoders and decoders 34 // registered. 35 func (pc PrimitiveCodecs) RegisterPrimitiveCodecs(rb *bsoncodec.RegistryBuilder) { 36 if rb == nil { 37 panic(errors.New("argument to RegisterPrimitiveCodecs must not be nil")) 38 } 39 40 rb. 41 RegisterTypeEncoder(tRawValue, bsoncodec.ValueEncoderFunc(pc.RawValueEncodeValue)). 42 RegisterTypeEncoder(tRaw, bsoncodec.ValueEncoderFunc(pc.RawEncodeValue)). 43 RegisterTypeDecoder(tRawValue, bsoncodec.ValueDecoderFunc(pc.RawValueDecodeValue)). 44 RegisterTypeDecoder(tRaw, bsoncodec.ValueDecoderFunc(pc.RawDecodeValue)) 45 } 46 47 // RawValueEncodeValue is the ValueEncoderFunc for RawValue. 48 // 49 // If the RawValue's Type is "invalid" and the RawValue's Value is not empty or 50 // nil, then this method will return an error. 51 // 52 // Deprecated: Use bson.NewRegistry to get a registry with all primitive 53 // encoders and decoders registered. 54 func (PrimitiveCodecs) RawValueEncodeValue(_ bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error { 55 if !val.IsValid() || val.Type() != tRawValue { 56 return bsoncodec.ValueEncoderError{ 57 Name: "RawValueEncodeValue", 58 Types: []reflect.Type{tRawValue}, 59 Received: val, 60 } 61 } 62 63 rawvalue := val.Interface().(RawValue) 64 65 if !rawvalue.Type.IsValid() { 66 return fmt.Errorf("the RawValue Type specifies an invalid BSON type: %#x", byte(rawvalue.Type)) 67 } 68 69 return bsonrw.Copier{}.CopyValueFromBytes(vw, rawvalue.Type, rawvalue.Value) 70 } 71 72 // RawValueDecodeValue is the ValueDecoderFunc for RawValue. 73 // 74 // Deprecated: Use bson.NewRegistry to get a registry with all primitive encoders and decoders 75 // registered. 76 func (PrimitiveCodecs) RawValueDecodeValue(_ bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { 77 if !val.CanSet() || val.Type() != tRawValue { 78 return bsoncodec.ValueDecoderError{Name: "RawValueDecodeValue", Types: []reflect.Type{tRawValue}, Received: val} 79 } 80 81 t, value, err := bsonrw.Copier{}.CopyValueToBytes(vr) 82 if err != nil { 83 return err 84 } 85 86 val.Set(reflect.ValueOf(RawValue{Type: t, Value: value})) 87 return nil 88 } 89 90 // RawEncodeValue is the ValueEncoderFunc for Reader. 91 // 92 // Deprecated: Use bson.NewRegistry to get a registry with all primitive encoders and decoders 93 // registered. 94 func (PrimitiveCodecs) RawEncodeValue(_ bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error { 95 if !val.IsValid() || val.Type() != tRaw { 96 return bsoncodec.ValueEncoderError{Name: "RawEncodeValue", Types: []reflect.Type{tRaw}, Received: val} 97 } 98 99 rdr := val.Interface().(Raw) 100 101 return bsonrw.Copier{}.CopyDocumentFromBytes(vw, rdr) 102 } 103 104 // RawDecodeValue is the ValueDecoderFunc for Reader. 105 // 106 // Deprecated: Use bson.NewRegistry to get a registry with all primitive encoders and decoders 107 // registered. 108 func (PrimitiveCodecs) RawDecodeValue(_ bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { 109 if !val.CanSet() || val.Type() != tRaw { 110 return bsoncodec.ValueDecoderError{Name: "RawDecodeValue", Types: []reflect.Type{tRaw}, Received: val} 111 } 112 113 if val.IsNil() { 114 val.Set(reflect.MakeSlice(val.Type(), 0, 0)) 115 } 116 117 val.SetLen(0) 118 119 rdr, err := bsonrw.Copier{}.AppendDocumentBytes(val.Interface().(Raw), vr) 120 val.Set(reflect.ValueOf(rdr)) 121 return err 122 }