-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec.go
1253 lines (1140 loc) · 31.2 KB
/
spec.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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package edx12
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
)
var (
pathSeparator = "/"
specChildTypes = map[SpecType][]SpecType{
TransactionSetSpec: {LoopSpec, SegmentSpec},
LoopSpec: {LoopSpec, SegmentSpec},
SegmentSpec: {ElementSpec, CompositeSpec},
CompositeSpec: {ElementSpec},
ElementSpec: {},
}
specNodeTypes = map[SpecType][]NodeType{
TransactionSetSpec: {TransactionNode},
LoopSpec: {LoopNode, TransactionNode},
SegmentSpec: {SegmentNode},
ElementSpec: {ElementNode, RepeatElementNode},
CompositeSpec: {CompositeNode, RepeatElementNode, ElementNode},
}
)
type SpecError struct {
Spec *X12Spec
Err error
}
func (e *SpecError) Error() string {
var b strings.Builder
if specName := e.Spec.Name; specName != "" {
_, _ = fmt.Fprintf(&b, "name: %s ", specName)
}
if specPath := e.Spec.path; specPath != "" {
_, _ = fmt.Fprintf(&b, "path: %s ", specPath)
}
if specLabel := e.Spec.Label; specLabel != "" {
_, _ = fmt.Fprintf(&b, "label: %s ", specLabel)
}
if specDesc := e.Spec.Description; specDesc != "" {
_, _ = fmt.Fprintf(&b, "description: '%s' ", specDesc)
}
bs := strings.TrimSpace(b.String())
if bs == "" {
return e.Err.Error()
}
bs = fmt.Sprintf("[%s]: %s", bs, e.Err.Error())
return bs
}
func (e *SpecError) Unwrap() error {
return e.Err
}
// newSpecErr creates a new SpecError referencing the given X12Spec
func newSpecErr(e error, spec *X12Spec) error {
return &SpecError{
Spec: spec,
Err: e,
}
}
type DataType uint
const (
UnknownDataType DataType = iota
String
Numeric
Identifier
Decimal
Date
Time
Binary
)
func (d DataType) String() string {
names := map[DataType]string{
UnknownDataType: "",
String: "AN",
Numeric: "N",
Identifier: "ID",
Decimal: "R",
Date: "DT",
Time: "TM",
Binary: "B",
}
return names[d]
}
func (d DataType) GoString() string {
s := map[DataType]string{
UnknownDataType: "UnknownDataType",
String: "String",
Numeric: "Numeric",
Identifier: "Identifier",
Decimal: "Decimal",
Date: "Date",
Time: "Time",
Binary: "Binary",
}
return s[d]
}
func (d DataType) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}
func (d *DataType) UnmarshalJSON(b []byte) error {
var typeName string
if err := json.Unmarshal(b, &typeName); err != nil {
return err
}
switch strings.ToLower(typeName) {
case "":
*d = UnknownDataType
case "an":
*d = String
case "n":
*d = Numeric
case "id":
*d = Identifier
case "r":
*d = Decimal
case "dt":
*d = Date
case "tm":
*d = Time
case "b":
*d = Binary
default:
return fmt.Errorf("unknown DataType: '%#v' / '%#v'", d, typeName)
}
return nil
}
type ConditionCode uint
const (
UnknownConditionCode ConditionCode = iota
PairedOrMultiple
RequiredCondition
Exclusion
Conditional
ListConditional
)
func (c ConditionCode) String() string {
names := map[ConditionCode]string{
UnknownConditionCode: "",
PairedOrMultiple: "P",
RequiredCondition: "R",
Exclusion: "E",
Conditional: "C",
ListConditional: "L",
}
return names[c]
}
func (c ConditionCode) GoString() string {
s := map[ConditionCode]string{
UnknownConditionCode: "UnknownConditionCode",
PairedOrMultiple: "PairedOrMultiple",
RequiredCondition: "RequiredCondition",
Exclusion: "Exclusion",
Conditional: "Conditional",
ListConditional: "ListConditional",
}
return s[c]
}
func (c ConditionCode) MarshalJSON() ([]byte, error) {
return json.Marshal(c.String())
}
func (c *ConditionCode) UnmarshalJSON(b []byte) error {
var conditionName string
if err := json.Unmarshal(b, &conditionName); err != nil {
return err
}
switch conditionName {
case "":
*c = UnknownConditionCode
case "P":
*c = PairedOrMultiple
case "R":
*c = RequiredCondition
case "E":
*c = Exclusion
case "C":
*c = Conditional
case "L":
*c = ListConditional
default:
return fmt.Errorf("unknown ConditionCode: %s", conditionName)
}
return nil
}
type SpecType uint
const (
UnknownSpec SpecType = iota
TransactionSetSpec
LoopSpec
SegmentSpec
CompositeSpec
ElementSpec
)
func (s SpecType) String() string {
names := map[SpecType]string{
TransactionSetSpec: "TransactionSet",
LoopSpec: "Loop",
SegmentSpec: "Segment",
CompositeSpec: "Composite",
ElementSpec: "Element",
}
return names[s]
}
func (s SpecType) GoString() string {
names := map[SpecType]string{
TransactionSetSpec: "TransactionSetSpec",
LoopSpec: "LoopSpec",
SegmentSpec: "SegmentSpec",
CompositeSpec: "CompositeSpec",
ElementSpec: "ElementSpec",
}
return names[s]
}
func (s SpecType) MarshalJSON() ([]byte, error) {
return json.Marshal(s.String())
}
func (s *SpecType) UnmarshalJSON(b []byte) error {
var nodeName string
if err := json.Unmarshal(b, &nodeName); err != nil {
return err
}
switch strings.ToLower(nodeName) {
case "transactionset":
*s = TransactionSetSpec
case "loop":
*s = LoopSpec
case "segment":
*s = SegmentSpec
case "composite":
*s = CompositeSpec
case "element":
*s = ElementSpec
default:
return fmt.Errorf("unknown SpecType: %s", nodeName)
}
return nil
}
type Usage int
const (
UnknownUsage Usage = iota
// Required indicates that the field is required
Required
// Situational indicates that the field is situational
Situational
// NotUsed indicates that the field is not used
NotUsed
)
func (u Usage) GoString() string {
names := map[Usage]string{
UnknownUsage: "UnknownUsage",
Required: "Required",
Situational: "Situational",
NotUsed: "NotUsed",
}
return names[u]
}
func (u Usage) MarshalJSON() ([]byte, error) {
return json.Marshal(u.String())
}
func (u Usage) String() string {
usageNames := map[Usage]string{
Required: "REQUIRED",
Situational: "SITUATIONAL",
NotUsed: "NOT USED",
}
return usageNames[u]
}
func (u *Usage) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
switch s {
case "REQUIRED":
*u = Required
case "SITUATIONAL":
*u = Situational
case "NOT USED":
*u = NotUsed
}
return nil
}
type X12Spec struct {
// Type indicates the type of the X12 structure this spec defines
Type SpecType `json:"type"`
// Name is the name of the field as it should appear in the x12 spec.
// Ex: `2000A`, `NM1`, ...
Name string `validate:"required,gte=1" json:"name"`
// Usage indicates whether the field is required, situational, or not used.
// If Type is ElementSpec, this may be situationally overridden based on
// a parent SegmentSpec's SegmentCondition list.
Usage Usage `json:"usage"`
Description string `json:"description,omitempty"`
// RepeatMin specifies the minimum number of times this structure should
// repeat (when used).
// If Usage is NotUsed, this has no effect.
// If Usage is Situational, the number of repetitions must be zero, or
// between this number and RepeatMax, inclusive (if RepeatMax > 0)
// If Usage is Required and this is not set, it will be set/overriden as 1.
// The number of repetitions must be between this
// number and RepeatMax, inclusive (if RepeatMax > 0)
RepeatMin int `json:"repeatMin,omitempty"`
// RepeatMax is the maximum number of times this can repeat.
// If Usage is NotUsed, this has no effect.
// If Usage is Situational, the number of repetitions must be at or
// below this number, and between either zero or RepeatMin, inclusive.
// If Usage is Required, the number of repetitions must be between
// RepeatMin and this number, inclusive. If RepeatMin isn't set, it must
// be between one and this number.
RepeatMax int `json:"repeatMax,omitempty"`
// Label sets the key for X12Node objects when marshalling to JSON.
Label string `json:"label,omitempty"`
// Structure consists of the child X12Specs of this X12Spec
Structure []*X12Spec `json:"structure,omitempty"`
// ValidCodes (for ElementSpec), is a list of valid values for this
// element
ValidCodes []string `json:"validCodes,omitempty"`
// MinLength (for ElementSpec) specifies the minimum length of the element
// when a value is provided.
// If Usage is NotUsed, this has no effect.
// If Usage is Situational, the length of the element must either be zero,
// or between this number and MaxLength, inclusive (if MaxLength > 0)
// If Usage is Required and MinLength is not set, it will be interpreted
// or overridden as 1. The length of the element must be between this
// and MaxLength, inclusive (if MaxLength > 0).
// For elements, this sets minLength in the generated JSON schema.
MinLength int `json:"minLength,omitempty"`
// MaxLength (for ElementSpec) specifies the maximum length of the element
// when a value is provided.
// If Usage is NotUsed, this has no effect.
// For elements, this sets maxLength in the generated JSON schema.
MaxLength int `json:"maxLength,omitempty"`
// DefaultVal specifies the default ElementNode value when Usage
// is Required, when generating messages with this spec.
// If Usage is NotUsed or Situational, this has no effect. The value must
// be present in ValidCodes, if not empty.
// For Required elements, this sets 'default' in the generated JSON schema.
DefaultVal string `json:"default,omitempty"`
// DataType indicates the ASC X12 data type of this element.
// When marshalling to JSON, this is used to convert element values to
// the appropriate type.
// For the generated JSON schema:
// - Decimal sets type=number and format=float
// - Numeric sets type=number
// - All others set type=string
DataType DataType `validate:"json:dataType,omitempty"`
// Syntax describes relational conditions across elements in a segment.
// Conditions defined here may situationally override child ElementSpec
// settings.
Syntax []SegmentCondition `json:"syntax,omitempty"`
// loopDepth is the depth of this X12Spec in the overall spec tree,
// or the depth of its parent loop
loopDepth int
// path is a materialized path of this X12Spec's position in
// the overall spec
path string
// parent is the parent of this X12Spec, or nil if this is the root
parent *X12Spec
jsonSchema *jsonSchema
}
// index returns the position of this X12Spec in its parent's Structure.
// If this X12Spec has no parent, -1 is returned. If this X12Spec is not
// found in its parent's Structure, -1 is returned along with an error.
func (x *X12Spec) index() (int, error) {
if x.parent == nil {
return -1, nil
}
ind := x.parent.childIndex(x)
if ind == -1 {
return -1, fmt.Errorf("X12Spec %s not found in parent", x.Name)
}
return ind, nil
}
// childIndex returns the position of the given child X12Spec in this
// X12Spec's Structure. If the child is not found, -1 is returned.
func (x *X12Spec) childIndex(childSpec *X12Spec) int {
for i, child := range x.Structure {
if child == childSpec {
return i
}
}
return -1
}
// nextSpec returns the next X12Spec in this X12Spec's parent's Structure,
// or nil if this is the last X12Spec in the parent's Structure.
// If this X12Spec isn't found in its parent's Structure, an error is returned.
func (x *X12Spec) nextSpec() (*X12Spec, error) {
ind, err := x.index()
if err != nil {
return nil, err
}
if ind == -1 {
return nil, nil
}
if ind+1 >= len(x.parent.Structure) {
return nil, nil
}
return x.parent.Structure[ind+1], nil
}
func (x *X12Spec) findName(name string) (matchingSpecs []*X12Spec) {
if x.Name == name {
matchingSpecs = append(matchingSpecs, x)
}
for _, child := range x.Structure {
matchingSpecs = append(matchingSpecs, child.findName(name)...)
}
return matchingSpecs
}
func (x *X12Spec) findType(specType SpecType) (matchingSpecs []*X12Spec) {
if x.Type == specType {
matchingSpecs = append(matchingSpecs, x)
}
for _, child := range x.Structure {
matchingSpecs = append(matchingSpecs, child.findType(specType)...)
}
return matchingSpecs
}
// newSchema creates a new jsonSchema for this X12Spec and any child
// specs, and assigns it to X12Spec.schema
func (x *X12Spec) newSchema() *jsonSchema {
if x.Usage == NotUsed {
return nil
}
j := &jsonSchema{
Description: x.Description,
}
if x.Type == ElementSpec {
j.Title = x.Name
switch x.DataType {
case Decimal:
j.Type = "number"
j.Format = "float"
case Numeric:
j.Type = "number"
case Date:
j.Type = "string"
switch x.MaxLength {
case 8:
j.Example = "20060102"
case 6:
j.Example = "200601"
}
case Time:
j.Type = "string"
switch x.MinLength {
case 4:
j.Example = "1504"
case 6:
j.Example = "150405"
case 8:
j.Example = "15040599"
}
default:
j.Type = "string"
if len(x.ValidCodes) > 0 {
j.Enum = x.ValidCodes
}
}
j.MinLength = x.MinLength
j.MaxLength = x.MaxLength
if x.Usage == Required {
if x.DefaultVal != "" {
j.Default = x.DefaultVal
}
} else {
if x.DefaultVal != "" {
j.Example = x.DefaultVal
} else if len(x.ValidCodes) > 0 {
j.Example = x.ValidCodes[0]
}
}
} else {
j.Type = "object"
j.Title = fmt.Sprintf("%s_%s", x.Type, x.Name)
j.Properties = make(map[string]*jsonSchema)
for _, child := range x.Structure {
childProperty := child.newSchema()
if childProperty != nil {
j.Properties[child.Label] = childProperty
if child.Usage == Required {
j.Required = append(j.Required, child.Label)
}
}
}
}
if x.IsRepeatable() {
aj := &jsonSchema{
Type: "array",
MinItems: x.RepeatMin,
MaxItems: x.RepeatMax,
Items: j,
}
x.jsonSchema = aj
return aj
}
x.jsonSchema = j
return j
}
// Path returns the path of this X12Spec, which is a materialized path of
// this X12Spec's position in the overall spec
func (x *X12Spec) Path() string {
return x.path
}
// Parent returns the parent of this X12Spec, or nil if this is the root
func (x *X12Spec) Parent() *X12Spec {
return x.parent
}
func (x *X12Spec) treeString(indent string) string {
var b strings.Builder
b.WriteString(fmt.Sprintf("%s- %s (%#v)\n", indent, x.Name, x.Type))
for _, child := range x.Structure {
switch child.Type {
case TransactionSetSpec, LoopSpec, SegmentSpec:
b.WriteString(child.treeString(indent + " "))
}
}
return b.String()
}
func (x *X12Spec) GoString() string {
var b strings.Builder
b.WriteString("&X12Spec{")
b.WriteString(fmt.Sprintf("Type: %#v, ", x.Type))
b.WriteString(fmt.Sprintf("Name: %q, ", x.Name))
b.WriteString(fmt.Sprintf("Usage: %#v, ", x.Usage))
if x.Description != "" {
b.WriteString(fmt.Sprintf("Description: %q, ", x.Description))
}
if x.Usage != NotUsed {
if x.RepeatMin != 0 {
b.WriteString(fmt.Sprintf("RepeatMin: %d, ", x.RepeatMin))
}
if x.RepeatMax != 0 {
b.WriteString(fmt.Sprintf("RepeatMax: %d, ", x.RepeatMax))
}
b.WriteString(fmt.Sprintf("Label: %q, ", x.Label))
switch x.Type {
case ElementSpec:
if len(x.ValidCodes) > 0 {
b.WriteString(fmt.Sprintf("ValidCodes: %#v, ", x.ValidCodes))
}
if x.MinLength != 0 {
b.WriteString(fmt.Sprintf("MinLength: %d, ", x.MinLength))
}
if x.MaxLength != 0 {
b.WriteString(fmt.Sprintf("MaxLength: %d, ", x.MaxLength))
}
if x.DefaultVal != "" {
b.WriteString(fmt.Sprintf("DefaultVal: %q, ", x.DefaultVal))
}
b.WriteString(fmt.Sprintf("DataType: %#v, ", x.DataType))
case SegmentSpec:
if len(x.Syntax) > 0 {
b.WriteString(fmt.Sprintf("Syntax: %#v, ", x.Syntax))
}
if len(x.Structure) > 0 {
b.WriteString(fmt.Sprintf("Structure: %#v, ", x.Structure))
}
default:
if len(x.Structure) > 0 {
b.WriteString(fmt.Sprintf("Structure: %#v, ", x.Structure))
}
}
}
b.WriteString("}")
return b.String()
}
// setPaths sets the path of each X12Spec, recursively, using the given
// parent path and suffix. The path map is used to check for duplicate paths,
// and associate each path to its X12Spec. An error is returned if any
// duplicate paths are detected.
func (x *X12Spec) setPaths(
parentPath string,
pathMap map[string]*X12Spec,
suffix string,
) error {
parentPath = strings.TrimRight(parentPath, pathSeparator)
x.path = parentPath + pathSeparator + x.Name + suffix
dupeVal, isDupe := pathMap[x.path]
if isDupe && dupeVal != x {
return fmt.Errorf("duplicate path %s", x.path)
}
pathMap[x.path] = x
childNameCount := make(map[string]int)
childNameCurrentCt := make(map[string]int)
for _, child := range x.Structure {
if child.parent == nil {
child.parent = x
}
childNameCount[child.Name]++
childNameCurrentCt[child.Name] = 0
}
for _, child := range x.Structure {
childSuffix := ""
if childNameCount[child.Name] > 1 {
childSuffix = fmt.Sprintf(
"[%d]",
childNameCurrentCt[child.Name],
)
}
childNameCurrentCt[child.Name]++
if e := child.setPaths(
x.path,
pathMap,
childSuffix,
); e != nil {
return e
}
}
return nil
}
func (x *X12Spec) Accept(v SpecVisitor) {
v.VisitSpec(x)
}
// validateRepeat validates the RepeatMin and RepeatMax fields of the X12Spec
func (x *X12Spec) validateRepeat() error {
var errs []error
if x.RepeatMin < 0 {
errs = append(
errs,
errors.New("repeatMin must be greater than or equal to 0"),
)
}
if x.RepeatMax < 0 {
errs = append(
errs,
errors.New("repeatMax must be greater than or equal to 0"),
)
}
if x.RepeatMin > x.RepeatMax && x.RepeatMax > 0 {
errs = append(
errs,
errors.New("repeatMin must be less than or equal to repeatMax"),
)
}
return errors.Join(errs...)
}
// finalize sets the default values for certain fields if the X12Spec is
func (x *X12Spec) finalize() {
switch x.Usage {
case NotUsed:
x.RepeatMin = 0
x.RepeatMax = 0
x.MaxLength = 0
x.MinLength = 0
x.ValidCodes = nil
x.DefaultVal = ""
x.Syntax = nil
x.Label = ""
default:
if x.Type != ElementSpec {
x.DataType = UnknownDataType
x.ValidCodes = nil
x.MaxLength = 0
x.MinLength = 0
x.ValidCodes = nil
x.DefaultVal = ""
}
if x.Type != SegmentSpec {
x.Syntax = nil
}
}
if x.Usage == Required {
switch x.Type {
case ElementSpec:
if x.MinLength == 0 {
x.MinLength = 1
}
default:
if x.RepeatMin == 0 {
x.RepeatMin = 1
if x.RepeatMax == 0 {
x.RepeatMax = 1
}
}
}
}
if x.parent != nil {
if x.Type == LoopSpec {
x.loopDepth = x.parent.loopDepth + 1
} else {
x.loopDepth = x.parent.loopDepth
}
}
for _, s := range x.Structure {
s.parent = x
s.finalize()
}
}
func (x *X12Spec) validateUsage() error {
var errs []error
switch x.Usage {
case Required, Situational:
if x.Type == LoopSpec && len(x.Structure) > 0 {
firstSpec := x.Structure[0]
if firstSpec.Type == SegmentSpec {
if firstSpec.Usage != x.Usage {
errs = append(
errs,
fmt.Errorf(
"first segment in loop must match loop usage (%s)",
x.Usage,
),
)
}
} else {
errs = append(
errs,
errors.New("first child of loop must be a segment"),
)
}
}
case NotUsed:
if x.Type != ElementSpec && x.Type != CompositeSpec {
errs = append(
errs,
fmt.Errorf(
"only elements and composites can have usage %s",
NotUsed,
),
)
}
default:
errs = append(
errs,
fmt.Errorf(
"usage must be one of: %s, %s, %s",
Required,
Situational,
NotUsed,
),
)
}
return errors.Join(errs...)
}
func (x *X12Spec) validateMinMaxLength() error {
var errs []error
if x.MinLength < 0 {
errs = append(
errs,
errors.New("minLength must be greater than or equal to 0"),
)
}
if x.MaxLength < 0 {
errs = append(
errs,
errors.New("maxLength must be greater than or equal to 0"),
)
}
// If this element is required, then MinLength defaults to 1 if not
// otherwise set, in which case it's OK if it's greater than MaxLength
// as long as MaxLength is greater than 0.
// Situational elements can either have MinLength 0 or something
// less than MaxLength.
if x.MinLength > x.MaxLength && (x.Usage != Required || x.MaxLength > 0) {
errs = append(
errs,
errors.New("minLength must be less than or equal to maxLength"),
)
}
return errors.Join(errs...)
}
func (x *X12Spec) validateValidCodes() error {
var errs []error
// If DefaultVal and ValidCodes are set, then ValidCodes needs to
// contain DefaultVal, or... it'll fail by default
if x.DefaultVal != "" && len(x.ValidCodes) > 0 && !sliceContains(
x.ValidCodes, x.DefaultVal,
) {
errs = append(
errs,
errors.New("defaultVal must be one of validCodes"),
)
}
for i := 0; i < len(x.ValidCodes); i++ {
c := x.ValidCodes[i]
if x.MinLength > 0 && len(c) < x.MinLength {
errs = append(
errs,
fmt.Errorf(
"validCode '%s' is shorter than minLength %d",
c,
x.MinLength,
),
)
}
if x.MaxLength > 0 && len(c) > x.MaxLength {
errs = append(
errs,
fmt.Errorf(
"validCode %s is longer than maxLength %d",
c,
x.MaxLength,
),
)
}
}
return errors.Join(errs...)
}
// IsRepeatable returns true if this X12Spec is either Situational
// or Required, and RepeatMin or RepeatMax are greater than 1.
func (x *X12Spec) IsRepeatable() bool {
if x.NotUsed() {
return false
}
// indicates an 'unbounded' repetition
if x.RepeatMin == 1 && x.RepeatMax == 0 {
return true
}
// Indicates bounded repetition
if x.RepeatMin == 1 && x.RepeatMax > 1 {
return true
}
if x.RepeatMin > 1 || x.RepeatMax > 1 {
return true
}
return false
}
func (x *X12Spec) validateSpec() error {
var errs []error
switch x.Type {
case TransactionSetSpec, LoopSpec:
if len(x.Structure) == 0 {
errs = append(
errs,
errors.New("transaction set/loop spec must have children"),
)
}
case ElementSpec:
if len(x.Structure) > 0 {
errs = append(errs, errors.New("element spec cannot have children"))
}
case CompositeSpec:
if x.IsRepeatable() {
errs = append(
errs,
errors.New("repeatable composites not currently supported"),
)
}
case SegmentSpec:
//
default:
errs = append(errs, errors.New("type is required"))
}
if x.Name == "" {
errs = append(errs, errors.New("name is required"))
}
errs = append(
errs,
x.validateValidCodes(),
x.validateRepeat(),
x.validateUsage(),
x.validateMinMaxLength(),
)
for i := 0; i < len(x.Structure); i++ {
errs = append(errs, x.Structure[i].validateSpec())
}
err := errors.Join(errs...)
if err != nil {
err = newSpecErr(err, x)
}
return err
}
func (x *X12Spec) NotUsed() bool {
return x.Usage == NotUsed
}
func (x *X12Spec) Required() bool {
return x.Usage == Required
}
func (x *X12Spec) Situational() bool {
return x.Usage == Situational
}
// SegmentCondition defines a segment syntax rule
type SegmentCondition struct {
// Indexes is a list of element indexes that this condition applies to
Indexes []int `validate:"required" json:"indexes,omitempty"`
// ConditionCode determines the behavior of this rule.
// It should be one of:
// - segmentSyntaxPairedOrMultiple
// - segmentSyntaxRequired
// - segmentSyntaxConditional
// - segmentSyntaxListConditional
// - segmentSyntaxExclusion
ConditionCode ConditionCode `json:"conditionCode,omitempty"`
Details string `json:"details,omitempty"`
}
// X12TransactionSetSpec defines the structure of an X12 transaction set
// bounded by ST/SE segments, as defined by X12 implementation guides.
// The first child X12Spec must be a SegmentSpec with the name ST.
// The last child X12Spec must be a SegmentSpec with the name SE.
type X12TransactionSetSpec struct {
// Key is a description of the transaction set. It should be unique.
Key string `json:"key"`
//// TransactionSetCode should reflect the value to populate ST01
//TransactionSetCode string `json:"transactionSetCode"`
//// Version should reflect the value to populate ST03
//Version string `json:"version"`
PathMap map[string]*X12Spec `json:"-"`
*X12Spec
}
// Matches returns True if the given TransactionSetNode matches the
// X12TransactionSetSpec header
func (xs *X12TransactionSetSpec) Matches(txn *TransactionSetNode) (
bool,
error,
) {
return segmentMatchesSpec(txn.Header, xs.headerSpec())
}
// headerSpec returns the first X12Spec in the structure named ST
func (xs *X12TransactionSetSpec) headerSpec() *X12Spec {
for _, s := range xs.Structure {
if s.Name == stSegmentId {
return s
}
}
return nil
}
func (xs *X12TransactionSetSpec) trailerSpec() *X12Spec {
s := xs.Structure[len(xs.Structure)-1]