Share semantic document traversal helpers
This commit is contained in:
23
metadata.go
23
metadata.go
@@ -85,8 +85,8 @@ func (m Metadata) hasHistoricalPath(path Path) bool {
|
||||
func finalizeMetadataPaths(m *Metadata, final *document.Node, applications []historyApplication, earlier ...*document.Node) {
|
||||
m.effectivePaths = make(map[string]Path)
|
||||
m.historicalPaths = make(map[string][]Path)
|
||||
walkDocumentPaths(final, Path{}, func(path Path) { m.effectivePaths[path.String()] = path.clone() })
|
||||
add := func(path Path) {
|
||||
walkDocument(final, Path{}, func(path Path, _ *document.Node) { m.effectivePaths[path.String()] = path.clone() })
|
||||
add := func(path Path, _ *document.Node) {
|
||||
pointer := path.String()
|
||||
for _, prior := range m.historicalPaths[pointer] {
|
||||
if matchesPathShape(path, prior) {
|
||||
@@ -95,9 +95,9 @@ func finalizeMetadataPaths(m *Metadata, final *document.Node, applications []his
|
||||
}
|
||||
m.historicalPaths[pointer] = append(m.historicalPaths[pointer], path.clone())
|
||||
}
|
||||
walkDocumentPaths(final, Path{}, add)
|
||||
walkDocument(final, Path{}, add)
|
||||
for _, tree := range earlier {
|
||||
walkDocumentPaths(tree, Path{}, add)
|
||||
walkDocument(tree, Path{}, add)
|
||||
}
|
||||
for _, application := range applications {
|
||||
// Overrides have mapping-only prefixes; parsed tokens are keys in this layer.
|
||||
@@ -105,19 +105,6 @@ func finalizeMetadataPaths(m *Metadata, final *document.Node, applications []his
|
||||
for _, segment := range application.path.segments {
|
||||
prefix = prefix.Key(segment.key)
|
||||
}
|
||||
walkDocumentPaths(application.value, prefix, add)
|
||||
}
|
||||
}
|
||||
|
||||
func walkDocumentPaths(node *document.Node, path Path, visit func(Path)) {
|
||||
if node == nil {
|
||||
return
|
||||
}
|
||||
visit(path)
|
||||
for _, entry := range node.Mapping {
|
||||
walkDocumentPaths(entry.Value, path.Key(entry.Key), visit)
|
||||
}
|
||||
for i, child := range node.Sequence {
|
||||
walkDocumentPaths(child, path.Index(i), visit)
|
||||
walkDocument(application.value, prefix, add)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user