utils

package
v1.18.1 Latest Latest
Warning

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

Go to latest
Published: May 23, 2024 License: Apache-2.0, BSD-2-Clause, BSD-3-Clause, + 3 more Imports: 29 Imported by: 15

Documentation

Overview

Package utils contains common utility functions

Index

Constants

View Source
const (
	// QueryDefaultOffset default query offset
	QueryDefaultOffset = 0
	// QueryDefaultLimit default query limit
	QueryDefaultLimit = 100
	// QueryMaxLimit default query max
	QueryMaxLimit = 100
	// MaxBatchSize max batch size
	MaxBatchSize = 100
	// MaxQuerySize max query size
	MaxQuerySize = 100

	// MaxMetadataLength metadata max length
	MaxMetadataLength = 64

	MaxBusinessLength   = 64
	MaxOwnersLength     = 1024
	MaxDepartmentLength = 1024
	MaxCommentLength    = 1024
	MaxNameLength       = 64

	// service表
	MaxDbServiceNameLength      = 128
	MaxDbServiceNamespaceLength = 64
	MaxDbServicePortsLength     = 8192
	MaxDbServiceBusinessLength  = 128
	MaxDbServiceDeptLength      = 1024
	MaxDbServiceCMDBLength      = 1024
	MaxDbServiceCommentLength   = 1024
	MaxDbServiceOwnerLength     = 1024
	MaxDbServiceToken           = 2048

	// instance表
	MaxDbInsHostLength     = 128
	MaxDbInsProtocolLength = 32
	MaxDbInsVersionLength  = 32
	MaxDbInsLogicSetLength = 128

	// circuitbreaker表
	MaxDbCircuitbreakerName       = 32
	MaxDbCircuitbreakerNamespace  = 64
	MaxDbCircuitbreakerBusiness   = 64
	MaxDbCircuitbreakerDepartment = 1024
	MaxDbCircuitbreakerComment    = 1024
	MaxDbCircuitbreakerOwner      = 1024
	MaxDbCircuitbreakerVersion    = 32
)

some options config

