update key map, set priority find by name
This commit is contained in:
46
key/map.go
46
key/map.go
@@ -6,11 +6,12 @@ import (
|
||||
|
||||
const (
|
||||
prefixByPath = "byPath"
|
||||
wrongIDx = -1
|
||||
)
|
||||
|
||||
func newMap() *Map {
|
||||
return &Map{
|
||||
idx: 0,
|
||||
idx: wrongIDx,
|
||||
wild: nil,
|
||||
children: nil,
|
||||
}
|
||||
@@ -69,31 +70,30 @@ func (m *Map) add(path []string) *Map {
|
||||
}
|
||||
|
||||
func (m *Map) byPath(path, sep string) (*Map, bool) {
|
||||
if len(path) == 0 {
|
||||
return m, m.isValid()
|
||||
}
|
||||
|
||||
for name := range m.children {
|
||||
if after, ok := strings.CutPrefix(path, name); ok {
|
||||
data := m.children[name]
|
||||
if len(after) == 0 {
|
||||
return data, true
|
||||
if len(after) == 0 || len(after) == len(sep) {
|
||||
return data, data.isValid()
|
||||
}
|
||||
|
||||
after, ok = strings.CutPrefix(after, sep)
|
||||
if !ok {
|
||||
return data, false
|
||||
}
|
||||
|
||||
if data.wild == nil {
|
||||
return data.byPath(after, sep)
|
||||
}
|
||||
|
||||
if idx := strings.Index(after, sep); idx != -1 {
|
||||
return data.wild.byPath(after[idx+1:], sep)
|
||||
}
|
||||
|
||||
return data, false
|
||||
return data.byPath(after[len(sep):], sep)
|
||||
}
|
||||
}
|
||||
|
||||
return m, false
|
||||
if m.wild == nil {
|
||||
return m, m.isValid()
|
||||
}
|
||||
|
||||
if idx := strings.Index(path, sep); idx != -1 {
|
||||
return m.wild.byPath(path[idx+1:], sep)
|
||||
}
|
||||
|
||||
return m, m.isValid()
|
||||
}
|
||||
|
||||
func (m *Map) find(path []string) (*Map, bool) {
|
||||
@@ -104,18 +104,22 @@ func (m *Map) find(path []string) (*Map, bool) {
|
||||
path = path[1:]
|
||||
}
|
||||
|
||||
if m.wild != nil {
|
||||
data, ok := m.children[name]
|
||||
if !ok && m.wild != nil {
|
||||
return m.wild.find(path)
|
||||
}
|
||||
|
||||
data, ok := m.children[name]
|
||||
if !ok {
|
||||
return data, false
|
||||
}
|
||||
|
||||
if last {
|
||||
return data, data.children == nil
|
||||
return data, data.isValid()
|
||||
}
|
||||
|
||||
return data.find(path)
|
||||
}
|
||||
|
||||
func (m *Map) isValid() bool {
|
||||
return m.idx != wrongIDx
|
||||
}
|
||||
|
||||
@@ -11,26 +11,33 @@ func TestMap_ByPath(t *testing.T) {
|
||||
|
||||
const (
|
||||
expID int = 1
|
||||
othID int = 0
|
||||
newID int = 42
|
||||
grpID int = 27
|
||||
)
|
||||
|
||||
data := key.Map{}
|
||||
data.Add(expID, []string{"test", "data", "three"})
|
||||
data.Add(expID, []string{"test", "other"})
|
||||
data.Add(othID, []string{"test", "other"})
|
||||
data.Add(newID, []string{"new", "{data}", "test"})
|
||||
data.Add(grpID, []string{"new", "group"})
|
||||
|
||||
idx, ok := data.Index(key.ByPath("test-other", "-"))
|
||||
if !ok {
|
||||
t.Error("key not found")
|
||||
}
|
||||
|
||||
if idx != expID {
|
||||
t.Errorf("idx exp:%v got:%v", expID, idx)
|
||||
if idx != othID {
|
||||
t.Errorf("idx exp:%v got:%v", othID, idx)
|
||||
}
|
||||
|
||||
if nidx, nok := data.Index(key.ByPath("new-service-test", "-")); !nok && nidx != newID {
|
||||
t.Errorf("idx exp:%v got:%v", newID, nidx)
|
||||
}
|
||||
|
||||
if gidx, gok := data.Index(key.ByPath("new-group", "-")); !gok && gidx != grpID {
|
||||
t.Errorf("idx %v exp:%v got:%v", gok, grpID, gidx)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMap_Add(t *testing.T) {
|
||||
@@ -65,11 +72,13 @@ func TestMap_Wild(t *testing.T) {
|
||||
|
||||
const (
|
||||
expID int = 1
|
||||
grpID int = 27
|
||||
newID int = 42
|
||||
)
|
||||
|
||||
data := key.Map{}
|
||||
data.Add(expID, []string{"test", "{data}", "id"})
|
||||
data.Add(grpID, []string{"test", "group"})
|
||||
data.Add(newID, []string{"new", "data"})
|
||||
|
||||
idx, ok := data.Index([]string{"test", "data", "id"})
|
||||
@@ -80,4 +89,13 @@ func TestMap_Wild(t *testing.T) {
|
||||
if idx != expID {
|
||||
t.Errorf("idx exp:%v got:%v", expID, idx)
|
||||
}
|
||||
|
||||
gidx, gok := data.Index([]string{"test", "group"})
|
||||
if !gok {
|
||||
t.Error("key not found")
|
||||
}
|
||||
|
||||
if gidx != grpID {
|
||||
t.Errorf("idx exp:%v got:%v", grpID, idx)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user