Skip to content

Commit 494ce69

Browse files
committed
ref
1 parent d321dbe commit 494ce69

File tree

6 files changed

+131
-86
lines changed

6 files changed

+131
-86
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ if err != nil {
6060
}
6161
if !isValid {
6262
log.Println("INN is invalid")
63-
return
63+
} else {
64+
log.Println("INN is valid")
6465
}
65-
log.Println("INN is valid")
6666
```
6767

6868
## Documentation

fts/fts.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,21 @@ func (trc *TaxRegionCode) Ints() []int {
5151
return nil
5252
}
5353

54-
res := make([]int, 0, subjectCodeLength+regionTaxServiceNumberLength)
55-
res = append(res, utils.CodeToInts(int(trc.subjectCode))...)
56-
res = append(res, utils.CodeToInts(int(trc.serviceNumber))...)
54+
res := make([]int, subjectCodeLength+regionTaxServiceNumberLength)
55+
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+
}
5769

5870
return res
5971
}

inn/inn.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,10 @@ func Generate() string {
2424

2525
// GenerateLegal generate legal type inn string value
2626
func GenerateLegal() string {
27-
// inn := strconv.FormatInt(utils.RandomDigits(9), 10)
28-
// innArr, _ := transformInn(inn)
29-
30-
// return strings.Join([]string{inn, strconv.Itoa(hash10(innArr))}, "")
3127
return NewINN(INNType(utils.RandomDigits(1)%2 + 1)).String()
3228
}
3329

3430
// GeneratePhysical generate physical type inn string value
3531
func GeneratePhysical() string {
36-
// inn := strconv.FormatInt(utils.RandomDigits(10), 10)
37-
// innArr, _ := transformInn(inn)
38-
39-
// hash1Num := hash11(innArr)
40-
// innArr = append(innArr, hash1Num)
41-
42-
// return strings.Join([]string{inn, strconv.Itoa(hash1Num), strconv.Itoa(hash12(innArr))}, "")
4332
return NewINN(INNType(Physical)).String()
4433
}

inn/inn_test.go

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import (
44
"fmt"
55
"testing"
66

7+
"github.com/sshaplygin/docs-code/models"
78
"github.com/stretchr/testify/assert"
89
"github.com/stretchr/testify/require"
9-
10-
"github.com/sshaplygin/docs-code/models"
11-
"github.com/sshaplygin/docs-code/utils"
1210
)
1311

1412
func TestValidate(t *testing.T) {
@@ -107,9 +105,9 @@ func TestGenerate(t *testing.T) {
107105
for i := 0; i < 10; i++ {
108106
inn = GenerateLegal()
109107
isValid, err := Validate(inn)
110-
require.NoError(t, err)
108+
require.NoError(t, err, inn)
111109

112-
require.True(t, isValid)
110+
require.True(t, isValid, inn)
113111
}
114112
})
115113

@@ -119,9 +117,9 @@ func TestGenerate(t *testing.T) {
119117
for i := 0; i < 10; i++ {
120118
inn = GeneratePhysical()
121119
isValid, err := Validate(inn)
122-
require.NoError(t, err)
120+
require.NoError(t, err, inn)
123121

124-
require.True(t, isValid)
122+
require.True(t, isValid, inn)
125123
}
126124
})
127125

@@ -131,46 +129,9 @@ func TestGenerate(t *testing.T) {
131129
for i := 0; i < 10; i++ {
132130
inn = Generate()
133131
isValid, err := Validate(inn)
134-
require.NoError(t, err)
135-
136-
require.True(t, isValid)
137-
}
138-
})
139-
140-
t.Run("generate random digits", func(t *testing.T) {
141-
tests := []struct {
142-
len int
143-
min int64
144-
max int64
145-
}{
146-
{
147-
-5,
148-
0,
149-
9,
150-
},
151-
{
152-
-10,
153-
0,
154-
9,
155-
},
156-
{
157-
1,
158-
0,
159-
9,
160-
},
161-
{
162-
3,
163-
100,
164-
999,
165-
},
166-
}
167-
var digits int64
168-
169-
for _, tc := range tests {
170-
tc := tc
132+
require.NoError(t, err, inn)
171133

172-
digits = utils.RandomDigits(tc.len)
173-
assert.True(t, digits >= tc.min && digits <= tc.max)
134+
require.True(t, isValid, inn)
174135
}
175136
})
176137
}

inn/models.go

Lines changed: 70 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ const (
1919
)
2020

2121
const (
22-
legalSerialNumberLength = 4
23-
physicalSerialNumberLength = 5
22+
legalSerialNumberLength = 5
23+
physicalSerialNumberLength = 6
2424
)
2525

2626
type INNType uint
@@ -43,17 +43,44 @@ const (
4343
ForeignLegal
4444
)
4545

46-
type SerialNumber int
46+
type SerialNumber struct {
47+
val int
48+
size int
49+
}
4750

4851
func (sn SerialNumber) String() string {
49-
return strconv.Itoa(int(sn))
52+
return utils.StrCode(sn.val, sn.size)
53+
}
54+
55+
func (sn *SerialNumber) Ints() []int {
56+
if sn == nil {
57+
return nil
58+
}
59+
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+
}
68+
69+
return res
5070
}
5171

5272
func GenerateSerailNumber(innType INNType) SerialNumber {
5373
if innType == Physical {
54-
return SerialNumber(utils.RandomDigits(physicalSerialNumberLength))
74+
return SerialNumber{
75+
val: int(utils.RandomDigits(physicalSerialNumberLength)),
76+
size: physicalSerialNumberLength,
77+
}
78+
}
79+
80+
return SerialNumber{
81+
val: int(utils.RandomDigits(legalSerialNumberLength)),
82+
size: legalSerialNumberLength,
5583
}
56-
return SerialNumber(utils.RandomDigits(legalSerialNumberLength))
5784
}
5885

5986
type CheckSums []int
@@ -67,6 +94,27 @@ func (cs CheckSums) String() string {
6794
return res.String()
6895
}
6996

97+
func GenerateCheckSums(innType INNType, nums []int) CheckSums {
98+
checkFuncs := []checkSumFuncType{
99+
hash10,
100+
}
101+
102+
shiftIdx := -1
103+
if innType == Physical {
104+
shiftIdx = -2
105+
checkFuncs = []checkSumFuncType{
106+
hash11, hash12,
107+
}
108+
}
109+
110+
for _, f := range checkFuncs {
111+
nums = append(nums, f(nums))
112+
}
113+
114+
fmt.Println(nums[len(nums)+shiftIdx:])
115+
return nums[len(nums)+shiftIdx:]
116+
}
117+
70118
type INNStruct struct {
71119
taxRegionCode *fts.TaxRegionCode
72120
serialNumber SerialNumber
@@ -82,6 +130,7 @@ func NewINN(innType INNType) *INNStruct {
82130
return &INNStruct{
83131
taxRegionCode: taxRegionCode,
84132
serialNumber: serialNumber,
133+
checkSums: GenerateCheckSums(innType, append(taxRegionCode.Ints(), serialNumber.Ints()...)),
85134
}
86135
}
87136

@@ -99,17 +148,19 @@ func ParseINN(inn string) (*INNStruct, error) {
99148
}
100149

101150
t := Physical
102-
parseIdx := len(inn) - 3
151+
snSize := physicalSerialNumberLength
152+
parseIdx := len(inn) - 2
103153
if len(inn) == legalLength {
154+
snSize = legalSerialNumberLength
104155
t = Legal
105-
parseIdx = len(inn) - 2
156+
parseIdx = len(inn) - 1
106157
const foreignLegalStartWith = "9909"
107158
if inn[0:4] == foreignLegalStartWith {
108159
t = ForeignLegal
109160
}
110161
}
111162

112-
seraclNumberArr, err := utils.StrToArr(inn[4:parseIdx])
163+
serialNumberArr, err := utils.StrToArr(inn[4:parseIdx])
113164
if err != nil {
114165
return nil, fmt.Errorf("parse raw serial number %s: %w", packageName, err)
115166
}
@@ -121,9 +172,12 @@ func ParseINN(inn string) (*INNStruct, error) {
121172

122173
return &INNStruct{
123174
taxRegionCode: taxRegionCode,
124-
serialNumber: SerialNumber(utils.SliceToInt(seraclNumberArr)),
125-
checkSums: checkSums,
126-
t: t,
175+
serialNumber: SerialNumber{
176+
val: utils.SliceToInt(serialNumberArr),
177+
size: snSize,
178+
},
179+
checkSums: checkSums,
180+
t: t,
127181
}, nil
128182
}
129183

@@ -134,14 +188,14 @@ func (inn *INNStruct) IsValid() (bool, error) {
134188
return false, ErrNilINN
135189
}
136190

137-
nums := append(inn.taxRegionCode.Ints(), utils.CodeToInts(int(inn.serialNumber))...)
191+
nums := append(inn.taxRegionCode.Ints(), inn.serialNumber.Ints()...)
138192

139193
checkFuncs := []checkSumFuncType{
140194
hash10,
141195
}
142196

143197
if inn.IsPhysical() {
144-
if len(nums) != physicalLength {
198+
if len(nums) != physicalLength-2 {
145199
return false, fmt.Errorf("invalid nums length for %s type", inn.t)
146200
}
147201

@@ -155,7 +209,7 @@ func (inn *INNStruct) IsValid() (bool, error) {
155209
}
156210

157211
if inn.IsLegal() {
158-
if len(nums) != legalLength {
212+
if len(nums) != legalLength-1 {
159213
return false, fmt.Errorf("invalid nums length for %s type", inn.t)
160214
}
161215

@@ -176,23 +230,15 @@ func (inn *INNStruct) IsValid() (bool, error) {
176230

177231
func (inn *INNStruct) String() string {
178232
n := physicalLength
179-
snRequired := physicalSerialNumberLength
180233
if inn.IsLegal() {
181234
n = legalLength
182-
snRequired = legalSerialNumberLength
183235
}
184236

185237
var res strings.Builder
186238
res.Grow(n)
187239

188240
res.WriteString(inn.taxRegionCode.String())
189-
190-
sn := inn.serialNumber.String()
191-
if len(sn) < snRequired {
192-
sn = strings.Repeat("0", snRequired-len(sn)) + sn
193-
}
194-
195-
res.WriteString(sn)
241+
res.WriteString(inn.serialNumber.String())
196242
res.WriteString(inn.checkSums.String())
197243

198244
return res.String()

utils/helpers_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,40 @@ func Test_Generate_InvalidInput(t *testing.T) {
100100
})
101101
}
102102
}
103+
104+
func Test_GenerateRandomDigits(t *testing.T) {
105+
tests := []struct {
106+
len int
107+
min int64
108+
max int64
109+
}{
110+
{
111+
-5,
112+
0,
113+
9,
114+
},
115+
{
116+
-10,
117+
0,
118+
9,
119+
},
120+
{
121+
1,
122+
0,
123+
9,
124+
},
125+
{
126+
3,
127+
100,
128+
999,
129+
},
130+
}
131+
var digits int64
132+
133+
for _, tc := range tests {
134+
tc := tc
135+
136+
digits = RandomDigits(tc.len)
137+
assert.True(t, digits >= tc.min && digits <= tc.max)
138+
}
139+
}

0 commit comments

Comments
 (0)