util

package
v0.0.0-...-9853328 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2023 License: MIT Imports: 38 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UTF8    = Charset("UTF-8")
	GB18030 = Charset("GB18030")
)
View Source
const (
	BigEndian    = true
	LittleEndian = false

	ByteSize    = 1
	BoolSize    = 1
	Int16Size   = 2 * ByteSize
	Int32Size   = 4 * ByteSize
	Int64Size   = 8 * ByteSize
	Float32Size = 4 * ByteSize
	Float64Size = 8 * ByteSize

	ByteType = iota
	BoolType
	IntType
	Int16Type
	Int32Type
	Int64Type
	Float32Type
	Float64Type
	StringType
)
View Source
const (
	NONE = iota
	UPPER
	DOWN
)

Variables

View Source
var (
	ErrArgsTooLittle = errors.New("arguments is too little")

	ErrInvalidModuleName = errors.New("invalid module name")

	ErrDumplicateJobName = errors.New("dumplicate job name")

	ErrCreateSession  = errors.New("fail to create session")
	ErrExecuteCommand = errors.New("fail to execute command")
	ErrAllocTerm      = errors.New("fail to allocate term")
	ErrAllocShell     = errors.New("fail to allocate shell")

	// net
	ErrInvalidPort      = errors.New("invalid port")
	ErrInvalidURL       = errors.New("invalid url")
	ErrInvalidMethod    = errors.New("invalid method")
	ErrBadCreateRequest = errors.New("error createing request")
	ErrBadSendRequest   = errors.New("error sending request")

	ErrNoSupportPlatform = errors.New("not support the platform")
	ErrNoSupportHardward = errors.New("not support the hardward")

	ErrInvalidPath = errors.New("invalid path")

	ErrBadCreateDir  = errors.New("fail to create dir")
	ErrBadCreateFile = errors.New("fail to create file")

	ErrInvalidFile  = errors.New("invalid file")
	ErrFileNotExist = errors.New("file not exist")
	ErrPathNotFound = errors.New("path not found")
	ErrFileExist    = errors.New("file exist")

	// yock plugin
	ErrPluginExist = errors.New("plugin exist already")
	ErrDomainExist = errors.New("domain exist already")
	ErrAliasExist  = errors.New("alias exist already")

	ErrUserNotFound = errors.New("user not found")

	ErrPermDenied = errors.New("permission denied")

	ErrOutRange = errors.New("out of range")

	ErrGeneral = errors.New("err happen")
)
View Source
var (
	JsonTrue  = NewJsonBool(true)
	JsonFalse = NewJsonBool(false)
	JsonNil   = NewJsonNull()
)
View Source
var (
	// YockBuild has two values, dev and release,
	// which correspond to two different modes.
	//
	// The value YOCK_PATH under dev is taken from main.go under the /ctl.
	// If it is in release mode, the actual location of the executable file shall prevail.
	//
	// This value will be changed by -ldflag at compile time. Details to see /auto/build.lua.
	YockBuild   = "dev"
	YockVersion = ""
)
View Source
var (
	// WorkSpace is the .yock path in the UserHome
	//
	// You can think of it as yock's global workspace
	// for storing user's information.
	WorkSpace  string
	PluginPath string
	DriverPath string
	// executable file path
	YockPath string
)

Functions

func BytesToInt32

func BytesToInt32(buf []byte) int32

func BytesToInt64

func BytesToInt64(buf []byte) int64

func CPU

func CPU() *psutilCPU

func ConvertByte2String

func ConvertByte2String(b []byte, charset Charset) string

func DecodeAESWithKey

func DecodeAESWithKey(key, str string) string

func Disk

func Disk() *psutilDisk

func EncodeAESWithKey

func EncodeAESWithKey(key, str string) string

func EqualAES

func EqualAES(key, raw, hash string) bool

func Exec

func Exec(arg ...string) ([]byte, error)

Exec automatically fit in os enviroment to execute command. windows 10+ -> powershell, others -> cmd; linux, darwin -> /bin/bash

func ExecStr

func ExecStr(args string) ([]byte, error)

ExecStr automatically split string to string arrary, then call Exec to execute

func Filename

func Filename(fullpath string) string

Filename returns the last element name of fullpath.

func FmtTimestamp

