...

Source file src/go.mongodb.org/mongo-driver/bson/bsoncodec/bsoncodec_test.go

Documentation: go.mongodb.org/mongo-driver/bson/bsoncodec

     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  	"fmt"
    11  	"reflect"
    12  	"testing"
    13  	"time"
    14  
    15  	"go.mongodb.org/mongo-driver/bson/bsonrw"
    16  	"go.mongodb.org/mongo-driver/bson/bsontype"
    17  	"go.mongodb.org/mongo-driver/bson/primitive"
    18  )
    19  
    20  func ExampleValueEncoder() {
    21  	var _ ValueEncoderFunc = func(ec EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
    22  		if val.Kind() != reflect.String {
    23  			return ValueEncoderError{Name: "StringEncodeValue", Kinds: []reflect.Kind{reflect.String}, Received: val}
    24  		}
    25  
    26  		return vw.WriteString(val.String())
    27  	}
    28  }
    29  
    30  func ExampleValueDecoder() {
    31  	var _ ValueDecoderFunc = func(dc DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
    32  		if !val.CanSet() || val.Kind() != reflect.String {
    33  			return ValueDecoderError{Name: "StringDecodeValue", Kinds: []reflect.Kind{reflect.String}, Received: val}
    34  		}
    35  
    36  		if vr.Type() != bsontype.String {
    37  			return fmt.Errorf("cannot decode %v into a string type", vr.Type())
    38  		}
    39  
    40  		str, err := vr.ReadString()
    41  		if err != nil {
    42  			return err
    43  		}
    44  		val.SetString(str)
    45  		return nil
    46  	}
    47  }
    48  
    49  func noerr(t *testing.T, err error) {
    50  	if err != nil {
    51  		t.Helper()
    52  		t.Errorf("Unexpected error: (%T)%v", err, err)
    53  		t.FailNow()
    54  	}
    55  }
    56  
    57  func compareTime(t1, t2 time.Time) bool {
    58  	if t1.Location() != t2.Location() {
    59  		return false
    60  	}
    61  	return t1.Equal(t2)
    62  }
    63  
    64  func compareErrors(err1, err2 error) bool {
    65  	if err1 == nil && err2 == nil {
    66  		return true
    67  	}
    68  
    69  	if err1 == nil || err2 == nil {
    70  		return false
    71  	}
    72  
    73  	if err1.Error() != err2.Error() {
    74  		return false
    75  	}
    76  
    77  	return true
    78  }
    79  
    80  func compareDecimal128(d1, d2 primitive.Decimal128) bool {
    81  	d1H, d1L := d1.GetBytes()
    82  	d2H, d2L := d2.GetBytes()
    83  
    84  	if d1H != d2H {
    85  		return false
    86  	}
    87  
    88  	if d1L != d2L {
    89  		return false
    90  	}
    91  
    92  	return true
    93  }
    94  
    95  type noPrivateFields struct {
    96  	a string
    97  }
    98  
    99  func compareNoPrivateFields(npf1, npf2 noPrivateFields) bool {
   100  	return npf1.a != npf2.a // We don't want these to be equal
   101  }
   102  
   103  type zeroTest struct {
   104  	reportZero bool
   105  }
   106  
   107  func (z zeroTest) IsZero() bool { return z.reportZero }
   108  
   109  func compareZeroTest(_, _ zeroTest) bool { return true }
   110  
   111  type llCodec struct {
   112  	t         *testing.T
   113  	decodeval interface{}
   114  	encodeval interface{}
   115  	err       error
   116  }
   117  
   118  func (llc *llCodec) EncodeValue(_ EncodeContext, _ bsonrw.ValueWriter, i interface{}) error {
   119  	if llc.err != nil {
   120  		return llc.err
   121  	}
   122  
   123  	llc.encodeval = i
   124  	return nil
   125  }
   126  
   127  func (llc *llCodec) DecodeValue(_ DecodeContext, _ bsonrw.ValueReader, val reflect.Value) error {
   128  	if llc.err != nil {
   129  		return llc.err
   130  	}
   131  
   132  	if !reflect.TypeOf(llc.decodeval).AssignableTo(val.Type()) {
   133  		llc.t.Errorf("decodeval must be assignable to val provided to DecodeValue, but is not. decodeval %T; val %T", llc.decodeval, val)
   134  		return nil
   135  	}
   136  
   137  	val.Set(reflect.ValueOf(llc.decodeval))
   138  	return nil
   139  }
   140  

View as plain text