tool

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2020 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultNilTimeNano is a large number to nil proto Time
	DefaultNilTimeNano = -6795364578871345152 // =DefaultNilTime
)
View Source
const (
	// DefaultPort is unused
	DefaultPort = 6666
)

Variables

View Source
var (
	// GormDefaultCopier used for gorm
	GormDefaultCopier = &copierImpl{KeepIfFromIsNil: true, ZeroIfEqualsFrom: true, KeepIfFromIsZero: true, EachFieldAlways: true}
	// StandardCopier is a normal copier
	StandardCopier = &copierImpl{}
)

Functions

func BlankIf

func BlankIf(in, defaultValue string) (out string)

BlankIf returns 'in' if it is not blank or returns the defaultValue

func Clone

func Clone(fromValue, toValue interface{}) interface{}

Clone deep copy source to target

func CloneMap

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

CloneMap do the clone on map[string]interface{}

func CopySlice

func CopySlice(s interface{}) interface{}

CopySlice do a generic copy from slice to new slice.

func DecodeURIComponent

func DecodeURIComponent(encoded string) (string, error)

DecodeURIComponent reverts the working by EncodeURIComponent

func DecodeZigZagInt

func DecodeZigZagInt(b []byte) (r int64, ate int)

DecodeZigZagInt decodes the protobuffer ZigZag number(int8,int16,int32,int64)

func DecodeZigZagUint

func DecodeZigZagUint(b []byte) (r uint64, ate int)

DecodeZigZagUint decodes the protobuffer ZigZag number(int8,int16,int32,int64)

func EncodeURIComponent

func EncodeURIComponent(rawString string) string

EncodeURIComponent convert 'a b/c' -> 'a+b%2fc' origin := "äöüHel/lo world" encoded := EncodeURIComponent(origin) fmt.Println(encoded)

s, _ := DecodeUriCompontent(encoded) fmt.Println(s)

func FileExists

func FileExists(name string) bool

FileExists returns true if the file `name` was existed.

func FindService

func FindService(what string)

FindService not implements

what: "addr", "grpc", "health"

func GetCurrentDir

func GetCurrentDir() string

GetCurrentDir returns the current os working directory pathname.

func GetExecutableDir

func GetExecutableDir() string

GetExecutableDir returns this executable file pathname

func HostToIP

func HostToIP(host string) string

HostToIP would parse string and transform host to ip addresses.

The valid formats are:

host[:port],
scheme://host[:port]/path,

func IncGRPCListen

func IncGRPCListen(prefix string) (listen string, port int)

IncGRPCListen for grpc server

func IncPort

func IncPort() int

IncPort adds 1 to the port

func IncPortAndAddr

func IncPortAndAddr(prefix string) (addr string)

IncPortAndAddr adds 1 to the port, and return the formatted address like "10.0.0.7:8080"

func Int64SecondsToTime

func Int64SecondsToTime(utcTimeSeconds int64) (tm time.Time)

Int64SecondsToTime converts the seconds 'utcTimeSeconds' to golang Time

func Int64ToTime

func Int64ToTime(utcTime int64) (tm time.Time)

Int64ToTime converts the nanoseconds 'utcTime' to golang Time

func Int64ToTimestamp

func Int64ToTimestamp(utcTime int64) (ts *timestamp.Timestamp)

Int64ToTimestamp converts the nanoseconds 'utcTime' to protobuffer timestamp

func Intv

func Intv(s string) (v int)

Intv 忽略任何错误转换字符串为整数值并返回。如果无法转换,返回值为0

func IsCancelled

func IsCancelled(err error) (ret bool)

IsCancelled tests 'err' if it is `context.Canceled`.

func IsCancelledOrDeadline

func IsCancelledOrDeadline(err error) (ret bool)

IsCancelledOrDeadline tests 'err' if it is `context.Canceled` or `context.DeadlineExceeded`.

func IsDeadline

func IsDeadline(err error) (ret bool)

IsDeadline tests 'err' if it is `context.DeadlineExceeded`.

func IsSlice

func IsSlice(v interface{}) bool

IsSlice test the type of 'v' if it is golang Slice.

func IsV4

func IsV4(ip net.IP) bool

IsV4 test ip if it's IPv4

func IsV6

func IsV6(ip net.IP) bool

IsV6 test ip if it's IPv6

func LoadGRPCListenConfig

func LoadGRPCListenConfig(prefix string) (listen string, id string, disabled bool, port int)

LoadGRPCListenConfig loads config info of MAIN grpc service Generally, it loads a map from `prefix` (such as "server.grpc.<service-name>") and extract sub-keys: "listen", "id", "disabled"

func LoadGRPCListenSpecialConfig

func LoadGRPCListenSpecialConfig(prefix, serviceName string) (listen string, id string, disabled bool, port int)

LoadGRPCListenSpecialConfig loads config info from `prefix`.`serviceName`

