util

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2024 License: AGPL-3.0 Imports: 62 Imported by: 116

Documentation

Overview

https://ericchiang.github.io/post/go-tls/

Index

Constants

View Source
const (
	BYTE     = 1.0
	KILOBYTE = 1024 * BYTE
	MEGABYTE = 1024 * KILOBYTE
	GIGABYTE = 1024 * MEGABYTE
	TERABYTE = 1024 * GIGABYTE
)

ByteSize unit definition

View Source
const (
	B  float64 = 1
	KB float64 = 1024
	MB         = 1024 * KB
	GB         = 1024 * MB
	TB         = 1024 * GB
	PB         = 1024 * TB
)
View Source
const (
	FieldsKey = "fields"
	TagsKey   = "tags"
)

Event metadata constants. These keys are used to identify metadata stored in an event.

View Source
const (
	Verb_GET    string = "GET"
	Verb_PUT    string = "PUT"
	Verb_POST   string = "POST"
	Verb_DELETE string = "DELETE"
	Verb_HEAD   string = "HEAD"
)
View Source
const ContentTypeForm = "application/x-www-form-urlencoded;charset=UTF-8"
View Source
const ContentTypeJson = "application/json;charset=utf-8"
View Source
const ContentTypeXml = "application/xml;charset=utf-8"
View Source
const LOCAL_ADDRESS = "127.0.0.1"
View Source
const TsLayout = "2006-01-02T15:04:05.000Z"

TsLayout is the layout to be used in the timestamp marshaling/unmarshaling everywhere. The timezone must always be UTC.

Variables

View Source
var (
	// ErrKeyNotFound indicates that the specified key was not found.
	ErrKeyNotFound = errors.New("key not found")
)
View Source
var NEWLINE = byte('\n')
View Source
var PrometheusMetricReplacer = strings.NewReplacer(
	".", "_",
	"-", "_",
)

Prometheus metric name rule: [a-zA-Z_:][a-zA-Z0-9_:]* See https://prometheus.io/docs/concepts/data_model/

View Source
var SPACE = byte(' ')

Functions

func AbsInt

func AbsInt(x int) int

func AbsInt64

func AbsInt64(x int64) int64

Abs returns the absolute value of x.

func AcquireTimer

func AcquireTimer(timeout time.Duration) *time.Timer

AcquireTimer returns a time.Timer from the pool and updates it to send the current time on its channel after at least timeout.

The returned Timer may be returned to the pool with ReleaseTimer when no longer needed. This allows reducing GC load.

func AddTags

func AddTags(ms MapStr, tags []string) error

AddTags appends a tag to the tags field of ms. If the tags field does not exist then it will be created. If the tags field exists and is not a []string then an error will be returned. It does not deduplicate the list of tags.

func AddTagsWithKey

func AddTagsWithKey(ms MapStr, key string, tags []string) error

AddTagsWithKey appends a tag to the key field of ms. If the field does not exist then it will be created. If the field exists and is not a []string then an error will be returned. It does not deduplicate the list.

func AesGcmDecrypt

func AesGcmDecrypt(hexEncodedBytes, secret, hexEncodedSalt []byte) ([]byte, error)

func AesGcmEncrypt

func AesGcmEncrypt(plaintext, secret []byte) ([]byte, []byte, error)

func AtomicFileRename

func AtomicFileRename(sourceFile, targetFile string) error

func AutoGetAddress

func AutoGetAddress(addr string) string

AutoGetAddress get valid address to listen, if the specify port is not available, auto choose the next one

func ByteSize

func ByteSize(bytes uint64) string

ByteSize returns a human-readable byte string of the form 10M, 12.5K, and so forth. The following units are available:

T: Terabyte
G: Gigabyte
M: Megabyte
K: Kilobyte
B: Byte

The unit that results in the smallest number greater than or equal to 1 is always chosen.

func BytesHasPrefix

func BytesHasPrefix(data, prefix []byte) bool

func BytesHasSuffix

func BytesHasSuffix(left, right []byte) bool

func BytesSearchValue

func BytesSearchValue(data, startTerm, endTerm, searchTrim []byte) bool

func BytesToInt

func BytesToInt(buf []byte) int

func BytesToInt64

func BytesToInt64(buf []byte) int64

func BytesToUint32

func BytesToUint32(b []byte) (v uint32)

BytesToUint32 convert bytes to uint32

func BytesToUint64

func BytesToUint64(b []byte) (v uint64)

BytesToUint64 convert bytes to type uint64

func CheckIPBinding

func CheckIPBinding(ip string) (bool, error)

func CheckInstanceLock

func CheckInstanceLock(p string)

CheckInstanceLock make sure there is not a lock placed before check, and place a lock after check

func CheckProcessExists

func CheckProcessExists(pid int) bool

CheckProcessExists check if the pid is running

func ClearAllID

func ClearAllID()

func ClearInstanceLock

func ClearInstanceLock()

ClearInstanceLock remove the lock

func ClientIP

func ClientIP(r *http.Request) string

func CompareInteger

func CompareInteger(x interface{}, y interface{}) bool

func CompareStringAndBytes

func CompareStringAndBytes(b []byte, s string) bool

func ContainStr

func ContainStr(s, substr string) bool

func ContainTags

func ContainTags(tag string, tags string) bool

func ContainsAnyInAnyIntArray

func ContainsAnyInAnyIntArray(i interface{}, v []interface{}) bool

func ContainsAnyInArray

func ContainsAnyInArray(s string, v []string) bool

func ContainsInAnyInt32Array

func ContainsInAnyInt32Array(i int, v []int) bool

func ContainsInAnyIntArray

func ContainsInAnyIntArray(i int64, v []int64) bool

func ConvertBytesFromString

func ConvertBytesFromString(formatedBytes string) (float64, error)

func ConvertStringToMap

