github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.mongodb.org/mongo-driver/mongo/options/mongooptions.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 options
     8  
     9  import (
    10  	"fmt"
    11  	"reflect"
    12  	"strconv"
    13  
    14  	"go.mongodb.org/mongo-driver/bson"
    15  	"go.mongodb.org/mongo-driver/bson/bsoncodec"
    16  	"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
    17  )
    18  
    19  // Collation allows users to specify language-specific rules for string comparison, such as
    20  // rules for lettercase and accent marks.
    21  type Collation struct {
    22  	Locale          string `bson:",omitempty"` // The locale
    23  	CaseLevel       bool   `bson:",omitempty"` // The case level
    24  	CaseFirst       string `bson:",omitempty"` // The case ordering
    25  	Strength        int    `bson:",omitempty"` // The number of comparison levels to use
    26  	NumericOrdering bool   `bson:",omitempty"` // Whether to order numbers based on numerical order and not collation order
    27  	Alternate       string `bson:",omitempty"` // Whether spaces and punctuation are considered base characters
    28  	MaxVariable     string `bson:",omitempty"` // Which characters are affected by alternate: "shifted"
    29  	Normalization   bool   `bson:",omitempty"` // Causes text to be normalized into Unicode NFD
    30  	Backwards       bool   `bson:",omitempty"` // Causes secondary differences to be considered in reverse order, as it is done in the French language
    31  }
    32  
    33  // ToDocument converts the Collation to a bson.Raw.
    34  //
    35  // Deprecated: Marshaling a Collation to BSON will not be supported in Go Driver 2.0.
    36  func (co *Collation) ToDocument() bson.Raw {
    37  	idx, doc := bsoncore.AppendDocumentStart(nil)
    38  	if co.Locale != "" {
    39  		doc = bsoncore.AppendStringElement(doc, "locale", co.Locale)
    40  	}
    41  	if co.CaseLevel {
    42  		doc = bsoncore.AppendBooleanElement(doc, "caseLevel", true)
    43  	}
    44  	if co.CaseFirst != "" {
    45  		doc = bsoncore.AppendStringElement(doc, "caseFirst", co.CaseFirst)
    46  	}
    47  	if co.Strength != 0 {
    48  		doc = bsoncore.AppendInt32Element(doc, "strength", int32(co.Strength))
    49  	}
    50  	if co.NumericOrdering {
    51  		doc = bsoncore.AppendBooleanElement(doc, "numericOrdering", true)
    52  	}
    53  	if co.Alternate != "" {
    54  		doc = bsoncore.AppendStringElement(doc, "alternate", co.Alternate)
    55  	}
    56  	if co.MaxVariable != "" {
    57  		doc = bsoncore.AppendStringElement(doc, "maxVariable", co.MaxVariable)
    58  	}
    59  	if co.Normalization {
    60  		doc = bsoncore.AppendBooleanElement(doc, "normalization", true)
    61  	}
    62  	if co.Backwards {
    63  		doc = bsoncore.AppendBooleanElement(doc, "backwards", true)
    64  	}
    65  	doc, _ = bsoncore.AppendDocumentEnd(doc, idx)
    66  	return doc
    67  }
    68  
    69  // CursorType specifies whether a cursor should close when the last data is retrieved. See
    70  // NonTailable, Tailable, and TailableAwait.
    71  type CursorType int8
    72  
    73  const (
    74  	// NonTailable specifies that a cursor should close after retrieving the last data.
    75  	NonTailable CursorType = iota
    76  	// Tailable specifies that a cursor should not close when the last data is retrieved and can be resumed later.
    77  	Tailable
    78  	// TailableAwait specifies that a cursor should not close when the last data is retrieved and
    79  	// that it should block for a certain amount of time for new data before returning no data.
    80  	TailableAwait
    81  )
    82  
    83  // ReturnDocument specifies whether a findAndUpdate operation should return the document as it was
    84  // before the update or as it is after the update.
    85  type ReturnDocument int8
    86  
    87  const (
    88  	// Before specifies that findAndUpdate should return the document as it was before the update.
    89  	Before ReturnDocument = iota
    90  	// After specifies that findAndUpdate should return the document as it is after the update.
    91  	After
    92  )
    93  
    94  // FullDocument specifies how a change stream should return the modified document.
    95  type FullDocument string
    96  
    97  const (
    98  	// Default does not include a document copy.
    99  	Default FullDocument = "default"
   100  	// Off is the same as sending no value for fullDocumentBeforeChange.
   101  	Off FullDocument = "off"
   102  	// Required is the same as WhenAvailable but raises a server-side error if the post-image is not available.
   103  	Required FullDocument = "required"
   104  	// UpdateLookup includes a delta describing the changes to the document and a copy of the entire document that
   105  	// was changed.
   106  	UpdateLookup FullDocument = "updateLookup"
   107  	// WhenAvailable includes a post-image of the the modified document for replace and update change events
   108  	// if the post-image for this event is available.
   109  	WhenAvailable FullDocument = "whenAvailable"
   110  )
   111  
   112  // TODO(GODRIVER-2617): Once Registry is removed, ArrayFilters doesn't need to
   113  // TODO be a separate type. Remove the type and update all ArrayFilters fields
   114  // TODO to be type []interface{}.
   115  
   116  // ArrayFilters is used to hold filters for the array filters CRUD option. If a registry is nil, bson.DefaultRegistry
   117  // will be used when converting the filter interfaces to BSON.
   118  type ArrayFilters struct {
   119  	// Registry is the registry to use for converting filters. Defaults to bson.DefaultRegistry.
   120  	//
   121  	// Deprecated: Marshaling ArrayFilters to BSON will not be supported in Go Driver 2.0.
   122  	Registry *bsoncodec.Registry
   123  
   124  	Filters []interface{} // The filters to apply
   125  }
   126  
   127  // ToArray builds a []bson.Raw from the provided ArrayFilters.
   128  //
   129  // Deprecated: Marshaling ArrayFilters to BSON will not be supported in Go Driver 2.0.
   130  func (af *ArrayFilters) ToArray() ([]bson.Raw, error) {
   131  	registry := af.Registry
   132  	if registry == nil {
   133  		registry = bson.DefaultRegistry
   134  	}
   135  	filters := make([]bson.Raw, 0, len(af.Filters))
   136  	for _, f := range af.Filters {
   137  		filter, err := bson.MarshalWithRegistry(registry, f)
   138  		if err != nil {
   139  			return nil, err
   140  		}
   141  		filters = append(filters, filter)
   142  	}
   143  	return filters, nil
   144  }
   145  
   146  // ToArrayDocument builds a BSON array for the array filters CRUD option. If the registry for af is nil,
   147  // bson.DefaultRegistry will be used when converting the filter interfaces to BSON.
   148  //
   149  // Deprecated: Marshaling ArrayFilters to BSON will not be supported in Go Driver 2.0.
   150  func (af *ArrayFilters) ToArrayDocument() (bson.Raw, error) {
   151  	registry := af.Registry
   152  	if registry == nil {
   153  		registry = bson.DefaultRegistry
   154  	}
   155  
   156  	idx, arr := bsoncore.AppendArrayStart(nil)
   157  	for i, f := range af.Filters {
   158  		filter, err := bson.MarshalWithRegistry(registry, f)
   159  		if err != nil {
   160  			return nil, err
   161  		}
   162  
   163  		arr = bsoncore.AppendDocumentElement(arr, strconv.Itoa(i), filter)
   164  	}
   165  	arr, _ = bsoncore.AppendArrayEnd(arr, idx)
   166  	return arr, nil
   167  }
   168  
   169  // MarshalError is returned when attempting to transform a value into a document
   170  // results in an error.
   171  //
   172  // Deprecated: MarshalError is unused and will be removed in Go Driver 2.0.
   173  type MarshalError struct {
   174  	Value interface{}
   175  	Err   error
   176  }
   177  
   178  // Error implements the error interface.
   179  //
   180  // Deprecated: MarshalError is unused and will be removed in Go Driver 2.0.
   181  func (me MarshalError) Error() string {
   182  	return fmt.Sprintf("cannot transform type %s to a bson.Raw", reflect.TypeOf(me.Value))
   183  }