-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathsenderReference.go
107 lines (90 loc) · 2.75 KB
/
senderReference.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
// 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"
)
// SenderReference is the SenderReference of the wire
type SenderReference struct {
// tag
tag string
// SenderReference
SenderReference string `json:"senderReference,omitempty"`
// validator is composed for data validation
validator
// converters is composed for WIRE to GoLang Converters
converters
}
// NewSenderReference returns a new SenderReference
func NewSenderReference() *SenderReference {
sr := &SenderReference{
tag: TagSenderReference,
}
return sr
}
// Parse takes the input string and parses the SenderReference 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 (sr *SenderReference) Parse(record string) error {
if utf8.RuneCountInString(record) < 6 {
return NewTagMinLengthErr(6, len(record))
}
sr.tag = record[:6]
length := 6
value, read, err := sr.parseVariableStringField(record[length:], 16)
if err != nil {
return fieldError("SenderReference", err)
}
sr.SenderReference = value
length += read
if err := sr.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}
return nil
}
func (sr *SenderReference) UnmarshalJSON(data []byte) error {
type Alias SenderReference
aux := struct {
*Alias
}{
(*Alias)(sr),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
sr.tag = TagSenderReference
return nil
}
// String returns a fixed-width SenderReference record
func (sr *SenderReference) String() string {
return sr.Format(FormatOptions{
VariableLengthFields: false,
})
}
// Format returns a SenderReference record formatted according to the FormatOptions
func (sr *SenderReference) Format(options FormatOptions) string {
var buf strings.Builder
buf.Grow(22)
buf.WriteString(sr.tag)
buf.WriteString(sr.FormatSenderReference(options) + Delimiter)
return buf.String()
}
// Validate performs WIRE format rule checks on SenderReference and returns an error if not Validated
// The first error encountered is returned and stops that parsing.
func (sr *SenderReference) Validate() error {
if sr.tag != TagSenderReference {
return fieldError("tag", ErrValidTagForType, sr.tag)
}
if err := sr.isAlphanumeric(sr.SenderReference); err != nil {
return fieldError("SenderReference", err, sr.SenderReference)
}
return nil
}
// FormatSenderReference returns SenderReference formatted according to the FormatOptions
func (sr *SenderReference) FormatSenderReference(options FormatOptions) string {
return sr.formatAlphaField(sr.SenderReference, 16, options)
}