github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.mongodb.org/mongo-driver/bson/bsoncodec/array_codec.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 bsoncodec 8 9 import ( 10 "reflect" 11 12 "go.mongodb.org/mongo-driver/bson/bsonrw" 13 "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" 14 ) 15 16 // ArrayCodec is the Codec used for bsoncore.Array values. 17 // 18 // Deprecated: Use [go.mongodb.org/mongo-driver/bson.NewRegistry] to get a registry with the 19 // ArrayCodec registered. 20 type ArrayCodec struct{} 21 22 var defaultArrayCodec = NewArrayCodec() 23 24 // NewArrayCodec returns an ArrayCodec. 25 // 26 // Deprecated: Use [go.mongodb.org/mongo-driver/bson.NewRegistry] to get a registry with the 27 // ArrayCodec registered. 28 func NewArrayCodec() *ArrayCodec { 29 return &ArrayCodec{} 30 } 31 32 // EncodeValue is the ValueEncoder for bsoncore.Array values. 33 func (ac *ArrayCodec) EncodeValue(_ EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error { 34 if !val.IsValid() || val.Type() != tCoreArray { 35 return ValueEncoderError{Name: "CoreArrayEncodeValue", Types: []reflect.Type{tCoreArray}, Received: val} 36 } 37 38 arr := val.Interface().(bsoncore.Array) 39 return bsonrw.Copier{}.CopyArrayFromBytes(vw, arr) 40 } 41 42 // DecodeValue is the ValueDecoder for bsoncore.Array values. 43 func (ac *ArrayCodec) DecodeValue(_ DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error { 44 if !val.CanSet() || val.Type() != tCoreArray { 45 return ValueDecoderError{Name: "CoreArrayDecodeValue", Types: []reflect.Type{tCoreArray}, Received: val} 46 } 47 48 if val.IsNil() { 49 val.Set(reflect.MakeSlice(val.Type(), 0, 0)) 50 } 51 52 val.SetLen(0) 53 arr, err := bsonrw.Copier{}.AppendArrayBytes(val.Interface().(bsoncore.Array), vr) 54 val.Set(reflect.ValueOf(arr)) 55 return err 56 }