util

package
v0.0.32 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2021 License: Apache-2.0 Imports: 31 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Length32 = 32
	Length64 = 64
)

Constants

Variables

View Source
var Big0 = new(big.Int).SetInt64(0)

Big0 represents a zero value big.Int

View Source
var EmojiCodeMap = map[string]string{}/* 4670 elements not displayed */

Mapping from character to concrete escape code.

View Source
var EmptyBytes32 = Bytes32([Length32]byte{})

EmptyBytes32 is an empty Bytes32

View Source
var EmptyBytes64 = Bytes64([Length64]byte{})

EmptyBytes64 is an empty Bytes64

Functions

func CheckEvtArgs

func CheckEvtArgs(args []interface{}) error

CheckEvtArgs checks whether the arguments of an event conforms to the expected standard where only 2 arguments are expected; Argument 0: WaitForResult Argument 1: Error Panics if standard is not met

func CloneMap

func CloneMap(src map[string]interface{}) (dst map[string]interface{})

CloneMap copies src map to a new map

func CopyMap

func CopyMap(src, dst map[string]interface{})

CopyMap copies src map to dst

func DecodeMap

func DecodeMap(srcMap interface{}, dest interface{}, tagName ...string) error

DecodeMap decodes a map to a struct with weak conversion. Default tagname is 'json'

func DecodeMapStrict

func DecodeMapStrict(srcMap interface{}, dest interface{}, tagName ...string) error

DecodeMapStrict decodes a map to a struct with strict type check. Default tagname is 'json'

func DecodeNumber

func DecodeNumber(encNum []byte) uint64

DecodeNumber deserialize a number from BigEndian

func DecodeWithJSON

func DecodeWithJSON(src interface{}, dest interface{}) error

DecodeWithJSON is like DecodeMap but it marshals src and Unmarshal to dest using encode/json.

func EncodeNumber

func EncodeNumber(n uint64) []byte

EncodeNumber serializes a number to BigEndian

func FatalOnError

func FatalOnError(err error)

FatalOnError logs a message of Fatal level if err is not nil

func FromHex

func FromHex(hexValue string) ([]byte, error)

FromHex decodes hex value to bytes. If hex value is prefixed with '0x' it is trimmed before the decode operation.

func FromHexBytes

func FromHexBytes(src []byte) []byte

FromHexBytes decodes hex source to bytes

func GetPtrAddr

func GetPtrAddr(ptrAddr interface{}) *big.Int

GetPtrAddr takes a pointer and returns the address

func HexToInt64

func HexToInt64(hexVal string) (int64, error)

HexToInt64 attempts to convert an hex string to Int64. Expects the hex string to begin with '0x'.

func HexToStr

func HexToStr(hexStr string) (string, error)

HexToStr decodes an hex string to string. Expects hexStr to begin with '0x'

func Int64ToHex

func Int64ToHex(intVal int64) string

Int64ToHex converts an Int64 value to hex string. The resulting hex is prefixed by '0x'

func IsBoolChanClosed

func IsBoolChanClosed(c chan bool) bool

IsBoolChanClosed checks whether a boolean channel is closed

func IsEmojiValid

func IsEmojiValid(short string) bool

IsEmojiValid checks whether a given emoji is valid

func IsFileOk

func IsFileOk(path string) bool

IsFileOk checks if a path exists and it is a file

func IsFuncChanClosed

func IsFuncChanClosed(c <-chan func()) bool

IsFuncChanClosed checks whether a function channel is closed

func IsGitInstalled

func IsGitInstalled(path string) (bool, string)

IsGitInstalled checks whether git executable exist in the given path. Returns true and git version if installed or false.

func IsMapOrStruct

func IsMapOrStruct(o interface{}) bool

IsMapOrStruct checks whether o is a map or a struct (pointer to struct)

func IsPathOk

func IsPathOk(path string) bool

IsPathOk checks if a path exist and whether there are no permission errors

func IsString

func IsString(v interface{}) bool

IsString checks whether the interface is a string

func IsStructChanClosed

func IsStructChanClosed(c <-chan struct{}) bool

IsStructChanClosed checks whether a struct channel is closed

func IsZeroString

func IsZeroString(str string) bool

IsZeroString returns true if str is empty or equal "0"

func MayDecodeNumber

func MayDecodeNumber(encNum []byte) (r uint64, err error)

MayDecodeNumber is like DecodeNumber but returns an error instead of panicking

func MustFromHex

func MustFromHex(hexValue string) []byte

