Index mapping assembly for composition and overlays
This commit is contained in:
37
internal/engine/mapping_index.go
Normal file
37
internal/engine/mapping_index.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package engine
|
||||
|
||||
import "gitea.maximumdirect.net/eric/ConfigKit/internal/document"
|
||||
|
||||
// mappingIndex keeps ordered document entries and their lookup positions in
|
||||
// sync while one composition or overlay is assembled.
|
||||
type mappingIndex struct {
|
||||
byNode map[*document.Node]map[string]int
|
||||
}
|
||||
|
||||
func (m *mappingIndex) entries(node *document.Node) map[string]int {
|
||||
if m.byNode == nil {
|
||||
m.byNode = make(map[*document.Node]map[string]int)
|
||||
}
|
||||
if entries, ok := m.byNode[node]; ok {
|
||||
return entries
|
||||
}
|
||||
entries := make(map[string]int, len(node.Mapping))
|
||||
for index, entry := range node.Mapping {
|
||||
entries[entry.Key] = index
|
||||
}
|
||||
m.byNode[node] = entries
|
||||
return entries
|
||||
}
|
||||
|
||||
func (m *mappingIndex) find(node *document.Node, key string) int {
|
||||
if index, ok := m.entries(node)[key]; ok {
|
||||
return index
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func (m *mappingIndex) append(node *document.Node, entry document.Entry) {
|
||||
entries := m.entries(node)
|
||||
entries[entry.Key] = len(node.Mapping)
|
||||
node.Mapping = append(node.Mapping, entry)
|
||||
}
|
||||
Reference in New Issue
Block a user