-
Notifications
You must be signed in to change notification settings - Fork 19
/
swift.go
103 lines (84 loc) · 2.29 KB
/
swift.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
package swift
import "errors"
// Error codes returned by failures to validate an swift.
var (
ErrInvalidLength = errors.New("swift: invalid length")
ErrInvalidCase = errors.New("swift: invalid case")
ErrInvalidBankCode = errors.New("swift: invalid bank code")
ErrInvalidCountryCode = errors.New("swift: invalid country code")
ErrCountryCodeNotPresent = errors.New("swift: country code does not exist")
ErrInvalidLocationCode = errors.New("swift: invalid location code")
ErrInvalidBranchCode = errors.New("swift: invalid branch code")
)
// Type represents type of swift code.
type Type int
const (
// Type8 represents swift code with a length of 8 characters.
Type8 Type = iota
// Type11 represents swift code with a length of 11 characters.
Type11
)
// Swift represents a swift/bic code.
type Swift struct {
value string
}
// BankCode returns bank code of swift code.
func (s *Swift) BankCode() string {
return extractBankCode(s.value)
}
// CountryCode returns country code of swift code.
func (s *Swift) CountryCode() string {
return extractCountryCode(s.value)
}
// LocationCode returns location code of swift code.
func (s *Swift) LocationCode() string {
return extractLocationCode(s.value)
}
// BranchCode returns branch code of swift code.
func (s *Swift) BranchCode() string {
if hasBranchCode(s.value) {
return extractBranchCode(s.value)
}
return ""
}
// Type returns type of swift code.
func (s *Swift) Type() Type {
if hasBranchCode(s.value) {
return Type11
}
return Type8
}
// Validate validates swift code.
func Validate(value string) error {
if err := validateLength(value); err != nil {
return err
}
if err := validateCase(value); err != nil {
return err
}
if err := validateBankCode(value); err != nil {
return err
}
if err := validateCountryCode(value); err != nil {
return err
}
if err := validateLocationCode(value); err != nil {
return err
}
return validateBranchCode(value)
}
// New validates and creates new swift code.
func New(value string) (*Swift, error) {
if err := Validate(value); err != nil {
return nil, err
}
return &Swift{value}, nil
}
// MustParse tries to create new swift code, panics on failure.
func MustParse(value string) *Swift {
swft, err := New(value)
if err != nil {
panic(err)
}
return swft
}