Skip to content

Commit f25ba0e

Browse files
authored
Merge pull request #16 from sshaplygin/add-ogrn
add: ogrn and ogrnip
2 parents afa897e + 898e69e commit f25ba0e

File tree

17 files changed

+502
-110
lines changed

17 files changed

+502
-110
lines changed

BENCHMARKS.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Benchmarks
2+
3+
## BIK
4+
5+
goos: darwin
6+
goarch: arm64
7+
pkg: github.com/sshaplygin/docs-code/bik
8+
BenchmarkValidateCorrect-10 8064954 140.7 ns/op 256 B/op 3 allocs/op
9+
BenchmarkGenerate-10 615589 1972 ns/op 240 B/op 18 allocs/op
10+
PASS
11+
ok github.com/sshaplygin/docs-code/bik 2.635s
12+
13+
## INN
14+
15+
goos: darwin
16+
goarch: arm64
17+
pkg: github.com/sshaplygin/docs-code/inn
18+
BenchmarkValidateCorrectLegal-10 2719714 443.8 ns/op 616 B/op 23 allocs/op
19+
BenchmarkValidateCorrectPhysical-10 2076355 576.6 ns/op 936 B/op 30 allocs/op
20+
BenchmarkGenerate-10 394204 3133 ns/op 875 B/op 42 allocs/op
21+
BenchmarkGenerateLegal-10 354616 3213 ns/op 801 B/op 41 allocs/op
22+
BenchmarkGeneratePhysical-10 492985 2419 ns/op 974 B/op 41 allocs/op
23+
PASS
24+
ok github.com/sshaplygin/docs-code/inn 7.215s
25+
26+
## KPP
27+
28+
goos: darwin
29+
goarch: arm64
30+
pkg: github.com/sshaplygin/docs-code/kpp
31+
BenchmarkValidateCorrect-10 5280958 218.9 ns/op 216 B/op 8 allocs/op
32+
BenchmarkGenerate-10 484114 2434 ns/op 385 B/op 22 allocs/op
33+
PASS
34+
ok github.com/sshaplygin/docs-code/kpp 2.810s
35+
36+
## OGRN
37+
38+
goos: darwin
39+
goarch: arm64
40+
pkg: github.com/sshaplygin/docs-code/ogrn
41+
BenchmarkValidateCorrect-10 2583738 457.3 ns/op 728 B/op 18 allocs/op
42+
BenchmarkGenerate-10 294908 3938 ns/op 841 B/op 45 allocs/op
43+
PASS
44+
ok github.com/sshaplygin/docs-code/ogrn 3.074s
45+
46+
## OGRNIP
47+
48+
goos: darwin
49+
goarch: arm64
50+
pkg: github.com/sshaplygin/docs-code/ogrnip
51+
BenchmarkValidateCorrect-10 1991065 580.4 ns/op 1008 B/op 24 allocs/op
52+
BenchmarkGenerate-10 403179 3100 ns/op 1010 B/op 46 allocs/op
53+
PASS
54+
ok github.com/sshaplygin/docs-code/ogrnip 3.411s

bik/bik_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,13 @@ func TestValidate(t *testing.T) {
102102
}
103103

104104
func Test_Generate(t *testing.T) {
105-
bik := Generate()
106-
isValid, err := Validate(bik)
107-
require.NoError(t, err, fmt.Sprintf("invalid bik value: %s", bik))
105+
for i := 0; i < 10; i++ {
106+
bik := Generate()
107+
isValid, err := Validate(bik)
108+
require.NoError(t, err, fmt.Sprintf("invalid bik value: %s", bik))
108109

109-
assert.True(t, isValid)
110+
assert.True(t, isValid)
111+
}
110112
}
111113

112114
func Test_Exists(t *testing.T) {

fts/fts.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,8 @@ func (trc *TaxRegionCode) Ints() []int {
5353

5454
res := make([]int, subjectCodeLength+regionTaxServiceNumberLength)
5555

56-
nums := utils.CodeToInts(int(trc.subjectCode))
57-
idx := subjectCodeLength - 1
58-
for i := len(nums) - 1; i >= 0; i-- {
59-
res[idx] = nums[i]
60-
idx--
61-
}
62-
63-
nums = utils.CodeToInts(int(trc.serviceNumber))
64-
idx = len(res) - 1
65-
for i := len(nums) - 1; i >= 0; i-- {
66-
res[idx] = nums[i]
67-
idx--
68-
}
56+
utils.FillSlice(utils.CodeToInts(int(trc.subjectCode)), res, subjectCodeLength-1)
57+
utils.FillSlice(utils.CodeToInts(int(trc.serviceNumber)), res, len(res)-1)
6958

7059
return res
7160
}
@@ -154,6 +143,12 @@ func (csc ConstitutionRegionCode) GetName() string {
154143
return codeName
155144
}
156145

146+
func (csc ConstitutionRegionCode) Ints() []int {
147+
res := make([]int, subjectCodeLength)
148+
utils.FillSlice(utils.CodeToInts(int(csc)), res, len(res)-1)
149+
return res
150+
}
151+
157152
func GenerateConstitutionSubjectCode() ConstitutionRegionCode {
158153
return regionsCodes[utils.Random(0, len(regionsCodes)-1)]
159154
}

generate.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"github.com/sshaplygin/docs-code/bik"
55
"github.com/sshaplygin/docs-code/inn"
66
"github.com/sshaplygin/docs-code/kpp"
7+
"github.com/sshaplygin/docs-code/ogrn"
8+
"github.com/sshaplygin/docs-code/ogrnip"
79
)
810