func FmtTimestamp(ts int64) time.Time

FmtTimestamp return a time string whom format is 2006-01-02

func GetPkg

func GetPkg(name string) template.FuncMap

func Host

func Host() *psutilHost

func Int32ToBytes

func Int32ToBytes(i int32) []byte

func Int64ToBytes

func Int64ToBytes(i int64) []byte

func IsExist

func IsExist(path string) bool

func IsURL

func IsURL(urlStr string) bool

IsURL determine whether urlStr is a URL

func JsonStr

func JsonStr(v JsonValue) string

JsonStr return json string according to JsonValue.

func MD5

func MD5(str string) string

MD5 return the MD5 checksum of the data

func Mem

func Mem() *psutilMem

func Mkdirs

func Mkdirs(path string) error

Mkdirs recurse to create path

func Net

func Net() *psutilNet

func NowTimestamp

func NowTimestamp() int64

NowTimestamp returm current timestamp

func NowTimestampByString

func NowTimestampByString() string

NowTimestamp returm current timestamp string

func OpenConf

func OpenConf(path string, opts ...viper.Option) (*viper.Viper, error)

OpenConfFromPath unmarshal file which located in disk to memory according to path

func PathIsExist

func PathIsExist(path string) (bool, error)

PathIsExist judge whether path exist. If exist, return true.

func Pathf

func Pathf(path string) string

Pathf to format path

@/abc => {WorkSpace}/abc (WorkSpace = UserHome + .yock)

~/abc => {YockPath}/abc (YockPath = executable file path)

func Prinf

func Prinf(opt PrintfOpt, title []string, rows [][]string)

Printf represent title and rows with tidy

func RandString

func RandString(n int) string

RandString return string of length n

func RandomPort

func RandomPort() (int, error)

RandomPort return an available port with random.

func ReadLineFromFile

func ReadLineFromFile(file string, filter func(string) string) ([]byte, error)

ReadStraemFromFile return data to be filter from specify file

func ReadLineFromString

func ReadLineFromString(str string, filter func(string) string) ([]byte, error)

ReadStraemFromFile return data to be filter from string

func ReadStraemFromFile

func ReadStraemFromFile(file string) ([]byte, error)

ReadStraemFromFile return total data from specify file

func SHA256

func SHA256(str string) string

SHA256 return the SHA256 checksum of the data

func SafeBatchMkdirs

func SafeBatchMkdirs(dirs []string) error

SafeBatchMkdirs recurse to create dirs when path isn't exist

func SafeMkdirs

func SafeMkdirs(path string) error

SafeMkdirs recurse to create path when path isn't exist

func SafeWriteFile

func SafeWriteFile(file string, data []byte) error

WriteFile write data or create file to write data according to file when file isn't exist

func SetBytesMode

func SetBytesMode(_type bool)

func SimilarText

func SimilarText(first, second string, percent *float64) int

Powered by github.com/syyongx/php2go

func Title

func Title(str string) string

func WriteFile

func WriteFile(file string, data []byte) error

WriteFile write data or create file to write data according to file

Types

type ByteWalk

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

func NewByteWalk

func NewByteWalk(buf []byte) *ByteWalk

func (*ByteWalk) Buf

func (bw *ByteWalk) Buf() []byte

func (*ByteWalk) Cursor

func (bw *ByteWalk) Cursor() int

func (*ByteWalk) IsEnd

func (bw *ByteWalk) IsEnd() bool

func (*ByteWalk) Next

func (bw *ByteWalk) Next(step int) ([]byte, error)

func (*ByteWalk) Size

func (bw *ByteWalk) Size() int

type BytesDecode

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

func NewBytesDecode

func NewBytesDecode(_type bool) BytesDecode

func (BytesDecode) BytesToFloat32

func (decode BytesDecode) BytesToFloat32(buf []byte) float32

func (BytesDecode) BytesToFloat64

func (decode BytesDecode) BytesToFloat64(buf []byte) float64

func (BytesDecode) BytesToInt16

func (decode BytesDecode) BytesToInt16(buf []byte) int16

func (BytesDecode) BytesToInt32

func (decode BytesDecode) BytesToInt32(buf []byte) int32

func (BytesDecode) BytesToInt64

func (decode BytesDecode) BytesToInt64(buf []byte) int64

