@@ -30,7 +30,7 @@ import (
3030 "github.com/gohugoio/hugo/hugofs/files"
3131 "github.com/gohugoio/hugo/hugofs/hglob"
3232
33- radix "github.com/armon /go-radix"
33+ radix "github.com/gohugoio /go-radix"
3434 "github.com/spf13/afero"
3535)
3636
@@ -42,17 +42,12 @@ var _ ReverseLookupProvder = (*RootMappingFs)(nil)
4242// root mappings with some optional metadata about the root.
4343// Note that From represents a virtual root that maps to the actual filename in To.
4444func NewRootMappingFs (fs afero.Fs , rms ... * RootMapping ) (* RootMappingFs , error ) {
45- rootMapToReal := radix .New ()
46- realMapToRoot := radix .New ()
45+ rootMapToReal := radix .New [[] * RootMapping ] ()
46+ realMapToRoot := radix .New [[] * RootMapping ] ()
4747 id := fmt .Sprintf ("rfs-%d" , rootMappingFsCounter .Add (1 ))
4848
49- addMapping := func (key string , rm * RootMapping , to * radix.Tree ) {
50- var mappings []* RootMapping
51- v , found := to .Get (key )
52- if found {
53- // There may be more than one language pointing to the same root.
54- mappings = v .([]* RootMapping )
55- }
49+ addMapping := func (key string , rm * RootMapping , to * radix.Tree [[]* RootMapping ]) {
50+ mappings , _ := to .Get (key )
5651 mappings = append (mappings , rm )
5752 to .Insert (key , mappings )
5853 }
@@ -232,8 +227,8 @@ var _ FilesystemUnwrapper = (*RootMappingFs)(nil)
232227type RootMappingFs struct {
233228 id string
234229 afero.Fs
235- rootMapToReal * radix.Tree
236- realMapToRoot * radix.Tree
230+ rootMapToReal * radix.Tree [[] * RootMapping ]
231+ realMapToRoot * radix.Tree [[] * RootMapping ]
237232}
238233
239234var rootMappingFsCounter atomic.Int32
@@ -279,9 +274,8 @@ func (fs *RootMappingFs) UnwrapFilesystem() afero.Fs {
279274
280275// Filter creates a copy of this filesystem with only mappings matching a filter.
281276func (fs RootMappingFs ) Filter (f func (m * RootMapping ) bool ) * RootMappingFs {
282- rootMapToReal := radix .New ()
283- fs .rootMapToReal .Walk (func (b string , v any ) bool {
284- rms := v .([]* RootMapping )
277+ rootMapToReal := radix .New [[]* RootMapping ]()
278+ var walkFn radix.WalkFn [[]* RootMapping ] = func (b string , rms []* RootMapping ) (radix.WalkFlag , []* RootMapping , error ) {
285279 var nrms []* RootMapping
286280 for _ , rm := range rms {
287281 if f (rm ) {
@@ -291,8 +285,9 @@ func (fs RootMappingFs) Filter(f func(m *RootMapping) bool) *RootMappingFs {
291285 if len (nrms ) != 0 {
292286 rootMapToReal .Insert (b , nrms )
293287 }
294- return false
295- })
288+ return radix .WalkContinue , nil , nil
289+ }
290+ fs .rootMapToReal .Walk (walkFn )
296291
297292 fs .rootMapToReal = rootMapToReal
298293
@@ -385,21 +380,18 @@ func (fs *RootMappingFs) ReverseLookupComponent(component, filename string) ([]C
385380
386381func (fs * RootMappingFs ) hasPrefix (prefix string ) bool {
387382 hasPrefix := false
388- fs . rootMapToReal . WalkPrefix ( prefix , func (b string , v any ) bool {
383+ var walkFn radix. WalkFn [[] * RootMapping ] = func (b string , rms [] * RootMapping ) (radix. WalkFlag , [] * RootMapping , error ) {
389384 hasPrefix = true
390- return true
391- })
385+ return radix .WalkStop , nil , nil
386+ }
387+ fs .rootMapToReal .WalkPrefix (prefix , walkFn )
392388
393389 return hasPrefix
394390}
395391
396392func (fs * RootMappingFs ) getRoot (key string ) []* RootMapping {
397- v , found := fs .rootMapToReal .Get (key )
398- if ! found {
399- return nil
400- }
401-
402- return v .([]* RootMapping )
393+ v , _ := fs .rootMapToReal .Get (key )
394+ return v
403395}
404396
405397func (fs * RootMappingFs ) getRoots (key string ) (string , []* RootMapping ) {
@@ -418,7 +410,7 @@ func (fs *RootMappingFs) getRoots(key string) (string, []*RootMapping) {
418410 break
419411 }
420412
421- for _ , rm := range vv .([] * RootMapping ) {
413+ for _ , rm := range vv {
422414 if ! seen [rm ] {
423415 seen [rm ] = true
424416 roots = append (roots , rm )
@@ -439,34 +431,33 @@ func (fs *RootMappingFs) getRoots(key string) (string, []*RootMapping) {
439431
440432func (fs * RootMappingFs ) getRootsReverse (key string ) (string , []* RootMapping ) {
441433 tree := fs .realMapToRoot
442- s , v , found := tree .LongestPrefix (key )
443- if ! found {
444- return "" , nil
445- }
446- return s , v .([]* RootMapping )
434+ s , v , _ := tree .LongestPrefix (key )
435+ return s , v
447436}
448437
449438func (fs * RootMappingFs ) getRootsWithPrefix (prefix string ) []* RootMapping {
450439 var roots []* RootMapping
451- fs .rootMapToReal .WalkPrefix (prefix , func (b string , v any ) bool {
452- roots = append (roots , v .([]* RootMapping )... )
453- return false
454- })
440+ var walkFn radix.WalkFn [[]* RootMapping ] = func (b string , v []* RootMapping ) (radix.WalkFlag , []* RootMapping , error ) {
441+ roots = append (roots , v ... )
442+ return radix .WalkContinue , nil , nil
443+ }
444+ fs .rootMapToReal .WalkPrefix (prefix , walkFn )
455445
456446 return roots
457447}
458448
459449func (fs * RootMappingFs ) getAncestors (prefix string ) []keyRootMappings {
460450 var roots []keyRootMappings
461- fs . rootMapToReal . WalkPath ( prefix , func (s string , v any ) bool {
451+ var walkFn radix. WalkFn [[] * RootMapping ] = func (s string , v [] * RootMapping ) (radix. WalkFlag , [] * RootMapping , error ) {
462452 if strings .HasPrefix (prefix , s + filepathSeparator ) {
463453 roots = append (roots , keyRootMappings {
464454 key : s ,
465- roots : v .([] * RootMapping ) ,
455+ roots : v ,
466456 })
467457 }
468- return false
469- })
458+ return radix .WalkContinue , nil , nil
459+ }
460+ fs .rootMapToReal .WalkPath (prefix , walkFn )
470461
471462 return roots
472463}
@@ -593,7 +584,7 @@ func (rfs *RootMappingFs) collectDirEntries(prefix string) ([]iofs.DirEntry, err
593584
594585 // Next add any file mounts inside the given directory.
595586 prefixInside := prefix + filepathSeparator
596- rfs . rootMapToReal . WalkPrefix ( prefixInside , func (s string , v any ) bool {
587+ var walkFn radix. WalkFn [[] * RootMapping ] = func (s string , rms [] * RootMapping ) (radix. WalkFlag , [] * RootMapping , error ) {
597588 if (strings .Count (s , filepathSeparator ) - level ) != 1 {
598589 // This directory is not part of the current, but we
599590 // need to include the first name part to make it
@@ -603,7 +594,7 @@ func (rfs *RootMappingFs) collectDirEntries(prefix string) ([]iofs.DirEntry, err
603594 name := parts [0 ]
604595
605596 if seen [name ] {
606- return false
597+ return radix . WalkContinue , nil , nil
607598 }
608599 seen [name ] = true
609600 opener := func () (afero.File , error ) {
@@ -613,10 +604,9 @@ func (rfs *RootMappingFs) collectDirEntries(prefix string) ([]iofs.DirEntry, err
613604 fi := newDirNameOnlyFileInfo (name , nil , opener )
614605 fis = append (fis , fi )
615606
616- return false
607+ return radix . WalkContinue , nil , nil
617608 }
618609
619- rms := v .([]* RootMapping )
620610 for _ , rm := range rms {
621611 name := filepath .Base (rm .From )
622612 if seen [name ] {
@@ -630,8 +620,9 @@ func (rfs *RootMappingFs) collectDirEntries(prefix string) ([]iofs.DirEntry, err
630620 fis = append (fis , fi )
631621 }
632622
633- return false
634- })
623+ return radix .WalkContinue , nil , nil
624+ }
625+ rfs .rootMapToReal .WalkPrefix (prefixInside , walkFn )
635626
636627 // Finally add any ancestor dirs with files in this directory.
637628 ancestors := rfs .getAncestors (prefix )
0 commit comments