github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/conf/env/env_path_walker.go (about)

     1  package env
     2  
     3  import (
     4  	"bytes"
     5  	"strconv"
     6  )
     7  
     8  type PathWalker struct{ path []interface{} }
     9  
    10  func NewPathWalker() *PathWalker { return &PathWalker{} }
    11  
    12  func (p *PathWalker) Enter(i interface{}) { p.path = append(p.path, i) }
    13  
    14  func (p *PathWalker) Exit() { p.path = p.path[:len(p.path)-1] }
    15  
    16  func (p *PathWalker) Paths() []interface{} { return p.path }
    17  
    18  func (p *PathWalker) String() string { return StringifyPath(p.path...) }
    19  
    20  func StringifyPath(paths ...interface{}) string {
    21  	buf := bytes.NewBuffer(nil)
    22  	for i, key := range paths {
    23  		if i > 0 {
    24  			buf.WriteRune('_')
    25  		}
    26  		switch v := key.(type) {
    27  		case string:
    28  			buf.WriteString(v)
    29  		case int:
    30  			buf.WriteString(strconv.Itoa(v))
    31  		}
    32  	}
    33  	return buf.String()
    34  }