-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathmessageDisposition.go
177 lines (150 loc) · 5.92 KB
/
messageDisposition.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
// Copyright 2020 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package wire
import (
"encoding/json"
"strings"
"unicode/utf8"
)
// MessageDisposition is the message disposition of the wire
type MessageDisposition struct {
// tag
tag string
// FormatVersion 30
FormatVersion string `json:"formatVersion,omitempty"`
// TestTestProductionCode identifies if test or production
TestProductionCode string `json:"testProductionCode,omitempty"`
// MessageDuplicationCode * ` ` - Original Message * `R` - Retrieval of an original message * `P` - Resend
MessageDuplicationCode string `json:"messageDuplicationCode,omitempty"`
// MessageStatusIndicator
MessageStatusIndicator string `json:"messageStatusIndicator,omitempty"`
// validator is composed for data validation
// validator
// converters is composed for WIRE to GoLang Converters
converters
}
// NewMessageDisposition returns a new MessageDisposition
func NewMessageDisposition() *MessageDisposition {
md := &MessageDisposition{
tag: TagMessageDisposition,
FormatVersion: FormatVersion,
TestProductionCode: EnvironmentProduction,
MessageDuplicationCode: MessageDuplicationOriginal,
}
return md
}
// Parse takes the input string and parses the MessageDisposition values
//
// Parse provides no guarantee about all fields being filled in. Callers should make a Validate() call to confirm
// successful parsing and data validity.
func (md *MessageDisposition) Parse(record string) error {
if utf8.RuneCountInString(record) < 6 {
return NewTagMinLengthErr(6, len(record))
}
md.tag = record[:6]
length := 6
value, read, err := md.parseFixedStringField(record[length:], 2)
if err != nil {
return fieldError("FormatVersion", err)
}
md.FormatVersion = value
length += read
value, read, err = md.parseFixedStringField(record[length:], 1)
if err != nil {
return fieldError("TestProductionCode", err)
}
md.TestProductionCode = value
length += read
value, read, err = md.parseFixedStringField(record[length:], 1)
if err != nil {
return fieldError("MessageDuplicationCode", err)
}
md.MessageDuplicationCode = value
length += read
value, read, err = md.parseFixedStringField(record[length:], 1)
if err != nil {
return fieldError("MessageStatusIndicator", err)
}
md.MessageStatusIndicator = value
length += read
if err := md.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}
return nil
}
func (md *MessageDisposition) UnmarshalJSON(data []byte) error {
type Alias MessageDisposition
aux := struct {
*Alias
}{
(*Alias)(md),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
md.tag = TagMessageDisposition
return nil
}
// String returns a fixed-width MessageDisposition record
func (md *MessageDisposition) String() string {
return md.Format(FormatOptions{
VariableLengthFields: false,
})
}
// Format returns a MessageDisposition record formatted according to the FormatOptions
func (md *MessageDisposition) Format(options FormatOptions) string {
var buf strings.Builder
buf.Grow(11)
buf.WriteString(md.tag)
buf.WriteString(md.MessageDispositionFormatVersionField())
buf.WriteString(md.MessageDispositionTestProductionCodeField())
buf.WriteString(md.MessageDispositionMessageDuplicationCodeField())
buf.WriteString(md.MessageDispositionMessageStatusIndicatorField())
if options.VariableLengthFields {
return md.stripDelimiters(buf.String())
} else {
return buf.String()
}
}
// Validate performs WIRE format rule checks on MessageDisposition and returns an error if not Validated
// The first error encountered is returned and stops that parsing.
func (md *MessageDisposition) Validate() error {
// Currently no validation as the FED is responsible for the values
if md.tag != TagMessageDisposition {
return fieldError("tag", ErrValidTagForType, md.tag)
}
return nil
}
// MessageDispositionFormatVersionField gets a string of the FormatVersion field
func (md *MessageDisposition) MessageDispositionFormatVersionField() string {
return md.alphaField(md.FormatVersion, 2)
}
// MessageDispositionTestProductionCodeField gets a string of the TestProductionCoden field
func (md *MessageDisposition) MessageDispositionTestProductionCodeField() string {
return md.alphaField(md.TestProductionCode, 1)
}
// MessageDispositionMessageDuplicationCodeField gets a string of the MessageDuplicationCode field
func (md *MessageDisposition) MessageDispositionMessageDuplicationCodeField() string {
return md.alphaField(md.MessageDuplicationCode, 1)
}
// MessageDispositionMessageStatusIndicatorField gets a string of the MessageDuplicationCode field
func (md *MessageDisposition) MessageDispositionMessageStatusIndicatorField() string {
return md.alphaField(md.MessageStatusIndicator, 1)
}
// FormatMessageDispositionFormatVersion returns FormatVersion formatted according to the FormatOptions
func (md *MessageDisposition) FormatMessageDispositionFormatVersion(options FormatOptions) string {
return md.formatAlphaField(md.FormatVersion, 2, options)
}
// FormatMessageDispositionTestProductionCode returns TestProductionCode formatted according to the FormatOptions
func (md *MessageDisposition) FormatMessageDispositionTestProductionCode(options FormatOptions) string {
return md.formatAlphaField(md.TestProductionCode, 1, options)
}
// FormatMessageDispositionMessageDuplicationCode returns MessageDuplicationCode formatted according to the FormatOptions
func (md *MessageDisposition) FormatMessageDispositionMessageDuplicationCode(options FormatOptions) string {
return md.formatAlphaField(md.MessageDuplicationCode, 1, options)
}
// FormatMessageDispositionMessageStatusIndicator returns MessageStatusIndicator formatted according to the FormatOptions
func (md *MessageDisposition) FormatMessageDispositionMessageStatusIndicator(options FormatOptions) string {
return md.formatAlphaField(md.MessageStatusIndicator, 1, options)
}