MustFromHex is like FromHex except it panics if an error occurs

func MustToJSON

func MustToJSON(obj interface{}) string

MustToJSON converts the give obj to valid JSON. Panics if unsuccessful.

func NonZeroOrDefIn64

func NonZeroOrDefIn64(v int64, def int64) int64

NonZeroOrDefIn64 returns v if non-zero, otherwise it returns def

func NonZeroOrDefString

func NonZeroOrDefString(v string, def string) string

NonZeroOrDefString returns v if non-zero, otherwise it returns def

func Notify

func Notify(val ...interface{})

Notify displays a desktop notification

func ParseContentFrontMatter

func ParseContentFrontMatter(rdr io.Reader) (pageparser.ContentFrontMatter, error)

ParseFrontMatterContent parses a reader containing front matter + content.

func ParseExtArgs

func ParseExtArgs(extArgs map[string]string) (extsArgs map[string]map[string]string, common map[string]string)

ParseExtArgs parses an extension arguments map. It takes a map of the form: 'extName.arg = value' and returns 'extName={arg=value...arg2=value2}'

func ParseGitVersion

func ParseGitVersion(version string) (ver string)

ParseGitVersion extracts semver version from `git version` output

func ParseLogLevel

func ParseLogLevel(val string) (res map[string]logrus.Level)

ParseLogLevel parse value from --loglevel flag

func ParseVerbs

func ParseVerbs(tmp string, verbsValue map[string]interface{}) string

ParseVerbs parses a template containing verbs prefixed with %.

func PtrStrToFloat added in v0.0.30

func PtrStrToFloat(inp *string) float64

PtrStrToFloat cast string pointer to float64

func PtrStrToFloatE added in v0.0.30

func PtrStrToFloatE(inp *string) (float64, error)

PtrStrToFloatE is like PtrStrToFloat but returns error

func PtrStrToUInt64 added in v0.0.30

func PtrStrToUInt64(inp *string) uint64

PtrStrToUInt64 cast string pointer to uint64

func RESTApiErrorMsg

func RESTApiErrorMsg(msg, field string, code string) map[string]interface{}

RESTApiErrorMsg returns a message suitable for reporting REST API errors

func RandBytes

func RandBytes(n int) []byte

RandBytes gets random string of fixed length

func RandString

func RandString(n int) string

RandString is like RandBytes but returns string

func ReadFromEditor

func ReadFromEditor(editor string, stdIn io.Reader, stdOut, stdErr io.Writer) (string, error)

ReadFromEditor reads input from the specified editor program

func RemoveFlag

func RemoveFlag(args []string, flags ...string) []string

RemoveFlag takes a slice of arguments and remove specific flags

func RemoveFromStringSlice

func RemoveFromStringSlice(slice []string, str string) []string

RemoveFromStringSlice removes str from the given string slice

func SplitNamespaceDomain

func SplitNamespaceDomain(name string) (ns string, domain string, err error)

SplitNamespaceDomain splits a full namespace address into namespace and domain

func StrToDec

func StrToDec(val string) decimal.Decimal

StrToDec converts a numeric string to decimal. Panics if val could not be converted to decimal.

func StrToHex

func StrToHex(str string) string

StrToHex converts a string to hex. T The resulting hex is prefixed by '0x'

func ToByteSlice

func ToByteSlice(v []int) []byte

ToByteSlice converts a uint slice to byte slice

func ToBytes

func ToBytes(s interface{}) []byte

ToBytes returns msgpack encoded representation of s.

func ToHex

func ToHex(value []byte, noPrefix ...bool) string

ToHex encodes value to hex with a '0x' prefix only if noPrefix is unset

func ToHexBytes

func ToHexBytes(src []byte) []byte

ToHexBytes encodes source to hex bytes

func ToJSONMap

func ToJSONMap(s interface{}) (m map[string]interface{})

ToJSONMap converts s to json and then back to map. Panics if json encoding operation fails.

func ToMap

func ToMap(s interface{}, tagName ...string) map[string]interface{}

ToMap converts a struct to a map. If tagName is not provided, 'json' tag is used as a default.

func ToObject

func ToObject(bs []byte, dest interface{}) error

ToObject decodes bytes produced by ToBytes to the given dest object

func ToStringMapInter

func ToStringMapInter(m interface{}, structToMap ...bool) map[string]interface{}

ToStringMapInter converts a map to map[string]interface{}. If structToMap is true, struct element is converted to map. Panics if m is not a map with string key. Returns m if m is already a map[string]interface{}.

