bat

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2020 License: Apache-2.0 Imports: 28 Imported by: 13

README

go-bat

GoDoc Go Report Card

Batteries-included repository to deliver utility functions for standard Go types.

Refer to the godoc for more information.

Contributions are welcome!

Documentation

Overview

Package bat provides

* utility functions for builtin data structures * simple data structure implementations * common helpers

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertIsDir

func AssertIsDir(dir, key string, logger Logger)

AssertIsDir calls IsDir and panics if error is returned. `key` is the log argument to name the path entity.

func AssertIsFile

func AssertIsFile(filename, key string, logger Logger)

AssertIsFile calls IsFile and panics if error is returned. `key` is the log argument to name the path entity.

func Atof64

func Atof64(s string) (float64, error)

Atof64 converts 10-based string into float64 value.

func Atof64Errp

func Atof64Errp(s string, errp errstack.Putter) float64

Atof64Errp converts 10-based string into float64 value and sets the error in the putter.

func Atoi64

func Atoi64(s string) (int64, error)

Atoi64 converts 10-based string into int64 value.

func Atoi64Errp

func Atoi64Errp(s string, errp errstack.Putter) int64

Atoi64Errp converts 10-based string into int64 value and sets the error in the putter.

func Atoui64

func Atoui64(s string) (uint64, error)

Atoui64 converts 10-based string into uint64 value.

func Atoui64Errp

func Atoui64Errp(s string, errp errstack.Putter) uint64

Atoui64Errp converts 10-based string into uint64 value and sets the error in the putter.

func CmpInt64Pairs

func CmpInt64Pairs(ls [][2]int64) int

CmpInt64Pairs compares pairs of int64 for sorting

func DecodeJSON

func DecodeJSON(r io.Reader, dest interface{}) errstack.E

DecodeJSON decodes JSON data from `r` into `dest`. `dest` has to be a pointer! If reader implements a Closer interface then we will call Close after decoding the input.

func DecodeJSONFile

func DecodeJSONFile(fname string, dest interface{}, logger errstack.Logger) errstack.E

DecodeJSONFile as `DecodeJSON` but reads data from the file.

func F64toa

func F64toa(x float64, precision ...int) string

F64toa converts float64 value to 10-based string. Function takes optional argument - precision - which is described in strconv.FormatFloat

func FindRoot

func FindRoot(callerSkipOpt ...uint) (string, errstack.E)

FindRoot returns an absolute path to the project root, defined by the existance of .git directory or .hg directory based on the caller stack trace or path to the executable file. It takes one optional argument: `callerSkip` -- the number of additional stack frames to skip above *this* function call to use in by te stack trace strategy.

func GetRawStringOfTemplate

func GetRawStringOfTemplate(t *template.Template, values map[string]interface{}) (string, error)

GetRawStringOfTemplate executes a template with given values and returns resulting string

func GzipJSON

func GzipJSON(i interface{}) ([]byte, error)

GzipJSON encodes as JSON and compress with Gzip in default compression level

func GzipJSONBest

func GzipJSONBest(i interface{}) ([]byte, error)

GzipJSONBest encodes as JSON and compress with Gzip in best compression level

func GzipJSONFast

func GzipJSONFast(i interface{}) ([]byte, error)

GzipJSONFast encodes as JSON and compress with Gzip in fastest compression level

func HashOfFile

func HashOfFile(filePath string) (string, error)

HashOfFile computes murmur3 hash of the given file via its path

func HumanizeInt

func HumanizeInt(n uint64) string

HumanizeInt produces a human readable representation of an SI size.

func I64toa

func I64toa(x int64) string

I64toa converts int64 value to 10-based string

func I64tox

func I64tox(x int64) string

I64tox converts int64 value to 16-based string

func IsDir

func IsDir(dir string) errstack.E

IsDir check if `dir` is a valid path to a directory

func IsDirErrp

func IsDirErrp(dir string, errp errstack.Putter)

IsDirErrp check if `dir` is a valid path to a directory and sets the error in the putter.

func IsFile

func IsFile(filename string) errstack.E

IsFile check if `filename` is a valid path to a file

func IsFileErrp

func IsFileErrp(filename string, errp errstack.Putter)

IsFileErrp check if `filename` is a valid path to a file and sets the error in the putter.

func Levenshtein

func Levenshtein(str1, str2 []rune) int

