1 // Copyright 2017, The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE.md file.
15 // Path is a list of PathSteps describing the sequence of operations to get
16 // from some root type to the current position in the value tree.
17 // The first Path element is always an operation-less PathStep that exists
18 // simply to identify the initial type.
20 // When traversing structs with embedded structs, the embedded struct will
21 // always be accessed as a field before traversing the fields of the
22 // embedded struct themselves. That is, an exported field from the
23 // embedded struct will never be accessed directly from the parent struct.
26 // PathStep is a union-type for specific operations to traverse
27 // a value's tree structure. Users of this package never need to implement
28 // these types as values of this type will be returned by this package.
30 // Implementations of this interface are
31 // StructField, SliceIndex, MapIndex, Indirect, TypeAssertion, and Transform.
32 type PathStep interface {
35 // Type is the resulting type after performing the path step.
38 // Values is the resulting values after performing the path step.
39 // The type of each valid value is guaranteed to be identical to Type.
41 // In some cases, one or both may be invalid or have restrictions:
42 // • For StructField, both are not interface-able if the current field
43 // is unexported and the struct type is not explicitly permitted by
44 // AllowUnexported to traverse unexported fields.
45 // • For SliceIndex, one may be invalid if an element is missing from
46 // either the x or y slice.
47 // • For MapIndex, one may be invalid if an entry is missing from
48 // either the x or y map.
50 // The provided values must not be mutated.
51 Values() (vx, vy reflect.Value)
55 _ PathStep = StructField{}
56 _ PathStep = SliceIndex{}
57 _ PathStep = MapIndex{}
58 _ PathStep = Indirect{}
59 _ PathStep = TypeAssertion{}
60 _ PathStep = Transform{}
63 func (pa *Path) push(s PathStep) {
67 func (pa *Path) pop() {
68 *pa = (*pa)[:len(*pa)-1]
71 // Last returns the last PathStep in the Path.
72 // If the path is empty, this returns a non-nil PathStep that reports a nil Type.
73 func (pa Path) Last() PathStep {
77 // Index returns the ith step in the Path and supports negative indexing.
78 // A negative index starts counting from the tail of the Path such that -1
79 // refers to the last step, -2 refers to the second-to-last step, and so on.
80 // If index is invalid, this returns a non-nil PathStep that reports a nil Type.
81 func (pa Path) Index(i int) PathStep {
85 if i < 0 || i >= len(pa) {
91 // String returns the simplified path to a node.
92 // The simplified path only contains struct field accesses.
95 // MyMap.MySlices.MyField
96 func (pa Path) String() string {
98 for _, s := range pa {
99 if _, ok := s.(StructField); ok {
100 ss = append(ss, s.String())
103 return strings.TrimPrefix(strings.Join(ss, ""), ".")
106 // GoString returns the path to a specific node using Go syntax.
109 // (*root.MyMap["key"].(*mypkg.MyStruct).MySlices)[2][3].MyField
110 func (pa Path) GoString() string {
111 var ssPre, ssPost []string
113 for i, s := range pa {
114 var nextStep PathStep
118 switch s := s.(type) {
121 pPre, pPost := "(", ")"
122 switch nextStep.(type) {
124 continue // Next step is indirection, so let them batch up
126 numIndirect-- // Automatic indirection on struct fields
128 pPre, pPost = "", "" // Last step; no need for parenthesis
131 ssPre = append(ssPre, pPre+strings.Repeat("*", numIndirect))
132 ssPost = append(ssPost, pPost)
137 ssPre = append(ssPre, s.trans.name+"(")
138 ssPost = append(ssPost, ")")
141 ssPost = append(ssPost, s.String())
143 for i, j := 0, len(ssPre)-1; i < j; i, j = i+1, j-1 {
144 ssPre[i], ssPre[j] = ssPre[j], ssPre[i]
146 return strings.Join(ssPre, "") + strings.Join(ssPost, "")
149 type pathStep struct {
154 func (ps pathStep) Type() reflect.Type { return ps.typ }
155 func (ps pathStep) Values() (vx, vy reflect.Value) { return ps.vx, ps.vy }
156 func (ps pathStep) String() string {
161 if s == "" || strings.ContainsAny(s, "{}\n") {
162 return "root" // Type too simple or complex to print
164 return fmt.Sprintf("{%s}", s)
167 // StructField represents a struct field access on a field called Name.
168 type StructField struct{ *structField }
169 type structField struct {
174 // These fields are used for forcibly accessing an unexported field.
175 // pvx, pvy, and field are only valid if unexported is true.
177 mayForce bool // Forcibly allow visibility
178 pvx, pvy reflect.Value // Parent values
179 field reflect.StructField // Field information
182 func (sf StructField) Type() reflect.Type { return sf.typ }
183 func (sf StructField) Values() (vx, vy reflect.Value) {
185 return sf.vx, sf.vy // CanInterface reports true
188 // Forcibly obtain read-write access to an unexported struct field.
190 vx = retrieveUnexportedField(sf.pvx, sf.field)
191 vy = retrieveUnexportedField(sf.pvy, sf.field)
192 return vx, vy // CanInterface reports true
194 return sf.vx, sf.vy // CanInterface reports false
196 func (sf StructField) String() string { return fmt.Sprintf(".%s", sf.name) }
198 // Name is the field name.
199 func (sf StructField) Name() string { return sf.name }
201 // Index is the index of the field in the parent struct type.
202 // See reflect.Type.Field.
203 func (sf StructField) Index() int { return sf.idx }
205 // SliceIndex is an index operation on a slice or array at some index Key.
206 type SliceIndex struct{ *sliceIndex }
207 type sliceIndex struct {
212 func (si SliceIndex) Type() reflect.Type { return si.typ }
213 func (si SliceIndex) Values() (vx, vy reflect.Value) { return si.vx, si.vy }
214 func (si SliceIndex) String() string {
216 case si.xkey == si.ykey:
217 return fmt.Sprintf("[%d]", si.xkey)
219 // [5->?] means "I don't know where X[5] went"
220 return fmt.Sprintf("[%d->?]", si.xkey)
222 // [?->3] means "I don't know where Y[3] came from"
223 return fmt.Sprintf("[?->%d]", si.ykey)
225 // [5->3] means "X[5] moved to Y[3]"
226 return fmt.Sprintf("[%d->%d]", si.xkey, si.ykey)
230 // Key is the index key; it may return -1 if in a split state
231 func (si SliceIndex) Key() int {
232 if si.xkey != si.ykey {
238 // SplitKeys are the indexes for indexing into slices in the
239 // x and y values, respectively. These indexes may differ due to the
240 // insertion or removal of an element in one of the slices, causing
241 // all of the indexes to be shifted. If an index is -1, then that
242 // indicates that the element does not exist in the associated slice.
244 // Key is guaranteed to return -1 if and only if the indexes returned
245 // by SplitKeys are not the same. SplitKeys will never return -1 for
247 func (si SliceIndex) SplitKeys() (ix, iy int) { return si.xkey, si.ykey }
249 // MapIndex is an index operation on a map at some index Key.
250 type MapIndex struct{ *mapIndex }
251 type mapIndex struct {
256 func (mi MapIndex) Type() reflect.Type { return mi.typ }
257 func (mi MapIndex) Values() (vx, vy reflect.Value) { return mi.vx, mi.vy }
258 func (mi MapIndex) String() string { return fmt.Sprintf("[%#v]", mi.key) }
260 // Key is the value of the map key.
261 func (mi MapIndex) Key() reflect.Value { return mi.key }
263 // Indirect represents pointer indirection on the parent type.
264 type Indirect struct{ *indirect }
265 type indirect struct {
269 func (in Indirect) Type() reflect.Type { return in.typ }
270 func (in Indirect) Values() (vx, vy reflect.Value) { return in.vx, in.vy }
271 func (in Indirect) String() string { return "*" }
273 // TypeAssertion represents a type assertion on an interface.
274 type TypeAssertion struct{ *typeAssertion }
275 type typeAssertion struct {
279 func (ta TypeAssertion) Type() reflect.Type { return ta.typ }
280 func (ta TypeAssertion) Values() (vx, vy reflect.Value) { return ta.vx, ta.vy }
281 func (ta TypeAssertion) String() string { return fmt.Sprintf(".(%v)", ta.typ) }
283 // Transform is a transformation from the parent type to the current type.
284 type Transform struct{ *transform }
285 type transform struct {
290 func (tf Transform) Type() reflect.Type { return tf.typ }
291 func (tf Transform) Values() (vx, vy reflect.Value) { return tf.vx, tf.vy }
292 func (tf Transform) String() string { return fmt.Sprintf("%s()", tf.trans.name) }
294 // Name is the name of the Transformer.
295 func (tf Transform) Name() string { return tf.trans.name }
297 // Func is the function pointer to the transformer function.
298 func (tf Transform) Func() reflect.Value { return tf.trans.fnc }
300 // Option returns the originally constructed Transformer option.
301 // The == operator can be used to detect the exact option used.
302 func (tf Transform) Option() Option { return tf.trans }
304 // isExported reports whether the identifier is exported.
305 func isExported(id string) bool {
306 r, _ := utf8.DecodeRuneInString(id)
307 return unicode.IsUpper(r)