Source File
path.go
Belonging Package
github.com/google/go-cmp/cmp
package cmp
import (
)
func ( Path) () string {
var []string
for , := range {
if , := .(StructField); {
= append(, .String())
}
}
return strings.TrimPrefix(strings.Join(, ""), ".")
}
func ( Path) () string {
var , []string
var int
for , := range {
var PathStep
if +1 < len() {
= [+1]
}
switch s := .(type) {
case Indirect:
++
, := "(", ")"
switch .(type) {
case Indirect:
continue // Next step is indirection, so let them batch up
case StructField:
-- // Automatic indirection on struct fields
case nil:
, = "", "" // Last step; no need for parenthesis
}
if > 0 {
= append(, +strings.Repeat("*", ))
= append(, )
}
= 0
continue
case Transform:
= append(, .trans.name+"(")
= append(, ")")
continue
}
= append(, .String())
}
for , := 0, len()-1; < ; , = +1, -1 {
[], [] = [], []
}
return strings.Join(, "") + strings.Join(, "")
}
type pathStep struct {
typ reflect.Type
vx, vy reflect.Value
}
func ( pathStep) () reflect.Type { return .typ }
func ( pathStep) () (, reflect.Value) { return .vx, .vy }
func ( pathStep) () string {
if .typ == nil {
return "<nil>"
}
:= .typ.String()
if == "" || strings.ContainsAny(, "{}\n") {
return "root" // Type too simple or complex to print
}
return fmt.Sprintf("{%s}", )
}
type StructField struct{ *structField }
type structField struct {
pathStep
name string
idx int
unexported bool
mayForce bool // Forcibly allow visibility
paddr bool // Was parent addressable?
pvx, pvy reflect.Value // Parent values (always addressible)
field reflect.StructField // Field information
}
func ( StructField) () reflect.Type { return .typ }
func ( StructField) () (, reflect.Value) {
if !.unexported {
return .vx, .vy // CanInterface reports true
}
func ( StructField) () string { return .name }
func ( StructField) () int { return .idx }
type SliceIndex struct{ *sliceIndex }
type sliceIndex struct {
pathStep
xkey, ykey int
isSlice bool // False for reflect.Array
}
func ( SliceIndex) () reflect.Type { return .typ }
func ( SliceIndex) () (, reflect.Value) { return .vx, .vy }
func ( SliceIndex) () string {
switch {
case .xkey == .ykey:
return fmt.Sprintf("[%d]", .xkey)
func ( SliceIndex) () int {
if .xkey != .ykey {
return -1
}
return .xkey
}
func ( SliceIndex) () (, int) { return .xkey, .ykey }
type TypeAssertion struct{ *typeAssertion }
type typeAssertion struct {
pathStep
}
func ( TypeAssertion) () reflect.Type { return .typ }
func ( TypeAssertion) () (, reflect.Value) { return .vx, .vy }
func ( TypeAssertion) () string { return fmt.Sprintf(".(%v)", .typ) }
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |