Documentation ¶
Overview ¶
Package util is the junk drawer of helper functions. It should not be expanded unless necessary.
Index ¶
- Constants
- Variables
- func AnyError(err ...error) error
- func GetIP(r *http.Request) string
- func GetParamByName(r *http.Request, name string) string
- func OptionalBool(value bool) *bool
- func OptionalDuration(value time.Duration) *time.Duration
- func OptionalFloat32(value float32) *float32
- func OptionalFloat64(value float64) *float64
- func OptionalInt(value int) *int
- func OptionalInt16(value int16) *int16
- func OptionalInt32(value int32) *int32
- func OptionalInt64(value int64) *int64
- func OptionalString(value string) *string
- func OptionalTime(value time.Time) *time.Time
- func OptionalUInt(value uint) *uint
- func OptionalUInt16(value uint16) *uint16
- func OptionalUInt64(value uint64) *uint64
- func OptionalUInt8(value uint8) *uint8
- func RandomProvider() *rand.Rand
- func WriteJSON(w http.ResponseWriter, statusCode int, response interface{}) (int, error)
- type AnsiColorCode
- type Boolean
- type EncryptedStream
- type GCMEncryptionResult
- type PatchStringer
- type Patcher
- type ReadChunkHandler
- type ReadLineHandler
- type StreamDecrypter
- type StreamEncrypter
Constants ¶
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 )
const ( // FieldTagEnv is the struct tag for what environment variable to use to populate a field. FieldTagEnv = "env" // FieldFlagCSV is a field tag flag (say that 10 times fast). FieldFlagCSV = "csv" // FieldFlagBase64 is a field tag flag (say that 10 times fast). FieldFlagBase64 = "base64" // FieldFlagBytes is a field tag flag (say that 10 times fast). FieldFlagBytes = "bytes" )
PatchStrings options.
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') )
const (
// Epsilon represents the minimum amount of relevant delta we care about.
Epsilon = 0.00000001
)
const ( // ErrBooleanUnmarshal is an exception class. ErrBooleanUnmarshal = exception.Class("cannot unmarshal boolean") )
Variables ¶
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...) )
var Base64 base64Util
Base64 is a namespace singleton for (2) methods.
var (
// BitFlag is a namespace for bitflag functions.
BitFlag = bitFlag{}
)
var Coalesce coalesceUtil
Coalesce is a set of utilities for coalesced values.
var (
// Combinatorics is a namespace containing combinatoric functions.
Combinatorics = combinatorics{}
)
var Crypto = cryptoUtil{}
Crypto is a namespace for crypto related functions.
var File = &fileUtil{}
File contains helper functions for files.
var Format formatUtil
Format is a singleton namespace with utilties for formatting common values.
var GCM = gcmUtil{}
GCM is a namespace for gcm related utility functions
var (
// GZip is a namespace for gzip utilities.
GZip = gzipUtil{}
)
var (
// JSON is a namespace for json utils.
JSON = jsonUtil{}
)
var (
// Math is a namespace for math utilities.
Math = mathUtil{}
)
var (
// Parallel contains parallel utils.
Parallel = parallelUtil{}
)
var Parse = new(parseUtil)
Parse contains utilities to parse strings.
var (
// Reflection is a namespace for reflection utilities.
Reflection = reflectionUtil{}
)
var Sequence sequenceUtil
Sequence is a namespace singleton for sequence utilities.
var (
// String is a namesapce for string utility functions.
String = stringUtil{}
)
var Ternary = ternaryUtil{}
Ternary is a collection of ternary functions.
var (
// Time is a namespace for time utility functions.
Time = timeUtil{}
)
var (
// Vector is a namespace for vector functions.
Vector = vectorUtil{}
)
var (
// XML is a namespace for xml utilities.
XML = xmlUtil{}
)
Functions ¶
func GetIP ¶
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 ¶
GetParamByName returns a named parameter from either the querystring, the headers, the cookies, the form or the post form of a request.
func OptionalDuration ¶
OptionalDuration returns a pointer to a value
func OptionalFloat32 ¶
OptionalFloat32 returns a pointer to a value
func OptionalFloat64 ¶
OptionalFloat64 returns a pointer to a value
func OptionalInt16 ¶
OptionalInt16 returns a pointer to a value
func OptionalInt32 ¶
OptionalInt32 returns a pointer to a value
func OptionalInt64 ¶
OptionalInt64 returns a pointer to a value
func OptionalString ¶
OptionalString returns a pointer to a value
func OptionalTime ¶
OptionalTime returns a pointer to a value
func OptionalUInt16 ¶
OptionalUInt16 returns a pointer to a value
func OptionalUInt64 ¶
OptionalUInt64 returns a pointer to a value
func OptionalUInt8 ¶
OptionalUInt8 returns a pointer to a value
func RandomProvider ¶
RandomProvider returns a centralized random provider, seeded with time.Now().UnixNano().
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.
func (*Boolean) UnmarshalJSON ¶
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 ¶
GCMEncryptionResult is a struct for a gcm encryption result
type PatchStringer ¶
PatchStringer is a type that handles unmarshalling a map of strings into itself.
type ReadChunkHandler ¶
ReadChunkHandler is a receiver for a chunk of a file.
type ReadLineHandler ¶
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
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