util

package module
v0.0.0-...-95ed4ff Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2018 License: MIT Imports: 32 Imported by: 1

README

go-util

This is a repository full of helper functions I've written over the years to make writing in golang a little easier.

Most things have tests.

Documentation

Index

Constants

View Source
const (
	// Kilobyte represents the bytes in a kilobyte.
	Kilobyte int64 = 1 << 10
	// Megabyte represents the bytes in a megabyte.
	Megabyte int64 = Kilobyte << 10
	// Gigabyte represents the bytes in a gigabyte.
	Gigabyte int64 = Megabyte << 10
)
View Source
const (
	// StringEmpty is the empty string
	StringEmpty string = ""

	// RuneSpace is a single rune representing a space.
	RuneSpace rune = ' '

	// RuneNewline is a single rune representing a newline.
	RuneNewline rune = '\n'

	// LowerA is the ascii int value for 'a'
	LowerA uint = uint('a')
	// LowerZ is the ascii int value for 'z'
	LowerZ uint = uint('z')
)
View Source
const (

	// Epsilon represents the minimum amount of relevant delta we care about.
	Epsilon = 0.00000001
)

Variables

View Source
var (

	// LowerLetters is a runset of lowercase letters.
	LowerLetters = []rune("abcdefghijklmnopqrstuvwxyz")

	// UpperLetters is a runset of uppercase letters.
	UpperLetters = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")

	// Letters is a runset of both lower and uppercase letters.
	Letters = append(LowerLetters, UpperLetters...)

	// Numbers is a runset of numeric characters.
	Numbers = []rune("0123456789")

	// LettersAndNumbers is a runset of letters and numeric characters.
	LettersAndNumbers = append(Letters, Numbers...)

	// Symbols is a runset of symbol characters.
	Symbols = []rune(`!@#$%^&*()_+-=[]{}\|:;`)

	// LettersNumbersAndSymbols is a runset of letters, numbers and symbols.
	LettersNumbersAndSymbols = append(LettersAndNumbers, Symbols...)
)
View Source
var Base64 base64Util

Base64 is a namespace singleton for (2) methods.

View Source
var (
	// BitFlag is a namespace for bitflag functions.
	BitFlag = bitFlag{}
)
View Source
var Coalesce coalesceUtil

Coalesce is a set of utilities for coalesced values.

View Source
var (
	// Combinatorics is a namespace containing combinatoric functions.
	Combinatorics = combinatorics{}
)
View Source
var Crypto = cryptoUtil{}

Crypto is a namespace for crypto related functions.

View Source
var File = &fileUtil{}

File contains helper functions for files.

View Source
var Format formatUtil

Format is a singleton namespace with utilties for formatting common values.

View Source
var GCM = gcmUtil{}

GCM is a namespace for gcm related utility functions

View Source
var (
	// GZip is a namespace for gzip utilities.
	GZip = gzipUtil{}
)
View Source
var (
	// JSON is a namespace for json utils.
	JSON = jsonUtil{}
)
View Source
var (
	// Math is a namespace for math utilities.
	Math = mathUtil{}
)
View Source
var (
	// Parallel contains parallel utils.
	Parallel = parallelUtil{}
)
View Source
var Parse = new(parseUtil)

Parse contains utilities to parse strings.

View Source
var (
	// Reflection is a namespace for reflection utilities.
	Reflection = reflectionUtil{}
)
View Source
var Sequence sequenceUtil

Sequence is a namespace singleton for sequence utilities.

View Source
var (
	// String is a namesapce for string utility functions.
	String = stringUtil{}
)
View Source
var Ternary = ternaryUtil{}

Ternary is a collection of ternary functions.

View Source
var (
	// Time is a namespace for time utility functions.
	Time = timeUtil{}
)
View Source
var (
	// Vector is a namespace for vector functions.
	Vector = vectorUtil{}
)
View Source
var (
	// XML is a namespace for xml utilities.
	XML = xmlUtil{}
)