func ConvertStringToMap(str string, splitter string) (k, v string, err error)

convert a->b to a,b

func Copy

func Copy(sourceStruct interface{}, pointToTarget interface{}) (err error)

source should be a struct, target should be a pointer to the struct

func CopyFile

func CopyFile(src, dst string) (w int64, err error)

CopyFile copy file from src to dst

func CreateCert

func CreateCert(template, parent *x509.Certificate, pub interface{}, parentPriv interface{}) (
	cert *x509.Certificate, certPEM []byte, err error)

func CreateFile

func CreateFile(dir string, name string) (string, error)

CreateFile create file

func DeepCopy

func DeepCopy(value interface{}) interface{}

DeepCopy return a deep copied object

func DiffTwoObject

func DiffTwoObject(a, b interface{}) (diff.Changelog, error)

func EncodeToBytes

func EncodeToBytes(key interface{}) ([]byte, error)

func EscapeNewLine

func EscapeNewLine(input []byte) []byte

escape "\r\n" to "\\n"

func Expand

func Expand(value map[string]interface{}) map[string]interface{}

func ExpandPrefixed

func ExpandPrefixed(value map[string]interface{}, prefix string) map[string]interface{}

func ExpandPrefixedToResult

func ExpandPrefixedToResult(value map[string]interface{}, prefix string, result map[string]interface{})

func ExtractBool

func ExtractBool(unk interface{}) (bool, error)

ExtractBool extracts a bool from an unknown type.

func ExtractFieldFromBytes

func ExtractFieldFromBytes(data *[]byte, start, end []byte, removedFromValue []byte) []byte

func ExtractFieldFromBytesWitSkipBytes

func ExtractFieldFromBytesWitSkipBytes(data *[]byte, start, toBeSkipedBytes, end []byte, removedFromValue []byte) []byte

func ExtractFieldFromJson

func ExtractFieldFromJson(data *[]byte, fieldStartWith []byte, fieldEndWith []byte, leftMustContain []byte) (bool, []byte)

func ExtractFieldFromJsonOrder

func ExtractFieldFromJsonOrder(data *[]byte, fieldStartWith []byte, fieldEndWith []byte, leftMustContain []byte, reverse bool) (bool, []byte)

func ExtractFloat

func ExtractFloat(unk interface{}) (float64, error)

ExtractFloat extracts a float from an unknown type.

func ExtractInt

func ExtractInt(unk interface{}) (int64, error)

ExtractInt extracts an int from an unknown type.

func ExtractString

func ExtractString(unk interface{}) (string, error)

ExtractString extracts a string from an unknown type.

func FileAppendContentWithByte

func FileAppendContentWithByte(file string, content []byte) (int, error)

FileAppendContentWithByte append bytes to the end of the file

func FileAppendNewLine

func FileAppendNewLine(file string, content string) (int, error)

FileAppendNewLine append new line to the end of the file

func FileAppendNewLineWithByte

func FileAppendNewLineWithByte(file string, content []byte) (int, error)

FileAppendNewLineWithByte append bytes and break line(\n) to the end of the file

func FileDelete

func FileDelete(file string) error

FileDelete delete file

func FileExists

func FileExists(path string) bool

FileExists check if the path are exists

func FileExtension

func FileExtension(file string) string

FileExtension extract file extension from file name

func FileGetContent

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

FileGetContent get string from text file

func FileGetLines

func FileGetLines(filePath string) []string

func FileLinesWalk

func FileLinesWalk(filePath string, f func([]byte)) error

func FileMTime

func FileMTime(file string) (int64, error)

FileMTime get file modified time

func FilePutContent

func FilePutContent(file string, content string) (int, error)

FilePutContent put string to file

func FilePutContentWithByte

func FilePutContentWithByte(file string, content []byte) (int, error)

FilePutContentWithByte put string to file

func FileSize

func FileSize(file string) (int64, error)

FileSize get file size as how many bytes

func FilesExists

func FilesExists(path ...string) bool

func FilterDirName

func FilterDirName(name string) bool

FilterDirName guess the file or directory is or contains Go source files.

func FilterSpecialChar

func FilterSpecialChar(keyword string) string

func Flatten

func Flatten(value interface{}, ignoreNil bool) map[string]interface{}

func FlattenJSON

func FlattenJSON(value interface{}, ignoreNil bool) (m map[string]interface{})

func FlattenJSONString

func FlattenJSONString(nestedstr, prefix string, ignoreNil bool) (string, error)

func FlattenPrefixed

func FlattenPrefixed(value interface{}, prefix string, ignoreNil bool) map[string]interface{}

func FlattenPrefixedToResult

func FlattenPrefixedToResult(value interface{}, prefix string, m map[string]interface{}, ignoreNil bool)

func FlattenPrefixedToResultWithTagAsFieldName

func FlattenPrefixedToResultWithTagAsFieldName(value interface{}, prefix string, m map[string]interface{}, tag string, ignoreNil bool)

func FormatBytes

func FormatBytes(bytes float64, precision int) string

func FormatNumber

func FormatNumber(input float64) string

func FormatTime

func FormatTime(date time.Time) string

func FormatTimeForFileName

func FormatTimeForFileName(date time.Time) string

func FormatTimeWithLocalTZ

func FormatTimeWithLocalTZ(date time.Time) string

func FormatTimeWithTZ

func FormatTimeWithTZ(date time.Time) string

func FormatUnixTimestamp

func FormatUnixTimestamp(unix int64) string

func FromJSONBytes

func FromJSONBytes(b []byte, v interface{}) (err error)

func FromJson

func FromJson(str string, to interface{}) error

func FromUnixTimestamp

func FromUnixTimestamp(unix int64) time.Time

func FromUnixTimestampInMicro

func FromUnixTimestampInMicro(unix int64) time.Time

func FromUnixTimestampInMilli

func FromUnixTimestampInMilli(unix int64) time.Time

func GenRsaKey

func GenRsaKey() (prvkey, pubkey []byte)

RSA公钥私钥产生

func GenerateRandomString

func GenerateRandomString(cnum int) string

func GenerateServerCert

func GenerateServerCert(rootCert *x509.Certificate, rootKey *rsa.PrivateKey, rootCertPEM []byte, dnsNames []string) (servCertPEM, servKeyPEM []byte, err error)

func GetAddress

func GetAddress(adr string) *net.TCPAddr

func GetAvailablePort

func GetAvailablePort(ip string, port int) int

GetAvailablePort get valid port to listen, if the specify port is not available, auto choose the next one

func GetBytes

func GetBytes(key interface{}) []byte

func GetCertTemplate

func GetCertTemplate(dnsNames []string) (*x509.Certificate, error)

func GetCertTemplateWithSingleDomain

func GetCertTemplateWithSingleDomain(dnsNames string) (*x509.Certificate, error)

helper function to create a cert template with a serial number and other required fields

func GetClientCert

func GetClientCert(rootCert *x509.Certificate, rootKey any) (clientTLSCert tls.Certificate, clientCertPEM, clientKeyPEM []byte)

func GetDurationOrDefault

func GetDurationOrDefault(str string, defaultV time.Duration) time.Duration

func GetFieldAndTags

func GetFieldAndTags(any interface{}, tags []string) []map[string]string

return field and tags, field name is using key: NAME

func GetFieldValueByTagName

func GetFieldValueByTagName(any interface{}, tagName string, tagValue string) string

GetFieldValueByTagName returns the field value which field was tagged with the specified tagName, only supporting string fields.

func GetHost

func GetHost(url string) string

GetHost return the host from a url

func GetHostName

func GetHostName() string

func GetIPPrefix

func GetIPPrefix(ip string) string

func GetIncrementID

func GetIncrementID(bucket string) uint32

func GetIncrementID64

func GetIncrementID64(bucket string) uint64

func GetInt64Value

func GetInt64Value(any interface{}) int64

func GetIntMapKeys

func GetIntMapKeys(m map[int]int) []int

func GetIntranetIP

func GetIntranetIP() (string, error)

func GetLocalIPs

func GetLocalIPs() []string

GetLocalIP returns the non loopback local IP of the host

func GetLocalZone

func GetLocalZone() string

GetLocalZone return a local timezone

func GetLowPrecisionCurrentTime

func GetLowPrecisionCurrentTime() time.Time

func GetMapKeys

func GetMapKeys(m map[string]KV) []string

func GetMapValueByKeys

func GetMapValueByKeys(keys []string, m map[string]interface{}) (interface{}, bool)

func GetPublishNetworkDeviceInfo

func GetPublishNetworkDeviceInfo(pattern string) (dev string, ip string, mask string, err error)

func GetRootCert

func GetRootCert() (rootCert *x509.Certificate, rootKey *rsa.PrivateKey, rootCertPEM []byte)

func GetRootUrl

func GetRootUrl(source *uri.URL) string

GetRootUrl parse to get url root

func GetRuntimeErrorMessage

func GetRuntimeErrorMessage(r runtime.Error) string

func GetSafetyInternalAddress

func GetSafetyInternalAddress(addr string) string

func GetSkipHostnameVerifyFunc

func GetSkipHostnameVerifyFunc(pool *x509.CertPool) func([][]byte, [][]*x509.Certificate) error

func GetSplitFunc

func GetSplitFunc(split []byte) func(data []byte, atEOF bool) (advance int, token []byte, err error)

func GetStringIntMapKeys

func GetStringIntMapKeys(m map[string]int) []string

func GetSyncMapSize

func GetSyncMapSize(m *sync.Map) int

func GetTypeAndPackageName

func GetTypeAndPackageName(any interface{}, lowercase bool) (string, string)

func GetTypeName

func GetTypeName(any interface{}, lowercase bool) string

func GetUUID

func GetUUID() string

GetUUID return a generated uuid

func GetValidAddress

func GetValidAddress(addr string) string

GetValidAddress get valid address, input: :8001 -> output: 127.0.0.1:8001

func GetValueByKeys

func GetValueByKeys(keys []string, m MapStr) (interface{}, bool)

func GzipCompress

func GzipCompress(a *[]byte, level int) []byte

gzip.BestCompression

func GzipDecompress

func GzipDecompress(data *[]byte) ([]byte, error)

func HasSudoPermission

func HasSudoPermission() bool

func InsertBytesAfterField

func InsertBytesAfterField(data *[]byte, start, toBeSkipedBytes, end []byte, bytesToInsert []byte) []byte

func Int64ToBytes

func Int64ToBytes(i int64) []byte

func Int64ToString

func Int64ToString(num int64) string

func IntToBytes

func IntToBytes(n int) []byte

func IntToString

func IntToString(num int) string

func InterfaceToInt

func InterfaceToInt(y interface{}) int

func Invoke

func Invoke(any interface{}, name string, args ...interface{})

Invoke dynamic execute function via function name and parameters

func Ipv4MaskString

func Ipv4MaskString(m []byte) string

func IsBytesEndingWith

func IsBytesEndingWith(data *[]byte, ending []byte) bool

func IsBytesEndingWithOrder

func IsBytesEndingWithOrder(data *[]byte, ending []byte, reverse bool) bool

func IsExist

func IsExist(path string) bool

IsExist returns whether a file or directory exists.

func IsFile

func IsFile(file string) bool

IsFile returns false when it's a directory or does not exist.

func IsFileWithinFolder

func IsFileWithinFolder(file, path string) bool

func IsLocalAddress

func IsLocalAddress(address []string, localIPs []string) bool

IsLocalAddress check if the address is local address

func IsPublicIP

func IsPublicIP(IP net.IP) bool

func IsRootUser

func IsRootUser() bool

func IsValidEmail

func IsValidEmail(email string) bool

func IsValidTLD

func IsValidTLD(tld string) bool

IsValidTLD returns true if tld is a valid tld, eg: com

func JoinArray

func JoinArray(array []string, delimiter string) string

func JoinInterfaceArray

func JoinInterfaceArray(array []interface{}, delimiter string, valueFunc func(str string) string) string

func JoinMap

func JoinMap(array map[string]interface{}, delimiter string) string

func JoinMapInt

func JoinMapInt(array map[string]int, delimiter string) string

func JoinMapString

func JoinMapString(array map[string]string, delimiter string) string

func JoinPath

func JoinPath(filenames ...string) string

JoinPath return joined file path

func LimitedBytesSearch

func LimitedBytesSearch(data []byte, term []byte, limit int) bool

func ListAllFiles

func ListAllFiles(path string) ([]string, error)

func MD5digest

func MD5digest(str string) string

func MD5digestBytes

func MD5digestBytes(b []byte) [16]byte

func MD5digestString

func MD5digestString(b []byte) string

func MapLength

func MapLength(m *sync.Map) int

func MaxDuration

func MaxDuration(d1 time.Duration, d2 time.Duration) time.Duration

func MaxInt64

func MaxInt64(x int64, y int64) int64

func MergeFields

func MergeFields(ms, fields MapStr, underRoot bool) error

MergeFields merges the top-level keys and values in each source map (it does not perform a deep merge). If the same key exists in both, the value in fields takes precedence. If underRoot is true then the contents of the fields MapStr is merged with the value of the 'fields' key in ms.

An error is returned if underRoot is true and the value of ms.fields is not a MapStr.

func MergeSpace

func MergeSpace(in string) (out string)

Removes all unnecessary whitespaces

func MinDuration

func MinDuration(d1 time.Duration, d2 time.Duration) time.Duration

func MinInt64

func MinInt64(x int64, y int64) int64

func Mod

func Mod(data, max int) int

func ModString

func ModString(data string, max int) int

func MustFromJSONBytes

func MustFromJSONBytes(b []byte, v interface{})

MustFromJSONBytes simply do json unmarshal

func MustToJSON

func MustToJSON(v interface{}) string

func MustToJSONBytes

func MustToJSONBytes(v interface{}) []byte

func NearestThousandFormat

func NearestThousandFormat(num float64) string
fmt.Println(num, " = ", NearestThousandFormat(float64(num)))

10 = 10 100 = 100 1000 = 1.0k 10000 = 10.0k 3.12345678912e+09 = 3.1b 999.4 = 999 -372712 = -372.7k -37271922 = -37.2m -198 = -198

func NoWordBreak

func NoWordBreak(in string) string

func NumberFormat

func NumberFormat(number float64, decimals int, decPoint, thousandsSep string) string

func ParseDuration

func ParseDuration(s string) (time.Duration, error)

func ParsePrivateKey

func ParsePrivateKey(der []byte) (any, error)

func ParseStandardTime

func ParseStandardTime(value string) (time.Time, error)

func ParseTime

func ParseTime(str string) time.Time

func PickRandomCnLegendName

func PickRandomCnLegendName() string

func PickRandomCnName

func PickRandomCnName() string

func PickRandomName

func PickRandomName() string

func PickRandomNameByDict

func PickRandomNameByDict(str string) string

func PickRandomNumber

func PickRandomNumber(seed int) int

func PidExists

func PidExists(pid int32) (bool, error)

func Post

func Post(url string, values url.Values) []byte

Post http post with a list of values

func PrefixAnyInArray

func PrefixAnyInArray(s string, v []string) bool

func PrefixStr

func PrefixStr(s, substr string) bool

func PrintStringByteLines

func PrintStringByteLines(array [][]byte) string

func ProcessJsonData

func ProcessJsonData(data *[]byte, blockSplit []byte, limitBlockSize int, validBlockMustContain [][]byte, reverse bool, matchBlockStartWith []byte, matchBlocksEndWith []byte, maxSpan int, matchedBlockProcessHandler func(matchedData []byte, globalStartOffset, globalEndOffset int)) bool

[]byte("gte"), []byte("strict_date_optional_time"), []byte("range") blockSplit 按照那个部分来进行划分多个区块; validBlockMustContain 区块必须包含的关键字 reverse 是从区块的前面还是后面进行处理 processBlockStartWithBlock 从什么地方开始处理 processBlockEndWithBlock 从什么地方结束处理 maxSpan 开始和结束位置的最大间隔,约束范围 匹配位置的处理函数

func PromptYesNo

func PromptYesNo(prompt string, defaultAnswer bool) bool

PromptYesNo Returns true if the user has entered Y or YES, capitalization is ignored, we are matching elasticsearch behavior

func RandomBytes

func RandomBytes(length int) ([]byte, error)

func ReadInput

func ReadInput() (string, error)

ReadInput Capture user input for a question

func RegexPatternMatch

func RegexPatternMatch(pattern, value string) bool

func ReleaseTimer

func ReleaseTimer(t *time.Timer)

ReleaseTimer returns the time.Timer acquired via AcquireTimer to the pool and prevents the Timer from firing.

Do not access the released time.Timer or read from it's channel otherwise data races may occur.

func RemoveSpaces

func RemoveSpaces(str string) string

func Rename

func Rename(file string, to string) error

Rename handle file rename

func ReplaceByte

func ReplaceByte(str []byte, old, new []byte) []byte

TODO optimize performance ReplaceByte simply replace old bytes to new bytes, the two bytes should have same length

func RestorePersistID

func RestorePersistID(path string)

RestorePersistID will take the snapshot and restore to id seeds

func Round

func Round(num float64) int

func RoundInt

func RoundInt(input float64) int

func RsaDecrypt

func RsaDecrypt(ciphertext, keyBytes []byte) []byte

私钥解密

func RsaEncrypt

func RsaEncrypt(data, keyBytes []byte) []byte

公钥加密

func RsaSignWithSha256

func RsaSignWithSha256(data []byte, keyBytes []byte) []byte

签名

func RsaVerySignWithSha256

func RsaVerySignWithSha256(data, signData, keyBytes []byte) bool

验证

func SetupTimeNowRefresh

func SetupTimeNowRefresh()

func Sha1Hash

func Sha1Hash(str string) string

func Since

func Since(t1 time.Time) time.Duration

func SnapshotPersistID

func SnapshotPersistID()

SnapshotPersistID will make a snapshot and persist id stats to disk

func StringArrayIntersection

func StringArrayIntersection(arr1 []string, arr2 []string) []string

func StringDefault

func StringDefault(val, defaultV string) string

func StringInArray

func StringInArray(s []string, element string) bool

func StringToFloat

func StringToFloat(s string) (float64, error)

StringToFloat converts a string to a float64 Returns an error if the conversion is not possible

func StringToUTF16

func StringToUTF16(s string) []uint16

func StripCtlAndExtFromBytes

func StripCtlAndExtFromBytes(str string) string

func StripCtlAndExtFromUTF8

func StripCtlAndExtFromUTF8(str string) string

func StripCtlAndExtFromUnicode

func StripCtlAndExtFromUnicode(str string) string

Advanced Unicode normalization and filtering, see http://blog.golang.org/normalization and http://godoc.org/golang.org/x/text/unicode/norm for more details.

func StripCtlFromBytes

func StripCtlFromBytes(str string) string

two byte-oriented functions identical except for operator comparing c to 127.

func StripCtlFromUTF8

func StripCtlFromUTF8(str string) string

two UTF-8 functions identical except for operator comparing c to 127

func SubString

func SubString(str string, begin, length int) (substr string)

func SubStringWithSuffix

func SubStringWithSuffix(str string, length int, suffix string) string

func SuffixAnyInArray

func SuffixAnyInArray(s string, v []string) bool

func SuffixStr

func SuffixStr(s, substr string) bool

func TestListenOnTCPPort

func TestListenOnTCPPort(ip string, port int) bool

TestListenOnTCPPort check availability of port with ip

func TestPort

func TestPort(port int) bool

TestPort check port availability

func TestTCPAddress

func TestTCPAddress(host string, timeout time.Duration) bool

func TestTCPPort

func TestTCPPort(host string, port int, duration time.Duration) bool

func ToBytes

func ToBytes(s string) (uint64, error)

ToBytes parses a string formatted by ByteSize as bytes.

func ToFixed

func ToFixed(num float64, precision int) float64

func ToFloat

func ToFloat(s string, defaultValue float64) float64

func ToInt

func ToInt(str string) (int, error)

func ToInt64

func ToInt64(str string) (int64, error)

func ToJSONBytes

func ToJSONBytes(v interface{}) ([]byte, error)

func ToJson

func ToJson(in interface{}, indent bool) string

func ToLowercase

func ToLowercase(str []byte) []byte

ToLowercase convert string bytes to lowercase

func ToMegabytes

func ToMegabytes(s string) (uint64, error)

ToMegabytes parses a string formatted by ByteSize as megabytes.

func ToString

func ToString(obj interface{}) string

func ToUppercase

func ToUppercase(str []byte) []byte

ToUppercase convert string bytes to uppercase

func TrimLeftStr

func TrimLeftStr(str string, left string) string

func TrimRightStr

func TrimRightStr(str string, right string) string

func TrimSpaces

func TrimSpaces(str string) string

TrimSpaces will trim space and line break

func TryGetFileAbsPath

func TryGetFileAbsPath(filePath string, ignoreMissing bool) string

Smart get file abs path

func TypeIsArray

func TypeIsArray(any interface{}) bool

func TypeIsMap

func TypeIsMap(any interface{}) bool

func Uint32toBytes

func Uint32toBytes(b []byte, v uint32)

Uint32toBytes convert uint32 to bytes, max uint: 4294967295

func Uint64toBytes

func Uint64toBytes(b []byte, v uint64)

Uint64toBytes convert uint64 to bytes

func UnicodeIndex

func UnicodeIndex(str, substr string) int

func UnifyLocalAddress

func UnifyLocalAddress(host string) string

func UnsafeBytesToString

func UnsafeBytesToString(bs []byte) string

func UnsafeStringToBytes

func UnsafeStringToBytes(s string) []byte

func UrlDecode

func UrlDecode(str string) string

func UrlEncode

func UrlEncode(str string) string

func VersionCompare

func VersionCompare(v1, v2 string) (int, error)

func WaitServerUp

func WaitServerUp(addr string, duration time.Duration) error

func WalkBytesAndReplace

func WalkBytesAndReplace(data []byte, find, replace byte)

func XSSHandle

func XSSHandle(src string) string

func XXHash

func XXHash(data string) uint32

Types

type Annotation

type Annotation struct {
	Field      string       `json:"field,omitempty"`
	Type       string       `json:"type,omitempty"`
	Tag        string       `json:"tag,omitempty"`
	Annotation []Annotation `json:"annotation,omitempty"`
}

func GetTagsByTagName

func GetTagsByTagName(any interface{}, tagName string) []Annotation

type AtomicID

type AtomicID struct {
	Sequence    uint32
	MaxSequence uint32

	Sequence64    uint64
	MaxSequence64 uint64
	// contains filtered or unexported fields
}

func GetAutoIncrement32ID

func GetAutoIncrement32ID(bucket string, rangeFrom, rangeTo uint32) *AtomicID

func GetAutoIncrement64ID

func GetAutoIncrement64ID(bucket string, rangeFrom, rangeTo uint64) *AtomicID

func (*AtomicID) Increment

func (id *AtomicID) Increment() uint32

func (*AtomicID) Increment64

func (id *AtomicID) Increment64() uint64

type ByteValue

type ByteValue struct {
	Size float64
}

func (ByteValue) String

func (this ByteValue) String() string

type Cache

type Cache struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Cache is a semi-persistent mapping of keys to values. Elements added to the cache are store until they are explicitly deleted or are expired due time- based eviction based on last access time.

Expired elements are not visible through classes methods, but they do remain stored in the cache until CleanUp() is invoked. Therefore CleanUp() must be invoked periodically to prevent the cache from becoming a memory leak. If you want to start a goroutine to perform periodic clean-up then see StartJanitor().

Cache does not support storing nil values. Any attempt to put nil into the cache will cause a panic.

func NewCache

func NewCache(d time.Duration, initialSize int) *Cache

NewCache creates and returns a new Cache. d is the length of time after last access that cache elements expire. initialSize is the initial allocation size used for the Cache's underlying map.

func NewCacheWithExpireOnAdd

func NewCacheWithExpireOnAdd(d time.Duration, initialSize int) *Cache

NewCacheWithExpireOnAdd creates and returns a new Cache that does not updated the expiration time when the object is accessed. The expiration is only set when a new object is added to the cache and is not updated until it expires and re-inserted.

func NewCacheWithRemovalListener

func NewCacheWithRemovalListener(d time.Duration, initialSize int, l RemovalListener) *Cache

NewCacheWithRemovalListener creates and returns a new Cache and register a RemovalListener callback function. d is the length of time after last access that cache elements expire. initialSize is the initial allocation size used for the Cache's underlying map. l is the callback function that will be invoked when cache elements are removed from the map on CleanUp.

func (*Cache) CleanUp

func (c *Cache) CleanUp() int

CleanUp performs maintenance on the cache by removing expired elements from the cache. If a RemoveListener is registered it will be invoked for each element that is removed during this clean up operation. The RemovalListener is invoked on the caller's goroutine.

func (*Cache) Delete

func (c *Cache) Delete(k Key) Value

Delete a key from the map and return the value or nil if the key does not exist. The RemovalListener is not notified for explicit deletions.

func (*Cache) Entries

func (c *Cache) Entries() map[Key]Value

Entries returns a shallow copy of the non-expired elements in the cache.

func (*Cache) Get

func (c *Cache) Get(k Key) Value

Get the current value associated with a key or nil if the key is not present. The last access time of the element is updated.

func (*Cache) Put

func (c *Cache) Put(k Key, v Value) Value

Put writes the given key and value to the map replacing any existing value if it exists. The previous value associated with the key returned or nil if the key was not present.

func (*Cache) PutIfAbsent

func (c *Cache) PutIfAbsent(k Key, v Value) Value

PutIfAbsent writes the given key and value to the cache only if the key is absent from the cache. Nil is returned if the key-value pair were written, otherwise the old value is returned.

func (*Cache) PutIfAbsentWithTimeout

func (c *Cache) PutIfAbsentWithTimeout(k Key, v Value, timeout time.Duration) Value

PutIfAbsentWithTimeout writes the given key and value to the cache only if the key is absent from the cache. Nil is returned if the key-value pair were written, otherwise the old value is returned. The cache expiration time will be overwritten by timeout of the key being inserted.

func (*Cache) PutWithTimeout

func (c *Cache) PutWithTimeout(k Key, v Value, timeout time.Duration) Value

PutWithTimeout writes the given key and value to the map replacing any existing value if it exists. The previous value associated with the key returned or nil if the key was not present. The cache expiration time will be overwritten by timeout of the key being inserted.

func (*Cache) Replace

func (c *Cache) Replace(k Key, v Value) Value

Replace overwrites the value for a key only if the key exists. The old value is returned if the value is updated, otherwise nil is returned.

func (*Cache) ReplaceWithTimeout

func (c *Cache) ReplaceWithTimeout(k Key, v Value, timeout time.Duration) Value

ReplaceWithTimeout overwrites the value for a key only if the key exists. The old value is returned if the value is updated, otherwise nil is returned. The cache expiration time will be overwritten by timeout of the key being inserted.

func (*Cache) Size

func (c *Cache) Size() int

Size returns the number of elements in the cache. The number includes both active elements and expired elements that have not been cleaned up.

func (*Cache) StartJanitor

func (c *Cache) StartJanitor(interval time.Duration)

StartJanitor starts a goroutine that will periodically invoke the cache's CleanUp() method.

func (*Cache) StopJanitor

func (c *Cache) StopJanitor()

StopJanitor stops the goroutine created by StartJanitor.

type Diff

type Diff struct {
	Diff string

	Yaml1Struct *RawYaml
	Yaml2Struct *RawYaml

	Status DiffStatus
	// contains filtered or unexported fields
}

type DiffStatus

type DiffStatus int
const (
	DiffStatusExists   DiffStatus = 1
	DiffStatusSame     DiffStatus = 2
	DiffStatus1Missing DiffStatus = 3
	DiffStatus2Missing DiffStatus = 4
)

type Diffs

type Diffs []*Diff

func Do

func Do(list1 RawYamlList, list2 RawYamlList) Diffs

type EventMetadata

type EventMetadata struct {
	Fields          MapStr
	FieldsUnderRoot bool `config:"fields_under_root"`
	Tags            []string
}

EventMetadata contains fields and tags that can be added to an event via configuration.

type KV

type KV struct {
	Key   string `json:"key,omitempty"`
	Value string `json:"value,omitempty"`
}

type Key

type Key interface{}

Key type used in the cache.

type KeyValue

type KeyValue struct {
	Key     string      `json:"key,omitempty"`
	Value   int64       `json:"value,omitempty"`
	Payload interface{} `json:"payload,omitempty"`
}

func SortKeyValueArray

func SortKeyValueArray(keyValuePairs []KeyValue, revers bool) []KeyValue

sort by value desc by default

func SortMapStrIntToKV

func SortMapStrIntToKV(data map[string]int) []KeyValue

type MapStr

type MapStr map[string]interface{}

MapStr is a map[string]interface{} wrapper with utility methods for common map operations like converting to JSON.

func MapStrUnion

func MapStrUnion(dict1 MapStr, dict2 MapStr) MapStr

MapStrUnion creates a new MapStr containing the union of the key-value pairs of the two maps. If the same key is present in both, the key-value pairs from dict2 overwrite the ones from dict1.

func (MapStr) Clone

func (m MapStr) Clone() MapStr

Clone returns a copy of the MapStr. It recursively makes copies of inner maps.

func (MapStr) CopyFieldsTo

func (m MapStr) CopyFieldsTo(to MapStr, key string) error

CopyFieldsTo copies the field specified by key to the given map. It will overwrite the key if it exists. An error is returned if the key does not exist in the source map.

func (MapStr) DeepUpdate

func (m MapStr) DeepUpdate(d MapStr)

DeepUpdate recursively copies the key-value pairs from d to this map. If the key is present and a map as well, the sub-map will be updated recursively via DeepUpdate. DeepUpdateNoOverwrite is a version of this function that does not overwrite existing values.

func (MapStr) Delete

func (m MapStr) Delete(key string) error

Delete deletes the given key from the map.

func (MapStr) Equals

func (m MapStr) Equals(dst MapStr) bool

func (MapStr) Flatten

func (m MapStr) Flatten() MapStr

Flatten flattens the given MapStr and returns a flat MapStr.

Example:

"hello": MapStr{"world": "test" }

This is converted to:

"hello.world": "test"

This can be useful for testing or logging.

func (MapStr) GetValue

func (m MapStr) GetValue(key string) (interface{}, error)

GetValue gets a value from the map. If the key does not exist then an error is returned.

func (MapStr) HasKey

func (m MapStr) HasKey(key string) (bool, error)

func (*MapStr) Merge

func (m *MapStr) Merge(vars map[string]interface{})

func (MapStr) Put

func (m MapStr) Put(key string, value interface{}) (interface{}, error)

Put associates the specified value with the specified key. If the map previously contained a mapping for the key, the old value is replaced and returned. The key can be expressed in dot-notation (e.g. x.y) to put a value into a nested map.

If you need insert keys containing dots then you must use bracket notation to insert values (e.g. m[key] = value).

func (MapStr) SafetyHasKey

func (m MapStr) SafetyHasKey(key string) bool

HasKey returns true if the key exist. If an error occurs then false is returned with a non-nil error.

func (MapStr) String

func (m MapStr) String() string

String returns the MapStr as JSON.

func (MapStr) StringToPrint

func (m MapStr) StringToPrint() string

StringToPrint returns the MapStr as pretty JSON.

func (MapStr) Update

func (m MapStr) Update(d MapStr)

Update copies all the key-value pairs from d to this map. If the key already exists then it is overwritten. This method does not merge nested maps.

type MapStrPointer

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

MapStrPointer stores a pointer to atomically get/set a MapStr object This should give faster access for use cases with lots of reads and a few changes. It's important to note that modifying the map is not thread safe, only fully replacing it.

func NewMapStrPointer

func NewMapStrPointer(m MapStr) MapStrPointer

NewMapStrPointer initializes and returns a pointer to the given MapStr

func (MapStrPointer) Get

func (m MapStrPointer) Get() MapStr

Get returns the MapStr stored under this pointer

func (*MapStrPointer) Set

func (m *MapStrPointer) Set(p MapStr)

Set stores a pointer the given MapStr, replacing any previous one

type RawYaml

type RawYaml struct {
	Raw interface{}
	// contains filtered or unexported fields
}

type RawYamlList

type RawYamlList []*RawYaml

type RemovalListener

type RemovalListener func(k Key, v Value)

RemovalListener is the callback function type that can be registered with the cache to receive notification of the removal of expired elements.

type Request

type Request struct {
	Agent  string
	Method string
	Path   string
	Url    string
	Cookie string
	Proxy  string
	Body   []byte

	ContentType string
	Compress    bool

	Context context.Context
	// contains filtered or unexported fields
}

func NewDeleteRequest

func NewDeleteRequest(url string, body []byte) *Request

NewDeleteRequest issue a simple http delete request

func NewGetRequest

func NewGetRequest(url string, body []byte) *Request

NewGetRequest issue a simple http get request

func NewPostRequest

func NewPostRequest(url string, body []byte) *Request

NewPostRequest issue a simple http post request

func NewPutRequest

func NewPutRequest(url string, body []byte) *Request

NewPutRequest issue a simple http put request

func NewRequest

func NewRequest(method, url string) *Request

func (*Request) AcceptGzip

func (r *Request) AcceptGzip() *Request

func (*Request) AddHeader

func (r *Request) AddHeader(key, v string) *Request

func (*Request) SetAgent

func (r *Request) SetAgent(agent string) *Request

func (*Request) SetBasicAuth

func (r *Request) SetBasicAuth(username, password string) *Request

SetBasicAuth set user and password for request

func (*Request) SetContentType

func (r *Request) SetContentType(contentType string) *Request

func (*Request) SetProxy

func (r *Request) SetProxy(proxy string) *Request

type Result

type Result struct {
	Host       string
	Url        string
	Headers    map[string][]string
	Body       []byte
	StatusCode int
	Size       uint64
}

Result is the http request result

func ExecuteRequest

func ExecuteRequest(req *Request) (result *Result, err error)

ExecuteRequest issue a request

func ExecuteRequestWithCatchFlag

