Make parent lookup recursive instead of write

This commit is contained in:
Ian Gulliver
2023-10-16 15:28:58 -07:00
parent 72950ba0ba
commit a7a8fd5272
2 changed files with 16 additions and 4 deletions

View File

@@ -22,6 +22,16 @@ func NewDocumentWithData(data any) *Document {
return doc
}
func (d *Document) AllParents() []*Document {
ret := append([]*Document{}, d.Parents...)
for _, parent := range d.Parents {
ret = append(ret, parent.AllParents()...)
}
return ret
}
func (d *Document) String() string {
return d.ID.String()
}

10
file.go
View File

@@ -101,10 +101,12 @@ func (p *Parser) loadFileAndParents(path string, child *file) ([]*file, error) {
}
func (f *file) setParents() {
for iter := f.child; iter != nil; iter = iter.child {
for _, doc := range iter.docs {
doc.Parents = append(doc.Parents, f.docs...)
}
if f.child == nil {
return
}
for _, doc := range f.child.docs {
doc.Parents = append(doc.Parents, f.docs...)
}
}