github.com/sagernet/sing@v0.4.0-beta.19.0.20240518125136-f67a0988a636/common/json/internal/contextjson/decode_context.go (about)

     1  package json
     2  
     3  import "strconv"
     4  
     5  type decodeContext struct {
     6  	parent *decodeContext
     7  	index  int
     8  	key    string
     9  }
    10  
    11  func (d *decodeState) formatContext() string {
    12  	var description string
    13  	context := d.context
    14  	var appendDot bool
    15  	for context != nil {
    16  		if appendDot {
    17  			description = "." + description
    18  		}
    19  		if context.key != "" {
    20  			description = context.key + description
    21  			appendDot = true
    22  		} else {
    23  			description = "[" + strconv.Itoa(context.index) + "]" + description
    24  			appendDot = false
    25  		}
    26  		context = context.parent
    27  	}
    28  	return description
    29  }
    30  
    31  type contextError struct {
    32  	parent  error
    33  	context string
    34  	index   bool
    35  }
    36  
    37  func (c *contextError) Unwrap() error {
    38  	return c.parent
    39  }
    40  
    41  func (c *contextError) Error() string {
    42  	//goland:noinspection GoTypeAssertionOnErrors
    43  	switch c.parent.(type) {
    44  	case *contextError:
    45  		return c.context + "." + c.parent.Error()
    46  	default:
    47  		return c.context + ": " + c.parent.Error()
    48  	}
    49  }