View Source
const (
	// ReleaseTypeNormal 发布类型,全量发布
	ReleaseTypeNormal = "normal"
	// ReleaseTypeGray 灰度发布
	ReleaseTypeGray = "betaing"
	// ReleaseTypeCancelGray 取消灰度发布
	ReleaseTypeCancelGray = "cancel-gray"
	// ReleaseTypeDelete 发布类型,删除配置发布
	ReleaseTypeDelete = "delete"
	// ReleaseTypeRollback 发布类型 回滚
	ReleaseTypeRollback = "rollback"
	// ReleaseTypeClean 发布类型,清空配置发布
	ReleaseTypeClean = "clean"

	// ReleaseStatusSuccess 发布成功状态
	ReleaseStatusSuccess = "success"
	// ReleaseStatusFail 发布失败状态
	ReleaseStatusFail = "failure"
	// ReleaseStatusToRelease 待发布状态
	ReleaseStatusToRelease = "to-be-released"

	// 文件格式
	FileFormatText       = "text"
	FileFormatYaml       = "yaml"
	FileFormatXml        = "xml"
	FileFormatJson       = "json"
	FileFormatHtml       = "html"
	FileFormatProperties = "properties"

	FileIdSeparator = "+"

	// MaxRequestBodySize 导入配置文件请求体最大 4M
	MaxRequestBodySize = 4 * 1024 * 1024
	// ConfigFileFormKey 配置文件表单键
	ConfigFileFormKey = "config"
	// ConfigFileMetaFileName 配置文件元数据文件名
	ConfigFileMetaFileName = "META"
	// ConfigFileImportConflictSkip 导入配置文件发生冲突跳过
	ConfigFileImportConflictSkip = "skip"
	// ConfigFileImportConflictOverwrite 导入配置文件发生冲突覆盖原配置文件
	ConfigFileImportConflictOverwrite = "overwrite"
)
View Source
const (
	// PolarisCode polaris code
	PolarisCode = "X-Polaris-Code"
	// PolarisMessage polaris message
	PolarisMessage = "X-Polaris-Message"
	// PolarisRequestID request_id
	PolarisRequestID = "Request-Id"
)
View Source
const (
	// HeaderAuthTokenKey auth token key
	HeaderAuthTokenKey string = "X-Polaris-Token"
	// HeaderIsOwnerKey is owner key
	HeaderIsOwnerKey string = "X-Is-Owner"
	// HeaderUserIDKey user id key
	HeaderUserIDKey string = "X-User-ID"
	// HeaderOwnerIDKey owner id key
	HeaderOwnerIDKey string = "X-Owner-ID"
	// HeaderUserRoleKey user role key
	HeaderUserRoleKey string = "X-Polaris-User-Role"

	// ContextAuthTokenKey auth token key
	ContextAuthTokenKey = StringContext(HeaderAuthTokenKey)
	// ContextIsOwnerKey is owner key
	ContextIsOwnerKey = StringContext(HeaderIsOwnerKey)
	// ContextUserIDKey user id key
	ContextUserIDKey = StringContext(HeaderUserIDKey)
	// ContextOwnerIDKey owner id key
	ContextOwnerIDKey = StringContext(HeaderOwnerIDKey)
	// ContextUserRoleIDKey user role key
	ContextUserRoleIDKey = StringContext(HeaderUserRoleKey)
	// ContextAuthContextKey auth context key
	ContextAuthContextKey = StringContext("X-Polaris-AuthContext")
	// ContextUserNameKey users name key
	ContextUserNameKey = StringContext("X-User-Name")
	// ContextClientAddress client address key
	ContextClientAddress = StringContext("client-address")
	// ContextOpenAsyncRegis open async register key
	ContextOpenAsyncRegis = StringContext("client-asyncRegis")
	// ContextGrpcHeader grpc header key
	ContextGrpcHeader = StringContext("grpc-header")
	// ContextIsFromClient is from client
	ContextIsFromClient = StringContext("from-client")
	// ContextIsFromSystem is from polaris system
	ContextIsFromSystem = StringContext("from-system")
	// ContextOperator operator info
	ContextOperator = StringContext("operator")
)
View Source
const (
	// EmptyErrString empty error string
	EmptyErrString = "empty"
	// NilErrString null pointer error string
	NilErrString = "nil"
	// MatchAll rule match all service or namespace value
	MatchAll = "*"
)

Variables

View Source
var (
	// LocalHost local host
	LocalHost = "127.0.0.1"
	// LocalPort default listen port
	LocalPort = 8091
	// ConfDir default config dir
	ConfDir = "conf/"
)

Functions

func BoolPtr added in v1.18.0

func BoolPtr(v bool) *bool

func CalculateInstanceID

func CalculateInstanceID(namespace string, service string, vpcID string, host string, port uint32) (string, error)

CalculateInstanceID 计算实例ID

func CalculateRuleID

func CalculateRuleID(name, namespace string) string

CalculateRuleID 计算规则ID

func CheckContractInterfaceTetrad added in v1.18.0

func CheckContractInterfaceTetrad(contractId string, source apiservice.InterfaceDescriptor_Source,
	req *apiservice.InterfaceDescriptor) (string, *apiservice.Response)

func CheckContractTetrad added in v1.18.0

func CheckContractTetrad(req *apiservice.ServiceContract) (string, *apiservice.Response)

CheckContractTetrad 根据服务实例四元组计算ID

func CheckDbMetaDataFieldLen

func CheckDbMetaDataFieldLen(metaData map[string]string) error

CheckDbMetaDataFieldLen 检查metadata的K,V是否超过DB中对应字段的最大字符长度限制

func CheckDbRawStrFieldLen added in v1.14.0

func CheckDbRawStrFieldLen(param string, dbLen int) error