Functions

func AnyError

func AnyError(err ...error) error

AnyError returns any variadic error that is not nil.

func GetIP

func GetIP(r *http.Request) string

GetIP gets the origin/client ip for a request. X-FORWARDED-FOR is checked. If multiple IPs are included the first one is returned X-REAL-IP is checked. If multiple IPs are included the first one is returned Finally r.RemoteAddr is used Only benevolent services will allow access to the real IP

func GetParamByName

func GetParamByName(r *http.Request, name string) string

GetParamByName returns a named parameter from either the querystring, the headers, the cookies, the form or the post form of a request.

func OptionalBool

func OptionalBool(value bool) *bool

OptionalBool returns a pointer to a value

func OptionalDuration

func OptionalDuration(value time.Duration) *time.Duration

OptionalDuration returns a pointer to a value

func OptionalFloat32

func OptionalFloat32(value float32) *float32

OptionalFloat32 returns a pointer to a value

func OptionalFloat64

func OptionalFloat64(value float64) *float64

OptionalFloat64 returns a pointer to a value

func OptionalInt

func OptionalInt(value int) *int

OptionalInt returns a pointer to a value

func OptionalInt16

func OptionalInt16(value int16) *int16

OptionalInt16 returns a pointer to a value

func OptionalInt32

func OptionalInt32(value int32) *int32

OptionalInt32 returns a pointer to a value

func OptionalInt64

func OptionalInt64(value int64) *int64

OptionalInt64 returns a pointer to a value

func OptionalString

func OptionalString(value string) *string

OptionalString returns a pointer to a value

func OptionalTime

func OptionalTime(value time.Time) *time.Time

OptionalTime returns a pointer to a value

func OptionalUInt

func OptionalUInt(value uint) *uint

OptionalUInt returns a pointer to a value

func OptionalUInt16

func OptionalUInt16(value uint16) *uint16

OptionalUInt16 returns a pointer to a value

func OptionalUInt64

func OptionalUInt64(value uint64) *uint64

OptionalUInt64 returns a pointer to a value

func OptionalUInt8

func OptionalUInt8(value uint8) *uint8

OptionalUInt8 returns a pointer to a value

func RandomProvider

func RandomProvider() *rand.Rand

RandomProvider returns a centralized random provider, seeded with time.Now().UnixNano().

func WriteJSON

func WriteJSON(w http.ResponseWriter, statusCode int, response interface{}) (int, error)

WriteJSON writes an object to a response as json.

Types

type AnsiColorCode

type AnsiColorCode string

AnsiColorCode represents an ansi color code fragment.

const (
	// ColorBlack is the posix escape code fragment for black.
	ColorBlack AnsiColorCode = "30m"

	// ColorRed is the posix escape code fragment for red.
	ColorRed AnsiColorCode = "31m"

	// ColorGreen is the posix escape code fragment for green.
	ColorGreen AnsiColorCode = "32m"

	// ColorYellow is the posix escape code fragment for yellow.
	ColorYellow AnsiColorCode = "33m"

	// ColorBlue is the posix escape code fragment for blue.
	ColorBlue AnsiColorCode = "34m"

	// ColorPurple is the posix escape code fragement for magenta (purple)
	ColorPurple AnsiColorCode = "35m"

	// ColorCyan is the posix escape code fragement for cyan.
	ColorCyan AnsiColorCode = "36m"

	// ColorWhite is the posix escape code fragment for white.
	ColorWhite AnsiColorCode = "37m"

	// ColorLightBlack is the posix escape code fragment for black.
	ColorLightBlack AnsiColorCode = "90m"

	// ColorLightRed is the posix escape code fragment for red.
	ColorLightRed AnsiColorCode = "91m"

	// ColorLightGreen is the posix escape code fragment for green.
	ColorLightGreen AnsiColorCode = "92m"

	// ColorLightYellow is the posix escape code fragment for yellow.
	ColorLightYellow AnsiColorCode = "93m"

	// ColorLightBlue is the posix escape code fragment for blue.
	ColorLightBlue AnsiColorCode = "94m"

	// ColorLightPurple is the posix escape code fragement for magenta (purple)
	ColorLightPurple AnsiColorCode = "95m"

	// ColorLightCyan is the posix escape code fragement for cyan.
	ColorLightCyan AnsiColorCode = "96m"

	// ColorLightWhite is the posix escape code fragment for white.
	ColorLightWhite AnsiColorCode = "97m"

	// ColorGray is an alias to ColorLightWhite to preserve backwards compatibility.
	ColorGray AnsiColorCode = ColorLightBlack

	// ColorReset is the posix escape code fragment to reset all formatting.
	ColorReset AnsiColorCode = "0m"
)

func (AnsiColorCode) Apply

func (acc AnsiColorCode) Apply(text string) string

Apply returns a string with the color code applied.

type Boolean

type Boolean bool

Boolean is a type alias for bool that can be unmarshaled from 0|1, true|false etc.

var (
	// BooleanTrue represents a true value for a boolean.
	BooleanTrue Boolean = true

	// BooleanFalse represents a false value for a boolean.
	BooleanFalse Boolean = false
)

func (Boolean) AsBool

func (bit Boolean) AsBool() bool

AsBool returns the stdlib bool value for the boolean.

func (*Boolean) UnmarshalJSON

func (bit *Boolean) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the boolean from json.

type EncryptedStream

type EncryptedStream struct {
	// IV is the initial value for the crypto function
	IV []byte
	// Hash is the sha256 hmac of the stream
	Hash []byte
}

EncryptedStream is metadata about an encrypted stream

type GCMEncryptionResult

type GCMEncryptionResult struct {
	CipherText []byte
	Nonce      []byte
}

GCMEncryptionResult is a struct for a gcm encryption result

type KeyValuePairOfString

type KeyValuePairOfString struct {
	Key, Value string
}

KeyValuePairOfString is a pair of string values.

type Patchable

type Patchable interface {
	Patch(values map[string]interface{}) error
}

Patchable describes an object that can be patched with raw values.

type ReadChunkHandler

type ReadChunkHandler func(line []byte) error

ReadChunkHandler is a receiver for a chunk of a file.

type ReadLineHandler

type ReadLineHandler func(line string) error

ReadLineHandler is a receiver for a line of a file.

type StreamDecrypter

type StreamDecrypter struct {
	// contains filtered or unexported fields
}

StreamDecrypter is a decrypter for a stream of data with authentication

func NewStreamDecrypter

func NewStreamDecrypter(key []byte, meta *EncryptedStream, cipherText io.Reader) (*StreamDecrypter, error)

NewStreamDecrypter creates a new stream decrypter

func (*StreamDecrypter) Authenticate

func (s *StreamDecrypter) Authenticate() error

Authenticate verifys that the hash of the stream is correct. This should only be called after processing is finished

func (*StreamDecrypter) Read

func (s *StreamDecrypter) Read(p []byte) (int, error)

Read reads bytes from the underlying reader and then decrypts them

type StreamEncrypter

type StreamEncrypter struct {
	// contains filtered or unexported fields
}

StreamEncrypter is an encrypter for a stream of data with authentication

func NewStreamEncrypter

func NewStreamEncrypter(key []byte, plainText io.Reader) (*StreamEncrypter, error)

NewStreamEncrypter creates a new stream encrypter

func (*StreamEncrypter) Meta

func (s *StreamEncrypter) Meta() *EncryptedStream

Meta returns the encrypted stream metadata for use in decrypting. This should only be called after the stream is finished

func (*StreamEncrypter) Read

func (s *StreamEncrypter) Read(p []byte) (int, error)

Read encrypts the bytes of the inner reader and places them into p

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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