util

package
v0.0.1-0...-c966c28 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 22, 2020 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package util provides commonly used utility functions.

Index

Constants

View Source
const (
	ExitSuccess int = iota
	ExitFailure
)
View Source
const (
	InvalidDBChars         = "/\\. \"\x00$"
	InvalidCollectionChars = "$\x00"
	DefaultHost            = "localhost"
	DefaultPort            = "27017"
)

Variables

View Source
var (
	ErrTerminated = errors.New("received termination signal")
)

Functions

func BuildURI

func BuildURI(host, port string) string

BuildURI assembles a URI from host and port arguments, including a possible replica set name on the host part

func CreateConnectionAddrs

func CreateConnectionAddrs(host, port string) []string

Split the host string into the individual nodes to connect to, appending the port if necessary.

func EnableTCPKeepAlive

func EnableTCPKeepAlive(conn net.Conn, keepAlivePeriod time.Duration) error

EnableTCPKeepAlive enables TCP keepalive on the underlying TCP connection.

func EscapeCollectionName

func EscapeCollectionName(collName string) string

func FormatDate

func FormatDate(v string) (interface{}, error)

func GetFieldsFromFile

func GetFieldsFromFile(path string) ([]string, error)

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 MaxInt

func MaxInt(a, b int) int

Return the max of two ints

func Pluralize

func Pluralize(amount int, singular, plural string) string

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 SanitizeURI(u string) string

func ShortUsage

func ShortUsage(tool string) string

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

func SplitAndValidateNamespace(namespace string) (string, string, error)

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

func SplitHostArg(connString string) ([]string, string)

Extract the replica set name and the list of hosts from the connection string

func SplitNamespace

func SplitNamespace(namespace string) (string, string)

SplitNamespace splits a namespace path into a database and collection, returned in that order.

func StringSliceContains

func StringSliceContains(slice []string, str string) bool

StringSliceContains reports whether str is in the slice.

func StringSliceIndex

func StringSliceIndex(slice []string, str string) int

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

func TimestampGreaterThan(lhs, rhs primitive.Timestamp) bool

TimestampGreaterThan returns true if lhs comes after rhs, false otherwise.

func TimestampLessThan

func TimestampLessThan(lhs, rhs primitive.Timestamp) bool

TimestampLessThan returns true if lhs comes before rhs, false otherwise.

func ToFloat64

func ToFloat64(number interface{}) (float64, error)

ToFloat64 is a function for converting any numeric type into a float64.

func ToInt

func ToInt(number interface{}) (int, error)

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

func ToUInt32(number interface{}) (uint32, error)

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

func ToUniversalPath(path string) string

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 UnescapeCollectionName(escapedCollName string) (string, error)

func ValidateCollectionGrammar

func ValidateCollectionGrammar(collection string) error

ValidateCollectionGrammar validates the collection for character and length errors without erroring on system collections. For validation of functionality that manipulates system collections.

func ValidateCollectionName

func ValidateCollectionName(collection string) error

ValidateCollectionName validates that a string is a valid name for a mongodb collection. An error is returned if it is not valid.

func ValidateDBName

func ValidateDBName(database string) error

ValidateDBName validates that a string is a valid name for a mongodb database. An error is returned if it is not valid.

func ValidateFullNamespace

func ValidateFullNamespace(namespace string) error

ValidateFullNamespace validates a full mongodb namespace (database + collection), returning an error if it is invalid.

Types

type CloserCursor

type CloserCursor struct {
	*mongo.Cursor
}

Wrapper around Cursor to implement Closer

func (*CloserCursor) Close

func (cursor *CloserCursor) Close() error

type DeferredCloser

type DeferredCloser struct {
	io.Closer
	// contains filtered or unexported fields
}

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.

func (SetupError) Error

func (se SetupError) Error() string

Error implements the error interface.

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL