utils

package
v0.0.0-...-8431c9c Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: Apache-2.0 Imports: 23 Imported by: 648

Documentation

Index

Constants

View Source
const (
	// EncryptHeaderV1 ...
	EncryptHeaderV1 = "<enc-v1>"
	// SHA1 is the name of sha1 hash alg
	SHA1 = "sha1"
	// SHA256 is the name of sha256 hash alg
	SHA256 = "sha256"
)

Variables

View Source
var HashAlg = map[string]func() hash.Hash{
	SHA1:   sha1.New,
	SHA256: sha256.New,
}

HashAlg used to get correct alg for hash

Functions

func ConvertMapToStruct

func ConvertMapToStruct(object interface{}, values interface{}) error

ConvertMapToStruct is used to fill the specified struct with map.

func CronParser

func CronParser() cronlib.Parser

CronParser returns the parser of cron string with format of "* * * * * *"

func Encrypt

func Encrypt(content string, salt string, encrptAlg string) string

Encrypt encrypts the content with salt

func FindNamedMatches

func FindNamedMatches(regex *regexp.Regexp, str string) map[string]string

FindNamedMatches returns named matches of the regexp groups

func GenerateRandomString

func GenerateRandomString() string

GenerateRandomString generate a random string with 32 byte length

func GenerateRandomStringWithLen

func GenerateRandomStringWithLen(length int) string

GenerateRandomStringWithLen generates a random string with length

func GetStrValueOfAnyType

func GetStrValueOfAnyType(value interface{}) string

GetStrValueOfAnyType return string format of any value, for map, need to convert to json

func IsIllegalLength

func IsIllegalLength(s string, min int, max int) bool

IsIllegalLength ...

func IsLocalPath

func IsLocalPath(path string) bool

IsLocalPath checks if path is local, includes the empty path

func MostMatchSorter

func MostMatchSorter(a, b string, matchWord string) bool

MostMatchSorter is a sorter for the most match, usually invoked in sort Less function usage:

sort.Slice(input, func(i, j int) bool {
	return MostMatchSorter(input[i].GroupName, input[j].GroupName, matchWord)
})

a is the field to be used for sorting, b is the other field, matchWord is the word to be matched the return value is true if a is less than b for example, search with "user", input is {"harbor_user", "user", "users, "admin_user"} it returns with this order {"user", "users", "admin_user", "harbor_user"}

func NextSchedule

func NextSchedule(cron string, curTime time.Time) time.Time

NextSchedule return next scheduled time with a cron string and current time provided the cron string could contain 6 tokens if the cron string is invalid, it returns a zero time

func ParseEndpoint

func ParseEndpoint(endpoint string) (*url.URL, error)

ParseEndpoint parses endpoint to a URL

func ParseJSONInt

func ParseJSONInt(value interface{}) (int, bool)

ParseJSONInt ...

func ParseProjectIDOrName

func ParseProjectIDOrName(value interface{}) (int64, string, error)

ParseProjectIDOrName parses value to ID(int64) or name(string)

func ParseRepository

func ParseRepository(repository string) (project, rest string)

ParseRepository splits a repository into two parts: project and rest

func ParseTimeStamp

func ParseTimeStamp(timestamp string) (*time.Time, error)

ParseTimeStamp parse timestamp to time

func ReversibleDecrypt

func ReversibleDecrypt(str, key string) (string, error)

ReversibleDecrypt decrypts the str with aes/base64 or base 64 depending on "header"

func ReversibleEncrypt

func ReversibleEncrypt(str, key string) (string, error)

ReversibleEncrypt encrypts the str with aes/base64

func SafeCastBool

func SafeCastBool(value interface{}) bool

SafeCastBool --

func SafeCastFloat64

func SafeCastFloat64(value interface{}) float64

SafeCastFloat64 --

func SafeCastInt

func SafeCastInt(value interface{}) int

SafeCastInt --

func SafeCastString

func SafeCastString(value interface{}) string

SafeCastString -- cast a object to string saftely

func TestTCPConn

func TestTCPConn(addr string, timeout, interval int) error

TestTCPConn tests TCP connection timeout: the total time before returning if something is wrong with the connection, in second interval: the interval time for retring after failure, in second

func TrimLower

func TrimLower(str string) string

TrimLower ...

func ValidateCronString

func ValidateCronString(cron string) error

ValidateCronString check whether it is a valid cron string and whether the 1st field (indicating Seconds of time) of the cron string is a fixed value of 0 or not

Types

type LimitedConcurrentRunner

type LimitedConcurrentRunner interface {
	// AddTask adds a task to run
	AddTask(task func() error)
	// Wait waits all the tasks to be finished, returns error if the any of the tasks gets error
	Wait() (err error)
	// Cancel cancels all tasks, tasks that already started will continue to run
	Cancel(err error)
}

LimitedConcurrentRunner is used to run tasks, but limit the max concurrency.

func NewLimitedConcurrentRunner

func NewLimitedConcurrentRunner(limit int) LimitedConcurrentRunner

NewLimitedConcurrentRunner creates a runner

type PassportsPool

type PassportsPool interface {
	// Apply applies a passport from the pool.
	Apply() bool
	// Revoke revokes a passport to the pool
	Revoke() bool
}

PassportsPool holds a given number of passports, they can be applied or be revoked. PassportsPool is used to control the concurrency of tasks, the pool size determine the max concurrency. When users want to start a goroutine to perform some task, they must apply a passport firstly, and after finish the task, the passport must be revoked.

func NewPassportsPool

func NewPassportsPool(size int, stopped <-chan struct{}) PassportsPool

NewPassportsPool creates a passports pool with given size

Directories

Path Synopsis
uaa

Jump to

Keyboard shortcuts

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