Skip to content

Commit 682aca1

Browse files
committed
ref: bik
1 parent 07e89ca commit 682aca1

File tree

2 files changed

+85
-84
lines changed

2 files changed

+85
-84
lines changed

bik/bik.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
func Validate(bik string) (bool, error) {
1010
bikData, err := ParseBIK(bik)
1111
if err != nil {
12-
return false, fmt.Errorf("create %s model: %w", packageName, err)
12+
return false, fmt.Errorf("parse %s model: %w", packageName, err)
1313
}
1414

1515
return bikData.IsValid()
@@ -25,7 +25,7 @@ func Exists(bik string) (bool, error) {
2525

2626
bikData, err := ParseBIK(bik)
2727
if err != nil {
28-
return false, fmt.Errorf("create %s model: %w", packageName, err)
28+
return false, fmt.Errorf("parse %s model: %w", packageName, err)
2929
}
3030

3131
isValid, err := bikData.IsValid()

bik/models.go

Lines changed: 83 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ var (
5252
)
5353

5454
type (
55-
// CountryCode Required length 2.
55+
// CountryCode required length 2.
5656
CountryCode int
5757

5858
// UnitConditionalNumber required length 2.
@@ -62,40 +62,74 @@ type (
6262
// or the conditional number of the structural division of the Bank of Russia.
6363
UnitConditionalNumber int
6464

65-
// LastAccountNumbers required length 3. It is last correspondent account of the bank. Possible values [050, 999]
65+
// LastAccountNumbers required length 3.
66+
// It is last correspondent account of the bank. Possible values [050, 999]
6667
LastAccountNumbers int
6768
)
6869

69-
const codeLength = 9
70+
func (cc CountryCode) IsValid() bool {
71+
if cc < minCountryCodeLength || cc > maxCountryCodeLength {
72+
return false
73+
}
7074

71-
type BIKStruct struct {
72-
country CountryCode
73-
territoryCode okato.StateCode
74-
unitNumber UnitConditionalNumber
75-
lastNumber LastAccountNumbers
75+
_, ok := supportedCountryCodes[cc]
76+
return ok
7677
}
7778

78-
// generateOptions TODO
79-
type generateOptions struct {
79+
func (cc CountryCode) String() string {
80+
_, ok := supportedCountryCodes[cc]
81+
if !ok {
82+
return RussiaCountryCode.String()
83+
}
84+
85+
return utils.StrCode(int(cc), countryCodeLength)
8086
}
8187

82-
type GenerateOpt func(options *generateOptions)
88+
func (cc CountryCode) GetName() string {
89+
codeName, ok := supportedCountryCodes[cc]
90+
if !ok {
91+
return unspecifiedCountryCode
92+
}
8393

84-
func NewBIK(opts ...GenerateOpt) *BIKStruct {
85-
var options generateOptions
94+
return codeName
95+
}
8696

87-
for _, o := range opts {
88-
o(&options)
89-
}
97+
func GenerateCountryCode() CountryCode {
98+
return countryCodes[utils.Random(0, len(countryCodes)-1)]
99+
}
90100

91-
return &BIKStruct{
92-
country: GenerateCountryCode(),
93-
territoryCode: okato.GenerateStateCode(),
94-
unitNumber: GenerateUnitConditionalNumber(),
95-
lastNumber: GenerateLastAccountNumbers(),
101+
func (ucn UnitConditionalNumber) IsValid() bool {
102+
return ucn >= minUnitConditionalNumber && ucn <= maxUnitConditionalNumber
103+
}
104+
105+
func (ucn UnitConditionalNumber) String() string {
106+
return utils.StrCode(int(ucn), unitConditionalNumberLength)
107+
}
108+
109+
func GenerateUnitConditionalNumber() UnitConditionalNumber {
110+
return UnitConditionalNumber(utils.Random(minUnitConditionalNumber, maxUnitConditionalNumber))
111+
}
112+
113+
const specialCode = 12
114+
115+
func (lan LastAccountNumbers) IsValid() bool {
116+
if lan == specialCode {
117+
return true
96118
}
119+
120+
return lan >= minLastAccountNumbers && lan <= maxLastAccountNumbers
121+
}
122+
123+
func (lan LastAccountNumbers) String() string {
124+
return utils.StrCode(int(lan), lastAccountNumbersLength)
97125
}
98126

127+
func GenerateLastAccountNumbers() LastAccountNumbers {
128+
return LastAccountNumbers(utils.Random(minLastAccountNumbers, maxLastAccountNumbers))
129+
}
130+
131+
const codeLength = 9
132+
99133
func ParseBIK(bik string) (*BIKStruct, error) {
100134
if len(bik) != codeLength {
101135
return nil, &models.CommonError{
@@ -117,6 +151,34 @@ func ParseBIK(bik string) (*BIKStruct, error) {
117151
}, nil
118152
}
119153

154+
type BIKStruct struct {
155+
country CountryCode
156+
territoryCode okato.StateCode
157+
unitNumber UnitConditionalNumber
158+
lastNumber LastAccountNumbers
159+
}
160+
161+
// generateOptions TODO
162+
type generateOptions struct {
163+
}
164+
165+
type GenerateOpt func(options *generateOptions)
166+
167+
func NewBIK(opts ...GenerateOpt) *BIKStruct {
168+
var options generateOptions
169+
170+
for _, o := range opts {
171+
o(&options)
172+
}
173+
174+
return &BIKStruct{
175+
country: GenerateCountryCode(),
176+
territoryCode: okato.GenerateStateCode(),
177+
unitNumber: GenerateUnitConditionalNumber(),
178+
lastNumber: GenerateLastAccountNumbers(),
179+
}
180+
}
181+
120182
func (bs *BIKStruct) IsValid() (bool, error) {
121183
if bs == nil {
122184
return false, ErrNilBIK
@@ -161,64 +223,3 @@ func (bs *BIKStruct) Exists() (bool, error) {
161223
_, ok := existsBIKs[bs.String()]
162224
return ok, nil
163225
}
164-
165-
func GenerateCountryCode() CountryCode {
166-
return countryCodes[utils.Random(0, len(countryCodes)-1)]
167-
}
168-
169-
func GenerateUnitConditionalNumber() UnitConditionalNumber {
170-
return UnitConditionalNumber(utils.Random(minUnitConditionalNumber, maxUnitConditionalNumber))
171-
}
172-
173-
func GenerateLastAccountNumbers() LastAccountNumbers {
174-
return LastAccountNumbers(utils.Random(minLastAccountNumbers, maxLastAccountNumbers))
175-
}
176-
177-
func (cc CountryCode) IsValid() bool {
178-
if cc < minCountryCodeLength || cc > maxCountryCodeLength {
179-
return false
180-
}
181-
182-
_, ok := supportedCountryCodes[cc]
183-
return ok
184-
}
185-
186-
func (cc CountryCode) String() string {
187-
_, ok := supportedCountryCodes[cc]
188-
if !ok {
189-
return RussiaCountryCode.String()
190-
}
191-
192-
return utils.StrCode(int(cc), countryCodeLength)
193-
}
194-
195-
func (cc CountryCode) GetName() string {
196-
codeName, ok := supportedCountryCodes[cc]
197-
if !ok {
198-
return unspecifiedCountryCode
199-
}
200-
201-
return codeName
202-
}
203-
204-
func (ucn UnitConditionalNumber) IsValid() bool {
205-
return ucn >= minUnitConditionalNumber && ucn <= maxUnitConditionalNumber
206-
}
207-
208-
func (ucn UnitConditionalNumber) String() string {
209-
return utils.StrCode(int(ucn), unitConditionalNumberLength)
210-
}
211-
212-
const specialCode = 12
213-
214-
func (lan LastAccountNumbers) IsValid() bool {
215-
if lan == specialCode {
216-
return true
217-
}
218-
219-
return lan >= minLastAccountNumbers && lan <= maxLastAccountNumbers
220-
}
221-
222-
func (lan LastAccountNumbers) String() string {
223-
return utils.StrCode(int(lan), lastAccountNumbersLength)
224-
}

0 commit comments

Comments
 (0)