func VMSet

func VMSet(vm *otto.Otto, name string, value interface{}) interface{}

VMSet sets a value in the vm context only if it has not been set before.

func WriteJSON

func WriteJSON(w http.ResponseWriter, statuscode int, respObj interface{})

WriteJSON encodes respObj to json and writes it to w

func XorBytes

func XorBytes(a, b []byte) []byte

XorBytes xor a and b

Types

type BlockNonce

type BlockNonce [8]byte

BlockNonce represents a 64-bit

func EncodeNonce

func EncodeNonce(i uint64) BlockNonce

EncodeNonce converts the given integer to a block nonce.

func (BlockNonce) MarshalJSON

func (n BlockNonce) MarshalJSON() ([]byte, error)

func (BlockNonce) Uint64

func (n BlockNonce) Uint64() uint64

Uint64 returns the integer value of a block nonce.

type Bytes

type Bytes []byte

Bytes represents a byte slice

func (Bytes) Big

func (b Bytes) Big() *big.Int

Big returns the bytes as big integer

func (Bytes) Bytes

func (b Bytes) Bytes() []byte

Bytes returns a slice of bytes

func (Bytes) Equal

func (b Bytes) Equal(o Bytes) bool

Equal checks equality between h and o

func (Bytes) Hex

func (b Bytes) Hex() []byte

Hex encodes the bytes to hex

func (Bytes) HexStr

func (b Bytes) HexStr(noPrefix ...bool) string

HexStr encodes the bytes to hex. noPrefix removes '0x' prefix.

func (Bytes) IsEmpty

func (b Bytes) IsEmpty() bool

IsEmpty checks whether the object is empty (having zero values)

func (Bytes) MarshalJSON

func (b Bytes) MarshalJSON() ([]byte, error)

func (Bytes) String

func (b Bytes) String() string

type Bytes32

type Bytes32 [Length32]byte

Bytes32 represents a 32-bytes value

func BytesToBytes32

func BytesToBytes32(b []byte) Bytes32

BytesToBytes32 copies b to a Bytes32

func HexToBytes32

func HexToBytes32(hex string) (Bytes32, error)

HexToBytes32 creates an hex value to Bytes32

func StrToBytes32

func StrToBytes32(s string) Bytes32

StrToBytes32 converts a string to a Bytes32

func (Bytes32) Big

func (b Bytes32) Big() *big.Int

Big returns the bytes as big integer

func (Bytes32) Bytes

func (b Bytes32) Bytes() []byte

Bytes returns a slice of bytes

func (Bytes32) Equal

func (b Bytes32) Equal(o Bytes32) bool

Equal checks equality between h and o

func (Bytes32) Hex

func (b Bytes32) Hex() []byte

Hex encodes the bytes to hex

func (Bytes32) HexStr

func (b Bytes32) HexStr() string

HexStr encodes the bytes to hex, prefixed with 0x

func (Bytes32) IsEmpty

func (b Bytes32) IsEmpty() bool

IsEmpty checks whether the object is empty (having zero values)

func (Bytes32) MarshalJSON

func (b Bytes32) MarshalJSON() ([]byte, error)

func (Bytes32) String

func (b Bytes32) String() string

func (Bytes32) ToHexBytes

func (b Bytes32) ToHexBytes() HexBytes

ToHexBytes wraps b in HexBytes

type Bytes64

type Bytes64 [Length64]byte

Bytes64 represents a 32-bytes value

func BytesToBytes64

func BytesToBytes64(b []byte) Bytes64

BytesToBytes64 copies b to a Bytes64

func (Bytes64) Big

func (b Bytes64) Big() *big.Int

Big returns the bytes as big integer

func (Bytes64) Bytes

func (b Bytes64) Bytes() []byte

Bytes returns a slice of bytes

func (Bytes64) Equal

func (b Bytes64) Equal(o Bytes64) bool

Equal checks equality between h and o

func (Bytes64) Hex

func (b Bytes64) Hex() []byte

Hex encodes the bytes to hex

func (Bytes64) HexStr

func (b Bytes64) HexStr() string

HexStr encodes the bytes to hex, prefixed with 0x

func (Bytes64) IsEmpty

func (b Bytes64) IsEmpty() bool

IsEmpty checks whether the object is empty (having zero values)

func (Bytes64) MarshalJSON

func (b Bytes64) MarshalJSON() ([]byte, error)

func (Bytes64) String

