Skip to content

Commit dd4bbc2

Browse files
committed
ref
1 parent ff6028e commit dd4bbc2

File tree

10 files changed

+226
-1706
lines changed

10 files changed

+226
-1706
lines changed

bik/bik.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ func Exists(bik string) (bool, error) {
4242

4343
// Generate method generate a valid BIK code, but possible usaged or not usaged in reality.
4444
// Method guaranteed that code will be valid, but not guaranteed that code will be exists.
45-
func Generate() string {
46-
return NewBIK().String()
45+
func Generate(opts ...GenerateOpt) string {
46+
return NewBIK(opts...).String()
4747
}

bik/bik_test.go

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,19 @@ func TestValidate(t *testing.T) {
2222
t.Run("invalid bik length", func(t *testing.T) {
2323
testCases := []testCase{
2424
{
25-
Code: "1234567888776",
26-
Error: models.ErrInvalidLength,
27-
IsValid: false,
25+
Code: "1234567888776",
26+
Error: models.ErrInvalidLength,
2827
},
2928
{
30-
Code: "044525",
31-
Error: models.ErrInvalidLength,
32-
IsValid: false,
29+
Code: "044525",
30+
Error: models.ErrInvalidLength,
3331
},
3432
{
3533
Code: "044525225",
36-
Error: nil,
3734
IsValid: true,
3835
},
3936
{
4037
Code: "044525012",
41-
Error: nil,
4238
IsValid: true,
4339
},
4440
}
@@ -47,12 +43,13 @@ func TestValidate(t *testing.T) {
4743
tc := tc
4844

4945
isValid, err := Validate(tc.Code)
50-
assert.Equal(t, tc.IsValid, isValid, tc.Code)
5146
if err != nil {
52-
assert.ErrorAs(t, err, &tc.Error, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
47+
require.ErrorAs(t, err, &tc.Error, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
5348
} else {
54-
assert.NoError(t, err, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
49+
require.NoError(t, err, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
5550
}
51+
52+
assert.Equal(t, tc.IsValid, isValid, tc.Code)
5653
}
5754
})
5855

@@ -65,58 +62,56 @@ func TestValidate(t *testing.T) {
6562

6663
testCases := []testCase{
6764
{
68-
Code: "0445?5226",
69-
Error: models.ErrInvalidValue,
70-
IsValid: false,
65+
Code: "0445?5226",
66+
Error: models.ErrInvalidValue,
7167
},
7268
{
73-
Code: "054525225",
74-
Error: ErrInvalidCountryCode,
75-
IsValid: false,
69+
Code: "054525225",
70+
Error: ErrInvalidCountryCode,
7671
},
7772
{
78-
Code: "104525225",
79-
Error: ErrInvalidCountryCode,
80-
IsValid: false,
73+
Code: "104525225",
74+
Error: ErrInvalidCountryCode,
8175
},
8276
{
83-
Code: "044#55#25",
84-
Error: models.ErrInvalidValue,
85-
IsValid: false,
77+
Code: "044#55#25",
78+
Error: models.ErrInvalidValue,
8679
},
8780
{
8881
Code: "044525225",
89-
Error: nil,
9082
IsValid: true,
9183
},
9284
{
9385
Code: "044525012",
94-
Error: nil,
9586
IsValid: true,
9687
},
9788
}
9889
for i, tc := range testCases {
9990
tc := tc
10091

10192
isValid, err := Validate(tc.Code)
102-
assert.Equal(t, tc.IsValid, isValid, tc.Code, tc.IsValid)
10393
if err != nil {
104-
assert.ErrorAs(t, err, &tc.Error, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
94+
require.ErrorAs(t, err, &tc.Error, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
10595
} else {
106-
assert.Empty(t, err, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
96+
require.Empty(t, err, fmt.Sprintf("invalid test case %d: input: %s", i, tc.Code))
10797
}
98+
99+
assert.Equal(t, tc.IsValid, isValid, tc.Code, tc.IsValid)
108100
}
109101
})
110102
}
111103

112104
func Test_Generate(t *testing.T) {
113-
require.Panics(t, func() {
114-
Generate()
115-
})
105+
bik := Generate()
106+
isValid, err := Validate(bik)
107+
108+
require.NoError(t, err, fmt.Sprintf("invalid bik value: %s", bik))
109+
require.True(t, isValid)
116110
}
117111

118112
func Test_Exists(t *testing.T) {
119-
require.Panics(t, func() {
120-
Exists("")
121-
})
113+
is, err := Exists("044525677") // АО "Яндекс Банк".
114+
require.NoError(t, err)
115+
116+
assert.True(t, is)
122117
}

bik/data.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
package bik
22

3+
var countryCodes []CountryCode
4+
5+
var supportedCountryCodes = map[CountryCode]string{
6+
DirectParticipationCounty: "Участник платежной системы с прямым участием",
7+
IndirectParticipationCounty: "Участник платежной системы с косвенным участием",
8+
NotMemberClientCBRF: "Клиент Банка России, не являющийся участником платежной системы",
9+
RussiaCountryCode: "Код Российской Федерации",
10+
}
11+
312
var existsBIKs = map[string]string{
413
"041280103": `УФК по Астраханской области`,
514
"046577906": `УРАЛЬСКИЙ ФИЛИАЛ АО "РАЙФФАЙЗЕНБАНК"`,
@@ -1608,3 +1617,10 @@ var existsBIKs = map[string]string{
16081617
"041012718": `ПАО КБ "ВОСТОЧНЫЙ"`,
16091618
"044525682": `Московский филиал ПАО КБ "Восточный"`,
16101619
}
1620+
1621+
func init() {
1622+
countryCodes = make([]CountryCode, 0, len(supportedCountryCodes))
1623+
for code := range supportedCountryCodes {
1624+
countryCodes = append(countryCodes, code)
1625+
}
1626+
}

bik/models.go

Lines changed: 67 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,46 @@ import (
1111

1212
const packageName = "bik"
1313

14+
const validateErrorTmpl = "%w: %s"
15+
1416
const (
15-
maxCountryCodeLength = 99
16-
minInitConditionalNumber = 0
17-
maxInitConditionalNumber = 99
18-
minLastAccountNumbers = 50
19-
maxLastAccountNumbers = 999
17+
countryCodeLength = 2
18+
19+
minCountryCodeLength = 0
20+
maxCountryCodeLength = 99
21+
)
22+
23+
const (
24+
unitConditionalNumberLength = 2
25+
26+
minUnitConditionalNumber = 0
27+
maxUnitConditionalNumber = 99
28+
)
29+
30+
const (
31+
lastAccountNumbersLength = 3
32+
33+
minLastAccountNumbers = 50
34+
maxLastAccountNumbers = 999
2035
)
2136

2237
const (
2338
unspecifiedCountryCode = "Неопределенный код страны"
2439
)
2540

2641
var (
27-
// directParticipationCounty - участник платежной системы с прямым участием
28-
directParticipationCounty CountryCode = 0
42+
// DirectParticipationCounty - участник платежной системы с прямым участием
43+
DirectParticipationCounty CountryCode = 0
2944

30-
// indirectParticipationCounty - участник платежной системы с косвенным участием
31-
indirectParticipationCounty CountryCode = 1
45+
// IndirectParticipationCounty - участник платежной системы с косвенным участием
46+
IndirectParticipationCounty CountryCode = 1
3247

33-
// notMemberClientCBRF - клиент Банка России, не являющийся участником платежной системы
34-
notMemberClientCBRF CountryCode = 2
48+
// NotMemberClientCBRF - клиент Банка России, не являющийся участником платежной системы
49+
NotMemberClientCBRF CountryCode = 2
3550

36-
russiaCountryCode CountryCode = 4
51+
RussiaCountryCode CountryCode = 4
3752
)
3853

39-
var supportedCountryCodes = map[CountryCode]string{
40-
directParticipationCounty: "Участник платежной системы с прямым участием",
41-
indirectParticipationCounty: "Участник платежной системы с косвенным участием",
42-
notMemberClientCBRF: "Клиент Банка России, не являющийся участником платежной системы",
43-
russiaCountryCode: "Код Российской Федерации",
44-
}
45-
4654
type (
4755
// CountryCode Required length 2.
4856
CountryCode int
@@ -67,7 +75,19 @@ type BIKStruct struct {
6775
lastNumber LastAccountNumbers
6876
}
6977

70-
func NewBIK() *BIKStruct {
78+
// generateOptions TODO
79+
type generateOptions struct {
80+
}
81+
82+
type GenerateOpt func(options *generateOptions)
83+
84+
func NewBIK(opts ...GenerateOpt) *BIKStruct {
85+
var options generateOptions
86+
87+
for _, o := range opts {
88+
o(&options)
89+
}
90+
7191
return &BIKStruct{
7292
country: GenerateCountryCode(),
7393
territoryCode: okato.GenerateStateCode(),
@@ -103,19 +123,19 @@ func (bs *BIKStruct) IsValid() (bool, error) {
103123
}
104124

105125
if !bs.country.IsValid() {
106-
return false, ErrInvalidCountryCode
126+
return false, fmt.Errorf(validateErrorTmpl, ErrInvalidCountryCode, bs.country)
107127
}
108128

109129
if !bs.territoryCode.IsValid() {
110-
return false, ErrInvalidTerritoryCode
130+
return false, fmt.Errorf(validateErrorTmpl, ErrInvalidTerritoryCode, bs.territoryCode)
111131
}
112132

113133
if !bs.unitNumber.IsValid() {
114-
return false, ErrInvalidUnitConditionalNumber
134+
return false, fmt.Errorf(validateErrorTmpl, ErrInvalidUnitConditionalNumber, bs.unitNumber)
115135
}
116136

117137
if !bs.lastNumber.IsValid() {
118-
return false, ErrInvalidLastAccountNumbers
138+
return false, fmt.Errorf(validateErrorTmpl, ErrInvalidLastAccountNumbers, bs.lastNumber)
119139
}
120140

121141
return true, nil
@@ -125,10 +145,10 @@ func (bs *BIKStruct) String() string {
125145
var res strings.Builder
126146
res.Grow(codeLength)
127147

128-
res.WriteString(bs.country.ToString())
129-
res.WriteString(bs.territoryCode.ToString())
130-
res.WriteString(bs.unitNumber.ToString())
131-
res.WriteString(bs.lastNumber.ToString())
148+
res.WriteString(bs.country.String())
149+
res.WriteString(bs.territoryCode.String())
150+
res.WriteString(bs.unitNumber.String())
151+
res.WriteString(bs.lastNumber.String())
132152

133153
return res.String()
134154
}
@@ -143,47 +163,50 @@ func (bs *BIKStruct) Exists() (bool, error) {
143163
}
144164

145165
func GenerateCountryCode() CountryCode {
146-
// len(supportedCountryCodes)
147-
return russiaCountryCode
166+
return countryCodes[utils.Random(0, len(countryCodes)-1)]
148167
}
149168

150169
func GenerateUnitConditionalNumber() UnitConditionalNumber {
151-
return 0
170+
return UnitConditionalNumber(utils.Random(minUnitConditionalNumber, maxUnitConditionalNumber))
152171
}
153172

154173
func GenerateLastAccountNumbers() LastAccountNumbers {
155-
return 0
174+
return LastAccountNumbers(utils.Random(minLastAccountNumbers, maxLastAccountNumbers))
156175
}
157176

158177
func (cc CountryCode) IsValid() bool {
159-
if cc > maxCountryCodeLength {
178+
if cc < minCountryCodeLength || cc > maxCountryCodeLength {
160179
return false
161180
}
162181

163182
_, ok := supportedCountryCodes[cc]
164-
165183
return ok
166184
}
167185

168186
func (cc CountryCode) String() string {
169-
res, ok := supportedCountryCodes[cc]
187+
_, ok := supportedCountryCodes[cc]
170188
if !ok {
171-
return unspecifiedCountryCode
189+
return RussiaCountryCode.String()
172190
}
173191

174-
return res
192+
return utils.StrCode(int(cc), countryCodeLength)
175193
}
176194

177-
func (cc CountryCode) ToString() string {
178-
return ""
195+
func (cc CountryCode) GetName() string {
196+
codeName, ok := supportedCountryCodes[cc]
197+
if !ok {
198+
return unspecifiedCountryCode
199+
}
200+
201+
return codeName
179202
}
180203

181204
func (ucn UnitConditionalNumber) IsValid() bool {
182-
return ucn >= minInitConditionalNumber && ucn <= maxInitConditionalNumber
205+
return ucn >= minUnitConditionalNumber && ucn <= maxUnitConditionalNumber
183206
}
184207

185-
func (ucn UnitConditionalNumber) ToString() string {
186-
return ""
208+
func (ucn UnitConditionalNumber) String() string {
209+
return utils.StrCode(int(ucn), unitConditionalNumberLength)
187210
}
188211

189212
const specialCode = 12
@@ -196,6 +219,6 @@ func (lan LastAccountNumbers) IsValid() bool {
196219
return lan >= minLastAccountNumbers && lan <= maxLastAccountNumbers
197220
}
198221

199-
func (lan LastAccountNumbers) ToString() string {
200-
return ""
222+
func (lan LastAccountNumbers) String() string {
223+
return utils.StrCode(int(lan), lastAccountNumbersLength)
201224
}

okato/data.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package okato
22

3-
var fisrtLevel = map[int]string{
3+
var statesCodes []StateCode
4+
5+
var fisrtLevelCodes = map[StateCode]string{
46
0: "Территория не определена", // Территория находится за пределами России
57
1: "Алтайский край (г.Барнаул)",
68
3: "Краснодарский край (г.Краснодар)",
@@ -89,3 +91,10 @@ var fisrtLevel = map[int]string{
8991
98: "Республика Саха (Якутия) (г.Якутск)",
9092
99: "Еврейская АО (г.Биробиджан)",
9193
}
94+
95+
func init() {
96+
statesCodes = make([]StateCode, 0, len(fisrtLevelCodes))
97+
for code := range fisrtLevelCodes {
98+
statesCodes = append(statesCodes, code)
99+
}
100+
}

0 commit comments

Comments
 (0)