-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.go
753 lines (688 loc) · 21 KB
/
reader.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
package edx12
import (
"bytes"
"context"
"errors"
"fmt"
"strings"
"sync"
"unicode"
"unicode/utf8"
)
var ParseError = errors.New("parse error")
var _defaultReader = &Reader{}
var defaultTransactionSpecs = []*X12TransactionSetSpec{
X270v005010X279A1,
X271v005010X279A1,
X835v005010X221A1,
}
type RawSegment []string
func (s RawSegment) ID() string {
if len(s) == 0 {
return ""
}
return s[0]
}
func (r RawMessage) newSegmentNodes() ([]*X12Node, error) {
nodes := make([]*X12Node, 0, len(r.segments))
errs := []error{}
errFmt := "segment %d, element %d: %w"
for ind, rawSegment := range r.segments {
segID := rawSegment.ID()
segNode := &X12Node{
Type: SegmentNode,
Children: make([]*X12Node, 0, len(rawSegment)),
index: ind,
Name: segID,
}
nodes = append(nodes, segNode)
idNode, _ := NewNode(ElementNode, segID, segID)
segNode.Children = append(segNode.Children, idNode)
idNode.Parent = segNode
idSpec := &X12Spec{
Type: ElementSpec,
Usage: Required,
RepeatMin: 1,
RepeatMax: 1,
ValidCodes: []string{segID},
Description: "Segment ID",
DefaultVal: segID,
}
if err := idNode.SetSpec(idSpec); err != nil {
errs = append(errs, newNodeError(idNode, err))
}
if len(rawSegment) == 1 {
continue
}
for elemInd := 1; elemInd < len(rawSegment); elemInd++ {
// Segment ID is the first element, any element after that
// gets an index, so the first element value being `REF` means
// the next element gets named `REF01`, ...
elemNode := &X12Node{
Parent: segNode,
Type: ElementNode,
Name: fmt.Sprintf("%s%02d", segID, elemInd),
}
segNode.Children = append(segNode.Children, elemNode)
v := rawSegment[elemInd]
var containsRepetitionSeparator bool
var containsComponentSeparator bool
if segID != isaSegmentId {
containsRepetitionSeparator = strings.Contains(
v,
string(r.RepetitionSeparator),
)
containsComponentSeparator = strings.Contains(
v,
string(r.ComponentSeparator),
)
}
switch {
case containsRepetitionSeparator:
elemNode.Type = RepeatElementNode
elemNode.Value = strings.Split(
v,
string(r.RepetitionSeparator),
)
case containsComponentSeparator:
elemNode.Type = CompositeNode
values := strings.Split(
v,
string(r.ComponentSeparator),
)
if e := populateCompositeNode(
elemNode,
values...,
); e != nil {
errs = append(
errs,
newNodeError(
segNode,
fmt.Errorf(errFmt, ind, elemInd, e),
),
)
}
default:
elemNode.Value = make([]string, 1, 1)
elemNode.Value[0] = v
}
}
var controlSpec *X12Spec
switch segID {
case isaSegmentId:
controlSpec = isaHeaderSpec
case ieaSegmentId:
controlSpec = ieaTrailerSpec
case gsSegmentId:
controlSpec = gsHeaderSpec
case geSegmentId:
controlSpec = geTrailerSpec
case stSegmentId:
controlSpec = stHeaderSpec
case seSegmentId:
controlSpec = seTrailerSpec
}
if controlSpec != nil {
if err := segNode.SetSpec(controlSpec); err != nil {
errs = append(errs, newNodeError(segNode, err))
}
}
}
return nodes, errors.Join(errs...)
}
//func NewRawMessage(data []byte) (RawMessage, error) {
// msg := RawMessage{}
// if err := msg.Read(data); err != nil {
// return msg, err
// }
// return msg, nil
//}
type groupIndex struct {
Start int
}
// groupByFirstIndexValue takes a slice of slices, and returns a slice of slices
// where each slice's first element starts with the given startValue, and
// the last slice's first element ends with the given endValue.
// The returned slice of slices will contain all segments between them,
// inclusive
func groupByFirstIndexValue(
segments []RawSegment,
startValue string,
endValue string,
) (segmentGroups [][]RawSegment, err error) {
var indexes []*groupIndex
var currentIndex *groupIndex
for ind, segment := range segments {
segmentName := segment[0]
if segmentName == startValue {
if currentIndex != nil {
return segmentGroups, fmt.Errorf(
"found '%v' before '%v'",
startValue,
endValue,
)
}
currentIndex = &groupIndex{Start: ind}
indexes = append(indexes, currentIndex)
} else if segmentName == endValue {
if currentIndex == nil {
return segmentGroups, fmt.Errorf(
"unexpected state- found segment Trailer %v, but not currently in a segment group (already have: %v)",
segmentName,
segmentGroups,
)
}
segmentGroups = append(
segmentGroups,
segments[currentIndex.Start:ind+1],
)
currentIndex = nil
}
}
return segmentGroups, err
}
type RawMessage struct {
SegmentTerminator rune
ElementSeparator rune
RepetitionSeparator rune
ComponentSeparator rune
text string
segments []RawSegment
transactionSetSpecs []*X12TransactionSetSpec
reader *Reader
}
func (r *RawMessage) String() string {
var b strings.Builder
for _, segment := range r.segments {
for i, element := range segment {
if i > 0 {
b.WriteRune(r.ElementSeparator)
}
b.WriteString(element)
}
b.WriteRune(r.SegmentTerminator)
}
return b.String()
}
func (r *RawMessage) Segments() []RawSegment {
return r.segments
}
// FunctionalGroupSegments a slice of slices, where each slice begins with a GS
// segment and ends with a GE segment
func (r *RawMessage) FunctionalGroupSegments() ([][]RawSegment, error) {
return groupByFirstIndexValue(r.segments, gsSegmentId, geSegmentId)
}
// TransactionSegments returns a slice of slices, where each slice begins
// with an ST segment and ends with an SE segment
func (r *RawMessage) TransactionSegments() ([][]RawSegment, error) {
return groupByFirstIndexValue(r.segments, stSegmentId, seSegmentId)
}
// UnmarshalText implements the encoding.TextUnmarshaler interface
func (r *RawMessage) UnmarshalText(data []byte) error {
if r.reader == nil {
r.reader = _defaultReader
}
rawMessage, err := r.reader.Read(data)
*r = *rawMessage
return err
}
// Reader returns the Reader that was used to read/create the RawMessage,
// if any.
func (r *RawMessage) Reader() *Reader {
return r.reader
}
// Message creates a new Message node from the RawMessage segments.
// All TransactionSetNode nodes will attempt to have a TransactionSetSpec
// set, if one matches. Either the default set of specs will be used, or
// the ones attached to the Reader that was used to read the RawMessage.
// Additional specs can be provided, which will take precedence.
func (r *RawMessage) Message(
ctx context.Context,
transactionSetSpecs ...*X12TransactionSetSpec,
) (*Message, error) {
for _, txnSpec := range transactionSetSpecs {
if err := txnSpec.Validate(); err != nil {
return nil, err
}
}
if r.reader != nil {
for _, txnSpec := range r.reader.transactionSpecs {
transactionSetSpecs = append(transactionSetSpecs, txnSpec)
}
}
msg := &Message{
X12Node: &X12Node{
Type: MessageNode,
Name: "X12",
},
rawMessage: r,
}
var validationErrors []error
var isaNode *X12Node
var ieaNode *X12Node
segmentNodes, err := r.newSegmentNodes()
msg.segments = segmentNodes
msg.Children = []*X12Node{}
if err != nil {
validationErrors = append(validationErrors, err)
}
if len(segmentNodes) > 0 {
isaNode = segmentNodes[0]
ieaNode = segmentNodes[len(segmentNodes)-1]
} else {
validationErrors = append(
validationErrors,
fmt.Errorf("expected at least one segment, got zero"),
)
err = errors.Join(validationErrors...)
return msg, err
}
msg.Header = isaNode
msg.Trailer = ieaNode
for _, n := range segmentNodes {
n.Parent = msg.X12Node
}
// Group segments by ISA/IEA, then group the segments by GS/GE
groupedEnvelope, err := groupNodesByName(
isaSegmentId,
ieaSegmentId,
segmentNodes,
)
if err != nil {
validationErrors = append(validationErrors, err)
}
if len(groupedEnvelope) == 0 {
validationErrors = append(
validationErrors,
fmt.Errorf("expected at least one functional group, got zero"),
)
}
newMsgChildren := make(
[]*X12Node,
0,
len(groupedEnvelope)+2,
)
newMsgChildren = append(newMsgChildren, isaNode)
groupedSegments, err := groupNodesByName(
gsSegmentId, geSegmentId, groupedEnvelope[0],
)
if err != nil {
validationErrors = append(validationErrors, err)
}
msg.functionalGroups = make([]*FunctionalGroupNode, 0, len(groupedSegments))
// Create functional groups from gsSegmentId/geSegmentId-bounded segments
for _, functionalSegments := range groupedSegments {
fGroup := &FunctionalGroupNode{
X12Node: &X12Node{
Type: GroupNode,
Name: functionalGroupName,
},
}
msg.functionalGroups = append(msg.functionalGroups, fGroup)
newMsgChildren = append(newMsgChildren, fGroup.X12Node)
fGroup.Header = functionalSegments[0]
fGroup.Trailer = functionalSegments[len(functionalSegments)-1]
fGroup.Children = append(fGroup.Children, fGroup.Header)
transactionSegments, grpErr := groupNodesByName(
stSegmentId,
seSegmentId,
functionalSegments,
)
if grpErr != nil {
validationErrors = append(validationErrors, grpErr)
}
for _, stSegments := range transactionSegments {
t := &TransactionSetNode{
segments: stSegments,
X12Node: &X12Node{
Type: TransactionNode,
Name: transactionSetName,
Parent: fGroup.X12Node,
},
}
fGroup.Transactions = append(fGroup.Transactions, t)
fGroup.Children = append(fGroup.Children, t.X12Node)
t.Group = fGroup
t.Header = stSegments[0]
for i := 1; i < len(t.Header.Children); i++ {
switch i {
case stIndexTransactionSetCode:
t.TransactionSetCode = t.Header.Children[i].Value[0]
case stIndexVersionCode:
t.VersionCode = t.Header.Children[i].Value[0]
case stIndexControlNumber:
t.ControlNumber = t.Header.Children[i].Value[0]
}
}
t.Trailer = stSegments[len(stSegments)-1]
t.Children = append(t.Children, t.Header)
for ti := 1; ti < len(stSegments); ti++ {
t.Children = append(t.Children, stSegments[ti])
stSegments[ti].Parent = t.X12Node
}
var levels []*HierarchicalLevel
levels, err = createHierarchicalLevels(t.X12Node)
t.HierarchicalLevels = levels
if err != nil {
validationErrors = append(
validationErrors,
newNodeError(t.X12Node, err),
)
}
}
fGroup.Children = append(fGroup.Children, fGroup.Trailer)
for gi := 0; gi < len(fGroup.Children); gi++ {
fGroup.Children[gi].Parent = fGroup.X12Node
}
if ctx.Err() != nil {
break
}
}
newMsgChildren = append(newMsgChildren, ieaNode)
msg.Children = newMsgChildren
for i := 0; i < len(msg.Children); i++ {
msg.Children[i].Parent = msg.X12Node
}
validationErrors = append(validationErrors, msg.X12Node.setPath())
txnSets := msg.TransactionSets()
for i := 0; i < len(txnSets); i++ {
var txn *TransactionSetNode
txn = txnSets[i]
if err = txn.TransformWithContext(
ctx,
transactionSetSpecs...,
); err != nil {
validationErrors = append(validationErrors, err)
}
}
validationErrors = append(validationErrors, msg.X12Node.detectCycles())
err = errors.Join(validationErrors...)
return msg, err
}
type InterchangeHeader struct {
SegmentID string `json:"-"` // ISA segment ID
AuthInfoQualifier string `json:"authorizationQualifier"` // ISA01
AuthInfo string `json:"authorizationInformation"` // ISA02
SecurityInfoQualifier string `json:"securityInformationQualifier"` // ISA03
SecurityInfo string `json:"securityInformation"` // ISA04
SenderIDQualifier string `json:"senderIdQualifier"` // ISA05
SenderID string `json:"senderId"` // ISA06
ReceiverIDQualifier string `json:"receiverIdQualifier"` // ISA07
ReceiverID string `json:"receiverId"` // ISA08
Date string `json:"date"` // ISA09
Time string `json:"time"` // ISA10
RepetitionSeparator string `json:"repetitionSeparator"` // ISA11
Version string `json:"controlVersionNumber"` // ISA12
ControlNumber string `json:"controlNumber"` // ISA13
AckRequested string `json:"acknowledgmentRequested"` // ISA14
UsageIndicator string `json:"usageIndicator"` // ISA15
ComponentElementSeparator string `json:"componentElementSeparator"` // ISA16
}
type InterchangeTrailer struct {
SegmentID string `json:"-"` // IEA segment ID
NumberOfIncludedFunctionalGroups string `json:"functionalGroupCount"` // IEA01
ControlNumber string `json:"controlNumber"` // IEA02
}
type FunctionalGroupHeader struct {
SegmentID string `json:"-"` // GS segment ID
IdentifierCode string `json:"functionalIdentifierCode"` // GS01
ApplicationSenderCode string `json:"applicationSenderCode"` // GS02
ApplicationReceiverCode string `json:"applicationReceiverCode"` // GS03
Date string `json:"date"` // GS04
Time string `json:"time"` // GS05
ControlNumber string `json:"controlNumber"` // GS06
ResponsibleAgencyCode string `json:"responsibleAgencyCode"` // GS07
VersionReleaseIndustryIdentifierCode string `json:"versionCode"` // GS08
}
type FunctionalGroupTrailer struct {
SegmentID string `json:"-"' ` // GE segment ID
NumberOfTransactionSetsIncluded string `json:"transactionSetCount" ` // GE01
ControlNumber string `json:"controlNumber"` // GE02
}
// Header returns the ISA segment as an InterchangeHeader struct, or
// nil if the message has no ISA segment
func (r *RawMessage) Header() *InterchangeHeader {
if len(r.segments) == 0 {
return nil
}
seg := r.segments[0]
if len(seg) == 0 || seg[0] != isaSegmentId {
return nil
}
isaSegment := make([]string, isaElementCount, isaElementCount)
copy(isaSegment, seg)
return &InterchangeHeader{
isaSegment[isaIndexSegmentId],
isaSegment[isaIndexAuthInfoQualifier],
isaSegment[isaIndexAuthInfo],
isaSegment[isaIndexSecurityInfoQualifier],
isaSegment[isaIndexSecurityInfo],
isaSegment[isaIndexSenderIdQualifier],
isaSegment[isaIndexSenderId],
isaSegment[isaIndexReceiverIdQualifier],
isaSegment[isaIndexReceiverId],
isaSegment[isaIndexDate],
isaSegment[isaIndexTime],
isaSegment[isaIndexRepetitionSeparator],
isaSegment[isaIndexVersion],
isaSegment[isaIndexControlNumber],
isaSegment[isaIndexAckRequested],
isaSegment[isaIndexUsageIndicator],
isaSegment[isaIndexComponentElementSeparator],
}
}
// Trailer returns the IEA segment as an InterchangeTrailer struct
func (r *RawMessage) Trailer() *InterchangeTrailer {
if len(r.segments) == 0 {
return nil
}
seg := r.segments[len(r.segments)-1]
if len(seg) == 0 || seg[0] != ieaSegmentId {
return nil
}
ieaSegment := make([]string, 3, 3)
copy(ieaSegment, seg)
return &InterchangeTrailer{
ieaSegment[0],
ieaSegment[1],
ieaSegment[2],
}
}
// textCleanup removes any characters that are not in the extended character
// set, or the segment terminator, from the given string
func textCleanup(msg string, segmentTerminator rune) string {
msg = strings.TrimSpace(msg)
var keepChars []string
for _, c := range strings.Split(extendedCharacterSet, "") {
keepChars = append(keepChars, c)
}
switch segmentTerminator {
case '\n':
keepChars = append(keepChars, "\n")
case '\r':
keepChars = append(keepChars, "\r")
case '\t':
keepChars = append(keepChars, "\t")
}
msgChars := strings.Split(msg, "")
var b strings.Builder
for _, c := range msgChars {
if sliceContains(keepChars, c) {
b.WriteString(c)
}
}
return b.String()
}
// Read parses the given byte slice into a RawMessage, using the default
// Reader with default X12TransactionSetSpec instances.
func Read(data []byte) (*RawMessage, error) {
return _defaultReader.Read(data)
}
// Reader is used to read X12 messages into RawMessage instances.
type Reader struct {
transactionSpecs []*X12TransactionSetSpec
transactionSpecsMu sync.RWMutex
}
func (r *Reader) TransactionSpecs() []*X12TransactionSetSpec {
r.transactionSpecsMu.RLock()
defer r.transactionSpecsMu.RUnlock()
return r.transactionSpecs
}
// Read parses the given byte slice into a RawMessage, using the given Reader
// instance.
func (r *Reader) Read(data []byte) (rawMessage *RawMessage, err error) {
if !utf8.Valid(data) {
return rawMessage, fmt.Errorf("message text is not valid UTF-8")
}
readErrors := []error{}
var addError = func(err error) {
if err != nil {
readErrors = append(readErrors, err)
}
}
text := bytes.TrimLeftFunc(data, unicode.IsSpace)
runes := bytes.Runes(text)
if len(runes) < isaByteCount {
addError(
fmt.Errorf(
"%w: message too short to accommodate ISA segment (expected at least %d bytes, got %d)",
ParseError,
isaByteCount,
len(runes),
),
)
return rawMessage, errors.Join(readErrors...)
}
rawMessage = &RawMessage{reader: r}
isaLine := runes[:isaByteCount]
rawMessage.SegmentTerminator = isaLine[isaByteCount-1]
rawMessage.ElementSeparator = isaLine[isaElementSeparatorIndex]
if rawMessage.SegmentTerminator == rawMessage.ElementSeparator {
addError(
fmt.Errorf(
"%w: %w: segment terminator %q cannot be the same as the element separator",
ParseError, ErrInvalidISA, rawMessage.SegmentTerminator,
),
)
return rawMessage, errors.Join(readErrors...)
}
msgText := textCleanup(string(runes), rawMessage.SegmentTerminator)
rawMessage.text = msgText
lines := strings.Split(msgText, string(rawMessage.SegmentTerminator))
rawMessage.segments = make([]RawSegment, 0, len(lines))
if len(lines) == 0 {
addError(fmt.Errorf("%w: no segments found", ParseError))
return rawMessage, errors.Join(readErrors...)
}
headerLine := make([]string, isaElementCount, isaElementCount)
headerElems := strings.Split(lines[0], string(rawMessage.ElementSeparator))
if len(headerElems) < isaElementCount {
addError(
fmt.Errorf(
"%w: %w: ISA segment too short (expected %d elements, got %d)",
ParseError,
ErrInvalidISA,
isaElementCount,
len(headerElems),
),
)
}
copy(headerLine, headerElems)
rawMessage.segments = append(rawMessage.segments, headerLine)
componentSeparator := headerLine[isaIndexComponentElementSeparator]
if len(componentSeparator) != 1 {
addError(
fmt.Errorf(
"%w: %w: invalid component separator '%s' (expected a single character)",
ParseError,
ErrInvalidISA,
componentSeparator,
),
)
return rawMessage, errors.Join(readErrors...)
}
rawMessage.ComponentSeparator = rune(componentSeparator[0])
repetitionSeparator := headerLine[isaIndexRepetitionSeparator]
if len(repetitionSeparator) != 1 {
addError(
fmt.Errorf(
"%w: %w: invalid repetition separator '%s' (expected a single character)",
ParseError, ErrInvalidISA, repetitionSeparator,
),
)
return rawMessage, errors.Join(readErrors...)
}
rawMessage.RepetitionSeparator = rune(repetitionSeparator[0])
uq := uniqueElements(
[]string{
string(rawMessage.RepetitionSeparator),
string(rawMessage.ComponentSeparator),
string(rawMessage.ElementSeparator),
string(rawMessage.SegmentTerminator),
},
)
if len(uq) != 4 {
addError(
fmt.Errorf(
"%w: separators must be unique (got %v)",
ParseError, uq,
),
)
}
for i := 1; i < len(lines); i++ {
line := lines[i]
if strings.TrimSpace(line) == "" {
continue
}
seg := strings.Split(line, string(rawMessage.ElementSeparator))
if len(seg) == 0 {
addError(
fmt.Errorf(
"%w: segment %d is empty",
ParseError, i,
),
)
return rawMessage, errors.Join(readErrors...)
}
rawMessage.segments = append(rawMessage.segments, seg)
}
return rawMessage, errors.Join(readErrors...)
}
// NewReader creates a new Reader with the given transaction set specs. The
// specs will be validated, and used to transform ST/SE-bounded transaction
// sets if RawMessage.Message() is called on the result of any
// Reader.Read() call.
func NewReader(transactionSetSpecs ...*X12TransactionSetSpec) (*Reader, error) {
addSpecs := []*X12TransactionSetSpec{}
specErrors := []error{}
for _, txnSpec := range transactionSetSpecs {
if err := txnSpec.Validate(); err != nil {
specErrors = append(
specErrors,
fmt.Errorf("%s: %w", txnSpec.Key, err),
)
continue
}
addSpecs = append(addSpecs, txnSpec)
}
return &Reader{transactionSpecs: addSpecs}, errors.Join(specErrors...)
}
func init() {
_defaultReader.transactionSpecsMu.Lock()
defer _defaultReader.transactionSpecsMu.Unlock()
for _, txnSpec := range defaultTransactionSpecs {
if err := txnSpec.Validate(); err != nil {
panic(
fmt.Sprintf(
"unable to validate initial spec %s: %s",
txnSpec.Key,
err.Error(),
),
)
}
_defaultReader.transactionSpecs = append(
_defaultReader.transactionSpecs,
txnSpec,
)
}
}