gitee.com/h79/goutils@v1.22.10/discovery/service/kv.go (about) 1 package service 2 3 import "strings" 4 5 // Key 6 /** 一个KEY 值是由路径+服务名称组合 */ 7 type Key struct { 8 Path string 9 Service string 10 } 11 12 func (k Key) ToKey() string { 13 if len(k.Service) > 0 { 14 return k.Path + "/" + k.Service 15 } 16 return k.Path 17 } 18 19 func (k Key) ToMap() map[string]interface{} { 20 //{"type": "service", "service":""} 21 //{"type": "keyprefix", "prefix": k.ToKey()} 22 return map[string]interface{}{"type": "key", "key": k.ToKey()} 23 } 24 25 func NewKey(key string) Key { 26 index := strings.LastIndexByte(key, '/') 27 if index == -1 { 28 return Key{Service: key} 29 } 30 return Key{key[0:index], key[index+1:]} 31 } 32 33 type Data struct { 34 Key 35 Value string //json format 36 } 37 38 type Config interface { 39 Set(data Data) error 40 Get(key Key) ([]*Data, error) 41 }