...

Source file src/go.mongodb.org/mongo-driver/mongo/integration/unified/session_options.go

Documentation: go.mongodb.org/mongo-driver/mongo/integration/unified

     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 unified
     8  
     9  import (
    10  	"fmt"
    11  	"time"
    12  
    13  	"go.mongodb.org/mongo-driver/bson"
    14  	"go.mongodb.org/mongo-driver/mongo/options"
    15  )
    16  
    17  // transactionOptions is a wrapper for *options.transactionOptions. This type implements the bson.Unmarshaler interface
    18  // to convert BSON documents to a transactionOptions instance.
    19  type transactionOptions struct {
    20  	*options.TransactionOptions
    21  }
    22  
    23  var _ bson.Unmarshaler = (*transactionOptions)(nil)
    24  
    25  func (to *transactionOptions) UnmarshalBSON(data []byte) error {
    26  	var temp struct {
    27  		RC              *readConcern           `bson:"readConcern"`
    28  		RP              *ReadPreference        `bson:"readPreference"`
    29  		WC              *writeConcern          `bson:"writeConcern"`
    30  		MaxCommitTimeMS *int64                 `bson:"maxCommitTimeMS"`
    31  		Extra           map[string]interface{} `bson:",inline"`
    32  	}
    33  	if err := bson.Unmarshal(data, &temp); err != nil {
    34  		return fmt.Errorf("error unmarshalling to temporary transactionOptions object: %v", err)
    35  	}
    36  	if len(temp.Extra) > 0 {
    37  		return fmt.Errorf("unrecognized fields for transactionOptions: %v", mapKeys(temp.Extra))
    38  	}
    39  
    40  	to.TransactionOptions = options.Transaction()
    41  	if temp.MaxCommitTimeMS != nil {
    42  		mctms := time.Duration(*temp.MaxCommitTimeMS) * time.Millisecond
    43  		to.SetMaxCommitTime(&mctms)
    44  	}
    45  	if rc := temp.RC; rc != nil {
    46  		to.SetReadConcern(rc.toReadConcernOption())
    47  	}
    48  	if rp := temp.RP; rp != nil {
    49  		converted, err := rp.ToReadPrefOption()
    50  		if err != nil {
    51  			return fmt.Errorf("error parsing read preference document: %v", err)
    52  		}
    53  		to.SetReadPreference(converted)
    54  	}
    55  	if wc := temp.WC; wc != nil {
    56  		converted, err := wc.toWriteConcernOption()
    57  		if err != nil {
    58  			return fmt.Errorf("error parsing write concern document: %v", err)
    59  		}
    60  		to.SetWriteConcern(converted)
    61  	}
    62  	return nil
    63  }
    64  
    65  // sessionOptions is a wrapper for *options.sessionOptions. This type implements the bson.Unmarshaler interface to
    66  // convert BSON documents to a sessionOptions instance.
    67  type sessionOptions struct {
    68  	*options.SessionOptions
    69  }
    70  
    71  var _ bson.Unmarshaler = (*sessionOptions)(nil)
    72  
    73  func (so *sessionOptions) UnmarshalBSON(data []byte) error {
    74  	var temp struct {
    75  		Causal          *bool                  `bson:"causalConsistency"`
    76  		MaxCommitTimeMS *int64                 `bson:"maxCommitTimeMS"`
    77  		TxnOptions      *transactionOptions    `bson:"defaultTransactionOptions"`
    78  		Snapshot        *bool                  `bson:"snapshot"`
    79  		Extra           map[string]interface{} `bson:",inline"`
    80  	}
    81  	if err := bson.Unmarshal(data, &temp); err != nil {
    82  		return fmt.Errorf("error unmarshalling to temporary sessionOptions object: %v", err)
    83  	}
    84  	if len(temp.Extra) > 0 {
    85  		return fmt.Errorf("unrecognized fields for sessionOptions: %v", mapKeys(temp.Extra))
    86  	}
    87  
    88  	so.SessionOptions = options.Session()
    89  	if temp.Causal != nil {
    90  		so.SetCausalConsistency(*temp.Causal)
    91  	}
    92  	if temp.MaxCommitTimeMS != nil {
    93  		mctms := time.Duration(*temp.MaxCommitTimeMS) * time.Millisecond
    94  		so.SetDefaultMaxCommitTime(&mctms)
    95  	}
    96  	if temp.TxnOptions != nil {
    97  		if rc := temp.TxnOptions.ReadConcern; rc != nil {
    98  			so.SetDefaultReadConcern(rc)
    99  		}
   100  		if rp := temp.TxnOptions.ReadPreference; rp != nil {
   101  			so.SetDefaultReadPreference(rp)
   102  		}
   103  		if wc := temp.TxnOptions.WriteConcern; wc != nil {
   104  			so.SetDefaultWriteConcern(wc)
   105  		}
   106  	}
   107  	if temp.Snapshot != nil {
   108  		so.SetSnapshot(*temp.Snapshot)
   109  	}
   110  	return nil
   111  }
   112  

View as plain text