MCPcopy Create free account
hub / github.com/google/go-cmp / GoString

Method GoString

cmp/path.go:121–158  ·  view source on GitHub ↗

GoString returns the path to a specific node using Go syntax. For example: (*root.MyMap["key"].(*mypkg.MyStruct).MySlices)[2][3].MyField

()

Source from the content-addressed store, hash-verified

119//
120// (*root.MyMap["key"].(*mypkg.MyStruct).MySlices)[2][3].MyField
121func (pa Path) GoString() string {
122 var ssPre, ssPost []string
123 var numIndirect int
124 for i, s := range pa {
125 var nextStep PathStep
126 if i+1 < len(pa) {
127 nextStep = pa[i+1]
128 }
129 switch s := s.(type) {
130 case Indirect:
131 numIndirect++
132 pPre, pPost := "(", ")"
133 switch nextStep.(type) {
134 case Indirect:
135 continue // Next step is indirection, so let them batch up
136 case StructField:
137 numIndirect-- // Automatic indirection on struct fields
138 case nil:
139 pPre, pPost = "", "" // Last step; no need for parenthesis
140 }
141 if numIndirect > 0 {
142 ssPre = append(ssPre, pPre+strings.Repeat("*", numIndirect))
143 ssPost = append(ssPost, pPost)
144 }
145 numIndirect = 0
146 continue
147 case Transform:
148 ssPre = append(ssPre, s.trans.name+"(")
149 ssPost = append(ssPost, ")")
150 continue
151 }
152 ssPost = append(ssPost, s.String())
153 }
154 for i, j := 0, len(ssPre)-1; i < j; i, j = i+1, j-1 {
155 ssPre[i], ssPre[j] = ssPre[j], ssPre[i]
156 }
157 return strings.Join(ssPre, "") + strings.Join(ssPost, "")
158}
159
160type pathStep struct {
161 typ reflect.Type

Callers

nothing calls this directly

Calls 1

StringMethod · 0.95

Tested by

no test coverage detected