func (b Bytes64) String() string

type Cmd

type Cmd interface {
	Run() error
	Start() error
	Wait() error
	Output() ([]byte, error)
	CombinedOutput() ([]byte, error)
	StdinPipe() (io.WriteCloser, error)
	StdoutPipe() (io.ReadCloser, error)
	StderrPipe() (io.ReadCloser, error)
	SetStderr(writer io.Writer)
	SetStdout(writer io.Writer)
	SetStdin(rdr io.Reader)
	ProcessWait() error
}

Cmd provides an interface for exec.Cmd

func NewCommand

func NewCommand(name string, args ...string) Cmd

NewCommand creates a Cmd instance

type CodecUtil

type CodecUtil struct {
	// Version is the host object version. When the host object is encoded, this
	// version is added to the encoding before any other data fields.
	Version string

	// DecodeVersion is the version of the encoded data.
	DecodedVersion string
	// contains filtered or unexported fields
}

CodecUtil provides convenient methods to serialize and deserialize objects. It is expected to be embedded in a struct to provided code utilities.

func (*CodecUtil) DecodeMulti

func (h *CodecUtil) DecodeMulti(dec Decoder, v ...interface{}) error

DecodeMulti wraps msgpack.Decoder#DecodeMulti. It skips over version information. It ignores EOF errors.

func (*CodecUtil) DecodeVersion

func (h *CodecUtil) DecodeVersion(dec Decoder) (string, error)

DecodeVersion decodes and returns the encoding's version

func (*CodecUtil) EncodeMulti

func (h *CodecUtil) EncodeMulti(enc Encoder, v ...interface{}) error

EncodeMulti is a wraps msgpack.Encoder#EncodeMulti; It normalizes fields and performs pre-serialization operations

func (*CodecUtil) ResetCodec

func (h *CodecUtil) ResetCodec()

type CommandCreator

type CommandCreator func(name string, args ...string) Cmd

CommandCreator describes a function for creating a util.Cmd instance

type Decoder

type Decoder interface {
	DecodeMulti(v ...interface{}) error
	Skip() error
}

type EditorReaderFunc

type EditorReaderFunc func(editor string, stdIn io.Reader, stdOut, stdErr io.Writer) (string, error)

EditorReaderFunc describes a function that reads input from an editor program

type Encoder

type Encoder interface {
	EncodeMulti(v ...interface{}) error
}

type HexBytes

type HexBytes []byte

HexBytes represents a slice of bytes

func StrToHexBytes

func StrToHexBytes(s string) HexBytes

StrToHexBytes converts a string to a HexBytes

func (HexBytes) Bytes

func (h HexBytes) Bytes() []byte

Bytes returns bytes

func (HexBytes) Equal

func (h HexBytes) Equal(o HexBytes) bool

Equal checks equality between h and o

func (HexBytes) IsEmpty

func (h HexBytes) IsEmpty() bool

IsEmpty checks whether h is empty

func (HexBytes) MarshalJSON

func (h HexBytes) MarshalJSON() ([]byte, error)

func (HexBytes) String

func (h HexBytes) String() string

String returns the hex encoded version of h

type Int64

type Int64 int64

Int64 wraps int64

func (*Int64) Int64

func (i *Int64) Int64() int64

Int64 casts and returns int64

func (Int64) MarshalJSON

func (i Int64) MarshalJSON() ([]byte, error)

MarshalJSON marshals for JSON

func (*Int64) Set

func (i *Int64) Set(v int64)

Set sets the value

func (*Int64) UInt64 added in v0.0.29

func (i *Int64) UInt64() uint64

UInt64 casts and returns uint64

func (*Int64) UnmarshalJSON

func (i *Int64) UnmarshalJSON(data []byte) error

type Interrupt

type Interrupt chan struct{}

Interrupt is used to signal program interruption

func (*Interrupt) Close

func (i *Interrupt) Close()

Close closes the channel only when it is not closed

func (*Interrupt) IsClosed

func (i *Interrupt) IsClosed() bool

IsClosed checks if the channel is closed

func (*Interrupt) Wait

func (i *Interrupt) Wait()

Wait blocks the calling goroutine till the channel is closed

type Map

type Map map[string]interface{}

func StructSliceToMap

func StructSliceToMap(s interface{}, tagName ...string) []Map

StructSliceToMap converts struct slice s to a map slice. If tagName is not provided, 'json' tag is used as a default.

type String

type String string

String represents a custom string

func (String) Address

func (s String) Address() identifier.Address

Address converts the String to an Address

func (String) Bytes

func (s String) Bytes() []byte

Bytes returns the bytes equivalent of the string

func (String) Decimal

func (s String) Decimal() decimal.Decimal

Decimal returns the decimal representation of the numeric string. If s is empty, '0' is assumed. Panics if string failed to be converted to decimal.

func (String) Empty

func (s String) Empty() bool

Empty returns true if the string is empty

func (String) Equal

func (s String) Equal(o String) bool

Equal check whether s and o are the same

func (String) Float

func (s String) Float() float64

Float returns the float equivalent of the numeric value. Panics if not convertible to float64

func (String) FloatE added in v0.0.30

func (s String) FloatE() (float64, error)

FloatE is like Float but returns error if unable to cast s to float64.

func (String) IsDecimal

func (s String) IsDecimal() bool

IsDecimal checks whether the string can be converted to Decimal

func (String) IsNumeric

func (s String) IsNumeric() bool

IsNumeric checks whether s is numeric

func (String) IsZero

func (s String) IsZero() bool

IsZero returns true if str is empty or equal "0"

func (String) SS

func (s String) SS() string

SS returns a short version of String() with the middle characters truncated when length is at least 32

func (String) String

func (s String) String() string

func (String) UInt64 added in v0.0.30

func (s String) UInt64() uint64

UInt64 returns the uint64 equivalent of the numeric value. Panics if not convertible to uint64

type TMAddress

type TMAddress []byte

TMAddress represents a tendermint address

func (TMAddress) MarshalJSON

func (i TMAddress) MarshalJSON() ([]byte, error)

func (*TMAddress) UnmarshalJSON

func (i *TMAddress) UnmarshalJSON(data []byte) error

type UInt64

type UInt64 uint64

UInt64 wraps uint64

func ParseUint

func ParseUint(s string, base int, bitSize int) (UInt64, error)

ParseUint is like strconv.ParseUint but returns util.UIInt64

func (*UInt64) IsZero

func (i *UInt64) IsZero() bool

IsZero checks if the value is zero

func (UInt64) MarshalJSON

func (i UInt64) MarshalJSON() ([]byte, error)

MarshalJSON marshals for JSON

func (*UInt64) Set

func (i *UInt64) Set(v uint64)

Set sets the value

func (*UInt64) UInt64

func (i *UInt64) UInt64() uint64

UInt64 casts and returns uint64

func (*UInt64) UnmarshalJSON

func (i *UInt64) UnmarshalJSON(data []byte) error

type WrappedCmd

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

WrappedCmd implements Cmd which exec.Cmd conforms to.

func NewWrappedCmd

func NewWrappedCmd(cmd *exec.Cmd) *WrappedCmd

NewWrappedCmd creates an instance of WrappedCmd

func (*WrappedCmd) CombinedOutput

func (w *WrappedCmd) CombinedOutput() ([]byte, error)

WrappedCmd implements Cmd

func (*WrappedCmd) Output

func (w *WrappedCmd) Output() ([]byte, error)

WrappedCmd implements Cmd

func (*WrappedCmd) ProcessWait

func (w *WrappedCmd) ProcessWait() error

func (*WrappedCmd) Run

func (w *WrappedCmd) Run() error

WrappedCmd implements Cmd

func (*WrappedCmd) SetStderr

func (w *WrappedCmd) SetStderr(writer io.Writer)

func (*WrappedCmd) SetStdin

func (w *WrappedCmd) SetStdin(rdr io.Reader)

func (*WrappedCmd) SetStdout

func (w *WrappedCmd) SetStdout(writer io.Writer)

func (*WrappedCmd) Start

func (w *WrappedCmd) Start() error

WrappedCmd implements Cmd

func (*WrappedCmd) StderrPipe

func (w *WrappedCmd) StderrPipe() (io.ReadCloser, error)

WrappedCmd implements Cmd

func (*WrappedCmd) StdinPipe

func (w *WrappedCmd) StdinPipe() (io.WriteCloser, error)

WrappedCmd implements Cmd

func (*WrappedCmd) StdoutPipe

func (w *WrappedCmd) StdoutPipe() (io.ReadCloser, error)

WrappedCmd implements Cmd

func (*WrappedCmd) Wait

func (w *WrappedCmd) Wait() error

WrappedCmd implements Cmd

Directories

Path Synopsis
TODO: Move this package to a more appropriate home
TODO: Move this package to a more appropriate home
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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