-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheprefnanobot.go
444 lines (356 loc) · 10.2 KB
/
eprefnanobot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package errpref
import (
"strings"
"sync"
)
type errPrefNanobot struct {
lock *sync.Mutex
}
// addEPrefInfo - Receives new error prefix and context strings.
// The method then proceeds to convert this pair of strings to an
// ErrorPrefixInfo object before adding that object to the end of
// an array of ErrorPrefixInfo objects.
//
//
//
// ----------------------------------------------------------------
//
// Input Parameters
//
// newErrPrefix string
// - The new error prefix string. Typically this is the
// name of the function or method associated which is
// currently executing.
//
//
// newErrContext string
// - This is the error context information associated with the
// new error prefix ('newErrPrefix'). This parameter is
// optional and will accept an empty string.
//
//
// -----------------------------------------------------------------
//
// Return Values
//
// --- NONE ---
//
func (ePrefNanobot *errPrefNanobot) addEPrefInfo(
newErrPrefix string,
newErrContext string,
errPrefixCollection *[]ErrorPrefixInfo) {
if ePrefNanobot.lock == nil {
ePrefNanobot.lock = new(sync.Mutex)
}
ePrefNanobot.lock.Lock()
defer ePrefNanobot.lock.Unlock()
var (
lenPrefixCleanStr,
lenContextCleanStr int
)
ePrefElectron := errPrefElectron{}
newErrPrefix,
lenPrefixCleanStr =
ePrefElectron.cleanErrorPrefixStr(
newErrPrefix)
if lenPrefixCleanStr == 0 {
return
}
newErrContext,
lenContextCleanStr =
ePrefElectron.cleanErrorContextStr(
newErrContext)
newErrorPrefixInfo := ErrorPrefixInfo{}
newErrorPrefixInfo.SetErrPrefixStr(
newErrPrefix)
if lenContextCleanStr > 0 {
newErrorPrefixInfo.SetErrContextStr(
newErrContext)
}
if *errPrefixCollection == nil {
*errPrefixCollection = make([]ErrorPrefixInfo, 0)
}
*errPrefixCollection = append(
*errPrefixCollection,
newErrorPrefixInfo)
return
}
// extractLastErrPrfInfo - Extracts the last error prefix element
// from a string comprised of a series of error prefix elements.
//
// This method applies system default string delimiters when
// parsing error prefix strings.
//
func (ePrefNanobot *errPrefNanobot) extractLastErrPrfInfo(
errPref string) ErrorPrefixInfo {
if ePrefNanobot.lock == nil {
ePrefNanobot.lock = new(sync.Mutex)
}
ePrefNanobot.lock.Lock()
defer ePrefNanobot.lock.Unlock()
prefixContextCol := make([]ErrorPrefixInfo, 0)
delimiters := ErrPrefixDelimiters{}.NewDefaults()
errPrefixDtoAtom{}.
ptr().getEPrefContextArray(
errPref,
delimiters,
&prefixContextCol)
lenCollection := len(prefixContextCol)
if lenCollection == 0 {
return ErrorPrefixInfo{}
}
lastEPref := prefixContextCol[lenCollection-1]
lastEPref.SetIsLastIndex(false)
lastEPref.SetIsFirstIndex(false)
return lastEPref
}
// formatErrPrefixComponents - Returns a string of formatted error
// prefix information based on an array of error prefix elements.
//
//
// ----------------------------------------------------------------
//
// Input Parameters
//
// leadingTextStr string
// - This text string will be prefixed to the beginning of the
// formatted error prefix information returned by this
// method.
//
//
// trailingTextStr string
// - This text string will be appended or suffixed to the end
// of the formatted error prefix information returned by this
// method.
//
//
// maxErrPrefixTextLineLength uint
// - This unsigned integer value will be used to set the
// maximum number of characters allowed in a text display
// line for error prefix information.
//
// If 'maxErrPrefixTextLineLength' is set to a value of zero
// (0), this method automatically set this value to the
// current default maximum line length.
//
//
// isLastLineTerminatedWithNewLine bool
// - By default, the last line of error prefix strings ARE NOT
// terminated with a new line character ('\n'). In other
// words, by default, the last line of returned error prefix
// strings do not end with a new line character ('\n').
//
// If this parameter is set to 'true', the last line of the
// error prefix strings returned by this method WILL BE
// terminated with a new line character ('\n').
//
//
// delimiters ErrPrefixDelimiters
// - An instance of ErrPrefixDelimiters containing string
// delimiters used to join error prefix and error context
// elements.
//
// The key components of an ErrPrefixDelimiters object are
// listed as follows:
// New Line Error Prefix Delimiter
// In-Line Error Prefix Delimiter
// New Line Error Context Delimiter
// In-Line Error Context Delimiter
//
//
// prefixContextCol []ErrorPrefixInfo
// - An array of ErrorPrefixInfo objects containing the error
// prefix and error context information which will be
// converted to a single string returned by this method.
//
// -----------------------------------------------------------------
//
// Return Values
//
// string
// - This method will return a string containing formatted
// error prefix and error context information.
//
func (ePrefNanobot *errPrefNanobot) formatErrPrefixComponents(
leadingTextStr string,
trailingTextStr string,
maxErrPrefixTextLineLength uint,
isLastLineTerminatedWithNewLine bool,
delimiters ErrPrefixDelimiters,
prefixContextCol []ErrorPrefixInfo) string {
if ePrefNanobot.lock == nil {
ePrefNanobot.lock = new(sync.Mutex)
}
ePrefNanobot.lock.Lock()
defer ePrefNanobot.lock.Unlock()
localErrPrefix := "errPrefNanobot.formatErrPrefixComponents() "
lenPrefixContextCol := len(prefixContextCol)
if lenPrefixContextCol == 0 {
return localErrPrefix +
"len(prefixContextCol)==0\n"
}
err := delimiters.IsValidInstanceError(
localErrPrefix)
if err != nil {
return err.Error()
}
if maxErrPrefixTextLineLength == 0 {
maxErrPrefixTextLineLength =
errPrefQuark{}.ptr().
getErrPrefDisplayLineLength()
}
lineLenCalculator := EPrefixLineLenCalc{}
err =
lineLenCalculator.SetEPrefDelimiters(
delimiters,
localErrPrefix)
if err != nil {
return err.Error()
}
lineLenCalculator.SetMaxErrStringLength(
maxErrPrefixTextLineLength)
var b1 strings.Builder
b1.Grow(1024)
lineLenCalculator.SetCurrentLineStr("")
lastIdx := lenPrefixContextCol - 1
ePrefMolecule := errPrefMolecule{}
if len(leadingTextStr) > 0 {
b1.WriteString(leadingTextStr)
}
for i := 0; i < lenPrefixContextCol; i++ {
if i == lastIdx {
prefixContextCol[i].SetIsLastIndex(true)
}
err =
lineLenCalculator.SetErrPrefixInfo(
&prefixContextCol[i],
localErrPrefix)
if err != nil {
return err.Error()
}
err =
lineLenCalculator.IsValidInstanceError(
localErrPrefix)
if err != nil {
return err.Error()
}
if lineLenCalculator.ErrPrefixHasContext() {
err = ePrefMolecule.writeNewEPrefWithContext(
&b1,
&lineLenCalculator)
if err != nil {
return err.Error()
}
continue
} else {
err = ePrefMolecule.writeNewEPrefWithOutContext(
&b1,
&lineLenCalculator)
if err != nil {
return err.Error()
}
}
}
if lineLenCalculator.GetCurrLineStrLength() > 0 {
errPrefNeutron{}.ptr().writeCurrentLineStr(
&b1,
&lineLenCalculator)
}
if isLastLineTerminatedWithNewLine {
b1.WriteRune('\n')
}
if len(trailingTextStr) > 0 {
b1.WriteString(trailingTextStr)
}
lineLenCalculator.Empty()
return b1.String()
}
// ptr() - Returns a pointer to a new instance of
// errPrefNanobot.
//
func (ePrefNanobot errPrefNanobot) ptr() *errPrefNanobot {
if ePrefNanobot.lock == nil {
ePrefNanobot.lock = new(sync.Mutex)
}
ePrefNanobot.lock.Lock()
defer ePrefNanobot.lock.Unlock()
return &errPrefNanobot{}
}
// setLastCtx - Sets or resets the error context for the last
// error prefix in the error prefix collection passed as an input
// parameter. This operation either adds, or replaces, the error
// context string associated with the last error prefix the
// current list of error prefixes contained in the error prefix
// collection.
//
// If the last error prefix already has an error context string, it
// will be replaced by input parameter, 'newErrContext'.
//
// If the last error prefix does NOT have an associated error
// context, this new error context string will be associated
// with that error prefix.
//
//
// ----------------------------------------------------------------
//
// Input Parameters
//
// newErrContext string
// - This string holds the new error context information. If
// the last error prefix in the 'errPrefixCollection' already
// has an associated error context, that context will be
// deleted and replaced by 'newErrContext'. If, however, the
// last error prefix does NOT have an associated error
// context, this 'newErrContext' string will be added and
// associated with that last error prefix.
//
// If 'newErrContext' is 'empty', this method will take no
// action and exit.
//
//
// errPrefixCollection []ErrorPrefixInfo
// - An array of ErrorPrefixInfo objects. Each object defines
// an error prefix/context pair. The 'newErrContext' string
// will be configured as the error context data for the last
// ErrorPrefixInfo object in this collection.
//
// If the 'errPrefixCollection' is empty, this method will
// take no action and exit.
//
//
// -----------------------------------------------------------------
//
// Return Values
//
//
func (ePrefNanobot *errPrefNanobot) setLastCtx(
newErrContext string,
errPrefixCollection []ErrorPrefixInfo) {
if ePrefNanobot.lock == nil {
ePrefNanobot.lock = new(sync.Mutex)
}
ePrefNanobot.lock.Lock()
defer ePrefNanobot.lock.Unlock()
var (
lenCleanStr,
lenCollection int
)
newErrContext,
lenCleanStr =
errPrefElectron{}.ptr().cleanErrorContextStr(
newErrContext)
if lenCleanStr == 0 {
return
}
if errPrefixCollection == nil {
errPrefixCollection = make([]ErrorPrefixInfo, 0)
}
lenCollection = len(errPrefixCollection)
if lenCollection == 0 {
return
}
lenCollection--
errPrefixCollection[lenCollection].
SetErrContextStr(newErrContext)
return
}