github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.mongodb.org/mongo-driver/mongo/readpref/mode.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 readpref 8 9 import ( 10 "fmt" 11 "strings" 12 ) 13 14 // Mode indicates the user's preference on reads. 15 type Mode uint8 16 17 // Mode constants 18 const ( 19 _ Mode = iota 20 // PrimaryMode indicates that only a primary is 21 // considered for reading. This is the default 22 // mode. 23 PrimaryMode 24 // PrimaryPreferredMode indicates that if a primary 25 // is available, use it; otherwise, eligible 26 // secondaries will be considered. 27 PrimaryPreferredMode 28 // SecondaryMode indicates that only secondaries 29 // should be considered. 30 SecondaryMode 31 // SecondaryPreferredMode indicates that only secondaries 32 // should be considered when one is available. If none 33 // are available, then a primary will be considered. 34 SecondaryPreferredMode 35 // NearestMode indicates that all primaries and secondaries 36 // will be considered. 37 NearestMode 38 ) 39 40 // ModeFromString returns a mode corresponding to 41 // mode. 42 func ModeFromString(mode string) (Mode, error) { 43 switch strings.ToLower(mode) { 44 case "primary": 45 return PrimaryMode, nil 46 case "primarypreferred": 47 return PrimaryPreferredMode, nil 48 case "secondary": 49 return SecondaryMode, nil 50 case "secondarypreferred": 51 return SecondaryPreferredMode, nil 52 case "nearest": 53 return NearestMode, nil 54 } 55 return Mode(0), fmt.Errorf("unknown read preference %v", mode) 56 } 57 58 // String returns the string representation of mode. 59 func (mode Mode) String() string { 60 switch mode { 61 case PrimaryMode: 62 return "primary" 63 case PrimaryPreferredMode: 64 return "primaryPreferred" 65 case SecondaryMode: 66 return "secondary" 67 case SecondaryPreferredMode: 68 return "secondaryPreferred" 69 case NearestMode: 70 return "nearest" 71 default: 72 return "unknown" 73 } 74 } 75 76 // IsValid checks whether the mode is valid. 77 func (mode Mode) IsValid() bool { 78 switch mode { 79 case PrimaryMode, 80 PrimaryPreferredMode, 81 SecondaryMode, 82 SecondaryPreferredMode, 83 NearestMode: 84 return true 85 default: 86 return false 87 } 88 }