@@ -74,25 +74,26 @@ func (sc *Schema) GetEntry(pe []string) (*yang.Entry, error) {
7474 defer sc .m .RUnlock ()
7575
7676 // Assume we are always dealing with an absolute path here?
77- mod , err := sc .FindModuleForPathElement (sc .root , pe [0 ])
77+ mods , err := sc .FindPossibleModulesForPathElement (sc .root , pe [0 ])
7878 if err != nil {
7979 return nil , err
8080 }
8181
82- // do we only have the module name in that path? If so we are done
83- prefix , path , found := strings .Cut (pe [0 ], ":" )
84- if ! found {
85- path = prefix
86- prefix = ""
87- }
88- if mod .Name == path && len (pe ) == 1 {
89- return mod , nil
82+ for i , mod := range mods {
83+ entry , err := getEntry (mod , pe )
84+ if err == nil {
85+ return entry , nil
86+ }
87+ remainingMods := make ([]string , 0 , len (mods )- (i + 1 ))
88+ for _ , rMod := range mods [i + 1 :] {
89+ remainingMods = append (remainingMods , rMod .Name )
90+ }
91+ log .Debugf ("looking up path %s in module %s caused: %v. continuing to search in %v" , strings .Join (pe , "/" ), mod .Name , err , remainingMods )
9092 }
91-
92- return getEntry (mod , pe )
93+ return nil , fmt .Errorf ("schema entry %q not found" , strings .Join (pe , "/" ))
9394}
9495
95- func (sc * Schema ) FindModuleForPathElement (e * yang.Entry , pathElement string ) (* yang.Entry , error ) {
96+ func (sc * Schema ) FindPossibleModulesForPathElement (e * yang.Entry , pathElement string ) ([] * yang.Entry , error ) {
9697 if e == nil {
9798 return nil , errors .New ("nil yang entry" )
9899 }
@@ -102,21 +103,25 @@ func (sc *Schema) FindModuleForPathElement(e *yang.Entry, pathElement string) (*
102103 path = prefix
103104 prefix = ""
104105 }
105- // try to shortcut by returning the module if it exists
106+ // try to shortcut by returning the module directly if pathElement matches the name/prefix
106107 if mod , ok := e .Dir [path ]; ok && (! foundPrefix || mod .Prefix .Name == prefix ) {
107- return mod , nil
108+ return [] * yang. Entry { mod } , nil
108109 }
109110
110111 switch {
111112 case e .Node == nil && e .Name != RootName :
112113 return nil , fmt .Errorf ("entry has no Node but is not root, instead is %s" , e .Name )
113114 case e .Node == nil :
115+ entries := make ([]* yang.Entry , 0 )
114116 for _ , entry := range sc .root .Dir {
115117 if ee , ok := entry .Dir [path ]; ok && (! foundPrefix || ee .Prefix .Name == prefix ) {
116- return entry , nil
118+ entries = append ( entries , entry )
117119 }
118120 }
119- return nil , fmt .Errorf ("unable to find entry for prefix %q, path %q in root" , prefix , path )
121+ if len (entries ) == 0 {
122+ return nil , fmt .Errorf ("unable to find entry for prefix %q, path %q in root" , prefix , path )
123+ }
124+ return entries , nil
120125
121126 default :
122127 mod := yang .RootNode (e .Node )
@@ -126,7 +131,7 @@ func (sc *Schema) FindModuleForPathElement(e *yang.Entry, pathElement string) (*
126131 return nil , fmt .Errorf ("module %q not found in loaded modules" , mod .Name )
127132 }
128133 if _ , ok := foundEntry .Dir [path ]; ok {
129- return foundEntry , nil
134+ return [] * yang. Entry { foundEntry } , nil
130135 }
131136 }
132137
@@ -137,7 +142,7 @@ func (sc *Schema) FindModuleForPathElement(e *yang.Entry, pathElement string) (*
137142 return nil , fmt .Errorf ("module %q not found in loaded modules" , i .Name )
138143 }
139144 if _ , ok := foundEntry .Dir [path ]; ok {
140- return foundEntry , nil
145+ return [] * yang. Entry { foundEntry } , nil
141146 }
142147 }
143148 }
0 commit comments