Levenshtein computes the Levenshtein distance. It's a measure of the similarity between two strings, which we will refer to as the source string (s) and the target string (t). The distance is the number of deletions, insertions, or substitutions required to transform s into t. For example, the Levenshtein distance between "Asheville" and "Arizona" is 8.

func MapCloneStrInterface

func MapCloneStrInterface(source map[string]interface{}) map[string]interface{}

MapCloneStrInterface will clone a given map[str]interface{} to a new one

func MapCloneStrStr

func MapCloneStrStr(source map[string]string) map[string]string

MapCloneStrStr will clone a given map[string]string{} to a new one

func MapKeysInt64Bool

func MapKeysInt64Bool(m map[int64]bool) []int64

MapKeysInt64Bool returns a sorted list of keys from a map[int64]bool

func MapKeysStrBool

func MapKeysStrBool(m map[string]bool) []string

MapKeysStrBool returns a list of keys from a map[string]bool

func MapKeysStrInterface

func MapKeysStrInterface(m map[string]interface{}) []string

MapKeysStrInterface returns a sorted list of keys from a map[string]nterface{}

func MapKeysStrStr

func MapKeysStrStr(m map[string]string) []string

MapKeysStrStr returns a sorted list of keys from map[string]string

func MapKeysStrStruct

func MapKeysStrStruct(m map[string]struct{}) []string

MapKeysStrStruct returns a sorted list of keys from a map[string]struct{}

func MapReverseStrStr

func MapReverseStrStr(m map[string]string) map[string]string

MapReverseStrStr reverses keys and values of given map If same value exists multiple times, the one with bigger key wins to give deterministic ordering and to make it testable. {"a": "1", "b": 1} -> {"1": "b"}

func MapSortedKeysStrBool

func MapSortedKeysStrBool(m map[string]bool) []string

MapSortedKeysStrBool returns a list of sorted keys from a map[string]bool

func MapStrStrGetAnyValue

func MapStrStrGetAnyValue(m map[string]string) string

MapStrStrGetAnyValue returns any values from the map or empty string if map is empty

func MapUpdateStrInterface

func MapUpdateStrInterface(source map[string]interface{}, dest *map[string]interface{})

MapUpdateStrInterface loads destination map into source map.

func MapUpdateStrStr

func MapUpdateStrStr(source map[string]string, dest *map[string]string)

MapUpdateStrStr loads destination map into source map. If dest is empty it will create a new map.

func MatchesPrefixes

func MatchesPrefixes(s string, prefixes []string) bool

MatchesPrefixes checks if given string has a prefix from given prefix list

func Min3Int

func Min3Int(a, b, c int) int

Min3Int returns the minimum of 3 integeres.

func MurmurHash added in v1.0.1

func MurmurHash(i interface{}) ([]byte, error)

MurmurHash computes murmur hash of the provided object

func MurmurHashBase64 added in v1.0.1

func MurmurHashBase64(i interface{}) (string, error)

MurmurHashBase64 computes murmur string hash of the provided object

func MurmurHashHex added in v1.0.1

func MurmurHashHex(content []byte) (string, error)

MurmurHashHex takes murmur hash and encodes hash into hex

func MurmurHashSet added in v1.0.1

func MurmurHashSet(elems ...interface{}) ([]byte, error)

MurmurHashSet computes murmur hash of the provided set of objects. Hash will have the save value regardless of objects order.

func MurmurHashSetBase64 added in v1.0.1

func MurmurHashSetBase64(elems ...interface{}) (string, error)

MurmurHashSetBase64 computes murmur string hash of the provided set of objects

func RandomASCII

func RandomASCII(length int) string

RandomASCII uses RandomStrChars to returns a new random string consisting of ASCII alphabet.

func RandomCode

func RandomCode() string

RandomCode generates a random code where random long integer is serialized into hex string

func RandomStrChars

func RandomStrChars(length int, chars []byte) string

RandomStrChars returns a new random string of the provided length, consisting of the provided byte slice of allowed characters (maximum 256). The generated result is a cryptographically secure uniform (unbiased) strings. This code is based on the Go unri package and is a subject of public domain:

http://creativecommons.org/publicdomain/zero/1.0/

func ReadFile

func ReadFile(filename string, logger errstack.Logger) ([]byte, errstack.E)

ReadFile returns the bytes content of the file

func RmDiacritics

func RmDiacritics(s string) string

RmDiacritics removes diacritics from a string. If non-alphanumeric character is encountered diacritics are removed from it. If removing diacritics is not possible, character is removed.

