mirror of
https://github.com/taigrr/gopher-os
synced 2025-01-18 04:43:13 -08:00
acpi: change scopeVisit to visit entities in a visited entity's arglist
This commit is contained in:
parent
d9bd6f104e
commit
0a05164703
@ -36,12 +36,20 @@ func scopeVisit(depth int, ent Entity, entType EntityType, visitorFn Visitor) bo
|
|||||||
if !visitorFn(depth, ent) {
|
if !visitorFn(depth, ent) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Visit any args that are also entities
|
||||||
|
for _, arg := range ent.getArgs() {
|
||||||
|
if argEnt, isEnt := arg.(Entity); isEnt && !scopeVisit(depth+1, argEnt, entType, visitorFn) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the entity defines a scope we need to visit the child entities.
|
switch typ := ent.(type) {
|
||||||
if scopeEnt, ok := ent.(ScopeEntity); ok {
|
case ScopeEntity:
|
||||||
for _, child := range scopeEnt.Children() {
|
// If the entity defines a scope we need to visit the child entities.
|
||||||
scopeVisit(depth+1, child, entType, visitorFn)
|
for _, child := range typ.Children() {
|
||||||
|
_ = scopeVisit(depth+1, child, entType, visitorFn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,6 +9,9 @@ func TestScopeVisit(t *testing.T) {
|
|||||||
scopeMap := genTestScopes()
|
scopeMap := genTestScopes()
|
||||||
root := scopeMap[`\`].(*scopeEntity)
|
root := scopeMap[`\`].(*scopeEntity)
|
||||||
|
|
||||||
|
keepRecursing := func(Entity) bool { return true }
|
||||||
|
stopRecursing := func(Entity) bool { return false }
|
||||||
|
|
||||||
// Append special entities under IDE0
|
// Append special entities under IDE0
|
||||||
ide := scopeMap["IDE0"].(*scopeEntity)
|
ide := scopeMap["IDE0"].(*scopeEntity)
|
||||||
ide.Append(&Device{})
|
ide.Append(&Device{})
|
||||||
@ -26,26 +29,53 @@ func TestScopeVisit(t *testing.T) {
|
|||||||
ide.Append(&Method{})
|
ide.Append(&Method{})
|
||||||
ide.Append(&Method{})
|
ide.Append(&Method{})
|
||||||
ide.Append(&Method{})
|
ide.Append(&Method{})
|
||||||
|
ide.Append(&methodInvocationEntity{
|
||||||
|
unnamedEntity: unnamedEntity{
|
||||||
|
args: []interface{}{
|
||||||
|
&constEntity{val: uint64(1)},
|
||||||
|
&constEntity{val: uint64(2)},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
specs := []struct {
|
specs := []struct {
|
||||||
searchType EntityType
|
searchType EntityType
|
||||||
keepRecursing bool
|
keepRecursingFn func(Entity) bool
|
||||||
wantHits int
|
wantHits int
|
||||||
}{
|
}{
|
||||||
{EntityTypeAny, true, 21},
|
{EntityTypeAny, keepRecursing, 24},
|
||||||
{EntityTypeAny, false, 1},
|
{EntityTypeAny, stopRecursing, 1},
|
||||||
{EntityTypeDevice, true, 1},
|
{
|
||||||
{EntityTypeProcessor, true, 2},
|
EntityTypeAny,
|
||||||
{EntityTypePowerResource, true, 3},
|
func(ent Entity) bool {
|
||||||
{EntityTypeThermalZone, true, 4},
|
// Stop recursing after visiting the methodInvocationEntity
|
||||||
{EntityTypeMethod, true, 5},
|
_, isInv := ent.(*methodInvocationEntity)
|
||||||
|
return !isInv
|
||||||
|
},
|
||||||
|
22,
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
EntityTypeAny,
|
||||||
|
func(ent Entity) bool {
|
||||||
|
// Stop recursing after visiting the first constEntity
|
||||||
|
_, isConst := ent.(*constEntity)
|
||||||
|
return !isConst
|
||||||
|
},
|
||||||
|
23,
|
||||||
|
},
|
||||||
|
{EntityTypeDevice, keepRecursing, 1},
|
||||||
|
{EntityTypeProcessor, keepRecursing, 2},
|
||||||
|
{EntityTypePowerResource, keepRecursing, 3},
|
||||||
|
{EntityTypeThermalZone, keepRecursing, 4},
|
||||||
|
{EntityTypeMethod, keepRecursing, 5},
|
||||||
}
|
}
|
||||||
|
|
||||||
for specIndex, spec := range specs {
|
for specIndex, spec := range specs {
|
||||||
var hits int
|
var hits int
|
||||||
scopeVisit(0, root, spec.searchType, func(_ int, obj Entity) bool {
|
scopeVisit(0, root, spec.searchType, func(_ int, obj Entity) bool {
|
||||||
hits++
|
hits++
|
||||||
return spec.keepRecursing
|
return spec.keepRecursingFn(obj)
|
||||||
})
|
})
|
||||||
|
|
||||||
if hits != spec.wantHits {
|
if hits != spec.wantHits {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user