type BytesEncode

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

func NewBytesEncode

func NewBytesEncode(_type bool) BytesEncode

func (BytesEncode) Float32ToBytes

func (encode BytesEncode) Float32ToBytes(f float32) []byte

func (BytesEncode) Float64ToBytes

func (encode BytesEncode) Float64ToBytes(f float64) []byte

func (BytesEncode) Int16ToBytes

func (encode BytesEncode) Int16ToBytes(i int16) []byte

func (BytesEncode) Int32ToBytes

func (encode BytesEncode) Int32ToBytes(i int32) []byte

func (BytesEncode) Int64ToBytes

func (encode BytesEncode) Int64ToBytes(i int64) []byte

type BytesTransfomer

type BytesTransfomer struct {
	BytesEncode
	BytesDecode
}
var ByteTransfomer BytesTransfomer

func (BytesTransfomer) AutoToBytes

func (transfomer BytesTransfomer) AutoToBytes(data any) []byte

func (BytesTransfomer) AutoToType

func (transfomer BytesTransfomer) AutoToType(raw []byte, _type int) any

type CSSLiteralGenerator

type CSSLiteralGenerator struct{}

func (CSSLiteralGenerator) Selectors

func (CSSLiteralGenerator) Selectors(name string, selectors ...string) string

func (CSSLiteralGenerator) Style

func (CSSLiteralGenerator) Style(name string, selectors ...string) string

type Charset

type Charset string

type CheckedVersion

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

func NewCheckedVersion

func NewCheckedVersion(version string) *CheckedVersion

func (*CheckedVersion) Compare

func (want *CheckedVersion) Compare(got *CheckedVersion) bool

type Comparable

type Comparable interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 |
		~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64 | ~string
}

type Duration

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

func ParseDuration

func ParseDuration(s string) *Duration

func (*Duration) AddHour

func (d *Duration) AddHour(i float64)

func (*Duration) AddMinute

func (d *Duration) AddMinute(i float64)

func (*Duration) AddSecond

func (d *Duration) AddSecond(i float64)

func (*Duration) Clone

func (d *Duration) Clone() *Duration

func (*Duration) Format

func (d *Duration) Format(sep string) string

func (*Duration) Hour

func (d *Duration) Hour() int

func (*Duration) Interval

func (d *Duration) Interval() string

func (*Duration) String

func (d *Duration) String() string

func (*Duration) SubHour

func (d *Duration) SubHour(i float64)

func (*Duration) SubMinute

func (d *Duration) SubMinute(i float64)

func (*Duration) SubSecond

func (d *Duration) SubSecond(i float64)

type JsonArray

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

func NewJsonArray

func NewJsonArray(v []JsonValue) *JsonArray

func (*JsonArray) Value

func (obj *JsonArray) Value() string

type JsonBool

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

func NewJsonBool

func NewJsonBool(b bool) JsonBool

func (JsonBool) Value

func (obj JsonBool) Value() string

type JsonNull

type JsonNull struct{}

func NewJsonNull

func NewJsonNull() JsonNull

func (JsonNull) Value

func (JsonNull) Value() string

type JsonNumber

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

func NewJsonNumber

func NewJsonNumber(v int64) JsonNumber

func (JsonNumber) Value

func (obj JsonNumber) Value() string

type JsonObject

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

func NewJsonObject

func NewJsonObject(v map[string]JsonValue) *JsonObject

func (*JsonObject) Value

func (obj *JsonObject) Value() string

type JsonString

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

func NewJsonString

func NewJsonString(v string) JsonString

func (JsonString) Value

func (obj JsonString) Value() string

type JsonValue

type JsonValue interface {
	Value() string
}

JsonValue is an interface to abstract object of json type.

type LoadBalancer

type LoadBalancer[T any] interface {
	// Next returns an element and its index.
	// If error, returns -1.
	Next() (T, int)
	// Up increases the probability of element to be specified
	Up(idx int)
	// Down decreases the probability of element to be specified
	Down(idx int)
	Put(e T)
	Del(idx int)
	Weights() []float64
}

type LuaLiteralGenerator

type LuaLiteralGenerator struct {
	TemplateLiteralGenerator
}

func (LuaLiteralGenerator) Eq

