Debian Codegraph @

internal/ctxerr/path.go

raw
1 // Copyright (c) 2019, Maxime Soulé
2 // All rights reserved.
3 //
4 // This source code is licensed under the BSD-style license found in the
5 // LICENSE file in the root directory of this source tree.
6
7 package ctxerr
8
9 import (
10 "strconv"
11 "strings"
12
13 "github.com/maxatome/go-testdeep/internal/util"
14 )
15
16 // Path defines a structure depth path, typically used to mark a
17 // position during a deep traversal in case of error.
18 type Path []pathLevel
19
20 type pathLevelKind uint8
21
22 type pathLevel struct {
23 Content string
24 Pointers int
25 Kind pathLevelKind
26 }
27
28 const (
29 levelStruct pathLevelKind = iota
30 levelArray
31 levelMap
32 levelFunc
33 levelCustom
34 )
35
36 // NewPath returns a new [Path] initialized with root root node.
37 func NewPath(root string) Path {
38 return Path{
39 {
40 Kind: levelCustom,
41 Content: root,
42 },
43 }
44 }
45
46 // Len returns the number of levels, excluding pointers ones.
47 func (p Path) Len() int {
48 return len(p)
49 }
50
51 // Equal returns true if p and o are equal, false otherwise.
52 func (p Path) Equal(o Path) bool {
53 if len(p) != len(o) {
54 return false
55 }
56 for i := len(p) - 1; i >= 0; i-- {
57 if p[i] != o[i] {
58 return false
59 }
60 }
61 return true
62 }
63
64 func (p Path) addLevel(level pathLevel) Path {
65 np := make(Path, len(p), len(p)+1)
66 copy(np, p)
67 return append(np, level)
68 }
69
70 // Copy returns a new [Path], exact but independent copy of p.
71 func (p Path) Copy() Path {
72 if p == nil {
73 return nil
74 }
75
76 np := make(Path, len(p))
77 copy(np, p)
78 return np
79 }
80
81 // AddField adds a level corresponding to a struct field.
82 func (p Path) AddField(field string) Path {
83 if p == nil {
84 return nil
85 }
86
87 np := p.addLevel(pathLevel{
88 Kind: levelStruct,
89 Content: field,
90 })
91
92 if len(np) > 1 && np[len(np)-2].Pointers > 0 {
93 np[len(np)-2].Pointers--
94 }
95
96 return np
97 }
98
99 // AddArrayIndex adds a level corresponding to an array index.
100 func (p Path) AddArrayIndex(index int) Path {
101 if p == nil {
102 return nil
103 }
104
105 return p.addLevel(pathLevel{
106 Kind: levelArray,
107 Content: strconv.Itoa(index),
108 })
109 }
110
111 // AddMapKey adds a level corresponding to a map key.
112 func (p Path) AddMapKey(key any) Path {
113 if p == nil {
114 return nil
115 }
116
117 return p.addLevel(pathLevel{
118 Kind: levelMap,
119 Content: util.ToString(key),
120 })
121 }
122
123 // AddPtr adds num pointers levels.
124 func (p Path) AddPtr(num int) Path {
125 if p == nil {
126 return nil
127 }
128
129 np := p.Copy()
130 // Do not check len(np) > 0, as it should
131 np[len(np)-1].Pointers += num
132 return np
133 }
134
135 // AddFunctionCall adds a level corresponding to a function call.
136 func (p Path) AddFunctionCall(fn string) Path {
137 if p == nil {
138 return nil
139 }
140
141 return p.addLevel(pathLevel{
142 Kind: levelFunc,
143 Content: fn,
144 })
145 }
146
147 // AddCustomLevel adds a custom level.
148 func (p Path) AddCustomLevel(custom string) Path {
149 if p == nil {
150 return nil
151 }
152
153 return p.addLevel(pathLevel{
154 Kind: levelCustom,
155 Content: custom,
156 })
157 }
158
159 func (p Path) String() string {
160 if len(p) == 0 {
161 return ""
162 }
163
164 var str string
165
166 for i, level := range p {
167 var ptrs string
168 if level.Pointers > 0 {
169 ptrs = strings.Repeat("*", level.Pointers)
170 }
171
172 if level.Kind == levelFunc {
173 str = ptrs + level.Content + "(" + str + ")"
174 } else {
175 if i > 0 && p[i-1].Pointers > 0 {
176 // Last level contains pointer(s), protect them
177 str = ptrs + "(" + str + ")"
178 } else {
179 str = ptrs + str
180 }
181
182 switch level.Kind {
183 case levelStruct:
184 str += "." + level.Content
185 case levelArray, levelMap:
186 str += "[" + level.Content + "]"
187 default:
188 str += level.Content
189 }
190 }
191 }
192
193 return str
194 }