|
| 1 | +package errors |
| 2 | + |
| 3 | +import ( |
| 4 | + "regexp" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +const ( |
| 9 | + braketOpen = "[" |
| 10 | + parenOpen = "(" |
| 11 | + curlyOpen = "{" |
| 12 | + rootOpen = "*" |
| 13 | + customOpen = "~" |
| 14 | + rawOpen = "...|" |
| 15 | + |
| 16 | + braketClose = "]" |
| 17 | + parenClose = ")" |
| 18 | + curlyClose = "}" |
| 19 | + rawClose = "|..." |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + sepOpeners = map[string]string{ |
| 24 | + braketOpen: string(braketOpen), |
| 25 | + parenOpen: string(parenOpen), |
| 26 | + curlyOpen: string(curlyOpen), |
| 27 | + rootOpen: "", |
| 28 | + customOpen: "", |
| 29 | + rawOpen: "", |
| 30 | + } |
| 31 | + sepClosers = map[string]string{ |
| 32 | + braketOpen: string(braketClose), |
| 33 | + parenOpen: string(parenClose), |
| 34 | + curlyOpen: string(curlyClose), |
| 35 | + rootOpen: "", |
| 36 | + customOpen: "", |
| 37 | + rawOpen: "", |
| 38 | + } |
| 39 | +) |
| 40 | + |
| 41 | +type ErrorPiece struct { |
| 42 | + Value string |
| 43 | + |
| 44 | + ContainerType string // eg *,{,[,( |
| 45 | + Pieces []*ErrorPiece |
| 46 | + |
| 47 | + ReorganizePiecesAroundCommas bool |
| 48 | + FormatPiecesAsList bool |
| 49 | +} |
| 50 | + |
| 51 | +func NewErrorPiecesFromString(str string) (*ErrorPiece, bool) { |
| 52 | + rootPiece := &ErrorPiece{ |
| 53 | + ContainerType: rootOpen, |
| 54 | + } |
| 55 | + stack := []*ErrorPiece{rootPiece} |
| 56 | + |
| 57 | + for i, f := range str { |
| 58 | + if len(stack) == 0 { |
| 59 | + return rootPiece, false |
| 60 | + } |
| 61 | + |
| 62 | + charStr := string(f) |
| 63 | + |
| 64 | + if !stack[len(stack)-1].IsLeafContainer() { |
| 65 | + switch charStr { |
| 66 | + case braketOpen, curlyOpen, parenOpen: |
| 67 | + piece := &ErrorPiece{ContainerType: charStr} |
| 68 | + stack[len(stack)-1].AddPiece(piece) |
| 69 | + stack = append(stack, piece) |
| 70 | + continue |
| 71 | + |
| 72 | + case braketClose, curlyClose, parenClose: |
| 73 | + stack = stack[:len(stack)-1] |
| 74 | + continue |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + switch { |
| 79 | + case checkForward(str, i, rawOpen): |
| 80 | + piece := &ErrorPiece{ContainerType: rawOpen} |
| 81 | + piece.AddStr(charStr) |
| 82 | + stack[len(stack)-1].AddPiece(piece) |
| 83 | + stack = append(stack, piece) |
| 84 | + |
| 85 | + case checkBackward(str, i, rawClose): |
| 86 | + stack[len(stack)-1].AddStr(charStr) |
| 87 | + stack = stack[:len(stack)-1] |
| 88 | + |
| 89 | + default: |
| 90 | + stack[len(stack)-1].AddStr(charStr) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return rootPiece, len(stack) == 1 |
| 95 | +} |
| 96 | + |
| 97 | +func checkForward(str string, i int, sep string) bool { |
| 98 | + endIdx := i + len(sep) |
| 99 | + if endIdx < len(str) { |
| 100 | + return str[i:endIdx] == sep |
| 101 | + } |
| 102 | + return false |
| 103 | +} |
| 104 | + |
| 105 | +func checkBackward(str string, i int, sep string) bool { |
| 106 | + startIdx := i - len(sep) + 1 |
| 107 | + if startIdx >= 0 && i < len(str) { |
| 108 | + return str[startIdx:i+1] == sep |
| 109 | + } |
| 110 | + return false |
| 111 | +} |
| 112 | + |
| 113 | +func (p *ErrorPiece) IsContainer() bool { |
| 114 | + return len(p.ContainerType) > 0 |
| 115 | +} |
| 116 | + |
| 117 | +func (p *ErrorPiece) IsLeafContainer() bool { |
| 118 | + return p.ContainerType == rawOpen |
| 119 | +} |
| 120 | + |
| 121 | +func (p *ErrorPiece) AddPiece(piece *ErrorPiece) { |
| 122 | + p.Pieces = append(p.Pieces, piece) |
| 123 | +} |
| 124 | + |
| 125 | +func (p *ErrorPiece) AddStr(str string) { |
| 126 | + var lastPiece *ErrorPiece |
| 127 | + if len(p.Pieces) > 0 { |
| 128 | + lastPiece = p.Pieces[len(p.Pieces)-1] |
| 129 | + } |
| 130 | + if lastPiece == nil || lastPiece.IsContainer() { |
| 131 | + lastPiece = &ErrorPiece{} |
| 132 | + p.Pieces = append(p.Pieces, lastPiece) |
| 133 | + } |
| 134 | + lastPiece.Value += str |
| 135 | +} |
| 136 | + |
| 137 | +var ( |
| 138 | + listItemSep = ", " |
| 139 | + likelyListItemStart = regexp.MustCompile(`^[a-z\.\[\]:\s]{3,10}`) |
| 140 | +) |
| 141 | + |
| 142 | +func (p *ErrorPiece) ReorganizePieces() { |
| 143 | + for _, piece := range p.Pieces { |
| 144 | + piece.ReorganizePieces() |
| 145 | + } |
| 146 | + |
| 147 | + if p.ReorganizePiecesAroundCommas { |
| 148 | + p.ReorganizePiecesAroundCommas = false |
| 149 | + |
| 150 | + var newPieces []*ErrorPiece |
| 151 | + |
| 152 | + for _, piece := range p.Pieces { |
| 153 | + switch { |
| 154 | + case len(piece.Value) > 0: |
| 155 | + for i, val := range strings.Split(piece.Value, listItemSep) { |
| 156 | + switch { |
| 157 | + case len(newPieces) > 0 && i == 0: |
| 158 | + newPieces[len(newPieces)-1].AddStr(val) |
| 159 | + |
| 160 | + case len(newPieces) > 0 && !likelyListItemStart.MatchString(val): |
| 161 | + newPieces[len(newPieces)-1].AddStr(listItemSep + val) |
| 162 | + |
| 163 | + default: |
| 164 | + newPieces = append(newPieces, &ErrorPiece{ |
| 165 | + ContainerType: customOpen, |
| 166 | + Pieces: []*ErrorPiece{{Value: val}}, |
| 167 | + }) |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + case p.IsContainer(): |
| 172 | + if len(newPieces) > 0 { |
| 173 | + newPieces[len(newPieces)-1].AddPiece(piece) |
| 174 | + } else { |
| 175 | + newPieces = append(newPieces, &ErrorPiece{ |
| 176 | + ContainerType: customOpen, |
| 177 | + Pieces: []*ErrorPiece{piece}, |
| 178 | + }) |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + if len(newPieces) > 1 { |
| 184 | + p.Pieces = newPieces |
| 185 | + p.FormatPiecesAsList = true |
| 186 | + } |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +func (p *ErrorPiece) AsString() string { |
| 191 | + if len(p.Value) > 0 { |
| 192 | + return p.Value |
| 193 | + } |
| 194 | + |
| 195 | + var result string |
| 196 | + |
| 197 | + if p.FormatPiecesAsList { |
| 198 | + if len(p.Pieces) > 0 { |
| 199 | + result += "\n\n" |
| 200 | + } |
| 201 | + for _, piece := range p.Pieces { |
| 202 | + result += " - " + piece.AsString() + "\n\n" |
| 203 | + } |
| 204 | + } else { |
| 205 | + result += sepOpeners[p.ContainerType] |
| 206 | + for _, piece := range p.Pieces { |
| 207 | + result += piece.AsString() |
| 208 | + } |
| 209 | + result += sepClosers[p.ContainerType] |
| 210 | + } |
| 211 | + |
| 212 | + return result |
| 213 | +} |
| 214 | + |
| 215 | +func (p *ErrorPiece) AsIndentedString(indent string) string { |
| 216 | + if len(p.Value) > 0 { |
| 217 | + return indent + "+ " + p.Value + "\n" |
| 218 | + } |
| 219 | + var result string |
| 220 | + if p.IsContainer() { |
| 221 | + result += indent + "+ ...\n" |
| 222 | + } |
| 223 | + for _, piece := range p.Pieces { |
| 224 | + result += piece.AsIndentedString(indent + " ") |
| 225 | + } |
| 226 | + return result |
| 227 | +} |
0 commit comments