utils

package
v0.0.0-...-9931836 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2025 License: GPL-3.0 Imports: 18 Imported by: 0

Documentation

Overview

nolint: revive

nolint:revive

Index

Constants

View Source
const (
	// DefaultPrivateKey RSA 私钥 (PEM 格式)
	DefaultPrivateKey = `-----BEGIN RSA PRIVATE KEY-----
MIIBIjANBgkqhkiG9...你的私钥内容...
-----END RSA PRIVATE KEY-----`

	// DefaultPublicKey RSA 公钥 (PEM 格式)
	DefaultPublicKey = `` /* 450-byte string literal not displayed */

)
View Source
const (
	LabTaskPrefix    = "lab_task_queue_%s"
	LabControlPrefix = "lab_control_queue_%s"
	LabHeartPrefix   = "lab_heart_key_%s"

	LabHeartTime = 5 * time.Second
)

Variables

This section is empty.

Functions

func AppendUniqSlice

func AppendUniqSlice[T comparable](slice []T, elems ...T) []T

func BuildHierarchy

func BuildHierarchy[T comparable, D any](nodes []*Node[T, D]) ([][]D, error)

func CamelToSnake

func CamelToSnake(s string) string

func Compare

func Compare(a, b any) bool

func FilterSlice

func FilterSlice[S any, T any](sources []T, f func(i T) (S, bool)) []S

func FilterSliceErr

func FilterSliceErr[S any, T any](sources []T, f func(i T) (S, bool, error)) ([]S, error)

func FilterSliceWithErr

func FilterSliceWithErr[S any, T any](sources []T, f func(i T) ([]S, bool, error)) ([]S, error)

func FilterUniqSlice

func FilterUniqSlice[T comparable, V comparable](slice []T, f func(elem T) (V, bool)) []V

func FindDuplicates

func FindDuplicates[E any, T comparable](slice []E, kf func(ele E) T) (uniques []E, duplicates []E)

func FindTarget

func FindTarget[S any, T any](sources []T, f func(i T) (S, bool)) S

func GenerateJWTWithPrivateKey

func GenerateJWTWithPrivateKey(claims jwt.Claims, privateKeyPEM string) (string, error)

func GenerateJWTWithSecret

func GenerateJWTWithSecret(claims jwt.Claims, secret string) (string, error)

GenerateJWTWithSecret 保持兼容性 - 使用 HS256

func GetFilenameFromURL

func GetFilenameFromURL(rawURL string) string

func IfErrReturn

func IfErrReturn(fs ...func() error) error

func JSONEqual

func JSONEqual(a, b string) (bool, error)

func LabControlName

func LabControlName(labUUID uuid.UUID) string

func LabHeartName

func LabHeartName(labUUID uuid.UUID) string

func LabTaskName

func LabTaskName(labUUID uuid.UUID) string

func MapToSlice

func MapToSlice[K comparable, V any, T any](sources map[K]V, f func(key K, value V) (T, bool)) []T

func Or

func Or[T comparable](values ...T) T

func ParseJWT

func ParseJWT(tokenString string, claims jwt.Claims) error

ParseJWT 解析 JWT token (使用默认公钥)

func ParseJWTWithPublicKey

func ParseJWTWithPublicKey(tokenString, publicKeyPEM string, claims jwt.Claims) error

func ParseJWTWithSecret

func ParseJWTWithSecret(tokenString, secret string, claims jwt.Claims) error

ParseJWTWithSecret 保持兼容性 - 使用 HS256

func Range

func Range[T any](source []T, f func(index int, i T) bool)

func RangeMap

func RangeMap[T comparable, V any](source map[T]V, f func(key T, value V) bool)

func RemoveDuplicates

func RemoveDuplicates[T comparable](slice []T) []T

func RequestShutdown

func RequestShutdown() bool

func SafeValue

func SafeValue[T any](f func() T, defaultVal T) (res T)

func SafelyGo

func SafelyGo(function func(), handleError func(error))

func SafelyRun

func SafelyRun(function func()) (err error)

func SetDifference

func SetDifference[T comparable, V any](source map[T]V, target map[T]V) map[T]V

func SetupSignalContext

func SetupSignalContext() context.Context

func SetupSignalHandler

func SetupSignalHandler() <-chan struct{}

func Slice2Map

func Slice2Map[K comparable, V any, T any](sources []T, f func(i T) (K, V)) map[K]V

func SliceToMapSlice

func SliceToMapSlice[K comparable, V any, T any](sources []T, f func(i T) (K, V, bool)) map[K][]V

func Ternary

func Ternary[T any](condition bool, okValue T, defaultValue T) T

func TernaryLazy

func TernaryLazy[T any](condition bool, trueFn, falseFn func() T) T

TernaryLazy 延迟计算版本的三元运算符,避免预先计算所有参数 只有在需要时才会调用相应的函数来计算值

func Wrapper

func Wrapper[T any](f func() T) T

语法糖包装

Types

type Claims

type Claims struct {
	Exp      int64   `json:"exp"`
	Identity PayLoad `json:"identity"`
	jwt.RegisteredClaims
}

type Node

type Node[T comparable, D any] struct {
	Name   T
	Parent T
	Data   D
}

type PayLoad

type PayLoad struct {
	UserID uint64 `json:"userId"`
	Email  string `json:"email"`
	OrgID  uint64 `json:"orgId"`
}

Jump to

Keyboard shortcuts

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