Skip to content

Commit 237ac15

Browse files
authored
Merge pull request #12 from sshaplygin/kpp-code
add: kpp code
2 parents 8decd00 + eb4b514 commit 237ac15

32 files changed

+2901
-480
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*.xml
1111
*.csv
1212
*.html
13+
*.txt
1314

1415
.DS_Store
1516

README.md

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,7 @@ It is not production ready public API! It is API could be change it the future.
1212

1313
Status of implementation by code package:
1414

15-
- [x] BIK
16-
- [x] Validate method
17-
- [x] Generate method
18-
- [x] Exists method
19-
- [x] INN
20-
- [x] Generate method
21-
- [x] Validate method
22-
- [ ] KPP
23-
- [ ] Generate method
24-
- [x] Validate method
25-
- [x] OGRN
15+
- [ ] OGRN
2616
- [x] Generate method
2717
- [ ] Validate method
2818
- [ ] OGRNIP
@@ -37,13 +27,15 @@ Status of implementation by code package:
3727
- [ ] SNILS
3828
- [ ] Generate method
3929
- [x] Validate method
40-
- [ ] Swift
30+
- [ ] SWIFT
4131
- [ ] Generate method
4232
- [ ] Validate method
4333
- [ ] KS
4434
- [ ] Generate method
4535
- [ ] Validate method
4636

37+
Full supported codes: BIK, INN, KPP
38+
4739
## Usage
4840

4941
``` bash
@@ -57,17 +49,19 @@ go get github.com/sshaplygin/docs-code
5749
import (
5850
"log"
5951

60-
"github.com/sshaplygin/docs-code/inn"
52+
"github.com/sshaplygin/docs-code"
6153
)
6254

6355
...
6456

65-
isValid, err := inn.Validate("526317984689")
57+
isValid, err := docs_code.Validate(docs_code.INN, "526317984689")
6658
if err != nil {
67-
log.Error(err)
59+
log.Error(err)
6860
}
69-
if isValid {
70-
log.Println("INN is valid")
61+
if !isValid {
62+
log.Println("INN is invalid")
63+
} else {
64+
log.Println("INN is valid")
7165
}
7266
```
7367

bik/bik.go

Lines changed: 4 additions & 4 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()
@@ -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(opts ...GenerateOpt) string {
46-
return NewBIK(opts...).String()
45+
func Generate() string {
46+
return NewBIK().String()
4747
}

bik/bik_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ func TestValidate(t *testing.T) {
104104
func Test_Generate(t *testing.T) {
105105
bik := Generate()
106106
isValid, err := Validate(bik)
107-
108107
require.NoError(t, err, fmt.Sprintf("invalid bik value: %s", bik))
109-
require.True(t, isValid)
108+
109+
assert.True(t, isValid)
110110
}
111111

112112
func Test_Exists(t *testing.T) {

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)