func (LuaLiteralGenerator) GetVar

func (LuaLiteralGenerator) GetVar(name string) string

func (LuaLiteralGenerator) IRange

func (LuaLiteralGenerator) IRange(kv, t string) string

func (LuaLiteralGenerator) Range

func (LuaLiteralGenerator) Range(kv, t string) string

func (LuaLiteralGenerator) SetVar

func (LuaLiteralGenerator) SetVar(name, value string) string

func (LuaLiteralGenerator) StrJoin

func (LuaLiteralGenerator) StrJoin(name string, value ...string) string

func (LuaLiteralGenerator) Var

func (gen LuaLiteralGenerator) Var(name, value string) string

type Platform

type Platform struct {
	OS     string
	Ver    string
	Arch   string
	Locale string
	Lang   string
}
var CurPlatform Platform

func (Platform) Exf

func (pf Platform) Exf() string

func (Platform) Script

func (pf Platform) Script() string

func (Platform) Zip

func (pf Platform) Zip() string

type Plugin

type Plugin interface {
	// Func return PluginFunc which is an abstract function to be exported dynamic library
	// according to funcName. You can use PluginFunc to call function from dynamic library.
	Func(string) (PluginFunc, error)
}

Plugin is an interface to abstract dynamic library (dll, dylib, so)

func NewPlugin

func NewPlugin(path string) (Plugin, error)

type PluginFunc

type PluginFunc interface {
	// Call return excuted result from dynamic library
	Call(...uintptr) (uintptr, error)
	// Addr returns the address of function pointer
	Addr() uintptr
}

PluginFunc is an interface to abstract function to be exported dynamic library

type PosixPlugin

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

func (*PosixPlugin) Func

func (pp *PosixPlugin) Func(name string) (PluginFunc, error)

Func return PluginFunc which is an abstract function to be exported dynamic library according to funcName. You can use PluginFunc to call function from dynamic library.

type PosixPluginFunc

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

func (*PosixPluginFunc) Addr

func (ppf *PosixPluginFunc) Addr() uintptr

Call return excuted result from dynamic library

func (*PosixPluginFunc) Call

func (ppf *PosixPluginFunc) Call(params ...uintptr) (uintptr, error)

Call return excuted result from dynamic library

type PrintfOpt

type PrintfOpt struct {
	MaxLen int
}

type SafeMap

type SafeMap[T any] struct {
	// contains filtered or unexported fields
}

SafeMap is a simple alternative version of sync.map, designed specifically for SignalStream. Provides unlocked (unsafe) and locked (safe) to operate map to meet special scenarios to improve performance.

func NewSafeMap

func NewSafeMap[T any]() *SafeMap[T]

func (*SafeMap[T]) Delete

func (m *SafeMap[T]) Delete(k string)

func (*SafeMap[T]) Get

func (m *SafeMap[T]) Get(k string) (T, bool)

Get directly obtains the value of the specified k without locking. If the value doesn't exist, the second parameter returns false, and vice versa.

func (*SafeMap[T]) Keys

func (m *SafeMap[T]) Keys() (keys []string)

func (*SafeMap[T]) Range

func (m *SafeMap[T]) Range(handle func(k string, v T) bool)

Range ranges map without locking. You can set callback to implement demand.

func (*SafeMap[T]) SafeDelete

func (m *SafeMap[T]) SafeDelete(k string)

func (*SafeMap[T]) SafeGet

func (m *SafeMap[T]) SafeGet(k string) (T, bool)

SafeGet locks to get the value of the specified k. If the value doesn't exist, the second parameter returns false, and vice versa.

func (*SafeMap[T]) SafeRange

func (m *SafeMap[T]) SafeRange(handle func(k string, v T) bool)

SafeRange locks to range map. You can set callback to implement demand.

func (*SafeMap[T]) SafeSet

func (m *SafeMap[T]) SafeSet(k string, v T)

SafeSet locks to set value of key to be specified

func (*SafeMap[T]) Set

func (m *SafeMap[T]) Set(k string, v T)

SafeSet sets value of key to be specified with locking

type Template

type Template struct {
	*template.Template
	// contains filtered or unexported fields
}

func NewTemplate

func NewTemplate(opt ...TemplateOpt) *Template

func (*Template) LazyLoadFiles