911
type GenerateFunc func() string
@@ -17,6 +19,10 @@ func Generate(docType DocType) string {
1719
callFunc = inn.Generate
1820
case KPP:
1921
callFunc = kpp.Generate
22+
case OGRN:
23+
callFunc = ogrn.Generate
24+
case OGRNIP:
25+
callFunc = ogrnip.Generate
2026
}
2127

2228
if callFunc == nil {

inn/models.go

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,42 +44,36 @@ const (
4444
)
4545

4646
type SerialNumber struct {
47-
val int
48-
size int
47+
val int
48+
len int
4949
}
5050

5151
func (sn SerialNumber) String() string {
52-
return utils.StrCode(sn.val, sn.size)
52+
return utils.StrCode(sn.val, sn.len)
5353
}
5454

5555
func (sn *SerialNumber) Ints() []int {
5656
if sn == nil {
5757
return nil
5858
}
5959

60-
res := make([]int, sn.size)
61-
nums := utils.CodeToInts(sn.val)
62-
63-
idx := len(res) - 1
64-
for i := len(nums) - 1; i >= 0; i-- {
65-
res[idx] = nums[i]
66-
idx--
67-
}
60+
res := make([]int, sn.len)
61+
utils.FillSlice(utils.CodeToInts(sn.val), res, len(res)-1)
6862

6963
return res
7064
}
7165

7266
func GenerateSerailNumber(innType INNType) SerialNumber {
7367
if innType == Physical {
7468
return SerialNumber{
75-
val: int(utils.RandomDigits(physicalSerialNumberLength)),
76-
size: physicalSerialNumberLength,
69+
val: int(utils.RandomDigits(physicalSerialNumberLength)),
70+
len: physicalSerialNumberLength,
7771
}
7872
}
7973

8074
return SerialNumber{
81-
val: int(utils.RandomDigits(legalSerialNumberLength)),
82-
size: legalSerialNumberLength,
75+
val: int(utils.RandomDigits(legalSerialNumberLength)),
76+
len: legalSerialNumberLength,
8377
}
8478
}
8579

@@ -111,7 +105,6 @@ func GenerateCheckSums(innType INNType, nums []int) CheckSums {
111105
nums = append(nums, f(nums))
112106
}
113107

114-
fmt.Println(nums[len(nums)+shiftIdx:])
115108
return nums[len(nums)+shiftIdx:]
116109
}
117110

@@ -148,10 +141,10 @@ func ParseINN(inn string) (*INNStruct, error) {
148141
}
149142

150143
t := Physical
151-
snSize := physicalSerialNumberLength
144+
snlen := physicalSerialNumberLength
152145
parseIdx := len(inn) - 2
153146
if len(inn) == legalLength {
154-
snSize = legalSerialNumberLength
147+
snlen = legalSerialNumberLength
155148
t = Legal
156149
parseIdx = len(inn) - 1
157150
const foreignLegalStartWith = "9909"
@@ -173,8 +166,8 @@ func ParseINN(inn string) (*INNStruct, error) {
173166
return &INNStruct{
174167
taxRegionCode: taxRegionCode,
175168
serialNumber: SerialNumber{
176-
val: utils.SliceToInt(serialNumberArr),
177-
size: snSize,
169+
val: utils.SliceToInt(serialNumberArr),
170+
len: snlen,
178171
},
179172
checkSums: checkSums,
180173
t: t,

kpp/errors.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ var (
1212
// ErrInvalidReasonCode invalid reason code
1313
ErrInvalidReasonCode = errors.New("invalid reason code")
1414

15-
// ErrInvalidSerialNumber invalid serial number
16-
ErrInvalidSerialNumber = errors.New("invalid serial number")
15+
// ErrInvalidSerialNumbers invalid serial number
16+
ErrInvalidSerialNumbers = errors.New("invalid serial number")
1717
)

kpp/kpp_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,13 @@ func TestValidate(t *testing.T) {
130130
}
131131

132132
func Test_Generate(t *testing.T) {
133-
kpp := Generate()
134-
isValid, err := Validate(kpp)
135-
require.NoError(t, err, fmt.Sprintf("invalid kpp value: %s", kpp))
133+
for i := 0; i < 10; i++ {
134+
kpp := Generate()
135+
isValid, err := Validate(kpp)
136+
require.NoError(t, err, fmt.Sprintf("invalid kpp value: %s", kpp))
136137

137-
assert.True(t, isValid)
138+
assert.True(t, isValid)
139+
}
138140
}
139141

140142
func BenchmarkValidateCorrect(b *testing.B) {

kpp/models.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (kpp *KPPStruct) IsValid() (bool, error) {
116116
}
117117

118118
if !kpp.serialNumber.IsValid() {
119-
return false, fmt.Errorf(validateErrorTmpl, ErrInvalidSerialNumber, kpp.serialNumber)
119+
return false, fmt.Errorf(validateErrorTmpl, ErrInvalidSerialNumbers, kpp.serialNumber)
120120
}
121121

122122
return true, nil

ogrn/errors.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ogrn
2+
3+
import "errors"
4+
5+
var (
6+
// ErrNilOGRN try call methods for nil ogrn struct
7+
ErrNilOGRN = errors.New("nil ogrn struct")
8+
9+
// ErrInvalidCodeType invalid code type
10+
ErrInvalidCodeType = errors.New("invalid code type")
11+
12+
// ErrInvalidYearsNumbers invalid years number code
13+
ErrInvalidYearsNumbers = errors.New("invalid years number code")
14+
15+
// ErrInvalidRegion invalid region code
16+
ErrInvalidRegion = errors.New("invalid region code")
17+
18+
// ErrInvalidSerialNumbers invalid serial numbers
19+
ErrInvalidSerialNumbers = errors.New("invalid serial numbers")
20+
)

0 commit comments

Comments
 (0)