Documentation ¶
Overview ¶
Package util provides commonly used utility functions.
Index ¶
- Constants
- Variables
- func BuildURI(host, port string) string
- func CreateConnectionAddrs(host, port string) []string
- func EnableTCPKeepAlive(conn net.Conn, keepAlivePeriod time.Duration) error
- func EscapeCollectionName(collName string) string
- func FormatDate(v string) (interface{}, error)
- func GetFieldsFromFile(path string) ([]string, error)
- func IsFalsy(val interface{}) bool
- func IsTruthy(val interface{}) bool
- func MaxInt(a, b int) int
- func Pluralize(amount int, singular, plural string) string
- func SanitizeURI(u string) string
- func ShortUsage(tool string) string
- func SliceContains(slice, elt interface{}) bool
- func SliceCount(slice, elt interface{}) int
- func SplitAndValidateNamespace(namespace string) (string, string, error)
- func SplitHostArg(connString string) ([]string, string)
- func SplitNamespace(namespace string) (string, string)
- func StringSliceContains(slice []string, str string) bool
- func StringSliceIndex(slice []string, str string) int
- func TimestampGreaterThan(lhs, rhs primitive.Timestamp) bool
- func TimestampLessThan(lhs, rhs primitive.Timestamp) bool
- func ToFloat64(number interface{}) (float64, error)
- func ToInt(number interface{}) (int, error)
- func ToUInt32(number interface{}) (uint32, error)
- func ToUniversalPath(path string) string
- func UnescapeCollectionName(escapedCollName string) (string, error)
- func ValidateCollectionGrammar(collection string) error
- func ValidateCollectionName(collection string) error
- func ValidateDBName(database string) error
- func ValidateFullNamespace(namespace string) error
- type CloserCursor
- type DeferredCloser
- type SetupError
- type WrappedReadCloser
- type WrappedWriteCloser
Constants ¶
const ( ExitSuccess int = iota ExitFailure )
const ( InvalidDBChars = "/\\. \"\x00$" InvalidCollectionChars = "$\x00" DefaultHost = "localhost" DefaultPort = "27017" )
Variables ¶
var (
ErrTerminated = errors.New("received termination signal")
)
Functions ¶
func BuildURI ¶
BuildURI assembles a URI from host and port arguments, including a possible replica set name on the host part
func CreateConnectionAddrs ¶
Split the host string into the individual nodes to connect to, appending the port if necessary.
func EnableTCPKeepAlive ¶
EnableTCPKeepAlive enables TCP keepalive on the underlying TCP connection.
func EscapeCollectionName ¶
func FormatDate ¶
func GetFieldsFromFile ¶
GetFieldsFromFile fetches the first line from the contents of the file at "path"
func IsFalsy ¶
func IsFalsy(val interface{}) bool
IsFalsy returns true for values the server will interpret as "false". False values include numbers == 0, false, and nil
func IsTruthy ¶
func IsTruthy(val interface{}) bool
IsTruthy returns true for values the server will interpret as "true". True values include {}, [], "", true, and any numbers != 0
func Pluralize ¶
Pluralize takes an amount and two strings denoting the singular and plural noun the amount represents. If the amount is singular, the singular form is returned; otherwise plural is returned. E.g.
Pluralize(X, "mouse", "mice") -> 0 mice, 1 mouse, 2 mice, ...
func SanitizeURI ¶
func ShortUsage ¶
func SliceContains ¶
func SliceContains(slice, elt interface{}) bool
generic function that returns true if elt is in slice. panics if slice is not of Kind reflect.Slice
func SliceCount ¶
func SliceCount(slice, elt interface{}) int
generic function that returns number of instances of 'elt' in 'slice'. panics if slice is not of Kind reflect.Slice
func SplitAndValidateNamespace ¶
SplitAndValidateNamespace splits a namespace path into a database and collection, returned in that order. An error is returned if the namespace is invalid.
func SplitHostArg ¶
Extract the replica set name and the list of hosts from the connection string
func SplitNamespace ¶
SplitNamespace splits a namespace path into a database and collection, returned in that order.
func StringSliceContains ¶
StringSliceContains reports whether str is in the slice.
func StringSliceIndex ¶
StringSliceContains returns the first index at which the given element can be found in the slice, or -1 if it is not present.
func TimestampGreaterThan ¶
TimestampGreaterThan returns true if lhs comes after rhs, false otherwise.
func TimestampLessThan ¶
TimestampLessThan returns true if lhs comes before rhs, false otherwise.
func ToInt ¶
ToInt is a function for converting any numeric type into an int. This can easily result in a loss of information due to truncation of floats.
func ToUInt32 ¶
ToUInt32 is a function for converting any numeric type into a uint32. This can easily result in a loss of information due to truncation, so be careful.
func ToUniversalPath ¶
ToUniversalPath returns the result of replacing each slash ('/') character in "path" with an OS-sepcific separator character. Multiple slashes are replaced by multiple separators
func UnescapeCollectionName ¶
func ValidateCollectionGrammar ¶
ValidateCollectionGrammar validates the collection for character and length errors without erroring on system collections. For validation of functionality that manipulates system collections.
func ValidateCollectionName ¶
ValidateCollectionName validates that a string is a valid name for a mongodb collection. An error is returned if it is not valid.
func ValidateDBName ¶
ValidateDBName validates that a string is a valid name for a mongodb database. An error is returned if it is not valid.
func ValidateFullNamespace ¶
ValidateFullNamespace validates a full mongodb namespace (database + collection), returning an error if it is invalid.
Types ¶
type CloserCursor ¶
Wrapper around Cursor to implement Closer
func (*CloserCursor) Close ¶
func (cursor *CloserCursor) Close() error
type DeferredCloser ¶
Wrapper that can capture errors that occur when closing the underlying closer.
func (*DeferredCloser) CloseWithErrorCapture ¶
func (dc *DeferredCloser) CloseWithErrorCapture(deferredErr *error)
CloseWithErrorCapture closes the wrapped Closer and sets deferredErr to the error if one occurs. It will only assign an error deferredErr's pointee is nil and if the underlying Closer has not been closed yet.
type SetupError ¶
type SetupError struct { Err error // An optional message to be logged before exiting Message string }
SetupError is the error thrown by "New" functions used to convey what error occurred and the appropriate exit code.
type WrappedReadCloser ¶
type WrappedReadCloser struct { io.ReadCloser Inner io.ReadCloser }
func (*WrappedReadCloser) Close ¶
func (wrc *WrappedReadCloser) Close() error
type WrappedWriteCloser ¶
type WrappedWriteCloser struct { io.WriteCloser Inner io.WriteCloser }
func (*WrappedWriteCloser) Close ¶
func (wwc *WrappedWriteCloser) Close() error