func LoadHostDefinition

func LoadHostDefinition(prefix string) (addr string)

LoadHostDefinition loads the dependence info from config entry `server.deps.xxx`. LoadHostDefinition("server.deps.apply")

func LookupHostInfo

func LookupHostInfo(ipv6Prefer, ipv4Prefer bool) (ip net.IP, port int, err error)

LookupHostInfo scans the network adapters on local machine, finds all available IPs

func LookupHostInfoOld

func LookupHostInfoOld() (net.IP, int, error)

LookupHostInfoOld 依据配置文件的 server.rpc_address 尝试解释正确的rpc地址,通常是IPv4的

func Md5Sign

func Md5Sign(willSignStr string, secret string) string

func MergeMap

func MergeMap(maps ...map[string]interface{}) map[string]interface{}

MergeMap do the recursive merge on a map[string]interface{}

func MergeSlice

func MergeSlice(target interface{}, source interface{}) interface{}

MergeSlice do a merging on generic slice type.

func MyKeyRuleValidate

func MyKeyRuleValidate(cliKey string) bool

func NullIf

func NullIf(in, defaultValue interface{}) (out interface{})

NullIf return 'in' if it is not nil, or return the defaultValue if 'in' is nil.

func Pollard

func Pollard(in interface{}) interface{}

Pollard 转换一个泛型对象中的全部 map[interface{}]interface{} 子对象为 map[string]interface{} 在转换完毕之后,新的返回对象将能够正确地被json编码: text = `age: 12 name: joe` obj, err := yaml.Unmarshal(text) b, err := json.Marshal(Pollard(obj))

典型的用途在于将 golang 对象直接json输出时

func PollardArray

func PollardArray(in []interface{}) interface{}

PollardArray converts 'in' to new return object A.

func Port

func Port() int

Port returns the port

func PressAnyKeyToContinue

func PressAnyKeyToContinue(msg ...string) (input string)

PressAnyKeyToContinue prompts and waiting user's keystroke in terminal

func PressEnterToContinue

func PressEnterToContinue(msg ...string) (input string)

PressEnterToContinue prompts and waiting user's keystroke in terminal

func RandRandSeq

func RandRandSeq() string

RandRandSeq returns a random string with random length (1..127)

func RandSeq

func RandSeq(n int) string

RandSeq returns a random string with specified length 'n'

func RandSeqln

func RandSeqln(n int) string

RandSeqln returns a random string with specified length 'n', and it has a '\n' character tail.

func String

func String(length int) string

String returns a random string with length specified

func StringVariantLength

func StringVariantLength(minLength, maxLength int) string

StringVariantLength returns a random string with length specified

func StringWithCharset

func StringWithCharset(length int, charset string) string

StringWithCharset generate random string

func ThisHost

func ThisHost() (ip net.IP)

ThisHost 返回当前服务器的LAN ip,通过本机名进行反向解析

func ThisHostname

func ThisHostname() string

ThisHostname 返回本机名

func TimeToTimestamp

func TimeToTimestamp(tm time.Time) (ts *timestamp.Timestamp)

TimeToTimestamp converts the golang Time value to protobuffer timestamp

func TimestampToTime

func TimestampToTime(ts *timestamp.Timestamp) (tm time.Time)

TimestampToTime converts the protobuffer timestamp to golang Time

func URLEncodedSimple

func URLEncodedSimple(str string) (string, error)

URLEncodedSimple encodes a string like Javascript's encodeURIComponent() Example: Turns "My String" to "My%20String"

func UUid

func UUid() string

func ZeroIf

func ZeroIf(in, defaultValue uint64) (out uint64)

ZeroIf returns 'in' if it is zero (0) or returns the defaultValue

func ZeroIfAny

func ZeroIfAny(in, defaultValue interface{}) (out interface{})

ZeroIfAny returns 'in' if it is Golang zero-value or returns the defaultValue

Types

type Copier

type Copier interface {
	SetIgnoreNames(ignoreNames ...string) Copier
	SetEachFieldAlways(b bool) Copier
	Copy(toValue interface{}, fromValue interface{}, ignoreNames ...string) (err error)
}

Copier interface Copier is based on from github.com/jinzhu/copier

type DiskStatus

type DiskStatus struct {
	All  uint64 `json:"all"`
	Used uint64 `json:"used"`
	Free uint64 `json:"free"`
}

DiskStatus provides the disk usages

func DiskUsage

func DiskUsage(path string) (disk *DiskStatus)

DiskUsage is for disk usage of path/disk

type EscapeError

type EscapeError string

EscapeError is an error type

func (EscapeError) Error

func (e EscapeError) Error() string

type MapCopier

type MapCopier interface {
	SetIgnoreNames(ignoreNames ...string) MapCopier
	Copy(maps ...map[string]interface{}) (result map[string]interface{}, err error)
}

Jump to

Keyboard shortcuts

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