CheckDbRawStrFieldLen 检查name字段是否超过DB中对应字段的最大字符长度限制

func CheckDbStrFieldLen

func CheckDbStrFieldLen(param *wrappers.StringValue, dbLen int) error

CheckDbStrFieldLen 检查name字段是否超过DB中对应字段的最大字符长度限制

func CheckInstanceHost

func CheckInstanceHost(host *wrappers.StringValue) error

CheckInstanceHost 检查服务实例Host

func CheckInstancePort

func CheckInstancePort(port *wrappers.UInt32Value) error

CheckInstancePort 检查服务实例Port

func CheckInstanceTetrad

func CheckInstanceTetrad(req *apiservice.Instance) (string, *apiservice.Response)

CheckInstanceTetrad 根据服务实例四元组计算ID

func CheckMetadata

func CheckMetadata(meta map[string]string) error

CheckMetadata check metadata 检查metadata的个数 最大是64个 key/value是否符合要求

func CheckQueryLimit

func CheckQueryLimit(limit []string) (int, error)

CheckQueryLimit 检查查询参数Limit

func CheckQueryOffset

func CheckQueryOffset(offset []string) (int, error)

CheckQueryOffset 检查查询参数Offset

func CheckResourceName

func CheckResourceName(name *wrappers.StringValue) error

CheckResourceName 检查资源Name

func CheckResourceOwners

func CheckResourceOwners(owners *wrappers.StringValue) error

CheckResourceOwners 检查资源Owners

func CollectMapKeys

func CollectMapKeys(filters map[string]string) []string

CollectMapKeys collect filters key to slice

func ConvertFilter

func ConvertFilter(filters map[string]string) map[string][]string

ConvertFilter map[string]string to map[string][]string

func ConvertGRPCContext added in v1.18.0

func ConvertGRPCContext(ctx context.Context) context.Context

ConvertGRPCContext 将GRPC上下文转换成内部上下文

func ConvertStringValuesToSlice

func ConvertStringValuesToSlice(vals []*wrapperspb.StringValue) []string

ConvertStringValuesToSlice 转换StringValues为字符串切片

func DefaultString added in v1.18.0

func DefaultString(v, d string) string

func GenFileId

func GenFileId(namespace, group, fileName string) string

GenFileId 生成文件 Id

func GetUnexportedField added in v1.18.0

func GetUnexportedField(field reflect.Value) interface{}

func IsMatchAll added in v1.18.0

func IsMatchAll(v string) bool

func IsNotEqualMap added in v1.17.3

func IsNotEqualMap(req map[string]string, old map[string]string) bool

IsNotEqualMap metadata need update

func IsPrefixWildName added in v1.16.0

func IsPrefixWildName(name string) bool

IsPrefixWildName 判断名字是否为通配名字,只支持前缀索引(名字最后为*)

func IsSuffixWildName added in v1.16.0

func IsSuffixWildName(name string) bool

IsSuffixWildName 判断名字是否为通配名字,只支持后缀索引(名字第一个字符为*)

func IsWildMatch added in v1.16.0

func IsWildMatch(name, pattern string) bool

IsWildMatch 判断 name 是否匹配 pattern,pattern 可以是前缀或者后缀

func IsWildMatchIgnoreCase added in v1.16.0

func IsWildMatchIgnoreCase(name, pattern string) bool

IsWildMatchIgnoreCase 判断 name 是否匹配 pattern,pattern 可以是前缀或者后缀,忽略大小写

func IsWildName

func IsWildName(name string) bool

IsWildName 判断名字是否为通配名字,前缀或者后缀

func IsWildNotMatch added in v1.17.3

func IsWildNotMatch(name, pattern string) bool

IsWildNotMatch .

func MarshalToJsonString added in v1.14.0

func MarshalToJsonString(message proto.Message) (string, error)

MarshalToJsonString marshal json message to string

func MatchString added in v1.18.0

func MatchString(srcMetaValue string, matchValule *apimodel.MatchString, regexToPattern func(string) *regexp.Regexp) bool

func MustJson added in v1.17.3

func MustJson(v interface{}) string

func NewBoolValue

func NewBoolValue(value bool) *wrappers.BoolValue

NewBoolValue returns a new BoolValue with the given value.

func NewRoutingV2UUID

func NewRoutingV2UUID() string

NewUUID 返回一个随机的UUID

func NewStringValue

func NewStringValue(value string) *wrappers.StringValue

NewStringValue returns a new StringValue with the given value.

func NewUInt32Value

func NewUInt32Value(value uint32) *wrappers.UInt32Value

NewUInt32Value returns a new UInt32Value with the given value.

func NewUInt64Value

func NewUInt64Value(value uint64) *wrappers.UInt64Value

NewUInt64Value returns a new UInt64Value with the given value.

func NewUUID

func NewUUID() string

NewUUID 返回一个随机的UUID

func NewV2Revision

func NewV2Revision() string

NewV2Revision 返回一个随机的UUID

func ParseAuthToken

func ParseAuthToken(ctx context.Context) string

ParseAuthToken 从ctx中获取token

func ParseClientAddress

func ParseClientAddress(ctx context.Context) string

ParseClientAddress 从ctx中获取客户端地址

func ParseClientIP added in v1.18.0

func ParseClientIP(ctx context.Context) string

ParseClientIP .

func ParseFileId

func ParseFileId(fileId string) (namespace, group, fileName string)

ParseFileId 解析文件 Id

func ParseIsOwner

func ParseIsOwner(ctx context.Context) bool

ParseIsOwner 从ctx中获取token

func ParseOffsetAndLimit

func ParseOffsetAndLimit(query map[string]string) (uint32, uint32, error)

ParseOffsetAndLimit 统一格式化处理Offset和limit参数

func ParseOperator

func ParseOperator(ctx context.Context) string

ParseOperator 从ctx中获取operator

func ParseOwnerID

func ParseOwnerID(ctx context.Context) string

ParseOwnerID 从ctx解析Owner ID

func ParsePlatformID

func ParsePlatformID(ctx context.Context) string

ParsePlatformID 从ctx中获取Platform-Id

func ParsePlatformToken

func ParsePlatformToken(ctx context.Context) string

ParsePlatformToken 从ctx中获取Platform-Token

func ParseQueryLimit

func ParseQueryLimit(limit string) (uint32, error)

ParseQueryLimit 格式化处理limit参数

func ParseQueryOffset

func ParseQueryOffset(offset string) (uint32, error)

ParseQueryOffset 格式化处理offset参数

func ParseRequestID

func ParseRequestID(ctx context.Context) string

ParseRequestID 从ctx中获取Request-ID

func ParseToken

func ParseToken(ctx context.Context) string

ParseToken 从ctx中获取token

func ParseUserID

func ParseUserID(ctx context.Context) string

ParseUserID 从ctx中解析用户ID

func ParseUserName

func ParseUserName(ctx context.Context) string

ParseUserName 从ctx解析用户名称

func ParseWildName

func ParseWildName(name string) (string, bool)

ParseWildName 判断是否为格式化查询条件并且返回真正的查询信息

func ParseWildNameForSql added in v1.16.0

func ParseWildNameForSql(name string) string

ParseWildNameForSql 如果 name 是通配字符串,将通配字符*替换为sql中的%

func RequestID added in v1.17.3

func RequestID(ctx context.Context) zap.Field

RequestID 从ctx中获取Request-ID

func SetUnexportedField added in v1.18.0

func SetUnexportedField(field reflect.Value, value interface{})

func StringSliceDeDuplication

func StringSliceDeDuplication(s []string) []string

StringSliceDeDuplication 字符切片去重

func UnmarshalFromJsonString added in v1.14.0

