github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/httptransport/transformer/util_path_walker.go (about)

     1  package transformer
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  type PathWalker struct {
     9  	path []interface{}
    10  }
    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 {
    19  	b := &strings.Builder{}
    20  	for i := 0; i < len(p.path); i++ {
    21  		switch x := p.path[i].(type) {
    22  		case string:
    23  			if b.Len() != 0 {
    24  				b.WriteByte('.')
    25  			}
    26  			b.WriteString(x)
    27  		case int:
    28  			b.WriteString(fmt.Sprintf("[%d]", x))
    29  		}
    30  	}
    31  	return b.String()
    32  }