Documentation
¶
Overview ¶
Package namespace 用于构造和解析带命名空间的键。
命名空间是一组有序的字符串,配合分隔符拼装为键前缀或完整键名, 便于给 redis、etcd 这类自身不带命名空间概念的组件人为划分业务空间。
约定:
- 组合(ToString/FullName/RandomKey)与解析(FromFullName)使用同一套 Options, 因此 FullName 产出的全名可以被 FromFullName 解析回原始的命名空间和 key;
- 命名空间或 key 为空时不会输出多余的分隔符,避免产出悬挂分隔符的键名;
- 解析要求分隔符非空,且不接受空段落与空 key。
Index ¶
- Constants
- Variables
- func ReSetDefaultOptions()
- func SetDefaultOptions(opts ...optparams.Option[Options])
- func WithEtcdStyle() optparams.Option[Options]
- func WithKeyDelimiter(delimiter string) optparams.Option[Options]
- func WithNamespaceDelimiter(delimiter string) optparams.Option[Options]
- func WithPrefix(prefix string) optparams.Option[Options]
- func WithRandomKeyGen(algo idgener.Algorithm) optparams.Option[Options]
- func WithRedisStyle() optparams.Option[Options]
- type NameSpcaedeprecated
- type Namespace
- type Options
Examples ¶
Constants ¶
const ( //DefaultNamespaceDelimiter 默认的命名空间层级分隔符 DefaultNamespaceDelimiter = "::" //DefaultKeyDelimiter 默认的命名空间与key之间的分隔符 DefaultKeyDelimiter = "::" )
默认的分隔符
Variables ¶
var ( //ErrKeyNotHaveNamespace key没有命名空间 ErrKeyNotHaveNamespace = errors.New("key not have namespace") //ErrNamespaceFormatNotMatch 命名空间格式不匹配. //包括前缀不匹配、异名分隔符下出现多于两段、空段落以及空key. ErrNamespaceFormatNotMatch = errors.New("namespace format not match") //ErrInvalidDelimiter 命名空间分隔符或key分隔符为空 ErrInvalidDelimiter = errors.New("namespace delimiter or key delimiter is empty") )
Functions ¶
func ReSetDefaultOptions ¶
func ReSetDefaultOptions()
ReSetDefaultOptions 重置命名空间配置为内置默认值(分隔符"::"、无前缀、uuidv4随机算法)
func SetDefaultOptions ¶
SetDefaultOptions 设置默认命名空间配置 选项在已有默认配置的基础上叠加,未指定的字段保持不变;调整后所有不显式传选项的调用都会使用新配置. @params opts ...optparams.Option[Options] 设置项
Example ¶
//调整默认配置后,不显式传选项的调用都会使用新配置
SetDefaultOptions(WithEtcdStyle())
defer ReSetDefaultOptions()
ns, key, err := FromFullName("/a/b/c/d")
fmt.Println(ns, key, err)
Output: [a b c] d <nil>
func WithKeyDelimiter ¶
WithKeyDelimiter 设置键间的分割符
func WithNamespaceDelimiter ¶
WithNamespaceDelimiter 设置命名空间间的分割符
func WithRandomKeyGen ¶ added in v0.0.2
WithRandomKeyGen 指定随机生成key时使用的随机算法
func WithRedisStyle ¶
WithRedisStyle 设置redis风格的命名空间设置
Types ¶
type Namespace ¶ added in v1.0.0
type Namespace []string
Namespace 带命名空间的键
func FromFullName ¶
FromFullName 从全名字符串中解析出命名空间和key @params fullname string 待解析全名 @params opts ...optparams.Option[Options] 设置项 @return Namespace 命名空间 @return string key,解析失败且能定位到key时返回去掉前缀后的原字符串 @return error 解析错误:分隔符配置非法返回 ErrInvalidDelimiter;前缀不匹配、出现空段落或空key 返回 ErrNamespaceFormatNotMatch;全名中找不到分隔符返回 ErrKeyNotHaveNamespace
Example ¶
ns, key, err := FromFullName("/a/b/c/d", WithEtcdStyle())
fmt.Println(ns, key, err)
_, key, err = FromFullName("q")
fmt.Println(key, err)
Output: [a b c] d <nil> q key not have namespace
func (Namespace) FullName ¶ added in v1.0.0
FullName 在命名空间基础上创建一个key的全名 @params key string 用于标识的键 @params opts ...optparams.Option[Options] 设置项
Example ¶
ns := Namespace{"a", "b", "c"}
fmt.Println(ns.FullName("q", WithRedisStyle()))
fmt.Println(ns.FullName("q", WithEtcdStyle()))
Output: a::b::c::q /a/b/c/q
func (Namespace) RandomKey ¶ added in v1.0.0
RandomKey 在命名空间基础上创建一个随机的key的全名 @params opts ...optparams.Option[Options] 设置项 @return string 随机key的全名 @return error 分隔符配置非法或随机算法不可用时返回错误
Example ¶
ns := Namespace{"a", "b"}
key, err := ns.RandomKey(WithRedisStyle())
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(strings.HasPrefix(key, "a::b::"), len(strings.TrimPrefix(key, "a::b::")))
Output: true 36