func ExecuteRequestWithCatchFlag(client *http.Client, req *Request, catchError bool) (result *Result, err error)

func HttpDelete

func HttpDelete(resource string) (*Result, error)

HttpDelete issue a simple http delete request

func HttpGet

func HttpGet(resource string) (*Result, error)

HttpGet issue a simple http get request

func HttpGetWithCookie

func HttpGetWithCookie(resource string, cookie string, proxy string) (*Result, error)

HttpGetWithCookie issue http request with cookie

type SQLQueryString

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

func NewSQLQueryString

func NewSQLQueryString(query string) *SQLQueryString

func (SQLQueryString) AfterAll

func (p SQLQueryString) AfterAll(word string) (atAfters []string)

func (SQLQueryString) TableNames

func (p SQLQueryString) TableNames() (names []string, err error)

TableNames returns all table names of the SQL statement

type StringSet

type StringSet map[string]struct{}

func MakeStringSet

func MakeStringSet(strings ...string) StringSet

func (StringSet) Add

func (set StringSet) Add(s string)

func (StringSet) Count

func (set StringSet) Count() int

func (StringSet) Del

func (set StringSet) Del(s string)

func (StringSet) Has

func (set StringSet) Has(s string) (exists bool)

type Time

type Time time.Time

Time is an abstraction for the time.Time type

func MustParseTime

func MustParseTime(value string) Time

MustParseTime is a convenience equivalent of the ParseTime function that panics in case of errors.

func ParseTimeWithStandardSpec

func ParseTimeWithStandardSpec(value string) (Time, error)

ParseTime parses a time in the TsLayout format.

func (Time) Hash32

func (t Time) Hash32(h hash.Hash32) error

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface. The time is a quoted string in the JsTsLayout format.

func (Time) String

func (t Time) String() string

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements js.Unmarshaler interface. The time is expected to be a quoted string in TsLayout format.

type Value

type Value interface{}

Value type held in the cache. Cannot be nil.

type Version

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

Version is an opaque representation of a version number

func MustParseGeneric

func MustParseGeneric(str string) *Version

MustParseGeneric is like ParseGeneric except that it panics on error

func MustParseSemantic

func MustParseSemantic(str string) *Version

MustParseSemantic is like ParseSemantic except that it panics on error

func ParseGeneric

func ParseGeneric(str string) (*Version, error)

ParseGeneric parses a "generic" version string. The version string must consist of two or more dot-separated numeric fields (the first of which can't have leading zeroes), followed by arbitrary uninterpreted data (which need not be separated from the final numeric field by punctuation). For convenience, leading and trailing whitespace is ignored, and the version can be preceded by the letter "v". See also ParseSemantic.

func ParseSemantic

func ParseSemantic(str string) (*Version, error)

ParseSemantic parses a version string that exactly obeys the syntax and semantics of the "Semantic Versioning" specification (http://semver.org/) (although it ignores leading and trailing whitespace, and allows the version to be preceded by "v"). For version strings that are not guaranteed to obey the Semantic Versioning syntax, use ParseGeneric.

func (*Version) AtLeast

func (v *Version) AtLeast(min *Version) bool

AtLeast tests if a version is at least equal to a given minimum version. If both Versions are Semantic Versions, this will use the Semantic Version comparison algorithm. Otherwise, it will compare only the numeric components, with non-present components being considered "0" (ie, "1.4" is equal to "1.4.0").

func (*Version) BuildMetadata

func (v *Version) BuildMetadata() string

BuildMetadata returns the build metadata, if v is a Semantic Version, or ""

func (*Version) Compare

func (v *Version) Compare(other string) (int, error)

Compare compares v against a version string (which will be parsed as either Semantic or non-Semantic depending on v). On success it returns -1 if v is less than other, 1 if it is greater than other, or 0 if they are equal.

func (*Version) Components

func (v *Version) Components() []uint

Components returns the version number components

func (*Version) LessThan

func (v *Version) LessThan(other *Version) bool

LessThan tests if a version is less than a given version. (It is exactly the opposite of AtLeast, for situations where asking "is v too old?" makes more sense than asking "is v new enough?".)

func (*Version) Major

func (v *Version) Major() uint

Major returns the major release number

func (*Version) Minor

func (v *Version) Minor() uint

Minor returns the minor release number

func (*Version) Patch

func (v *Version) Patch() uint

Patch returns the patch release number if v is a Semantic Version, or 0

func (*Version) PreRelease

func (v *Version) PreRelease() string

PreRelease returns the prerelease metadata, if v is a Semantic Version, or ""

func (*Version) String

func (v *Version) String() string

String converts a Version back to a string; note that for versions parsed with ParseGeneric, this will not include the trailing uninterpreted portion of the version number.

func (*Version) WithBuildMetadata

func (v *Version) WithBuildMetadata(buildMetadata string) *Version

WithBuildMetadata returns copy of the version object with requested buildMetadata

func (*Version) WithMajor

func (v *Version) WithMajor(major uint) *Version

WithMajor returns copy of the version object with requested major number

func (*Version) WithMinor

func (v *Version) WithMinor(minor uint) *Version

WithMinor returns copy of the version object with requested minor number

func (*Version) WithPatch

func (v *Version) WithPatch(patch uint) *Version

WithPatch returns copy of the version object with requested patch number

func (*Version) WithPreRelease

func (v *Version) WithPreRelease(preRelease string) *Version

WithPreRelease returns copy of the version object with requested prerelease

Directories

Path Synopsis
Package file copied from github.com/elastic/beats https://github.com/elastic/beats/blob/master/LICENSE Licensed under the Apache License, Version 2.0 (the "License");
Package file copied from github.com/elastic/beats https://github.com/elastic/beats/blob/master/LICENSE Licensed under the Apache License, Version 2.0 (the "License");

Jump to

Keyboard shortcuts

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