func SliceConcat

func SliceConcat(slices ...[]interface{}) []interface{}

SliceConcat concats all given slices into a new slice

func SliceIdx

func SliceIdx(slice []interface{}, x interface{}) int

SliceIdx returns first index of `x` in `slice` and -1 if `x` is not present.

func SliceIntIdx

func SliceIntIdx(slice []int, x int) int

SliceIntIdx returns first index of `x` in `slice` and -1 if `x` is not present.

func SliceStrAsNil

func SliceStrAsNil(ls []string) []string

SliceStrAsNil returns initialized empty slice as nil

func SliceStrConcat

func SliceStrConcat(slices ...[]string) []string

SliceStrConcat concats all given string slices into a new slice

func SliceStrIdx

func SliceStrIdx(slice []string, x string) int

SliceStrIdx returns first index of `x` in `slice` and -1 if `x` is not present.

func SliceStrSliceIdx

func SliceStrSliceIdx(slices [][]string, x []string) int

SliceStrSliceIdx returns first index of `x` in `slices` and -1 if `x` is not present.

func SliceStrToMap

func SliceStrToMap(ls []string) map[string]struct{}

SliceStrToMap creates a map from string slice for frequent search in slice

func SliceStrUniqueAppend

func SliceStrUniqueAppend(src []string, strs ...string) []string

SliceStrUniqueAppend appends `strs` strings into source without excluding elements which alread exist in `src`.

func StrIsNum

func StrIsNum(s string) bool

StrIsNum check if a string is a number (contains only digits)

func StrIsWhitespace

func StrIsWhitespace(s string) bool

StrIsWhitespace check if string contains only whitespace characters

func StrJoin

func StrJoin(sep string, args ...string) string

StrJoin joins all tail arguments using the first argument

func StrTrimMultiSpace

func StrTrimMultiSpace(s string) string

StrTrimMultiSpace replaces multiple spaces with one space and also trims space from both ends

func StrTruncateUpTo

func StrTruncateUpTo(s string, r rune, n int) string

StrTruncateUpTo cut a part of a string `s` after n occurrence of `r`

func StrsEq

func StrsEq(a, b []string) bool

StrsEq checks if two string slices has the same content

func StrsOnlyWhitespace

func StrsOnlyWhitespace(ss []string) bool

StrsOnlyWhitespace checks if input consists of only whitespace

func StrsSame

func StrsSame(a, b []string) bool

StrsSame behaves like StringsEq but order doesn't matter

func ToInterfaceSlice

func ToInterfaceSlice(i interface{}) ([]interface{}, error)

ToInterfaceSlice converts given slice into empty interface slice. Mostly for batch DB operations

func TraverseObj

func TraverseObj(v interface{}, listener func(interface{}) error, errorOnCyclic bool) error

TraverseObj traverses `v` and sends all encountered values to the listener. Traversal is stable, so values will always come in the same order. Only simple values (like `int` or `string`) will be send to listener. Error is returned only if cycle is detected in the traversed object and `errorOnCyclic` is true. Otherwise cyclic references are ignored.

func TrimSuffixes

func TrimSuffixes(s string, suffixes ...string) string

TrimSuffixes returns s without any of the provided trailing suffixes strings.

func UnsafeByteArrayToStr

func UnsafeByteArrayToStr(b []byte) string

UnsafeByteArrayToStr uses unsafe to convert byte array into string. Supplied array cannot be altered after this functions is called

func UnsafeStrToByteArray

func UnsafeStrToByteArray(s string) []byte

UnsafeStrToByteArray uses unsafe to convert string into byte array. Returned array cannot be altered after this functions is called

func UnsafeToBytes

func UnsafeToBytes(source interface{}) ([]byte, error)

UnsafeToBytes converts string or []byte to []byte

func UnsafeToString

func UnsafeToString(source interface{}) (string, error)

UnsafeToString converts string or []byte to string

Types

type Logger

type Logger interface {
	Error(msg string, ctx ...interface{})

	// Fatal is a Crit log followed by panic
	Fatal(msg string, ctx ...interface{})
}

Logger interface

type Page

type Page struct {
	From int
	To   int
}

Page represents single range

func Pages

func Pages(step int, length int) []Page

Pages splits long range into smaller pages.

type StrIterator

type StrIterator interface {
	Get() string
	Next() bool
}

StrIterator provides a generator of names / strings

Jump to

Keyboard shortcuts

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