func (tmpl *Template) LazyLoadFiles() *Template

func (*Template) LazyLoadPkgs

func (tmpl *Template) LazyLoadPkgs() *Template

func (*Template) LoadFiles

func (tmpl *Template) LoadFiles(filename ...string) *Template

func (*Template) LoadPkgs

func (tmpl *Template) LoadPkgs(pkgs ...string) *Template

func (*Template) OnceParse

func (tmpl *Template) OnceParse(text string, data any) (string, error)

func (*Template) OnceParseFile

func (tmpl *Template) OnceParseFile(data any, filenames ...string) (string, error)

func (*Template) Preprocess

func (tmpl *Template) Preprocess(doc string) string

func (*Template) Run

func (tmpl *Template) Run(doc string) (string, error)

type TemplateLinker

type TemplateLinker struct {
	Pkgs  []string
	Files []string
}

func (*TemplateLinker) Load

func (ld *TemplateLinker) Load(files ...string) string

func (*TemplateLinker) Using

func (ld *TemplateLinker) Using(pkgs ...string) string

type TemplateLiteralGenerator

type TemplateLiteralGenerator struct{}

func (TemplateLiteralGenerator) Batch

func (TemplateLiteralGenerator) Batch(cmd ...string) string

func (TemplateLiteralGenerator) Block

func (TemplateLiteralGenerator) Block(name, scope, expression string) string

func (TemplateLiteralGenerator) Call

func (TemplateLiteralGenerator) Call(name string, params ...string) string

func (TemplateLiteralGenerator) Define

func (TemplateLiteralGenerator) Define(name, expression string) string

func (TemplateLiteralGenerator) Get

func (TemplateLiteralGenerator) If

func (TemplateLiteralGenerator) If(conditions []string, expressions []string) string

func (TemplateLiteralGenerator) Pipe

func (TemplateLiteralGenerator) Range

func (TemplateLiteralGenerator) Range(condition, expression string) string

func (TemplateLiteralGenerator) Ref

func (TemplateLiteralGenerator) Scope

func (TemplateLiteralGenerator) Str

func (TemplateLiteralGenerator) Template

func (TemplateLiteralGenerator) Template(name string) string

func (TemplateLiteralGenerator) Var

func (TemplateLiteralGenerator) Var(name, value string) string

func (TemplateLiteralGenerator) With

func (TemplateLiteralGenerator) With(condition, expression string) string

type TemplateOpt

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

type Time

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

func ParseTime

func ParseTime(layout, value string) *Time

func (*Time) AddDate

func (tm *Time) AddDate(years, months, days int)

func (Time) Clone

func (tm Time) Clone() *Time

func (*Time) Day

func (tm *Time) Day() int

func (*Time) Diff

func (tm *Time) Diff(t *Time) *Duration

func (*Time) DiffNow

func (tm *Time) DiffNow() *Duration

func (*Time) Format

func (tm *Time) Format(layout string) string

func (*Time) Month

func (tm *Time) Month() int

func (*Time) String

func (tm *Time) String() string

func (*Time) Year

func (tm *Time) Year() int

type Timer

type Timer struct {
}

func (*Timer) Add

func (timer *Timer) Add()

func (*Timer) Start

func (timer *Timer) Start()

type WeightedRandom

type WeightedRandom[T any] struct {
	// contains filtered or unexported fields
}

func NewWeightedRandom

func NewWeightedRandom[T any](
	elements []T,
	policy ...func(weights []float64) float64) *WeightedRandom[T]

func (*WeightedRandom[T]) Del

func (wr *WeightedRandom[T]) Del(idx int)

func (*WeightedRandom[T]) Down

func (wr *WeightedRandom[T]) Down(idx int)

Down decreases the probability of element to be specified

func (*WeightedRandom[T]) Next

func (wr *WeightedRandom[T]) Next() (T, int)

Next returns an element and its index. If error, returns -1.

func (*WeightedRandom[T]) Put

func (wr *WeightedRandom[T]) Put(e T)

func (*WeightedRandom[T]) Up

func (wr *WeightedRandom[T]) Up(idx int)

Up increases the probability of element to be specified

func (*WeightedRandom[T]) Weights

func (wr *WeightedRandom[T]) Weights() []float64

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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