func UnmarshalFromJsonString(message proto.Message, text string) error

UnmarshalFromJsonString unmarshal message from json string

func ValueLocalhost

func ValueLocalhost(ctx context.Context) string

ValueLocalhost 获取localhost

func WithLocalhost

func WithLocalhost(ctx context.Context, localhost string) context.Context

WithLocalhost 存储localhost

func ZapFileName added in v1.17.2

func ZapFileName(fileName string) zap.Field

ZapFileName 生成fileName的日志描述

func ZapGroup added in v1.17.2

func ZapGroup(group string) zap.Field

ZapGroup 生成group的日志描述

func ZapInstanceID

func ZapInstanceID(id string) zap.Field

ZapInstanceID 生成instanceID的日志描述

func ZapNamespace added in v1.17.2

func ZapNamespace(namespace string) zap.Field

ZapNamespace 生成namespace的日志描述

func ZapPlatformID

func ZapPlatformID(id string) zap.Field

ZapPlatformID 生成Platform-ID的日志描述

func ZapReleaseName added in v1.17.3

func ZapReleaseName(fileName string) zap.Field

ZapReleaseName 生成fileName的日志描述

func ZapRequestID

func ZapRequestID(id string) zap.Field

ZapRequestID 生成Request-ID的日志描述

Types

type AtomicValue added in v1.17.3

type AtomicValue[V any] struct {
	// contains filtered or unexported fields
}

func NewAtomicValue added in v1.17.3

func NewAtomicValue[V any](v V) *AtomicValue[V]

func (*AtomicValue[V]) Load added in v1.17.3

func (a *AtomicValue[V]) Load() V

func (*AtomicValue[V]) Store added in v1.17.3

func (a *AtomicValue[V]) Store(val V)

type ConfigFileMeta added in v1.15.0

type ConfigFileMeta struct {
	Tags    map[string]string `json:"tags"`
	Comment string            `json:"comment"`
}

ConfigFileMeta 导入配置文件ZIP包中的元数据结构

type ContextAPIServerSlot added in v1.18.0

type ContextAPIServerSlot struct{}

ContextAPIServerSlot

type Map added in v1.17.3

type Map[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Map

func NewMap added in v1.17.3

func NewMap[K comparable, V any]() *Map[K, V]

NewMap

func (*Map[K, V]) Delete added in v1.17.3

func (s *Map[K, V]) Delete(key K)

Delete

func (*Map[K, V]) Len added in v1.17.3

func (s *Map[K, V]) Len() int

Len

func (*Map[K, V]) Load added in v1.17.3

func (s *Map[K, V]) Load(key K) (V, bool)

Load

func (*Map[K, V]) Range added in v1.17.3

func (s *Map[K, V]) Range(f func(key K, val V))

Range

func (*Map[K, V]) Store added in v1.17.3

func (s *Map[K, V]) Store(key K, val V)

Store

func (*Map[K, V]) Values added in v1.18.0

func (s *Map[K, V]) Values() []V

Values .

type SegmentMap added in v1.17.0

type SegmentMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func NewSegmentMap added in v1.17.0

func NewSegmentMap[K comparable, V any](soltNum int, hashFunc func(k K) int) *SegmentMap[K, V]

func (*SegmentMap[K, V]) ComputeIfAbsent added in v1.17.3

func (s *SegmentMap[K, V]) ComputeIfAbsent(k K, supplier func(k K) V) (V, bool)

func (*SegmentMap[K, V]) Count added in v1.17.0

func (s *SegmentMap[K, V]) Count() uint64

func (*SegmentMap[K, V]) Del added in v1.17.0

func (s *SegmentMap[K, V]) Del(k K) bool

func (*SegmentMap[K, V]) Get added in v1.17.0

func (s *SegmentMap[K, V]) Get(k K) (V, bool)

func (*SegmentMap[K, V]) Put added in v1.17.0

func (s *SegmentMap[K, V]) Put(k K, v V)

func (*SegmentMap[K, V]) PutIfAbsent added in v1.17.0

func (s *SegmentMap[K, V]) PutIfAbsent(k K, v V) (V, bool)

func (*SegmentMap[K, V]) Range added in v1.17.0

func (s *SegmentMap[K, V]) Range(f func(k K, v V))

type Set added in v1.17.3

type Set[K comparable] struct {
	// contains filtered or unexported fields
}

func NewSet added in v1.17.3

func NewSet[K comparable]() *Set[K]

NewSet returns a new Set

func (*Set[K]) Add added in v1.17.3

func (set *Set[K]) Add(val K)

Add adds a string to the set

func (*Set[K]) Range added in v1.17.3

func (set *Set[K]) Range(fn func(val K))

func (*Set[K]) Remove added in v1.17.3

func (set *Set[K]) Remove(val K)

Remove removes a string from the set

func (*Set[K]) ToSlice added in v1.17.3

func (set *Set[K]) ToSlice() []K

type StringContext

type StringContext string

StringContext is a context key that carries a string.

type SyncMap added in v1.17.3

type SyncMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

SyncMap

func NewSyncMap added in v1.17.3

func NewSyncMap[K comparable, V any]() *SyncMap[K, V]

NewSyncMap

func (*SyncMap[K, V]) ComputeIfAbsent added in v1.17.3

func (s *SyncMap[K, V]) ComputeIfAbsent(k K, supplier func(k K) V) (V, bool)

ComputeIfAbsent

func (*SyncMap[K, V]) Delete added in v1.17.3

func (s *SyncMap[K, V]) Delete(key K) (V, bool)

Delete

func (*SyncMap[K, V]) Len added in v1.17.3

func (s *SyncMap[K, V]) Len() int

Len

func (*SyncMap[K, V]) Load added in v1.17.3

func (s *SyncMap[K, V]) Load(key K) (V, bool)

Load

func (*SyncMap[K, V]) Range added in v1.17.3

func (s *SyncMap[K, V]) Range(f func(key K, val V))

Range

func (*SyncMap[K, V]) ReadRange added in v1.18.0

func (s *SyncMap[K, V]) ReadRange(f func(key K, val V))

ReadRange .

func (*SyncMap[K, V]) Store added in v1.17.3

func (s *SyncMap[K, V]) Store(key K, val V)

Store

func (*SyncMap[K, V]) ToMap added in v1.18.0

func (s *SyncMap[K, V]) ToMap() map[K]V

func (*SyncMap[K, V]) Values added in v1.17.6

func (s *SyncMap[K, V]) Values() []V

Values

type SyncSet added in v1.17.3

type SyncSet[K comparable] struct {
	// contains filtered or unexported fields
}

func NewSyncSet added in v1.17.3

func NewSyncSet[K comparable]() *SyncSet[K]

NewSyncSet returns a new Set

func (*SyncSet[K]) Add added in v1.17.3

func (set *SyncSet[K]) Add(val K)

Add adds a string to the set

func (*SyncSet[K]) Contains added in v1.18.0

func (set *SyncSet[K]) Contains(val K) bool

Contains contains target value

func (*SyncSet[K]) Len added in v1.17.3

func (set *SyncSet[K]) Len() int

func (*SyncSet[K]) Range added in v1.17.3

func (set *SyncSet[K]) Range(fn func(val K))

func (*SyncSet[K]) Remove added in v1.17.3

func (set *SyncSet[K]) Remove(val K)

Remove removes a string from the set

func (*SyncSet[K]) String added in v1.18.0

func (set *SyncSet[K]) String() string

func (*SyncSet[K]) ToSlice added in v1.17.3

func (set *SyncSet[K]) ToSlice() []K

type WatchTimeoutCtx added in v1.18.0

type WatchTimeoutCtx struct{}

WatchTimeoutCtx .

Jump to

Keyboard shortcuts

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